flotilla/src/app/notifications.ts

103 lines
3 KiB
TypeScript
Raw Normal View History

import {derived} from "svelte/store"
2025-01-02 18:04:28 +00:00
import {synced, throttled} from "@welshman/store"
2024-12-16 19:24:10 +00:00
import {pubkey} from "@welshman/app"
2025-01-03 00:58:04 +00:00
import {prop, spec, identity, now, groupBy} from "@welshman/lib"
2024-12-16 19:24:10 +00:00
import type {TrustedEvent} from "@welshman/util"
2025-01-03 00:58:04 +00:00
import {MESSAGE, COMMENT, getTagValue} from "@welshman/util"
2024-12-16 19:24:10 +00:00
import {makeSpacePath, makeChatPath, makeThreadPath, makeRoomPath} from "@app/routes"
2024-12-16 18:30:42 +00:00
import {
2024-12-16 19:24:10 +00:00
THREAD_FILTER,
COMMENT_FILTER,
chats,
2025-01-02 18:04:28 +00:00
getUrlsForEvent,
2024-12-16 18:30:42 +00:00
userRoomsByUrl,
repositoryStore,
} from "@app/state"
2024-11-14 18:53:13 +00:00
// Checked state
export const checked = synced<Record<string, number>>("checked", {})
2024-11-14 18:53:13 +00:00
export const deriveChecked = (key: string) => derived(checked, prop(key))
2024-12-16 19:49:57 +00:00
export const setChecked = (key: string) => checked.update(state => ({...state, [key]: now()}))
2024-11-14 18:53:13 +00:00
2024-12-16 19:24:10 +00:00
// Derived notifications state
2024-11-14 18:53:13 +00:00
2024-12-16 19:24:10 +00:00
export const notifications = derived(
2025-01-02 18:04:28 +00:00
throttled(
1000,
derived([pubkey, checked, chats, userRoomsByUrl, repositoryStore, getUrlsForEvent], identity),
),
([$pubkey, $checked, $chats, $userRoomsByUrl, $repository, $getUrlsForEvent]) => {
const hasNotification = (path: string, latestEvent: TrustedEvent | undefined) => {
2024-12-16 19:24:10 +00:00
if (!latestEvent || latestEvent.pubkey === $pubkey) {
return false
}
2024-11-14 18:53:13 +00:00
2024-12-16 19:49:57 +00:00
for (const [entryPath, ts] of Object.entries($checked)) {
const isMatch = entryPath === "*" || entryPath.startsWith(path)
2024-11-15 22:03:54 +00:00
2024-12-16 19:49:57 +00:00
if (isMatch && ts > latestEvent.created_at) {
return false
}
2024-12-16 19:24:10 +00:00
}
2024-11-15 22:03:54 +00:00
2024-12-16 19:49:57 +00:00
return true
2024-12-16 19:24:10 +00:00
}
2024-11-14 18:53:13 +00:00
2024-12-16 19:24:10 +00:00
const paths = new Set<string>()
2024-11-14 20:16:44 +00:00
2024-12-16 19:24:10 +00:00
for (const {pubkeys, messages} of $chats) {
const chatPath = makeChatPath(pubkeys)
2024-12-16 18:30:42 +00:00
2025-01-02 18:04:28 +00:00
if (hasNotification(chatPath, messages[0])) {
2024-12-16 19:24:10 +00:00
paths.add("/chat")
paths.add(chatPath)
}
2024-12-16 18:30:42 +00:00
}
2025-01-02 18:04:28 +00:00
const allThreadEvents = $repository.query([THREAD_FILTER, COMMENT_FILTER])
const allMessageEvents = $repository.query([{kinds: [MESSAGE]}])
2024-12-16 19:24:10 +00:00
for (const [url, rooms] of $userRoomsByUrl.entries()) {
const spacePath = makeSpacePath(url)
const threadPath = makeThreadPath(url)
2025-01-03 00:58:04 +00:00
const threadEvents = allThreadEvents.filter(e => $getUrlsForEvent(e.id).includes(url))
2024-12-16 19:24:10 +00:00
2025-01-03 00:58:04 +00:00
if (hasNotification(threadPath, threadEvents[0])) {
2024-12-16 19:24:10 +00:00
paths.add(spacePath)
paths.add(threadPath)
}
2025-01-03 00:58:04 +00:00
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)
}
}
2024-12-16 19:24:10 +00:00
for (const room of rooms) {
const roomPath = makeRoomPath(url, room)
2025-01-02 18:04:28 +00:00
const latestEvent = allMessageEvents.find(
e =>
$getUrlsForEvent(e.id).includes(url) && e.tags.find(t => t[0] === "h" && t[1] === room),
)
2024-12-16 19:24:10 +00:00
2025-01-02 18:04:28 +00:00
if (hasNotification(roomPath, latestEvent)) {
2024-12-16 19:24:10 +00:00
paths.add(spacePath)
paths.add(roomPath)
2024-12-16 18:30:42 +00:00
}
2024-12-16 19:24:10 +00:00
}
}
2024-12-16 18:30:42 +00:00
2024-12-16 19:24:10 +00:00
return paths
2024-11-14 20:16:44 +00:00
},
)