Cache all the things

This commit is contained in:
2023-09-05 14:57:50 +01:00
parent 5521f685fc
commit b1459d0f49
49 changed files with 805 additions and 243 deletions

View File

@ -13,9 +13,9 @@ import {
UserMetadata,
encodeTLVEntries,
} from "@snort/system";
import { unwrap } from "@snort/shared";
import { unwrap, unixNow } from "@snort/shared";
import { Chats, GiftsCache } from "Cache";
import { findTag, unixNow } from "SnortUtils";
import { findTag } from "SnortUtils";
import { Nip29ChatSystem } from "./nip29";
import useModeration from "Hooks/useModeration";
import useLogin from "Hooks/useLogin";
@ -182,8 +182,8 @@ export function useNip24Chat() {
export function useChatSystem() {
const nip4 = useNip4Chat();
const nip24 = useNip24Chat();
//const nip24 = useNip24Chat();
const { muted, blocked } = useModeration();
return [...nip4, ...nip24].filter(a => !(muted.includes(a.id) || blocked.includes(a.id)));
return [...nip4].filter(a => !(muted.includes(a.id) || blocked.includes(a.id)));
}

View File

@ -1,4 +1,4 @@
import { ExternalStore, FeedCache, dedupe } from "@snort/shared";
import { ExternalStore, FeedCache } from "@snort/shared";
import {
EventKind,
NostrEvent,
@ -6,7 +6,6 @@ import {
RequestBuilder,
SystemInterface,
TLVEntryType,
decodeTLV,
encodeTLVEntries,
TaggedNostrEvent,
} from "@snort/system";
@ -50,32 +49,31 @@ export class Nip4ChatSystem extends ExternalStore<Array<Chat>> implements ChatSy
listChats(pk: string): Chat[] {
const myDms = this.#nip4Events();
const chatId = (a: NostrEvent) => {
return encodeTLVEntries("chat4" as NostrPrefix, {
type: TLVEntryType.Author,
value: inChatWith(a, pk),
length: 0,
});
};
const chats = myDms.reduce((acc, v) => {
const chatId = inChatWith(v, pk);
acc[chatId] ??= [];
acc[chatId].push(v);
return acc;
}, {} as Record<string, Array<NostrEvent>>);
return dedupe(myDms.map(chatId)).map(a => {
const messages = myDms.filter(b => chatId(b) === a);
return Nip4ChatSystem.createChatObj(a, messages);
});
return [...Object.entries(chats)].map(([k, v]) => Nip4ChatSystem.createChatObj(k, v));
}
static createChatObj(id: string, messages: Array<NostrEvent>) {
const last = lastReadInChat(id);
const pk = decodeTLV(id).find(a => a.type === TLVEntryType.Author)?.value as string;
return {
type: ChatType.DirectMessage,
id,
id: encodeTLVEntries("chat4" as NostrPrefix, {
type: TLVEntryType.Author,
value: id,
length: 0,
}),
unread: messages.reduce((acc, v) => (v.created_at > last ? acc++ : acc), 0),
lastMessage: messages.reduce((acc, v) => (v.created_at > acc ? v.created_at : acc), 0),
participants: [
{
type: "pubkey",
id: pk,
id: id,
},
],
messages: messages.map(m => ({
@ -90,7 +88,7 @@ export class Nip4ChatSystem extends ExternalStore<Array<Chat>> implements ChatSy
},
})),
createMessage: async (msg, pub) => {
return [await pub.sendDm(msg, pk)];
return [await pub.sendDm(msg, id)];
},
sendMessage: (ev, system: SystemInterface) => {
ev.forEach(a => system.BroadcastEvent(a));