Skip to content

Commit

Permalink
Fix auth for relay feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Staab committed Nov 26, 2024
1 parent f990206 commit 67cdc1e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 56 deletions.
38 changes: 13 additions & 25 deletions src/app/shared/FeedControls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {sortBy, not, equals, uniqBy} from "@welshman/lib"
import {getAddress} from "@welshman/util"
import {synced} from "@welshman/store"
import {isSearchFeed, makeSearchFeed, makeScopeFeed, Scope, getFeedArgs} from "@welshman/feeds"
import {isSearchFeed, makeSearchFeed, getFeedArgs} from "@welshman/feeds"
import {signer} from "@welshman/app"
import {toSpliced} from "src/util/misc"
import {slideAndFade} from "src/util/transition"
Expand All @@ -15,7 +15,7 @@
import Anchor from "src/partials/Anchor.svelte"
import FeedForm from "src/app/shared/FeedForm.svelte"
import {router} from "src/app/util"
import {normalizeFeedDefinition, readFeed, makeFeed, displayFeed} from "src/domain"
import {normalizeFeedDefinition, readFeed, displayFeed} from "src/domain"
import {userListFeeds, deleteEvent, userFeeds, userFavoritedFeeds} from "src/engine"
export let feed
Expand All @@ -25,8 +25,6 @@
const form = boolCtrl()
const expanded = synced("FeedControls/expanded", false)
const followsFeed = makeFeed({definition: normalizeFeedDefinition(makeScopeFeed(Scope.Follows))})
const networkFeed = makeFeed({definition: normalizeFeedDefinition(makeScopeFeed(Scope.Network))})
const toggleExpanded = () => expanded.update(not)
Expand Down Expand Up @@ -106,7 +104,9 @@
</div>
</Input>
<slot name="controls" />
<Anchor button low on:click={toggleExpanded}>Customize</Anchor>
{#if $signer}
<Anchor button low on:click={toggleExpanded}>Customize</Anchor>
{/if}
</div>
{#if $expanded}
<div transition:slideAndFade class="pt-4">
Expand All @@ -118,36 +118,24 @@
</Anchor>
</div>
<div class="flex flex-wrap gap-1">
<Chip
class="cursor-pointer"
accent={equals(followsFeed.definition, feed.definition)}
on:click={() => setFeed(followsFeed)}>
Follows
</Chip>
<Chip
class="cursor-pointer"
accent={equals(networkFeed.definition, feed.definition)}
on:click={() => setFeed(networkFeed)}>
Network
</Chip>
{#each allFeeds as other}
<Chip
class="cursor-pointer"
accent={equals(other.definition, feed.definition)}
on:click={() => setFeed(other)}>
{displayFeed(other)}
</Chip>
{:else}
<p>No custom feeds found - click "Edit feed" below to get started!</p>
{/each}
</div>
{#if $signer}
<div class="my-4 flex flex-col-reverse justify-between gap-2 sm:flex-row">
<div class="flex flex-col gap-2 sm:flex-row">
<Anchor button href={router.at("lists").toString()}>Manage lists</Anchor>
<Anchor button href={router.at("feeds").toString()}>Manage feeds</Anchor>
</div>
<Anchor button accent on:click={openForm}>Edit feed</Anchor>
<div class="my-4 flex flex-col-reverse justify-between gap-2 sm:flex-row">
<div class="flex flex-col gap-2 sm:flex-row">
<Anchor button href={router.at("lists").toString()}>Manage lists</Anchor>
<Anchor button href={router.at("feeds").toString()}>Manage feeds</Anchor>
</div>
{/if}
<Anchor button accent on:click={openForm}>Edit feed</Anchor>
</div>
</Card>
</div>
{/if}
Expand Down
2 changes: 2 additions & 0 deletions src/app/shared/FeedFormSectionRelays.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import {identity} from "@welshman/lib"
import {FeedType} from "@welshman/feeds"
import {relaySearch} from "@welshman/app"
import {displayRelayUrl} from "@welshman/util"
Expand All @@ -13,6 +14,7 @@
multiple
value={feed.slice(1)}
search={$relaySearch.searchValues}
termToItem={identity}
onChange={urls => onChange([FeedType.Relay, ...urls])}>
<span slot="item" let:item>{displayRelayUrl(item)}</span>
</SearchSelect>
12 changes: 6 additions & 6 deletions src/app/views/OnboardingKeys.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
return showWarning("Please use only numbers and lowercase letters.")
}
const handle = await loadHandle(`${state.username}@${handler.domain}`)
if (handle?.pubkey) {
return showWarning("Sorry, it looks like that username is already taken.")
}
loading = true
try {
const handle = await loadHandle(`${state.username}@${handler.domain}`)
if (handle?.pubkey) {
return showWarning("Sorry, it looks like that username is already taken.")
}
state.pubkey = await broker.createAccount(state.username, nip46Perms)
if (!state.pubkey) {
Expand Down
10 changes: 8 additions & 2 deletions src/engine/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,16 @@ export const makeFeedRequestHandler =
({forcePlatform}: FeedRequestHandlerOptions) =>
async ({relays, filters, onEvent}: RequestOpts) => {
const tracker = new Tracker()
const loadOptions = {onEvent, tracker, forcePlatform, skipCache: true, delay: 0}
const loadOptions = {
onEvent,
tracker,
forcePlatform,
skipCache: true,
delay: 0,
}

if (relays?.length > 0) {
await load({...loadOptions, filters, relays})
await load({...loadOptions, filters, relays, authTimeout: 3000})
} else {
// Break out selections by relay so we can complete early after a certain number
// of requests complete for faster load times
Expand Down
29 changes: 6 additions & 23 deletions src/engine/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,12 @@ import {
uniqBy,
} from "@welshman/lib"
import type {PublishRequest, Target} from "@welshman/net"
import {Executor, Local, Multi, Relays, SubscriptionEvent} from "@welshman/net"
import {Executor, AuthMode, Local, Multi, Relays, SubscriptionEvent} from "@welshman/net"
import {Nip01Signer, Nip59} from "@welshman/signer"
import {deriveEvents, deriveEventsMapped, throttled, withGetter} from "@welshman/store"
import type {
EventTemplate,
PublishedList,
SignedEvent,
StampedEvent,
TrustedEvent,
} from "@welshman/util"
import type {EventTemplate, PublishedList, SignedEvent, TrustedEvent} from "@welshman/util"
import {
APP_DATA,
CLIENT_AUTH,
DIRECT_MESSAGE,
FEED,
FEEDS,
Expand Down Expand Up @@ -1041,20 +1034,7 @@ if (!db) {
]

setContext({
net: getDefaultNetContext({
getExecutor,
signEvent: (event: StampedEvent) => {
if (
event.kind === CLIENT_AUTH &&
env.PLATFORM_RELAYS.length === 0 &&
!getSetting("auto_authenticate")
) {
return
}

return signer.get()?.sign(event)
},
}),
net: getDefaultNetContext({getExecutor}),
app: getDefaultAppContext({
dufflepudUrl: env.DUFFLEPUD_URL,
indexerRelays: env.INDEXER_RELAYS,
Expand All @@ -1066,6 +1046,9 @@ if (!db) {
})

userSettings.subscribe($settings => {
const autoAuthenticate = $settings.auto_authenticate || env.PLATFORM_RELAYS.length > 0

ctx.net.authMode = autoAuthenticate ? AuthMode.Implicit : AuthMode.Explicit
ctx.app.dufflepudUrl = getSetting("dufflepud_url")
})

Expand Down

0 comments on commit 67cdc1e

Please sign in to comment.