From 394a1e7d30309c1e2a85f7b3a09026e2a596dc3e Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 14 Apr 2025 16:45:52 -0700 Subject: [PATCH] Update to new thunk stuff --- src/app/commands.ts | 47 +++++----- src/app/components/ChannelMessage.svelte | 7 +- src/app/components/ThunkStatus.svelte | 107 ++++++++++++++--------- src/app/state.ts | 9 +- 4 files changed, 102 insertions(+), 68 deletions(-) diff --git a/src/app/commands.ts b/src/app/commands.ts index 31ab89a1..ea955cff 100644 --- a/src/app/commands.ts +++ b/src/app/commands.ts @@ -1,6 +1,6 @@ import * as nip19 from "nostr-tools/nip19" import {get} from "svelte/store" -import {randomId, poll, uniq, equals} from "@welshman/lib" +import {randomId, ifLet, poll, uniq, equals} from "@welshman/lib" import { DELETE, REPORT, @@ -38,7 +38,7 @@ import { signer, repository, publishThunk, - publishThunks, + MergedThunk, profilesByPubkey, relaySelectionsByPubkey, getWriteRelayUrls, @@ -54,6 +54,7 @@ import { tagEventForComment, tagEventForQuote, Router, + thunkIsComplete, } from "@welshman/app" import type {Thunk} from "@welshman/app" import { @@ -84,14 +85,20 @@ export const getPubkeyPetname = (pubkey: string) => { return display } -export const getThunkError = async (thunk: Thunk) => { - const result = await thunk.result - const [{status, message}] = Object.values(result) as any +export const getThunkError = (thunk: Thunk) => + new Promise(resolve => { + thunk.subscribe($thunk => { + for (const [relay, status] of Object.entries($thunk.status)) { + if (status === PublishStatus.Failure) { + resolve($thunk.details[relay]) + } + } - if (status !== PublishStatus.Success) { - return message - } -} + if (thunkIsComplete($thunk)) { + resolve("") + } + }) + }) export const prependParent = (parent: TrustedEvent | undefined, {content, tags}: EventContent) => { if (parent) { @@ -261,12 +268,10 @@ export const checkRelayAccess = async (url: string, claim = "") => { relays: [url], }) - const result = await thunk.result - - if (result[url].status === PublishStatus.Failure) { + ifLet(await getThunkError(thunk), error => { const message = socket.auth.details?.replace(/^.*: /, "") || - result[url].message?.replace(/^.*: /, "") || + error?.replace(/^.*: /, "") || "join request rejected" // If it's a strict NIP 29 relay don't worry about requesting access @@ -274,7 +279,7 @@ export const checkRelayAccess = async (url: string, claim = "") => { if (message !== "missing group (`h`) tag") { return `Failed to join relay (${message})` } - } + }) } export const checkRelayProfile = async (url: string) => { @@ -342,13 +347,15 @@ export const sendWrapped = async ({ }) => { const nip59 = Nip59.fromSigner(signer.get()!) - return publishThunks( + return new MergedThunk( await Promise.all( - uniq(pubkeys).map(async recipient => ({ - event: await nip59.wrap(recipient, stamp(template)), - relays: Router.get().PubkeyInbox(recipient).getUrls(), - delay, - })), + uniq(pubkeys).map(async recipient => + publishThunk({ + event: await nip59.wrap(recipient, stamp(template)), + relays: Router.get().PubkeyInbox(recipient).getUrls(), + delay, + }), + ), ), ) } diff --git a/src/app/components/ChannelMessage.svelte b/src/app/components/ChannelMessage.svelte index 4d4e89ad..a59e79f6 100644 --- a/src/app/components/ChannelMessage.svelte +++ b/src/app/components/ChannelMessage.svelte @@ -1,6 +1,5 @@ -{#if isFailure && failure} - {@const [url, {message, status}] = failure} -
- - {#snippet children()} - - - Failed to send! - - {/snippet} - -
-{:else if canCancel || isPending} -
- - - Sending... - {#if canCancel} - +{#if showFailure || showPending} +
+
+ {#if showFailure} + {@const url = failedUrls[0]} + {@const status = $thunk.status[url]} + {@const message = $thunk.details[url]} +
+ + {#snippet children()} + + + Failed to send! + + {/snippet} + +
+ {:else if canCancel || isPending} +
+ + + Sending... + + +
{/if} - +
{/if} diff --git a/src/app/state.ts b/src/app/state.ts index 3349a73b..d6709782 100644 --- a/src/app/state.ts +++ b/src/app/state.ts @@ -251,7 +251,7 @@ export const getUrlsForEvent = derived([trackerStore, thunks], ([$tracker, $thun const urls = Array.from($tracker.getRelays(id)) for (const thunk of getThunksByEventId().get(id) || []) { - for (const url of thunk.request.relays) { + for (const url of thunk.options.relays) { urls.push(url) } } @@ -661,7 +661,12 @@ export const deriveOtherRooms = (url: string) => // Other utils -export const encodeRelay = (url: string) => encodeURIComponent(normalizeRelayUrl(url)) +export const encodeRelay = (url: string) => + encodeURIComponent( + normalizeRelayUrl(url) + .replace(/^wss:\/\//, "") + .replace(/\/$/, ""), + ) export const decodeRelay = (url: string) => normalizeRelayUrl(decodeURIComponent(url))