From a351d9d792e39bc4b012091399e5efa806d2dee9 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Tue, 8 Oct 2024 13:20:55 -0700 Subject: [PATCH] Get nip 17 chat working --- src/app/commands.ts | 15 ++- src/app/components/ChatMessage.svelte | 112 +++++++++++----------- src/app/components/RoomCreate.svelte | 3 +- src/app/state.ts | 21 +--- src/routes/+layout.svelte | 42 ++++---- src/routes/home/+layout.svelte | 7 +- src/routes/home/[chat]/+page.svelte | 13 +-- src/routes/spaces/[nrelay]/+layout.svelte | 12 ++- 8 files changed, 106 insertions(+), 119 deletions(-) diff --git a/src/app/commands.ts b/src/app/commands.ts index f21dd8c..a735ceb 100644 --- a/src/app/commands.ts +++ b/src/app/commands.ts @@ -125,7 +125,7 @@ export const updateList = async (kind: number, modifyTags: ModifyTags) => { ? {...prev, tags: modifyTags(prev.tags)} : createEvent(kind, {tags: modifyTags([])}) - publishThunk(makeThunk({event, relays})) + return publishThunk(makeThunk({event, relays})) } export const addSpaceMembership = (url: string) => @@ -215,13 +215,12 @@ export const sendWrapped = async ({ await Promise.all( uniq(pubkeys).map(async recipient => { - const rumor = await nip59.wrap(recipient, stamp(template)) - const thunk = makeThunk({ - event: rumor.wrap, - relays: ctx.app.router.PublishMessage(recipient).getUrls(), - }) - - return publishThunk(thunk) + return publishThunk( + makeThunk({ + event: await nip59.wrap(recipient, stamp(template)), + relays: ctx.app.router.PublishMessage(recipient).getUrls(), + }) + ) }), ) } diff --git a/src/app/components/ChatMessage.svelte b/src/app/components/ChatMessage.svelte index 8a78e91..aa20a21 100644 --- a/src/app/components/ChatMessage.svelte +++ b/src/app/components/ChatMessage.svelte @@ -50,65 +50,67 @@ - {/each} - - {/if} - + {/each} + + {/if} + - + diff --git a/src/app/components/RoomCreate.svelte b/src/app/components/RoomCreate.svelte index b3c21f5..1c4bfc0 100644 --- a/src/app/components/RoomCreate.svelte +++ b/src/app/components/RoomCreate.svelte @@ -13,8 +13,7 @@ const back = () => history.back() const tryCreate = async () => { - await addRoomMembership(url, room) - + addRoomMembership(url, room) goto(makeSpacePath(url, room)) } diff --git a/src/app/state.ts b/src/app/state.ts index c631afd..74031e1 100644 --- a/src/app/state.ts +++ b/src/app/state.ts @@ -149,7 +149,10 @@ export const ensureUnwrapped = async (event: TrustedEvent) => { } if (rumor && isHashedEvent(rumor)) { + // Copy urls over to the rumor tracker.copy(event.id, rumor.id) + + // Send the rumor via our relay so listeners get updated relay.send("EVENT", rumor) } @@ -187,12 +190,6 @@ setContext({ }), }) -repository.on("update", ({added}) => { - for (const event of added) { - ensureUnwrapped(event) - } -}) - export const deriveEvent = (idOrAddress: string, hints: string[] = []) => { let attempted = false @@ -350,7 +347,7 @@ export const chats = derived( const messagesByChatId = new Map() for (const message of $messages) { - const chatId = makeChatId(getPubkeyTagValues(message.tags)) + const chatId = makeChatId(getPubkeyTagValues(message.tags).concat(message.pubkey)) pushToMapKey(messagesByChatId, chatId, message) } @@ -383,16 +380,6 @@ export const { name: "chats", store: chats, getKey: chat => chat.id, - load: async (id: string, request: Partial = {}) => { - const $pubkey = pubkey.get() - const chat = get(chatsById).get(id) - const timestamps = chat?.messages.map(e => e.created_at) || [] - const since = Math.max(0, max(timestamps) - 3600) - - if ($pubkey) { - await load({...request, filters: [{kinds: [WRAP], "#p": [$pubkey], since}]}) - } - }, }) export const chatSearch = derived(chats, $chats => diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index de9b010..c533529 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -68,28 +68,32 @@ } } - onMount(() => { + onMount(async () => { Object.assign(window, {get, ...app, ...state}) - ready = db - ? Promise.resolve() - : initStorage("flotilla", 4, { - events: storageAdapters.fromRepository(repository, {throttle: 300}), - relays: {keyPath: "url", store: throttled(1000, relays)}, - handles: {keyPath: "nip05", store: throttled(1000, handles)}, - publishStatus: storageAdapters.fromObjectStore(publishStatusData), - freshness: storageAdapters.fromObjectStore(freshness, {throttle: 1000}), - plaintext: storageAdapters.fromObjectStore(plaintext, {throttle: 1000}), - tracker: storageAdapters.fromTracker(tracker, {throttle: 1000}), - }).then(() => sleep(300)) // Wait an extra few ms because of repository throttle + if (!db) { + await initStorage("flotilla", 4, { + events: storageAdapters.fromRepository(repository, {throttle: 300}), + relays: {keyPath: "url", store: throttled(1000, relays)}, + handles: {keyPath: "nip05", store: throttled(1000, handles)}, + publishStatus: storageAdapters.fromObjectStore(publishStatusData), + freshness: storageAdapters.fromObjectStore(freshness, {throttle: 1000}), + plaintext: storageAdapters.fromObjectStore(plaintext, {throttle: 1000}), + tracker: storageAdapters.fromTracker(tracker, {throttle: 1000}), + }) - dialog.addEventListener("close", () => { - if (modal) { - clearModal() - } - }) + repository.on("update", ({added}) => { + for (const event of added) { + state.ensureUnwrapped(event) + } + }) + + dialog.addEventListener("close", () => { + if (modal) { + clearModal() + } + }) - ready.then(() => { for (const url of INDEXER_RELAYS) { loadRelay(url) } @@ -97,7 +101,7 @@ if ($pubkey) { loadUserData($pubkey) } - }) + } }) diff --git a/src/routes/home/+layout.svelte b/src/routes/home/+layout.svelte index c328879..f7a8071 100644 --- a/src/routes/home/+layout.svelte +++ b/src/routes/home/+layout.svelte @@ -1,5 +1,6 @@