refactor: RequestBuilder
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { AnyAction, ThunkDispatch } from "@reduxjs/toolkit";
|
||||
import { TaggedRawEvent, HexKey, Lists, EventKind } from "@snort/nostr";
|
||||
|
||||
import { getNewest } from "Util";
|
||||
import { getNewest, getNewestEventTagsByKey, unwrap } from "Util";
|
||||
import { makeNotification } from "Notifications";
|
||||
import { TaggedRawEvent, HexKey, Lists } from "@snort/nostr";
|
||||
import { Event, EventKind, Subscriptions } from "@snort/nostr";
|
||||
import {
|
||||
addDirectMessage,
|
||||
setFollows,
|
||||
@ -18,11 +18,12 @@ import {
|
||||
setLatestNotifications,
|
||||
} from "State/Login";
|
||||
import { RootState } from "State/Store";
|
||||
import useSubscription from "Feed/Subscription";
|
||||
import { barrierNip07 } from "Feed/EventPublisher";
|
||||
import { getMutedKeys } from "Feed/MuteList";
|
||||
import useModeration from "Hooks/useModeration";
|
||||
import { AnyAction, ThunkDispatch } from "@reduxjs/toolkit";
|
||||
import { FlatNoteStore, RequestBuilder } from "System";
|
||||
import useRequestBuilder from "Hooks/useRequestBuilder";
|
||||
import { EventExt } from "System/EventExt";
|
||||
|
||||
/**
|
||||
* Managed loading data for the current logged in user
|
||||
@ -37,143 +38,75 @@ export default function useLoginFeed() {
|
||||
} = useSelector((s: RootState) => s.login);
|
||||
const { isMuted } = useModeration();
|
||||
|
||||
const subMetadata = useMemo(() => {
|
||||
const subLogin = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
|
||||
const sub = new Subscriptions();
|
||||
sub.Id = `login:meta`;
|
||||
sub.Authors = new Set([pubKey]);
|
||||
sub.Kinds = new Set([EventKind.ContactList]);
|
||||
sub.Limit = 2;
|
||||
|
||||
return sub;
|
||||
}, [pubKey]);
|
||||
|
||||
const subNotification = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
|
||||
const sub = new Subscriptions();
|
||||
sub.Id = "login:notifications";
|
||||
// todo: add zaps
|
||||
sub.Kinds = new Set([EventKind.TextNote]);
|
||||
sub.PTags = new Set([pubKey]);
|
||||
sub.Limit = 1;
|
||||
return sub;
|
||||
}, [pubKey]);
|
||||
|
||||
const subMuted = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
|
||||
const sub = new Subscriptions();
|
||||
sub.Id = "login:muted";
|
||||
sub.Kinds = new Set([EventKind.PubkeyLists]);
|
||||
sub.Authors = new Set([pubKey]);
|
||||
sub.DTags = new Set([Lists.Muted]);
|
||||
sub.Limit = 1;
|
||||
|
||||
return sub;
|
||||
}, [pubKey]);
|
||||
|
||||
const subTags = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
|
||||
const sub = new Subscriptions();
|
||||
sub.Id = "login:tags";
|
||||
sub.Kinds = new Set([EventKind.TagLists]);
|
||||
sub.Authors = new Set([pubKey]);
|
||||
sub.DTags = new Set([Lists.Followed]);
|
||||
sub.Limit = 1;
|
||||
|
||||
return sub;
|
||||
}, [pubKey]);
|
||||
|
||||
const subPinned = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
|
||||
const sub = new Subscriptions();
|
||||
sub.Id = "login:pinned";
|
||||
sub.Kinds = new Set([EventKind.NoteLists]);
|
||||
sub.Authors = new Set([pubKey]);
|
||||
sub.DTags = new Set([Lists.Pinned]);
|
||||
sub.Limit = 1;
|
||||
|
||||
return sub;
|
||||
}, [pubKey]);
|
||||
|
||||
const subBookmarks = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
|
||||
const sub = new Subscriptions();
|
||||
sub.Id = "login:bookmarks";
|
||||
sub.Kinds = new Set([EventKind.NoteLists]);
|
||||
sub.Authors = new Set([pubKey]);
|
||||
sub.DTags = new Set([Lists.Bookmarked]);
|
||||
sub.Limit = 1;
|
||||
|
||||
return sub;
|
||||
}, [pubKey]);
|
||||
|
||||
const subDms = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
|
||||
const dms = new Subscriptions();
|
||||
dms.Id = "login:dms";
|
||||
dms.Kinds = new Set([EventKind.DirectMessage]);
|
||||
dms.PTags = new Set([pubKey]);
|
||||
|
||||
const dmsFromME = new Subscriptions();
|
||||
dmsFromME.Authors = new Set([pubKey]);
|
||||
dmsFromME.Kinds = new Set([EventKind.DirectMessage]);
|
||||
dms.AddSubscription(dmsFromME);
|
||||
|
||||
return dms;
|
||||
}, [pubKey]);
|
||||
|
||||
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 });
|
||||
const pinnedFeed = useSubscription(subPinned, { leaveOpen: true, cache: true });
|
||||
const tagsFeed = useSubscription(subTags, { leaveOpen: true, cache: true });
|
||||
const bookmarkFeed = useSubscription(subBookmarks, { leaveOpen: true, cache: true });
|
||||
|
||||
useEffect(() => {
|
||||
const contactList = metadataFeed.store.notes.filter(a => a.kind === EventKind.ContactList);
|
||||
for (const cl of contactList) {
|
||||
if (cl.content !== "" && cl.content !== "{}") {
|
||||
const relays = JSON.parse(cl.content);
|
||||
dispatch(setRelays({ relays, createdAt: cl.created_at }));
|
||||
}
|
||||
const pTags = cl.tags.filter(a => a[0] === "p").map(a => a[1]);
|
||||
dispatch(setFollows({ keys: pTags, createdAt: cl.created_at }));
|
||||
}
|
||||
}, [dispatch, metadataFeed.store]);
|
||||
|
||||
useEffect(() => {
|
||||
const replies = notificationFeed.store.notes.filter(
|
||||
a => a.kind === EventKind.TextNote && !isMuted(a.pubkey) && a.created_at > readNotifications
|
||||
);
|
||||
replies.forEach(nx => {
|
||||
dispatch(setLatestNotifications(nx.created_at));
|
||||
makeNotification(nx).then(notification => {
|
||||
if (notification) {
|
||||
(dispatch as ThunkDispatch<RootState, undefined, AnyAction>)(sendNotification(notification));
|
||||
}
|
||||
});
|
||||
const b = new RequestBuilder("login");
|
||||
b.withOptions({
|
||||
leaveOpen: true,
|
||||
});
|
||||
}, [dispatch, notificationFeed.store, readNotifications]);
|
||||
b.withFilter().authors([pubKey]).kinds([EventKind.ContactList, EventKind.DirectMessage]);
|
||||
b.withFilter().kinds([EventKind.TextNote]).tag("p", [pubKey]).limit(1);
|
||||
b.withFilter().kinds([EventKind.DirectMessage]).tag("p", [pubKey]);
|
||||
return b;
|
||||
}, [pubKey]);
|
||||
|
||||
const subLists = useMemo(() => {
|
||||
if (!pubKey) return null;
|
||||
const b = new RequestBuilder("login:lists");
|
||||
b.withOptions({
|
||||
leaveOpen: true,
|
||||
});
|
||||
b.withFilter()
|
||||
.authors([pubKey])
|
||||
.kinds([EventKind.PubkeyLists])
|
||||
.tag("d", [Lists.Muted, Lists.Followed, Lists.Pinned, Lists.Bookmarked]);
|
||||
|
||||
return b;
|
||||
}, [pubKey]);
|
||||
|
||||
const loginFeed = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subLogin);
|
||||
|
||||
// update relays and follow lists
|
||||
useEffect(() => {
|
||||
const muted = getMutedKeys(mutedFeed.store.notes);
|
||||
if (loginFeed.data) {
|
||||
const contactList = getNewest(loginFeed.data.filter(a => a.kind === EventKind.ContactList));
|
||||
if (contactList) {
|
||||
if (contactList.content !== "" && contactList.content !== "{}") {
|
||||
const relays = JSON.parse(contactList.content);
|
||||
dispatch(setRelays({ relays, createdAt: contactList.created_at }));
|
||||
}
|
||||
const pTags = contactList.tags.filter(a => a[0] === "p").map(a => a[1]);
|
||||
dispatch(setFollows({ keys: pTags, createdAt: contactList.created_at }));
|
||||
}
|
||||
|
||||
const dms = loginFeed.data.filter(a => a.kind === EventKind.DirectMessage);
|
||||
dispatch(addDirectMessage(dms));
|
||||
}
|
||||
}, [dispatch, loginFeed]);
|
||||
|
||||
// send out notifications
|
||||
useEffect(() => {
|
||||
if (loginFeed.data) {
|
||||
const replies = loginFeed.data.filter(
|
||||
a => a.kind === EventKind.TextNote && !isMuted(a.pubkey) && a.created_at > readNotifications
|
||||
);
|
||||
replies.forEach(nx => {
|
||||
dispatch(setLatestNotifications(nx.created_at));
|
||||
makeNotification(nx).then(notification => {
|
||||
if (notification) {
|
||||
(dispatch as ThunkDispatch<RootState, undefined, AnyAction>)(sendNotification(notification));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}, [dispatch, loginFeed, readNotifications]);
|
||||
|
||||
function handleMutedFeed(mutedFeed: TaggedRawEvent[]) {
|
||||
const muted = getMutedKeys(mutedFeed);
|
||||
dispatch(setMuted(muted));
|
||||
|
||||
const newest = getNewest(mutedFeed.store.notes);
|
||||
const newest = getNewest(mutedFeed);
|
||||
if (newest && newest.content.length > 0 && pubKey && newest.created_at > latestMuted) {
|
||||
decryptBlocked(newest, pubKey, privKey)
|
||||
.then(plaintext => {
|
||||
@ -192,57 +125,64 @@ export default function useLoginFeed() {
|
||||
})
|
||||
.catch(error => console.warn(error));
|
||||
}
|
||||
}, [dispatch, mutedFeed.store]);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const newest = getNewest(pinnedFeed.store.notes);
|
||||
function handlePinnedFeed(pinnedFeed: TaggedRawEvent[]) {
|
||||
const newest = getNewestEventTagsByKey(pinnedFeed, "e");
|
||||
if (newest) {
|
||||
const keys = newest.tags.filter(p => p && p.length === 2 && p[0] === "e").map(p => p[1]);
|
||||
dispatch(
|
||||
setPinned({
|
||||
keys,
|
||||
createdAt: newest.created_at,
|
||||
})
|
||||
);
|
||||
dispatch(setPinned(newest));
|
||||
}
|
||||
}, [dispatch, pinnedFeed.store]);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const newest = getNewest(tagsFeed.store.notes);
|
||||
function handleTagFeed(tagFeed: TaggedRawEvent[]) {
|
||||
const newest = getNewestEventTagsByKey(tagFeed, "t");
|
||||
if (newest) {
|
||||
const tags = newest.tags.filter(p => p && p.length === 2 && p[0] === "t").map(p => p[1]);
|
||||
dispatch(
|
||||
setTags({
|
||||
tags,
|
||||
createdAt: newest.created_at,
|
||||
tags: newest.keys,
|
||||
createdAt: newest.createdAt,
|
||||
})
|
||||
);
|
||||
}
|
||||
}, [dispatch, tagsFeed.store]);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const newest = getNewest(bookmarkFeed.store.notes);
|
||||
function handleBookmarkFeed(bookmarkFeed: TaggedRawEvent[]) {
|
||||
const newest = getNewestEventTagsByKey(bookmarkFeed, "e");
|
||||
if (newest) {
|
||||
const keys = newest.tags.filter(p => p && p.length === 2 && p[0] === "e").map(p => p[1]);
|
||||
dispatch(
|
||||
setBookmarked({
|
||||
keys,
|
||||
createdAt: newest.created_at,
|
||||
})
|
||||
);
|
||||
dispatch(setBookmarked(newest));
|
||||
}
|
||||
}, [dispatch, bookmarkFeed.store]);
|
||||
}
|
||||
|
||||
const listsFeed = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subLists);
|
||||
|
||||
useEffect(() => {
|
||||
const dms = dmsFeed.store.notes.filter(a => a.kind === EventKind.DirectMessage);
|
||||
dispatch(addDirectMessage(dms));
|
||||
}, [dispatch, dmsFeed.store]);
|
||||
if (listsFeed.data) {
|
||||
const getList = (evs: readonly TaggedRawEvent[], list: Lists) =>
|
||||
evs.filter(a => unwrap(a.tags.find(b => b[0] === "d"))[1] === list);
|
||||
|
||||
const mutedFeed = getList(listsFeed.data, Lists.Muted);
|
||||
handleMutedFeed(mutedFeed);
|
||||
|
||||
const pinnedFeed = getList(listsFeed.data, Lists.Pinned);
|
||||
handlePinnedFeed(pinnedFeed);
|
||||
|
||||
const tagsFeed = getList(listsFeed.data, Lists.Followed);
|
||||
handleTagFeed(tagsFeed);
|
||||
|
||||
const bookmarkFeed = getList(listsFeed.data, Lists.Bookmarked);
|
||||
handleBookmarkFeed(bookmarkFeed);
|
||||
}
|
||||
}, [dispatch, listsFeed]);
|
||||
|
||||
/*const fRelays = useRelaysFeedFollows(follows);
|
||||
useEffect(() => {
|
||||
FollowsRelays.bulkSet(fRelays).catch(console.error);
|
||||
}, [dispatch, fRelays]);*/
|
||||
}
|
||||
|
||||
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);
|
||||
return await EventExt.decryptData(raw.content, privKey, pubKey);
|
||||
} else {
|
||||
return await barrierNip07(() => window.nostr.nip04.decrypt(pubKey, raw.content));
|
||||
}
|
||||
|
Reference in New Issue
Block a user