diff --git a/src/app/commands.ts b/src/app/commands.ts index 164ebfdb3..5a4d33d21 100644 --- a/src/app/commands.ts +++ b/src/app/commands.ts @@ -1,5 +1,5 @@ -import {uniqBy, sleep, chunk, equals, choice, append} from "@welshman/lib" -import {DELETE, MUTES, FOLLOWS, REACTION, getPubkeyTagValues, createEvent, displayProfile} from "@welshman/util" +import {uniqBy, sleep, chunk, equals, nthNe, choice, append} from "@welshman/lib" +import {DELETE, PROFILE, INBOX_RELAYS, RELAYS, MUTES, FOLLOWS, REACTION, isSignedEvent, getPubkeyTagValues, createEvent, displayProfile, normalizeRelayUrl} from "@welshman/util" import type {TrustedEvent} from "@welshman/util" import type {SubscribeRequestWithHandlers} from "@welshman/net" import { @@ -17,6 +17,8 @@ import { tagEvent, tagPubkey, tagReactionTo, + getRelayUrls, + getInboxRelaySelections, } from "@welshman/app" import {tagRoom, MEMBERSHIPS, INDEXER_RELAYS} from "@app/state" @@ -78,7 +80,21 @@ export const loadUserData = ( return promise } -// Updates +// Synchronization + +export const broadcastUserData = async (relays: string[]) => { + const authors = [pubkey.get()!] + const kinds = [RELAYS, INBOX_RELAYS, FOLLOWS, PROFILE] + const events = repository.query([{kinds, authors}]) + + for (const event of events) { + if (isSignedEvent(event)) { + await publishThunk(makeThunk({event, relays})) + } + } +} + +// List updates export type ModifyTags = (tags: string[][]) => string[][] @@ -111,7 +127,6 @@ export const removeSpaceMembership = (url: string) => export const removeRoomMembership = (url: string, room: string) => updateList(MEMBERSHIPS, (tags: string[][]) => tags.filter(t => !equals(tagRoom(room, url), t))) - export const unfollowPerson = (pubkey: string) => updateList(FOLLOWS, tags => tags.filter(t => t[1] !== pubkey)) @@ -124,6 +139,54 @@ export const unmutePerson = (pubkey: string) => export const mutePerson = (pubkey: string) => updateList(MUTES, tags => append(tagPubkey(pubkey), tags)) +export const setRelayPolicy = (url: string, read: boolean, write: boolean) => + updateList(RELAYS, tags => { + tags = tags.filter(t => normalizeRelayUrl(t[1]) !== url) + + if (read && write) { + tags.push(["r", url]) + } else if (read) { + tags.push(["r", url, "read"]) + } else if (write) { + tags.push(["r", url, "write"]) + } + + console.log(tags) + + return tags + }) + +export const setInboxRelayPolicy = (url: string, enabled: boolean) => { + const urls = getRelayUrls(getInboxRelaySelections(pubkey.get()!)) + + // Only update inbox policies if they already exist or we're adding them + if (enabled || urls.includes(url)) { + updateList(INBOX_RELAYS, tags => { + tags = tags.filter(t => normalizeRelayUrl(t[1]) !== url) + + if (enabled) { + tags.push(["relay", url]) + } + + return tags + }) + } +} + +export const joinRelay = async (url: string, claim?: string) => { + if (claim) { + await publishThunk( + makeThunk({ + event: createEvent(28934, {tags: [["claim", claim]]}), + relays: [url], + }) + ) + } + + await setRelayPolicy(url, true, true) + await broadcastUserData([url]) +} + // Actions export const publishReaction = ({relays, event, content, tags = []}: { diff --git a/src/app/components/RelayAdd.svelte b/src/app/components/RelayAdd.svelte index 931ab9a7f..997403301 100644 --- a/src/app/components/RelayAdd.svelte +++ b/src/app/components/RelayAdd.svelte @@ -8,10 +8,8 @@ import RelayItem from "@app/components/RelayItem.svelte" import {discoverRelays} from "@app/state" - export let mode: string export let relays: Readable - - const addRelay = (url: string) => null + export let addRelay: (url: string) => void let term = "" let limit = 20 diff --git a/src/routes/settings/relays/+page.svelte b/src/routes/settings/relays/+page.svelte index bc88db5ec..b86eeb440 100644 --- a/src/routes/settings/relays/+page.svelte +++ b/src/routes/settings/relays/+page.svelte @@ -8,19 +8,35 @@ import RelayItem from "@app/components/RelayItem.svelte" import RelayAdd from "@app/components/RelayAdd.svelte" import {pushModal} from '@app/modal' + import {setRelayPolicy, setInboxRelayPolicy} from '@app/commands' const readRelayUrls = derived(userRelaySelections, getReadRelayUrls) const writeRelayUrls = derived(userRelaySelections, getWriteRelayUrls) const inboxRelayUrls = derived(userInboxRelaySelections, getRelayUrls) - const addRelay = (mode: string, relays: Readable) => - pushModal(RelayAdd, {mode, relays}) + const addReadRelay = () => + pushModal(RelayAdd, { + relays: readRelayUrls, + addRelay: (url: string) => setRelayPolicy(url, true, $writeRelayUrls.includes(url)), + }) - const removeReadRelay = (url: string) => null + const addWriteRelay = () => + pushModal(RelayAdd, { + relays: writeRelayUrls, + addRelay: (url: string) => setRelayPolicy(url, $readRelayUrls.includes(url), true), + }) - const removeWriteRelay = (url: string) => null + const addInboxRelay = () => + pushModal(RelayAdd, { + relays: inboxRelayUrls, + addRelay: (url: string) => setInboxRelayPolicy(url, true), + }) - const removeInboxRelay = (url: string) => null + const removeReadRelay = (url: string) => setRelayPolicy(url, false, $writeRelayUrls.includes(url)) + + const removeWriteRelay = (url: string) => setRelayPolicy(url, $readRelayUrls.includes(url), false) + + const removeInboxRelay = (url: string) => setInboxRelayPolicy(url, false)
@@ -47,7 +63,7 @@ {:else}

No relays found

{/each} - @@ -76,7 +92,7 @@ {:else}

No relays found

{/each} - @@ -105,7 +121,7 @@ {:else}

No relays found

{/each} -