fix: readonly feed loading

This commit is contained in:
Kieran 2023-09-25 12:11:56 +01:00
parent 8e414a10c5
commit 875348cc03
Signed by: Kieran
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()];
}
override async onEvent(evs: Array<TaggedNostrEvent>, pub: EventPublisher) {
override async onEvent(evs: Array<TaggedNostrEvent>, pub?: EventPublisher) {
if (!pub) return;
const unwrapped = (
await Promise.all(
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>> {
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

View File

@ -1,5 +1,5 @@
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 { bech32ToHex, getNewest, getNewestEventTagsByKey, unwrap } from "SnortUtils";
@ -49,13 +49,19 @@ export default function useLoginFeed() {
leaveOpen: true,
});
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()
.relay("wss://relay.snort.social")
.kinds([EventKind.SnortSubscriptions])
.authors([bech32ToHex(SnortPubKey)])
.tag("p", [pubKey])
.limit(1);
.authors([pubKey])
.kinds([EventKind.PubkeyLists])
.tag("d", [Lists.Muted, Lists.Followed, Lists.Pinned, Lists.Bookmarked]);
const n4Sub = Nip4Chats.subscription(login);
if (n4Sub) {
@ -68,25 +74,11 @@ export default function useLoginFeed() {
return b;
}, [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);
// update relays and follow lists
useEffect(() => {
if (loginFeed.data && publisher) {
if (loginFeed.data) {
const contactList = getNewest(loginFeed.data.filter(a => a.kind === EventKind.ContactList));
if (contactList) {
if (contactList.content !== "" && contactList.content !== "{}") {
@ -102,27 +94,29 @@ export default function useLoginFeed() {
Nip4Chats.onEvent(loginFeed.data);
Nip28Chats.onEvent(loginFeed.data);
const subs = loginFeed.data.filter(
a => a.kind === EventKind.SnortSubscriptions && a.pubkey === bech32ToHex(SnortPubKey),
);
Promise.all(
subs.map(async a => {
const dx = await publisher.decryptDm(a);
if (dx) {
const ex = JSON.parse(dx);
return {
id: a.id,
...ex,
} as SubscriptionEvent;
}
}),
).then(a => addSubscription(login, ...a.filter(a => a !== undefined).map(unwrap)));
if (publisher) {
const subs = loginFeed.data.filter(
a => a.kind === EventKind.SnortSubscriptions && a.pubkey === bech32ToHex(SnortPubKey),
);
Promise.all(
subs.map(async a => {
const dx = await publisher.decryptDm(a);
if (dx) {
const ex = JSON.parse(dx);
return {
id: a.id,
...ex,
} as SubscriptionEvent;
}
}),
).then(a => addSubscription(login, ...a.filter(a => a !== undefined).map(unwrap)));
const appData = getNewest(loginFeed.data.filter(a => a.kind === EventKind.AppData));
if (appData) {
publisher.decryptGeneric(appData.content, appData.pubkey).then(d => {
setAppData(login, JSON.parse(d) as SnortAppData, appData.created_at * 1000);
});
const appData = getNewest(loginFeed.data.filter(a => a.kind === EventKind.AppData));
if (appData) {
publisher.decryptGeneric(appData.content, appData.pubkey).then(d => {
setAppData(login, JSON.parse(d) as SnortAppData, appData.created_at * 1000);
});
}
}
}
}, [loginFeed, publisher]);
@ -183,26 +177,28 @@ export default function useLoginFeed() {
}
}
const listsFeed = useRequestBuilder(FlatNoteStore, subLists);
useEffect(() => {
if (listsFeed.data) {
if (loginFeed.data) {
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);
const pinnedFeed = getList(listsFeed.data, Lists.Pinned);
const pinnedFeed = getList(loginFeed.data, Lists.Pinned);
handlePinnedFeed(pinnedFeed);
const tagsFeed = getList(listsFeed.data, Lists.Followed);
const tagsFeed = getList(loginFeed.data, Lists.Followed);
handleTagFeed(tagsFeed);
const bookmarkFeed = getList(listsFeed.data, Lists.Bookmarked);
const bookmarkFeed = getList(loginFeed.data, Lists.Bookmarked);
handleBookmarkFeed(bookmarkFeed);
}
}, [listsFeed]);
}, [loginFeed]);
useEffect(() => {
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 tBuf: Array<TaggedNostrEvent> = [];
const releaseOnEvent = q.feed.onEvent(evs => {
if (!t && publisher) {
if (!t) {
tBuf = [...evs];
t = setTimeout(() => {
t = undefined;

View File

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