1
0
forked from Kieran/snort

review changes

This commit is contained in:
Kieran 2023-01-19 13:47:24 +00:00
parent 4714912df0
commit 22c0227a89
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -9,8 +9,8 @@ import { RootState } from "../state/Store";
import { db } from "../db";
import useSubscription from "./Subscription";
import { mapEventToProfile, MetadataCache } from "../db/User";
import { hexToBech32 } from "../Util";
import { getDisplayName } from "../element/ProfileImage";
import { MentionRegex } from "../Const";
/**
* Managed loading data for the current logged in user
@ -92,24 +92,43 @@ export default function useLoginFeed() {
async function makeNotification(ev: TaggedRawEvent) {
switch (ev.kind) {
case EventKind.TextNote: {
let from = await db.users.get(ev.pubkey);
let name = getDisplayName(from, ev.pubkey);
const pubkeys = new Set([ev.pubkey, ...ev.tags.filter(a => a[0] === "p").map(a => a[1]!)]);
const users = (await db.users.bulkGet(Array.from(pubkeys))).filter(a => a !== undefined).map(a => a!);
const fromUser = users.find(a => a?.pubkey === ev.pubkey);
const name = getDisplayName(fromUser, ev.pubkey);
const avatarUrl = (fromUser?.picture?.length ?? 0) === 0 ? Nostrich : fromUser?.picture;
return {
title: `Reply from ${name}`,
body: ev.content.substring(0, 50)
body: replaceTagsWithUser(ev, users).substring(0, 50),
icon: avatarUrl
}
}
}
return null;
}
function replaceTagsWithUser(ev: TaggedRawEvent, users: MetadataCache[]) {
return ev.content.split(MentionRegex).map(match => {
let matchTag = match.match(/#\[(\d+)\]/);
if (matchTag && matchTag.length === 2) {
let idx = parseInt(matchTag[1]);
let ref = ev.tags[idx];
if (ref && ref[0] === "p" && ref.length > 1) {
let u = users.find(a => a.pubkey === ref[1]);
return `@${getDisplayName(u, ref[1])}`;
}
}
return match;
}).join();
}
async function sendNotification(ev: TaggedRawEvent) {
let n = await makeNotification(ev);
if (n != null && Notification.permission === "granted") {
let worker = await navigator.serviceWorker.ready;
worker.showNotification(n.title, {
body: n.body,
icon: Nostrich,
icon: n.icon,
tag: "notification",
timestamp: ev.created_at * 1000,
vibrate: [500]