flotilla/src/app/notifications.ts

92 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-11-14 18:53:13 +00:00
import {writable, derived} from "svelte/store"
2024-11-15 22:03:54 +00:00
import {page} from "$app/stores"
2024-12-16 19:24:10 +00:00
import {pubkey} from "@welshman/app"
import {prop, max, sortBy, now} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
import {MESSAGE} from "@welshman/util"
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,
2024-12-16 18:30:42 +00:00
getEventsForUrl,
userRoomsByUrl,
repositoryStore,
} from "@app/state"
2024-11-14 18:53:13 +00:00
// Checked state
export const checked = writable<Record<string, number>>({})
export const deriveChecked = (key: string) => derived(checked, prop(key))
2024-12-16 19:24:10 +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(
[pubkey, checked, chats, userRoomsByUrl, repositoryStore],
([$pubkey, $checked, $chats, $userRoomsByUrl, $repository]) => {
const hasNotification = (path: string, events: TrustedEvent[]) => {
const [latestEvent] = sortBy($e => -$e.created_at, events)
2024-11-14 18:53:13 +00:00
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:24:10 +00:00
let checkPath = ""
let lastChecked = $checked["*"]
2024-11-15 22:03:54 +00:00
2024-12-16 19:24:10 +00:00
for (const segment of path.slice(1).split("/")) {
checkPath += "/" + segment
lastChecked = max([lastChecked, $checked[checkPath]])
}
2024-11-15 22:03:54 +00:00
2024-12-16 19:24:10 +00:00
return lastChecked < latestEvent.created_at
}
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
2024-12-16 19:24:10 +00:00
if (hasNotification(chatPath, messages)) {
paths.add("/chat")
paths.add(chatPath)
}
2024-12-16 18:30:42 +00:00
}
2024-12-16 19:24:10 +00:00
for (const [url, rooms] of $userRoomsByUrl.entries()) {
const spacePath = makeSpacePath(url)
const threadPath = makeThreadPath(url)
const threadFilters = [THREAD_FILTER, COMMENT_FILTER]
const threadEvents = getEventsForUrl($repository, url, threadFilters)
if (hasNotification(threadPath, threadEvents)) {
paths.add(spacePath)
paths.add(threadPath)
}
for (const room of rooms) {
const roomPath = makeRoomPath(url, room)
const roomFilters = [{kinds: [MESSAGE], "#h": [room]}]
const roomEvents = getEventsForUrl($repository, url, roomFilters)
if (hasNotification(roomPath, roomEvents)) {
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
},
)
2024-11-15 22:03:54 +00:00
2024-12-16 19:24:10 +00:00
export const inactiveNotifications = derived(
[page, notifications],
([$page, $notifications]) =>
new Set(Array.from($notifications).filter(path => !$page.url.pathname.startsWith(path))),
2024-11-15 22:03:54 +00:00
)