fix: compact push notifications

This commit is contained in:
2023-10-19 16:38:14 +01:00
parent 3f28c94a56
commit 234d354062

View File

@ -3,8 +3,8 @@ declare const self: ServiceWorkerGlobalScope & {
__WB_MANIFEST: (string | PrecacheEntry)[]; __WB_MANIFEST: (string | PrecacheEntry)[];
}; };
import { NostrEvent, NostrLink, NostrPrefix, mapEventToProfile, tryParseNostrLink } from "@snort/system"; import { NostrLink, NostrPrefix, tryParseNostrLink } from "@snort/system";
import { defaultAvatar, getDisplayName } from "SnortUtils"; import { defaultAvatar, hexToBech32 } from "SnortUtils";
import { clientsClaim } from "workbox-core"; import { clientsClaim } from "workbox-core";
import { PrecacheEntry, precacheAndRoute } from "workbox-precaching"; import { PrecacheEntry, precacheAndRoute } from "workbox-precaching";
@ -26,9 +26,18 @@ interface PushNotification {
data: object; data: object;
} }
interface PushNotificationMention { interface CompactMention {
profiles: Array<NostrEvent>; id: string;
events: Array<NostrEvent>; created_at: number;
content: string;
author: CompactProfile;
mentions: Array<CompactProfile>;
}
interface CompactProfile {
pubkey: string;
name?: string;
avatar?: string;
} }
self.addEventListener("notificationclick", event => { self.addEventListener("notificationclick", event => {
@ -52,20 +61,15 @@ self.addEventListener("push", async e => {
if (data) { if (data) {
switch (data.type) { switch (data.type) {
case PushType.Mention: { case PushType.Mention: {
const mention = data.data as PushNotificationMention; const evx = data.data as CompactMention;
for (const ev of mention.events) {
const userEvent = mention.profiles.find(a => a.pubkey === ev.pubkey);
const userProfile = userEvent ? mapEventToProfile(userEvent) : undefined;
const avatarUrl = userProfile?.picture ?? defaultAvatar(ev.pubkey);
await self.registration.showNotification(`Reply from ${getDisplayName(userProfile, ev.pubkey)}`, { await self.registration.showNotification(`Reply from ${displayNameOrDefault(evx.author)}`, {
body: replaceMentions(ev.content, mention.profiles).substring(0, 250), body: replaceMentions(evx.content, evx.mentions).substring(0, 250),
icon: avatarUrl, icon: evx.author.avatar ?? defaultAvatar(evx.author.pubkey),
timestamp: ev.created_at * 1000, timestamp: evx.created_at * 1000,
tag: NostrLink.fromEvent(ev).encode(), tag: new NostrLink(NostrPrefix.Event, evx.id, undefined, evx.author.pubkey, undefined).encode(),
vibrate: [500], vibrate: [500],
}); });
}
break; break;
} }
} }
@ -74,7 +78,7 @@ self.addEventListener("push", async e => {
const MentionNostrEntityRegex = /(nostr:n(?:pub|profile|event|ote|addr)1[acdefghjklmnpqrstuvwxyz023456789]+)/g; const MentionNostrEntityRegex = /(nostr:n(?:pub|profile|event|ote|addr)1[acdefghjklmnpqrstuvwxyz023456789]+)/g;
function replaceMentions(content: string, profiles: Array<NostrEvent>) { function replaceMentions(content: string, profiles: Array<CompactProfile>) {
return content return content
.split(MentionNostrEntityRegex) .split(MentionNostrEntityRegex)
.map(i => { .map(i => {
@ -82,11 +86,17 @@ function replaceMentions(content: string, profiles: Array<NostrEvent>) {
const link = tryParseNostrLink(i); const link = tryParseNostrLink(i);
if (link?.type === NostrPrefix.PublicKey || link?.type === NostrPrefix.Profile) { if (link?.type === NostrPrefix.PublicKey || link?.type === NostrPrefix.Profile) {
const px = profiles.find(a => a.pubkey === link.id); const px = profiles.find(a => a.pubkey === link.id);
const profile = px && mapEventToProfile(px); return `@${displayNameOrDefault(px ?? { pubkey: link.id })}`;
return `@${getDisplayName(profile, link.id)}`;
} }
} }
return i; return i;
}) })
.join(); .join();
} }
function displayNameOrDefault(p: CompactProfile) {
if ((p.name?.length ?? 0) > 0) {
return p.name;
}
return hexToBech32("npub", p.pubkey).slice(0, 12);
}