2026-01-20 18:40:33 +00:00
|
|
|
import type {Unsubscriber} from 'svelte/store'
|
2025-09-23 15:34:53 +00:00
|
|
|
import {derived, get} from "svelte/store"
|
2026-01-20 18:40:33 +00:00
|
|
|
import {Capacitor} from "@capacitor/core"
|
2025-11-12 01:49:22 +00:00
|
|
|
import {Badge} from "@capawesome/capacitor-badge"
|
2025-08-29 15:06:50 +00:00
|
|
|
import {synced, throttled} from "@welshman/store"
|
2025-11-20 23:54:06 +00:00
|
|
|
import {pubkey, tracker, repository, relaysByUrl} from "@welshman/app"
|
2026-01-20 18:40:33 +00:00
|
|
|
import {prop, ms, maybe, int, MINUTE, flatten, find, spec, first, identity, now, groupBy, hash} from "@welshman/lib"
|
2024-12-16 19:24:10 +00:00
|
|
|
import type {TrustedEvent} from "@welshman/util"
|
2026-01-20 18:40:33 +00:00
|
|
|
import {deriveEventsByIdByUrl, deriveEventsById, deriveEventsDesc, deriveDeduplicated} from "@welshman/store"
|
2026-01-07 00:52:15 +00:00
|
|
|
import {
|
2026-01-20 18:40:33 +00:00
|
|
|
DIRECT_MESSAGE,
|
|
|
|
|
DIRECT_MESSAGE_FILE,
|
2026-01-07 00:52:15 +00:00
|
|
|
ZAP_GOAL,
|
|
|
|
|
EVENT_TIME,
|
|
|
|
|
MESSAGE,
|
|
|
|
|
THREAD,
|
|
|
|
|
COMMENT,
|
|
|
|
|
getTagValue,
|
2026-01-20 18:40:33 +00:00
|
|
|
getPubkeyTagValues,
|
|
|
|
|
matchFilters,
|
2026-01-07 00:52:15 +00:00
|
|
|
sortEventsDesc,
|
|
|
|
|
} from "@welshman/util"
|
2025-02-06 17:29:30 +00:00
|
|
|
import {
|
|
|
|
|
makeSpacePath,
|
|
|
|
|
makeChatPath,
|
2025-10-06 18:23:19 +00:00
|
|
|
makeGoalPath,
|
2025-02-06 17:29:30 +00:00
|
|
|
makeThreadPath,
|
|
|
|
|
makeCalendarPath,
|
2025-06-25 00:36:14 +00:00
|
|
|
makeSpaceChatPath,
|
2025-02-06 17:29:30 +00:00
|
|
|
makeRoomPath,
|
2026-01-20 18:40:33 +00:00
|
|
|
goToEvent,
|
2025-08-21 22:01:31 +00:00
|
|
|
} from "@app/util/routes"
|
2025-09-23 15:34:53 +00:00
|
|
|
import {
|
2025-11-20 23:54:06 +00:00
|
|
|
chatsById,
|
2025-09-23 15:34:53 +00:00
|
|
|
hasNip29,
|
2026-01-20 18:40:33 +00:00
|
|
|
getSetting,
|
2025-11-20 23:12:16 +00:00
|
|
|
userGroupList,
|
|
|
|
|
getSpaceUrlsFromGroupList,
|
|
|
|
|
getSpaceRoomsFromGroupList,
|
2025-09-23 15:34:53 +00:00
|
|
|
} from "@app/core/state"
|
2025-11-12 01:49:22 +00:00
|
|
|
import {kv} from "@app/core/storage"
|
2024-11-14 18:53:13 +00:00
|
|
|
|
|
|
|
|
// Checked state
|
|
|
|
|
|
2025-07-15 18:06:47 +00:00
|
|
|
export const checked = synced<Record<string, number>>({
|
|
|
|
|
key: "checked",
|
|
|
|
|
defaultValue: {},
|
2025-11-12 01:49:22 +00:00
|
|
|
storage: kv,
|
2025-07-15 18:06:47 +00:00
|
|
|
})
|
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
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
const goalCommentFilters = [{kinds: [COMMENT], "#K": [String(ZAP_GOAL)]}]
|
|
|
|
|
const threadCommentFilters = [{kinds: [COMMENT], "#K": [String(THREAD)]}]
|
|
|
|
|
const calendarCommentFilters = [{kinds: [COMMENT], "#K": [String(EVENT_TIME)]}]
|
|
|
|
|
const messageFilters = [{kinds: [MESSAGE, THREAD, ZAP_GOAL, EVENT_TIME]}]
|
|
|
|
|
const dmFilters = [{kinds: [DIRECT_MESSAGE, DIRECT_MESSAGE_FILE]}]
|
|
|
|
|
const allFilters = flatten([goalCommentFilters, threadCommentFilters, calendarCommentFilters, messageFilters, dmFilters])
|
|
|
|
|
|
|
|
|
|
export const latestNotification = deriveDeduplicated(
|
|
|
|
|
deriveEventsDesc(deriveEventsById({repository, filters: allFilters})),
|
|
|
|
|
first
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export const notifications = derived(
|
|
|
|
|
throttled(
|
|
|
|
|
1000,
|
|
|
|
|
derived(
|
|
|
|
|
[
|
|
|
|
|
pubkey,
|
|
|
|
|
checked,
|
|
|
|
|
chatsById,
|
|
|
|
|
userGroupList,
|
|
|
|
|
relaysByUrl,
|
|
|
|
|
deriveEventsByIdByUrl({tracker, repository, filters: goalCommentFilters}),
|
|
|
|
|
deriveEventsByIdByUrl({tracker, repository, filters: threadCommentFilters}),
|
|
|
|
|
deriveEventsByIdByUrl({tracker, repository, filters: calendarCommentFilters}),
|
|
|
|
|
deriveEventsByIdByUrl({tracker, repository, filters: messageFilters}),
|
|
|
|
|
],
|
|
|
|
|
identity,
|
2025-07-17 22:35:49 +00:00
|
|
|
),
|
2026-01-20 18:40:33 +00:00
|
|
|
),
|
|
|
|
|
([
|
|
|
|
|
$pubkey,
|
|
|
|
|
$checked,
|
|
|
|
|
$chatsById,
|
|
|
|
|
$userGroupList,
|
|
|
|
|
$relaysByUrl,
|
|
|
|
|
goalCommentsByUrl,
|
|
|
|
|
threadCommentsByUrl,
|
|
|
|
|
calendarCommentsByUrl,
|
|
|
|
|
messagesByUrl,
|
|
|
|
|
]) => {
|
|
|
|
|
const hasNotification = (path: string, latestEvent: TrustedEvent | undefined) => {
|
|
|
|
|
if (!latestEvent || latestEvent.pubkey === $pubkey) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-11-14 18:53:13 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
for (const [entryPath, ts] of Object.entries($checked)) {
|
|
|
|
|
const isMatch =
|
|
|
|
|
entryPath === "*" ||
|
|
|
|
|
entryPath.startsWith(path) ||
|
|
|
|
|
(entryPath === "/chat/*" && path.startsWith("/chat/"))
|
2024-11-14 20:16:44 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (isMatch && ts > latestEvent.created_at) {
|
|
|
|
|
return false
|
2025-11-20 23:54:06 +00:00
|
|
|
}
|
2024-12-16 19:24:10 +00:00
|
|
|
}
|
2025-02-06 17:29:30 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
return true
|
|
|
|
|
}
|
2025-02-06 17:29:30 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
const paths = new Set<string>()
|
2025-10-06 18:23:19 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
for (const {pubkeys, messages} of $chatsById.values()) {
|
|
|
|
|
const chatPath = makeChatPath(pubkeys)
|
|
|
|
|
|
|
|
|
|
if (hasNotification(chatPath, messages[0])) {
|
|
|
|
|
paths.add("/chat")
|
|
|
|
|
paths.add(chatPath)
|
2025-11-20 23:54:06 +00:00
|
|
|
}
|
2026-01-20 18:40:33 +00:00
|
|
|
}
|
2025-10-06 18:23:19 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
for (const url of getSpaceUrlsFromGroupList($userGroupList)) {
|
|
|
|
|
const spacePath = makeSpacePath(url)
|
|
|
|
|
const spacePathMobile = spacePath + ":mobile"
|
|
|
|
|
const goalPath = makeGoalPath(url)
|
|
|
|
|
const threadPath = makeThreadPath(url)
|
|
|
|
|
const calendarPath = makeCalendarPath(url)
|
|
|
|
|
const messagesPath = makeSpaceChatPath(url)
|
|
|
|
|
const goalComments = sortEventsDesc(goalCommentsByUrl.get(url)?.values() || [])
|
|
|
|
|
const threadComments = sortEventsDesc(threadCommentsByUrl.get(url)?.values() || [])
|
|
|
|
|
const calendarComments = sortEventsDesc(calendarCommentsByUrl.get(url)?.values() || [])
|
|
|
|
|
const messages = sortEventsDesc(messagesByUrl.get(url)?.values() || [])
|
|
|
|
|
|
|
|
|
|
const commentsByGoalId = groupBy(
|
|
|
|
|
e => getTagValue("E", e.tags),
|
|
|
|
|
goalComments.filter(spec({kind: COMMENT})),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for (const [goalId, [comment]] of commentsByGoalId.entries()) {
|
|
|
|
|
const goalItemPath = makeGoalPath(url, goalId)
|
|
|
|
|
|
|
|
|
|
if (hasNotification(goalPath, comment)) {
|
|
|
|
|
paths.add(spacePathMobile)
|
|
|
|
|
paths.add(goalPath)
|
|
|
|
|
}
|
2025-10-06 18:23:19 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (hasNotification(goalItemPath, comment)) {
|
|
|
|
|
paths.add(goalItemPath)
|
2025-10-06 18:23:19 +00:00
|
|
|
}
|
2026-01-20 18:40:33 +00:00
|
|
|
}
|
2025-02-06 17:29:30 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
const commentsByThreadId = groupBy(
|
|
|
|
|
e => getTagValue("E", e.tags),
|
|
|
|
|
threadComments.filter(spec({kind: COMMENT})),
|
|
|
|
|
)
|
2025-01-03 00:58:04 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
for (const [threadId, [comment]] of commentsByThreadId.entries()) {
|
|
|
|
|
const threadItemPath = makeThreadPath(url, threadId)
|
2025-01-03 00:58:04 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (hasNotification(threadPath, comment)) {
|
|
|
|
|
paths.add(spacePathMobile)
|
|
|
|
|
paths.add(threadPath)
|
|
|
|
|
}
|
2025-10-06 18:23:19 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (hasNotification(threadItemPath, comment)) {
|
|
|
|
|
paths.add(threadItemPath)
|
2025-01-03 00:58:04 +00:00
|
|
|
}
|
2026-01-20 18:40:33 +00:00
|
|
|
}
|
2025-01-03 00:58:04 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
const commentsByEventId = groupBy(
|
|
|
|
|
e => getTagValue("E", e.tags),
|
|
|
|
|
calendarComments.filter(spec({kind: COMMENT})),
|
|
|
|
|
)
|
2025-02-06 17:29:30 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
for (const [eventId, [comment]] of commentsByEventId.entries()) {
|
|
|
|
|
const calendarItemPath = makeCalendarPath(url, eventId)
|
2025-10-06 18:23:19 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (hasNotification(calendarPath, comment)) {
|
|
|
|
|
paths.add(spacePathMobile)
|
|
|
|
|
paths.add(calendarPath)
|
|
|
|
|
}
|
2025-02-06 17:29:30 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (hasNotification(calendarItemPath, comment)) {
|
|
|
|
|
paths.add(calendarItemPath)
|
2025-02-06 17:29:30 +00:00
|
|
|
}
|
2026-01-20 18:40:33 +00:00
|
|
|
}
|
2025-02-06 17:29:30 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (hasNip29($relaysByUrl.get(url))) {
|
|
|
|
|
for (const h of getSpaceRoomsFromGroupList(url, $userGroupList)) {
|
|
|
|
|
const roomPath = makeRoomPath(url, h)
|
|
|
|
|
const latestEvent = find(e => e.tags.some(spec(["h", h])), messages)
|
2025-07-17 22:35:49 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
if (hasNotification(roomPath, latestEvent)) {
|
2025-10-06 18:23:19 +00:00
|
|
|
paths.add(spacePathMobile)
|
2025-07-17 22:35:49 +00:00
|
|
|
paths.add(spacePath)
|
2026-01-20 18:40:33 +00:00
|
|
|
paths.add(roomPath)
|
2025-07-17 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-20 18:40:33 +00:00
|
|
|
} else {
|
|
|
|
|
if (hasNotification(messagesPath, first(messages))) {
|
|
|
|
|
paths.add(spacePathMobile)
|
|
|
|
|
paths.add(spacePath)
|
|
|
|
|
paths.add(messagesPath)
|
|
|
|
|
}
|
2024-12-16 19:24:10 +00:00
|
|
|
}
|
2026-01-20 18:40:33 +00:00
|
|
|
}
|
2024-12-16 18:30:42 +00:00
|
|
|
|
2026-01-20 18:40:33 +00:00
|
|
|
return paths
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Badges
|
2025-09-23 15:34:53 +00:00
|
|
|
|
|
|
|
|
export const badgeCount = derived(notifications, notifications => {
|
|
|
|
|
return notifications.size
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-24 17:32:55 +00:00
|
|
|
export const handleBadgeCountChanges = async (count: number) => {
|
2026-01-20 18:40:33 +00:00
|
|
|
if (getSetting<boolean>('alerts_badge')) {
|
2025-09-26 18:20:43 +00:00
|
|
|
try {
|
|
|
|
|
await Badge.set({count})
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// failed to set badge
|
|
|
|
|
}
|
2025-09-23 15:34:53 +00:00
|
|
|
} else {
|
2025-09-24 17:32:55 +00:00
|
|
|
await clearBadges()
|
2025-09-23 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-24 17:32:55 +00:00
|
|
|
export const clearBadges = async () => {
|
|
|
|
|
await Badge.clear()
|
2025-09-23 15:34:53 +00:00
|
|
|
}
|
2026-01-20 18:40:33 +00:00
|
|
|
|
|
|
|
|
// Local notifications
|
|
|
|
|
|
|
|
|
|
interface IAlertsAdapter {
|
|
|
|
|
request: () => Promise<string>
|
|
|
|
|
start: () => Unsubscriber
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CapacitorNotifications implements IAlertsAdapter {
|
|
|
|
|
async request() {
|
|
|
|
|
return "denied"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start() {
|
|
|
|
|
return () => undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class WebNotifications implements IAlertsAdapter {
|
|
|
|
|
async request() {
|
|
|
|
|
if (Notification?.permission === "default") {
|
|
|
|
|
await Notification.requestPermission()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Notification?.permission || "denied"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notify(event: TrustedEvent, title: string, body: string) {
|
|
|
|
|
const notification = new Notification(title, {body, tag: event.id, icon: "/icon.png", badge: "/icon.png"})
|
|
|
|
|
|
|
|
|
|
notification.onclick = () => {
|
|
|
|
|
window.focus()
|
|
|
|
|
goToEvent(event)
|
|
|
|
|
notification.close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onVisibilityChange = () => {
|
|
|
|
|
if (document.visibilityState === "visible") {
|
|
|
|
|
notification.close()
|
|
|
|
|
document.removeEventListener("visibilitychange", onVisibilityChange)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.addEventListener("visibilitychange", onVisibilityChange)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start() {
|
|
|
|
|
let initialized = false
|
|
|
|
|
|
|
|
|
|
return latestNotification.subscribe(event => {
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
initialized = true
|
|
|
|
|
} else if (event && document.hidden && Notification?.permission === "granted") {
|
|
|
|
|
if (getSetting<boolean>('alerts_messages') && matchFilters(dmFilters, event)) {
|
|
|
|
|
this.notify(event, "New direct message", "Someone sent you a direct message.")
|
|
|
|
|
} else if (getSetting<boolean>('alerts_mentions') && event.pubkey !== pubkey.get() && getPubkeyTagValues(event.tags).includes(pubkey.get())) {
|
|
|
|
|
this.notify(event, "Someone mentioned you", "Someone tagged you in a message.")
|
|
|
|
|
} else if (getSetting<boolean>('alerts_spaces')) {
|
|
|
|
|
this.notify(event, "New activity", "Someone posted a new message.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Alerts {
|
|
|
|
|
static _adapter: IAlertsAdapter
|
|
|
|
|
static _unsubscriber: Unsubscriber
|
|
|
|
|
|
|
|
|
|
static _getAdapter() {
|
|
|
|
|
if (!Alerts._adapter) {
|
|
|
|
|
if (Capacitor.isNativePlatform()) {
|
|
|
|
|
Alerts._adapter = new CapacitorNotifications()
|
|
|
|
|
} else {
|
|
|
|
|
Alerts._adapter = new WebNotifications()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Alerts._adapter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static start() {
|
|
|
|
|
return Alerts._getAdapter().start()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static request() {
|
|
|
|
|
return Alerts._getAdapter().request()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static resume() {
|
|
|
|
|
if (getSetting<boolean>('alerts_push')) {
|
|
|
|
|
const promise = Alerts.request()
|
|
|
|
|
const controller = new AbortController()
|
|
|
|
|
|
|
|
|
|
promise.then(permissions => {
|
|
|
|
|
if (permissions === "granted" && !controller.signal.aborted) {
|
|
|
|
|
controller.signal.addEventListener('abort', Alerts.start())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Alerts._unsubscriber = () => controller.abort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Alerts.stop
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static stop() {
|
|
|
|
|
Alerts._unsubscriber?.()
|
|
|
|
|
Alerts._unsubscriber = undefined
|
|
|
|
|
}
|
|
|
|
|
}
|