2023-07-21 20:28:18 +00:00
|
|
|
import Nostrich from "public/logo_256.png";
|
2023-01-27 21:10:14 +00:00
|
|
|
|
2023-08-17 18:54:14 +00:00
|
|
|
import { TaggedNostrEvent, EventKind, MetadataCache } from "@snort/system";
|
2023-01-27 21:10:14 +00:00
|
|
|
import { getDisplayName } from "Element/ProfileImage";
|
|
|
|
import { MentionRegex } from "Const";
|
2023-05-24 10:12:23 +00:00
|
|
|
import { tagFilterOfTextRepost, unwrap } from "SnortUtils";
|
2023-06-26 10:30:37 +00:00
|
|
|
import { UserCache } from "Cache";
|
2023-04-14 11:33:19 +00:00
|
|
|
import { LoginSession } from "Login";
|
|
|
|
|
|
|
|
export interface NotificationRequest {
|
|
|
|
title: string;
|
|
|
|
body: string;
|
|
|
|
icon: string;
|
|
|
|
timestamp: number;
|
|
|
|
}
|
2023-01-27 21:10:14 +00:00
|
|
|
|
2023-08-17 18:54:14 +00:00
|
|
|
export async function makeNotification(ev: TaggedNostrEvent): Promise<NotificationRequest | null> {
|
2023-02-07 20:04:50 +00:00
|
|
|
switch (ev.kind) {
|
|
|
|
case EventKind.TextNote: {
|
2023-02-16 14:11:29 +00:00
|
|
|
if (ev.tags.some(tagFilterOfTextRepost(ev))) {
|
2023-02-16 09:16:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-02-09 12:26:54 +00:00
|
|
|
const pubkeys = new Set([ev.pubkey, ...ev.tags.filter(a => a[0] === "p").map(a => a[1])]);
|
2023-03-03 14:30:31 +00:00
|
|
|
await UserCache.buffer([...pubkeys]);
|
|
|
|
const allUsers = [...pubkeys]
|
2023-03-29 12:10:22 +00:00
|
|
|
.map(a => UserCache.getFromCache(a))
|
2023-03-03 14:30:31 +00:00
|
|
|
.filter(a => a)
|
|
|
|
.map(a => unwrap(a));
|
2023-03-29 12:10:22 +00:00
|
|
|
const fromUser = UserCache.getFromCache(ev.pubkey);
|
2023-02-07 20:04:50 +00:00
|
|
|
const name = getDisplayName(fromUser, ev.pubkey);
|
2023-02-07 19:47:57 +00:00
|
|
|
const avatarUrl = fromUser?.picture || Nostrich;
|
2023-02-07 20:04:50 +00:00
|
|
|
return {
|
|
|
|
title: `Reply from ${name}`,
|
2023-03-03 14:30:31 +00:00
|
|
|
body: replaceTagsWithUser(ev, allUsers).substring(0, 50),
|
2023-02-07 20:04:50 +00:00
|
|
|
icon: avatarUrl,
|
|
|
|
timestamp: ev.created_at * 1000,
|
|
|
|
};
|
2023-01-27 21:10:14 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
return null;
|
2023-01-27 21:10:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 18:54:14 +00:00
|
|
|
function replaceTagsWithUser(ev: TaggedNostrEvent, users: MetadataCache[]) {
|
2023-02-07 20:04:50 +00:00
|
|
|
return ev.content
|
|
|
|
.split(MentionRegex)
|
2023-02-09 12:26:54 +00:00
|
|
|
.map(match => {
|
2023-02-07 19:47:57 +00:00
|
|
|
const matchTag = match.match(/#\[(\d+)\]/);
|
2023-02-07 20:04:50 +00:00
|
|
|
if (matchTag && matchTag.length === 2) {
|
2023-02-07 19:47:57 +00:00
|
|
|
const idx = parseInt(matchTag[1]);
|
|
|
|
const ref = ev.tags[idx];
|
2023-02-07 20:04:50 +00:00
|
|
|
if (ref && ref[0] === "p" && ref.length > 1) {
|
2023-02-09 12:26:54 +00:00
|
|
|
const u = users.find(a => a.pubkey === ref[1]);
|
2023-02-07 20:04:50 +00:00
|
|
|
return `@${getDisplayName(u, ref[1])}`;
|
2023-01-27 21:10:14 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
return match;
|
|
|
|
})
|
|
|
|
.join();
|
2023-01-27 21:10:14 +00:00
|
|
|
}
|
2023-04-14 11:33:19 +00:00
|
|
|
|
|
|
|
export async function sendNotification(state: LoginSession, req: NotificationRequest) {
|
|
|
|
const hasPermission = "Notification" in window && Notification.permission === "granted";
|
|
|
|
const shouldShowNotification = hasPermission && req.timestamp > state.readNotifications;
|
|
|
|
if (shouldShowNotification) {
|
|
|
|
try {
|
|
|
|
const worker = await navigator.serviceWorker.ready;
|
|
|
|
worker.showNotification(req.title, {
|
|
|
|
tag: "notification",
|
|
|
|
vibrate: [500],
|
|
|
|
...req,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|