refactor: drop nip4

This commit is contained in:
2024-08-25 19:17:22 +03:00
parent 44a014b8c6
commit d9abcd0101
4 changed files with 32 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import { FeedCache } from "@snort/shared"; import { FeedCache } from "@snort/shared";
import { WorkerRelayInterface } from "@snort/worker-relay";
import { ReactNode, useEffect, useState, useSyncExternalStore } from "react"; import { ReactNode, useEffect, useState, useSyncExternalStore } from "react";
import { FormattedMessage, FormattedNumber } from "react-intl"; import { FormattedMessage, FormattedNumber } from "react-intl";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
@ -6,7 +7,6 @@ import { useNavigate } from "react-router-dom";
import { GiftsCache, Relay, RelayMetrics } from "@/Cache"; import { GiftsCache, Relay, RelayMetrics } from "@/Cache";
import AsyncButton from "@/Components/Button/AsyncButton"; import AsyncButton from "@/Components/Button/AsyncButton";
import useLogin from "@/Hooks/useLogin"; import useLogin from "@/Hooks/useLogin";
import { WorkerRelayInterface } from "@snort/worker-relay";
export function CacheSettings() { export function CacheSettings() {
return ( return (

View File

@ -13,7 +13,7 @@ import {
UserMetadata, UserMetadata,
} from "@snort/system"; } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react"; import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react"; import { useEffect, useMemo } from "react";
import { useEmptyChatSystem } from "@/Hooks/useEmptyChatSystem"; import { useEmptyChatSystem } from "@/Hooks/useEmptyChatSystem";
import useLogin from "@/Hooks/useLogin"; import useLogin from "@/Hooks/useLogin";
@ -24,6 +24,7 @@ import { LoginSession } from "@/Utils/Login";
import { Nip4Chats, Nip4ChatSystem } from "./nip4"; import { Nip4Chats, Nip4ChatSystem } from "./nip4";
import { Nip17Chats, Nip17ChatSystem } from "./nip17"; import { Nip17Chats, Nip17ChatSystem } from "./nip17";
import { Nip28Chats, Nip28ChatSystem } from "./nip28"; import { Nip28Chats, Nip28ChatSystem } from "./nip28";
import useEventPublisher from "@/Hooks/useEventPublisher";
export enum ChatType { export enum ChatType {
DirectMessage = 1, DirectMessage = 1,
@ -70,6 +71,11 @@ export interface ChatSystem {
* Create a list of chats for a given pubkey and set of events * Create a list of chats for a given pubkey and set of events
*/ */
listChats(pk: string, evs: Array<TaggedNostrEvent>): Array<Chat>; listChats(pk: string, evs: Array<TaggedNostrEvent>): Array<Chat>;
/**
* Process events received from the subscription
*/
processEvents(pub: EventPublisher, evs: Array<TaggedNostrEvent>): Promise<void>;
} }
/** /**
@ -167,12 +173,19 @@ export function createEmptyChatObject(id: string, messages?: Array<TaggedNostrEv
export function useChatSystem(chat: ChatSystem) { export function useChatSystem(chat: ChatSystem) {
const login = useLogin(); const login = useLogin();
const {publisher} = useEventPublisher();
const sub = useMemo(() => { const sub = useMemo(() => {
return chat.subscription(login); return chat.subscription(login);
}, [chat, login]); }, [chat, login]);
const data = useRequestBuilder(sub); const data = useRequestBuilder(sub);
const { isMuted } = useModeration(); const { isMuted } = useModeration();
useEffect(() => {
if(publisher) {
chat.processEvents(publisher, data);
}
}, [data, publisher]);
return useMemo(() => { return useMemo(() => {
if (login.publicKey) { if (login.publicKey) {
return chat.listChats( return chat.listChats(
@ -185,11 +198,11 @@ export function useChatSystem(chat: ChatSystem) {
} }
export function useChatSystems() { export function useChatSystems() {
const nip4 = useChatSystem(Nip4Chats); //const nip4 = useChatSystem(Nip4Chats);
const nip28 = useChatSystem(Nip28Chats); const nip28 = useChatSystem(Nip28Chats);
const nip17 = useChatSystem(Nip17Chats); const nip17 = useChatSystem(Nip17Chats);
return [...nip4, ...nip28, ...nip17]; return [...nip28, ...nip17];
} }
export function useChat(id: string) { export function useChat(id: string) {

View File

@ -3,9 +3,11 @@ import {
decodeTLV, decodeTLV,
encodeTLVEntries, encodeTLVEntries,
EventKind, EventKind,
EventPublisher,
NostrEvent, NostrEvent,
NostrPrefix, NostrPrefix,
RequestBuilder, RequestBuilder,
TaggedNostrEvent,
TLVEntry, TLVEntry,
TLVEntryType, TLVEntryType,
} from "@snort/system"; } from "@snort/system";
@ -19,6 +21,7 @@ import { GetPowWorker } from "@/Utils/wasm";
export class Nip17ChatSystem extends ExternalStore<Array<Chat>> implements ChatSystem { export class Nip17ChatSystem extends ExternalStore<Array<Chat>> implements ChatSystem {
#cache: GiftWrapCache; #cache: GiftWrapCache;
#seenEvents: Set<string> = new Set();
constructor(cache: GiftWrapCache) { constructor(cache: GiftWrapCache) {
super(); super();
@ -35,6 +38,12 @@ export class Nip17ChatSystem extends ExternalStore<Array<Chat>> implements ChatS
return rb; return rb;
} }
async processEvents(pub: EventPublisher, evs: Array<TaggedNostrEvent>) {
const evsPrcess = evs.filter(a => !this.#seenEvents.has(a.id) && !this.#cache.keysOnTable().includes(a.id));
await this.#cache.onEvent(evsPrcess, "", pub);
evsPrcess.forEach(a => this.#seenEvents.add(a.id));
}
listChats(pk: string): Chat[] { listChats(pk: string): Chat[] {
const evs = this.#nip24Events(); const evs = this.#nip24Events();
const messages = evs.filter(a => a.to === pk); const messages = evs.filter(a => a.to === pk);
@ -131,7 +140,7 @@ export class Nip17ChatSystem extends ExternalStore<Array<Chat>> implements ChatS
} }
#nip24Events() { #nip24Events() {
const sn = this.#cache.takeSnapshot(); const sn = this.#cache.snapshot();
return sn.filter(a => a.inner.kind === EventKind.SealedRumor); return sn.filter(a => a.inner.kind === EventKind.SealedRumor);
} }
} }

View File

@ -43,6 +43,11 @@ export class Nip28ChatSystem implements ChatSystem {
return rb; return rb;
} }
processEvents(): Promise<void> {
// nothing to do
return Promise.resolve();
}
listChats(pk: string, evs: Array<TaggedNostrEvent>): Chat[] { listChats(pk: string, evs: Array<TaggedNostrEvent>): Chat[] {
const chats = this.#chatChannels(evs); const chats = this.#chatChannels(evs);
const ret = Object.entries(chats).map(([k, v]) => { const ret = Object.entries(chats).map(([k, v]) => {