snort/src/Feed/LoginFeed.ts

167 lines
6.0 KiB
TypeScript
Raw Normal View History

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 { makeNotification } from "Notifications";
import { TaggedRawEvent, HexKey, Lists } from "Nostr";
2023-01-20 11:11:50 +00:00
import EventKind from "Nostr/EventKind";
import Event from "Nostr/Event";
2023-01-20 11:11:50 +00:00
import { Subscriptions } from "Nostr/Subscriptions";
import { addDirectMessage, setFollows, setRelays, setMuted, setBlocked, sendNotification } from "State/Login";
2023-01-20 11:11:50 +00:00
import { RootState } from "State/Store";
import { mapEventToProfile, MetadataCache } from "State/Users";
import { getDb } from "State/Users/Db";
2023-01-20 11:11:50 +00:00
import useSubscription from "Feed/Subscription";
import { getDisplayName } from "Element/ProfileImage";
2023-01-28 21:43:56 +00:00
import { barierNip07 } from "Feed/EventPublisher";
import { getMutedKeys, getNewest } from "Feed/MuteList";
2023-01-20 11:11:50 +00:00
import { MentionRegex } from "Const";
2023-01-27 07:21:48 +00:00
import useModeration from "Hooks/useModeration";
2022-12-28 23:28:28 +00:00
/**
* Managed loading data for the current logged in user
*/
export default function useLoginFeed() {
const dispatch = useDispatch();
const { publicKey: pubKey, privateKey: privKey } = useSelector((s: RootState) => s.login);
const { isMuted } = useModeration();
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]);
sub.Limit = 2
2023-01-22 11:17:50 +00:00
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-26 11:34:18 +00:00
const subMuted = useMemo(() => {
if (!pubKey) return null;
let sub = new Subscriptions();
sub.Id = "login:muted";
sub.Kinds = new Set([EventKind.Lists]);
sub.Authors = new Set([pubKey]);
2023-01-30 07:43:46 +00:00
sub.DTags = new Set([Lists.Muted]);
2023-01-26 11:34:18 +00:00
sub.Limit = 1;
return sub;
}, [pubKey]);
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-02-01 13:22:11 +00:00
const metadataFeed = useSubscription(subMetadata, { leaveOpen: true, cache: true });
const notificationFeed = useSubscription(subNotification, { leaveOpen: true, cache: true });
const dmsFeed = useSubscription(subDms, { leaveOpen: true, cache: true });
const mutedFeed = useSubscription(subMuted, { leaveOpen: true, cache: 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]);
dispatch(setFollows({ keys: pTags, createdAt: cl.created_at }));
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: null as MetadataCache | null });
2023-01-19 13:20:02 +00:00
if (maxProfile.profile) {
const db = getDb()
let existing = await db.find(maxProfile.profile.pubkey);
2023-01-19 13:20:02 +00:00
if ((existing?.created ?? 0) < maxProfile.created) {
await db.put(maxProfile.profile);
2023-01-19 13:20:02 +00:00
}
}
})().catch(console.warn);
}, [dispatch, metadataFeed.store]);
2023-01-22 11:17:50 +00:00
useEffect(() => {
const replies = notificationFeed.store.notes.filter(a => a.kind === EventKind.TextNote && !isMuted(a.pubkey))
replies.forEach(nx => {
makeNotification(nx).then(notification => {
if (notification) {
// @ts-ignore
dispatch(sendNotification(notification))
2023-01-22 11:17:50 +00:00
}
})
})
}, [dispatch, notificationFeed.store]);
2023-01-22 11:17:50 +00:00
2023-01-26 11:34:18 +00:00
useEffect(() => {
const muted = getMutedKeys(mutedFeed.store.notes)
dispatch(setMuted(muted))
const newest = getNewest(mutedFeed.store.notes)
if (newest && newest.content.length > 0 && pubKey) {
decryptBlocked(newest, pubKey, privKey).then((plaintext) => {
try {
const blocked = JSON.parse(plaintext)
const keys = blocked.filter((p:any) => p && p.length === 2 && p[0] === "p").map((p: any) => p[1])
dispatch(setBlocked({
keys,
createdAt: newest.created_at,
}))
} catch(error) {
console.debug("Couldn't parse JSON")
}
}).catch((error) => console.warn(error))
}
}, [dispatch, mutedFeed.store])
2023-01-26 11:34:18 +00:00
2023-01-22 11:17:50 +00:00
useEffect(() => {
let dms = dmsFeed.store.notes.filter(a => a.kind === EventKind.DirectMessage);
dispatch(addDirectMessage(dms));
}, [dispatch, dmsFeed.store]);
2023-01-19 13:47:24 +00:00
}
async function decryptBlocked(raw: TaggedRawEvent, pubKey: HexKey, privKey?: HexKey) {
const ev = new Event(raw)
if (pubKey && privKey) {
return await ev.DecryptData(raw.content, privKey, pubKey)
} else {
return await barierNip07(() => window.nostr.nip04.decrypt(pubKey, raw.content));
}
2023-01-26 11:34:18 +00:00
}