fix: readonly feed loading

This commit is contained in:
Kieran 2023-09-25 12:11:56 +01:00
parent 8e414a10c5
commit 875348cc03
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
5 changed files with 53 additions and 55 deletions

View File

@ -24,7 +24,9 @@ export class GiftWrapCache extends RefreshFeedCache<UnwrappedGift> {
return [...this.cache.values()]; return [...this.cache.values()];
} }
override async onEvent(evs: Array<TaggedNostrEvent>, pub: EventPublisher) { override async onEvent(evs: Array<TaggedNostrEvent>, pub?: EventPublisher) {
if (!pub) return;
const unwrapped = ( const unwrapped = (
await Promise.all( await Promise.all(
evs.map(async v => { evs.map(async v => {

View File

@ -6,7 +6,7 @@ export type TWithCreated<T> = (T | Readonly<T>) & { created_at: number };
export abstract class RefreshFeedCache<T> extends FeedCache<TWithCreated<T>> { export abstract class RefreshFeedCache<T> extends FeedCache<TWithCreated<T>> {
abstract buildSub(session: LoginSession, rb: RequestBuilder): void; abstract buildSub(session: LoginSession, rb: RequestBuilder): void;
abstract onEvent(evs: Readonly<Array<TaggedNostrEvent>>, pub: EventPublisher): void; abstract onEvent(evs: Readonly<Array<TaggedNostrEvent>>, pub?: EventPublisher): void;
/** /**
* Get latest event * Get latest event

View File

@ -1,5 +1,5 @@
import { useEffect, useMemo } from "react"; import { useEffect, useMemo } from "react";
import { TaggedNostrEvent, Lists, EventKind, FlatNoteStore, RequestBuilder, NoteCollection } from "@snort/system"; import { TaggedNostrEvent, Lists, EventKind, RequestBuilder, NoteCollection } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react"; import { useRequestBuilder } from "@snort/system-react";
import { bech32ToHex, getNewest, getNewestEventTagsByKey, unwrap } from "SnortUtils"; import { bech32ToHex, getNewest, getNewestEventTagsByKey, unwrap } from "SnortUtils";
@ -49,13 +49,19 @@ export default function useLoginFeed() {
leaveOpen: true, leaveOpen: true,
}); });
b.withFilter().authors([pubKey]).kinds([EventKind.ContactList]); b.withFilter().authors([pubKey]).kinds([EventKind.ContactList]);
b.withFilter().authors([pubKey]).kinds([EventKind.AppData]).tag("d", ["snort"]); if (!login.readonly) {
b.withFilter().authors([pubKey]).kinds([EventKind.AppData]).tag("d", ["snort"]);
b.withFilter()
.relay("wss://relay.snort.social")
.kinds([EventKind.SnortSubscriptions])
.authors([bech32ToHex(SnortPubKey)])
.tag("p", [pubKey])
.limit(1);
}
b.withFilter() b.withFilter()
.relay("wss://relay.snort.social") .authors([pubKey])
.kinds([EventKind.SnortSubscriptions]) .kinds([EventKind.PubkeyLists])
.authors([bech32ToHex(SnortPubKey)]) .tag("d", [Lists.Muted, Lists.Followed, Lists.Pinned, Lists.Bookmarked]);
.tag("p", [pubKey])
.limit(1);
const n4Sub = Nip4Chats.subscription(login); const n4Sub = Nip4Chats.subscription(login);
if (n4Sub) { if (n4Sub) {
@ -68,25 +74,11 @@ export default function useLoginFeed() {
return b; return b;
}, [login]); }, [login]);
const subLists = useMemo(() => {
if (!pubKey) return null;
const b = new RequestBuilder(`login:${pubKey.slice(0, 12)}: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(NoteCollection, subLogin); const loginFeed = useRequestBuilder(NoteCollection, subLogin);
// update relays and follow lists // update relays and follow lists
useEffect(() => { useEffect(() => {
if (loginFeed.data && publisher) { if (loginFeed.data) {
const contactList = getNewest(loginFeed.data.filter(a => a.kind === EventKind.ContactList)); const contactList = getNewest(loginFeed.data.filter(a => a.kind === EventKind.ContactList));
if (contactList) { if (contactList) {
if (contactList.content !== "" && contactList.content !== "{}") { if (contactList.content !== "" && contactList.content !== "{}") {
@ -102,27 +94,29 @@ export default function useLoginFeed() {
Nip4Chats.onEvent(loginFeed.data); Nip4Chats.onEvent(loginFeed.data);
Nip28Chats.onEvent(loginFeed.data); Nip28Chats.onEvent(loginFeed.data);
const subs = loginFeed.data.filter( if (publisher) {
a => a.kind === EventKind.SnortSubscriptions && a.pubkey === bech32ToHex(SnortPubKey), const subs = loginFeed.data.filter(
); a => a.kind === EventKind.SnortSubscriptions && a.pubkey === bech32ToHex(SnortPubKey),
Promise.all( );
subs.map(async a => { Promise.all(
const dx = await publisher.decryptDm(a); subs.map(async a => {
if (dx) { const dx = await publisher.decryptDm(a);
const ex = JSON.parse(dx); if (dx) {
return { const ex = JSON.parse(dx);
id: a.id, return {
...ex, id: a.id,
} as SubscriptionEvent; ...ex,
} } as SubscriptionEvent;
}), }
).then(a => addSubscription(login, ...a.filter(a => a !== undefined).map(unwrap))); }),
).then(a => addSubscription(login, ...a.filter(a => a !== undefined).map(unwrap)));
const appData = getNewest(loginFeed.data.filter(a => a.kind === EventKind.AppData)); const appData = getNewest(loginFeed.data.filter(a => a.kind === EventKind.AppData));
if (appData) { if (appData) {
publisher.decryptGeneric(appData.content, appData.pubkey).then(d => { publisher.decryptGeneric(appData.content, appData.pubkey).then(d => {
setAppData(login, JSON.parse(d) as SnortAppData, appData.created_at * 1000); setAppData(login, JSON.parse(d) as SnortAppData, appData.created_at * 1000);
}); });
}
} }
} }
}, [loginFeed, publisher]); }, [loginFeed, publisher]);
@ -183,26 +177,28 @@ export default function useLoginFeed() {
} }
} }
const listsFeed = useRequestBuilder(FlatNoteStore, subLists);
useEffect(() => { useEffect(() => {
if (listsFeed.data) { if (loginFeed.data) {
const getList = (evs: readonly TaggedNostrEvent[], list: Lists) => const getList = (evs: readonly TaggedNostrEvent[], list: Lists) =>
evs.filter(a => unwrap(a.tags.find(b => b[0] === "d"))[1] === list); evs
.filter(
a => a.kind === EventKind.TagLists || a.kind === EventKind.NoteLists || a.kind === EventKind.PubkeyLists,
)
.filter(a => unwrap(a.tags.find(b => b[0] === "d"))[1] === list);
const mutedFeed = getList(listsFeed.data, Lists.Muted); const mutedFeed = getList(loginFeed.data, Lists.Muted);
handleMutedFeed(mutedFeed); handleMutedFeed(mutedFeed);
const pinnedFeed = getList(listsFeed.data, Lists.Pinned); const pinnedFeed = getList(loginFeed.data, Lists.Pinned);
handlePinnedFeed(pinnedFeed); handlePinnedFeed(pinnedFeed);
const tagsFeed = getList(listsFeed.data, Lists.Followed); const tagsFeed = getList(loginFeed.data, Lists.Followed);
handleTagFeed(tagsFeed); handleTagFeed(tagsFeed);
const bookmarkFeed = getList(listsFeed.data, Lists.Bookmarked); const bookmarkFeed = getList(loginFeed.data, Lists.Bookmarked);
handleBookmarkFeed(bookmarkFeed); handleBookmarkFeed(bookmarkFeed);
} }
}, [listsFeed]); }, [loginFeed]);
useEffect(() => { useEffect(() => {
UserRelays.buffer(follows.item).catch(console.error); UserRelays.buffer(follows.item).catch(console.error);

View File

@ -29,7 +29,7 @@ export function useRefreshFeedCache<T>(c: RefreshFeedCache<T>, leaveOpen = false
let t: ReturnType<typeof setTimeout> | undefined; let t: ReturnType<typeof setTimeout> | undefined;
let tBuf: Array<TaggedNostrEvent> = []; let tBuf: Array<TaggedNostrEvent> = [];
const releaseOnEvent = q.feed.onEvent(evs => { const releaseOnEvent = q.feed.onEvent(evs => {
if (!t && publisher) { if (!t) {
tBuf = [...evs]; tBuf = [...evs];
t = setTimeout(() => { t = setTimeout(() => {
t = undefined; t = undefined;

View File

@ -33,7 +33,7 @@ export class Nip4ChatSystem extends ExternalStore<Array<Chat>> implements ChatSy
subscription(session: LoginSession) { subscription(session: LoginSession) {
const pk = session.publicKey; const pk = session.publicKey;
if (!pk) return; if (!pk || session.readonly) return;
const rb = new RequestBuilder(`nip4:${pk.slice(0, 12)}`); const rb = new RequestBuilder(`nip4:${pk.slice(0, 12)}`);
const dms = this.#cache.snapshot(); const dms = this.#cache.snapshot();