snort/src/feed/LoginFeed.ts

118 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-01-19 12:38:47 +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-19 12:59:37 +00:00
import { HexKey, TaggedRawEvent } from "../nostr";
2022-12-29 22:23:41 +00:00
import EventKind from "../nostr/EventKind";
import { Subscriptions } from "../nostr/Subscriptions";
2023-01-12 09:48:39 +00:00
import { addDirectMessage, addNotifications, setFollows, setRelays } from "../state/Login";
2023-01-15 19:40:47 +00:00
import { RootState } from "../state/Store";
2023-01-15 01:46:13 +00:00
import { db } from "../db";
2023-01-01 10:44:38 +00:00
import useSubscription from "./Subscription";
2023-01-19 13:20:02 +00:00
import { mapEventToProfile, MetadataCache } from "../db/User";
2023-01-19 12:59:37 +00:00
import { hexToBech32 } from "../Util";
import { getDisplayName } from "../element/ProfileImage";
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-01 10:44:38 +00:00
const sub = useMemo(() => {
2023-01-12 12:00:44 +00:00
if (!pubKey) {
2023-01-01 10:44:38 +00:00
return null;
}
let sub = new Subscriptions();
sub.Id = `login:${sub.Id}`;
2023-01-15 19:40:47 +00:00
sub.Authors = new Set([pubKey]);
sub.Kinds = new Set([EventKind.ContactList, EventKind.SetMetadata, EventKind.DirectMessage]);
2023-01-01 10:44:38 +00:00
let notifications = new Subscriptions();
2023-01-18 18:53:34 +00:00
notifications.Kinds = new Set([EventKind.TextNote]);
2023-01-15 19:40:47 +00:00
notifications.PTags = new Set([pubKey]);
2023-01-01 10:44:38 +00:00
notifications.Limit = 100;
sub.AddSubscription(notifications);
2023-01-18 18:53:34 +00:00
let dms = new Subscriptions();
dms.Kinds = new Set([EventKind.DirectMessage]);
dms.PTags = new Set([pubKey]);
sub.AddSubscription(dms);
2023-01-01 10:44:38 +00:00
return sub;
}, [pubKey]);
2023-01-15 19:40:47 +00:00
const main = useSubscription(sub, { leaveOpen: true });
2023-01-01 10:44:38 +00:00
2022-12-28 23:28:28 +00:00
useEffect(() => {
2023-01-15 19:40:47 +00:00
let contactList = main.notes.filter(a => a.kind === EventKind.ContactList);
let notifications = main.notes.filter(a => a.kind === EventKind.TextNote);
2023-01-15 22:59:05 +00:00
let metadata = main.notes.filter(a => a.kind === EventKind.SetMetadata);
let profiles = metadata.map(a => mapEventToProfile(a))
2023-01-15 19:40:47 +00:00
.filter(a => a !== undefined)
.map(a => a!);
let dms = main.notes.filter(a => a.kind === EventKind.DirectMessage);
2023-01-01 10:44:38 +00:00
2023-01-03 13:17:09 +00:00
for (let cl of contactList) {
2023-01-03 12:06:53 +00:00
if (cl.content !== "") {
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
}
if ("Notification" in window && Notification.permission === "granted") {
2023-01-19 12:09:34 +00:00
for (let nx of notifications.filter(a => (a.created_at * 1000) > readNotifications)) {
2023-01-19 12:59:37 +00:00
sendNotification(nx)
.catch(console.warn);
2023-01-03 13:17:09 +00:00
}
}
2023-01-03 12:06:53 +00:00
dispatch(addNotifications(notifications));
2023-01-12 09:48:39 +00:00
dispatch(addDirectMessage(dms));
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-15 19:40:47 +00:00
}, [main]);
2023-01-19 12:59:37 +00:00
}
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);
return {
title: `Reply from ${name}`,
body: ev.content.substring(0, 50)
}
}
}
return null;
}
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,
tag: "notification",
timestamp: ev.created_at * 1000,
vibrate: [500]
});
}
2022-12-28 23:28:28 +00:00
}