From b6defe59a8d06100b9c0cd57fd1b9f83a654853b Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 2 Jan 2025 16:58:04 -0800 Subject: [PATCH] Improve loading and notifications --- src/app/notifications.ts | 21 +++++++++++++++++---- src/app/requests.ts | 2 +- src/routes/spaces/[relay]/+layout.svelte | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/app/notifications.ts b/src/app/notifications.ts index 1c45c09..78b3ea7 100644 --- a/src/app/notifications.ts +++ b/src/app/notifications.ts @@ -1,9 +1,9 @@ import {derived} from "svelte/store" import {synced, throttled} from "@welshman/store" import {pubkey} from "@welshman/app" -import {prop, identity, now} from "@welshman/lib" +import {prop, spec, identity, now, groupBy} from "@welshman/lib" import type {TrustedEvent} from "@welshman/util" -import {MESSAGE} from "@welshman/util" +import {MESSAGE, COMMENT, getTagValue} from "@welshman/util" import {makeSpacePath, makeChatPath, makeThreadPath, makeRoomPath} from "@app/routes" import { THREAD_FILTER, @@ -63,13 +63,26 @@ export const notifications = derived( for (const [url, rooms] of $userRoomsByUrl.entries()) { const spacePath = makeSpacePath(url) const threadPath = makeThreadPath(url) - const latestEvent = allThreadEvents.find(e => $getUrlsForEvent(e.id).includes(url)) + const threadEvents = allThreadEvents.filter(e => $getUrlsForEvent(e.id).includes(url)) - if (hasNotification(threadPath, latestEvent)) { + if (hasNotification(threadPath, threadEvents[0])) { paths.add(spacePath) paths.add(threadPath) } + const commentsByThreadId = groupBy( + e => getTagValue("E", e.tags), + threadEvents.filter(spec({kind: COMMENT})), + ) + + for (const [threadId, [comment]] of commentsByThreadId.entries()) { + const threadItemPath = makeThreadPath(url, threadId) + + if (hasNotification(threadItemPath, comment)) { + paths.add(threadItemPath) + } + } + for (const room of rooms) { const roomPath = makeRoomPath(url, room) const latestEvent = allMessageEvents.find( diff --git a/src/app/requests.ts b/src/app/requests.ts index a4d9faa..708c9e6 100644 --- a/src/app/requests.ts +++ b/src/app/requests.ts @@ -50,7 +50,7 @@ export const listenForNotifications = () => { filters: [ {kinds: [THREAD], since: now()}, {kinds: [COMMENT], "#K": [String(THREAD)], since: now()}, - {kinds: [MESSAGE], "#h": Array.from(rooms), since: now()}, + ...Array.from(rooms).map(room => ({kinds: [MESSAGE], "#h": [room], since: now()})), ], }), ) diff --git a/src/routes/spaces/[relay]/+layout.svelte b/src/routes/spaces/[relay]/+layout.svelte index b9f5c11..2c3dbb1 100644 --- a/src/routes/spaces/[relay]/+layout.svelte +++ b/src/routes/spaces/[relay]/+layout.svelte @@ -64,8 +64,8 @@ // Load recent messages for user rooms to help with a quick page transition pullConservatively({relays, filters: rooms.map(r => ({kinds: [MESSAGE], "#h": [r], since}))}) - // Listen for deletes that would apply to messages we already have - const sub = subscribe({relays, filters: [{kinds: [DELETE], since}]}) + // Listen for deletes that would apply to messages we already have, and new groups + const sub = subscribe({relays, filters: [{kinds: [DELETE, GROUPS], since}]}) return () => { sub.close()