snort/src/Feed/LoginFeed.ts

162 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-01-20 11:11:50 +00:00
import Nostrich from "nostrich.jpg";
2023-01-01 10:44:38 +00:00
import { useEffect, useMemo } from "react";
2022-12-28 23:28:28 +00:00
import { useDispatch, useSelector } from "react-redux";
2023-01-20 11:11:50 +00:00
import { HexKey, TaggedRawEvent } from "Nostr";
import EventKind from "Nostr/EventKind";
import { Subscriptions } from "Nostr/Subscriptions";
import { addDirectMessage, addNotifications, setFollows, setRelays } from "State/Login";
import { RootState } from "State/Store";
import { db } from "Db";
import useSubscription from "Feed/Subscription";
import { mapEventToProfile, MetadataCache } from "Db/User";
import { getDisplayName } from "Element/ProfileImage";
import { MentionRegex } from "Const";
2022-12-28 23:28:28 +00:00
/**
* Managed loading data for the current logged in user
*/
export default function useLoginFeed() {
const dispatch = useDispatch();
2023-01-15 19:40:47 +00:00
const [pubKey, readNotifications] = useSelector<RootState, [HexKey | undefined, number]>(s => [s.login.publicKey, s.login.readNotifications]);
2022-12-28 23:28:28 +00:00
2023-01-22 11:17:50 +00:00
const subMetadata = useMemo(() => {
if (!pubKey) return null;
2023-01-01 10:44:38 +00:00
let sub = new Subscriptions();
2023-01-22 11:17:50 +00:00
sub.Id = `login:meta`;
2023-01-15 19:40:47 +00:00
sub.Authors = new Set([pubKey]);
2023-01-22 11:17:50 +00:00
sub.Kinds = new Set([EventKind.ContactList, EventKind.SetMetadata]);
return sub;
}, [pubKey]);
const subNotification = useMemo(() => {
if (!pubKey) return null;
let sub = new Subscriptions();
sub.Id = "login:notifications";
sub.Kinds = new Set([EventKind.TextNote]);
sub.PTags = new Set([pubKey]);
sub.Limit = 1;
return sub;
}, [pubKey]);
2023-01-01 10:44:38 +00:00
2023-01-22 11:17:50 +00:00
const subDms = useMemo(() => {
if (!pubKey) return null;
2023-01-01 10:44:38 +00:00
2023-01-18 18:53:34 +00:00
let dms = new Subscriptions();
2023-01-22 11:17:50 +00:00
dms.Id = "login:dms";
2023-01-18 18:53:34 +00:00
dms.Kinds = new Set([EventKind.DirectMessage]);
dms.PTags = new Set([pubKey]);
2023-01-22 11:17:50 +00:00
let dmsFromME = new Subscriptions();
dmsFromME.Authors = new Set([pubKey]);
dmsFromME.Kinds = new Set([EventKind.DirectMessage]);
dms.AddSubscription(dmsFromME);
return dms;
2023-01-01 10:44:38 +00:00
}, [pubKey]);
2023-01-22 11:17:50 +00:00
const metadataFeed = useSubscription(subMetadata, { leaveOpen: true });
const notificationFeed = useSubscription(subNotification, { leaveOpen: true });
const dmsFeed = useSubscription(subDms, { leaveOpen: true });
2023-01-01 10:44:38 +00:00
2022-12-28 23:28:28 +00:00
useEffect(() => {
2023-01-22 11:17:50 +00:00
let contactList = metadataFeed.store.notes.filter(a => a.kind === EventKind.ContactList);
let metadata = metadataFeed.store.notes.filter(a => a.kind === EventKind.SetMetadata);
2023-01-15 22:59:05 +00:00
let profiles = metadata.map(a => mapEventToProfile(a))
2023-01-15 19:40:47 +00:00
.filter(a => a !== undefined)
.map(a => a!);
2023-01-01 10:44:38 +00:00
2023-01-03 13:17:09 +00:00
for (let cl of contactList) {
2023-01-22 14:42:19 +00:00
if (cl.content !== "" && cl.content !== "{}") {
2023-01-03 12:06:53 +00:00
let relays = JSON.parse(cl.content);
2023-01-14 18:31:42 +00:00
dispatch(setRelays({ relays, createdAt: cl.created_at }));
2022-12-28 23:28:28 +00:00
}
2023-01-03 12:06:53 +00:00
let pTags = cl.tags.filter(a => a[0] === "p").map(a => a[1]);
2023-01-01 10:44:38 +00:00
dispatch(setFollows(pTags));
2022-12-28 23:28:28 +00:00
}
2023-01-19 13:20:02 +00:00
(async () => {
let maxProfile = profiles.reduce((acc, v) => {
if (v.created > acc.created) {
acc.profile = v;
acc.created = v.created;
}
return acc;
}, { created: 0, profile: <MetadataCache | null>null });
if (maxProfile.profile) {
let existing = await db.users.get(maxProfile.profile.pubkey);
if ((existing?.created ?? 0) < maxProfile.created) {
await db.users.put(maxProfile.profile);
}
}
})().catch(console.warn);
2023-01-22 11:17:50 +00:00
}, [metadataFeed.store]);
useEffect(() => {
let notifications = notificationFeed.store.notes.filter(a => a.kind === EventKind.TextNote);
if ("Notification" in window && Notification.permission === "granted") {
for (let nx of notifications.filter(a => (a.created_at * 1000) > readNotifications)) {
sendNotification(nx)
.catch(console.warn);
}
}
dispatch(addNotifications(notifications));
}, [notificationFeed.store]);
useEffect(() => {
let dms = dmsFeed.store.notes.filter(a => a.kind === EventKind.DirectMessage);
dispatch(addDirectMessage(dms));
}, [dmsFeed.store]);
2023-01-19 12:59:37 +00:00
}
async function makeNotification(ev: TaggedRawEvent) {
switch (ev.kind) {
case EventKind.TextNote: {
2023-01-19 13:47:24 +00:00
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;
2023-01-19 12:59:37 +00:00
return {
title: `Reply from ${name}`,
2023-01-19 13:47:24 +00:00
body: replaceTagsWithUser(ev, users).substring(0, 50),
icon: avatarUrl
2023-01-19 12:59:37 +00:00
}
}
}
return null;
}
2023-01-19 13:47:24 +00:00
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();
}
2023-01-19 12:59:37 +00:00
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,
2023-01-19 13:47:24 +00:00
icon: n.icon,
2023-01-19 12:59:37 +00:00
tag: "notification",
timestamp: ev.created_at * 1000,
vibrate: [500]
});
}
2022-12-28 23:28:28 +00:00
}