Fix chat notifications so they take nip 29 into account

This commit is contained in:
Jon Staab
2025-07-17 15:35:49 -07:00
parent a12eddb47b
commit 83abb5aa94
+23 -17
View File
@@ -1,6 +1,6 @@
import {derived} from "svelte/store" import {derived} from "svelte/store"
import {synced, localStorageProvider, throttled} from "@welshman/store" import {synced, localStorageProvider, throttled} from "@welshman/store"
import {pubkey} from "@welshman/app" import {pubkey, relaysByUrl} from "@welshman/app"
import {prop, spec, identity, now, groupBy} from "@welshman/lib" import {prop, spec, identity, now, groupBy} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util" import type {TrustedEvent} from "@welshman/util"
import {EVENT_TIME, MESSAGE, THREAD, COMMENT, getTagValue} from "@welshman/util" import {EVENT_TIME, MESSAGE, THREAD, COMMENT, getTagValue} from "@welshman/util"
@@ -12,7 +12,7 @@ import {
makeSpaceChatPath, makeSpaceChatPath,
makeRoomPath, makeRoomPath,
} from "@app/routes" } from "@app/routes"
import {chats, getUrlsForEvent, userRoomsByUrl, repositoryStore} from "@app/state" import {chats, hasNip29, getUrlsForEvent, userRoomsByUrl, repositoryStore} from "@app/state"
// Checked state // Checked state
@@ -31,9 +31,12 @@ export const setChecked = (key: string) => checked.update(state => ({...state, [
export const notifications = derived( export const notifications = derived(
throttled( throttled(
1000, 1000,
derived([pubkey, checked, chats, userRoomsByUrl, repositoryStore, getUrlsForEvent], identity), derived(
[pubkey, checked, chats, userRoomsByUrl, repositoryStore, getUrlsForEvent, relaysByUrl],
identity,
),
), ),
([$pubkey, $checked, $chats, $userRoomsByUrl, $repository, $getUrlsForEvent]) => { ([$pubkey, $checked, $chats, $userRoomsByUrl, $repository, $getUrlsForEvent, $relaysByUrl]) => {
const hasNotification = (path: string, latestEvent: TrustedEvent | undefined) => { const hasNotification = (path: string, latestEvent: TrustedEvent | undefined) => {
if (!latestEvent || latestEvent.pubkey === $pubkey) { if (!latestEvent || latestEvent.pubkey === $pubkey) {
return false return false
@@ -95,11 +98,6 @@ export const notifications = derived(
paths.add(calendarPath) paths.add(calendarPath)
} }
if (hasNotification(messagesPath, messagesEvents[0])) {
paths.add(spacePath)
paths.add(messagesPath)
}
const commentsByThreadId = groupBy( const commentsByThreadId = groupBy(
e => getTagValue("E", e.tags), e => getTagValue("E", e.tags),
threadEvents.filter(spec({kind: COMMENT})), threadEvents.filter(spec({kind: COMMENT})),
@@ -126,16 +124,24 @@ export const notifications = derived(
} }
} }
for (const room of rooms) { if (hasNip29($relaysByUrl.get(url))) {
const roomPath = makeRoomPath(url, room) for (const room of rooms) {
const latestEvent = allMessageEvents.find( const roomPath = makeRoomPath(url, room)
e => const latestEvent = allMessageEvents.find(
$getUrlsForEvent(e.id).includes(url) && e.tags.find(t => t[0] === "h" && t[1] === room), e =>
) $getUrlsForEvent(e.id).includes(url) &&
e.tags.find(t => t[0] === "h" && t[1] === room),
)
if (hasNotification(roomPath, latestEvent)) { if (hasNotification(roomPath, latestEvent)) {
paths.add(spacePath)
paths.add(roomPath)
}
}
} else {
if (hasNotification(messagesPath, messagesEvents[0])) {
paths.add(spacePath) paths.add(spacePath)
paths.add(roomPath) paths.add(messagesPath)
} }
} }
} }