refactor: drop nip4
This commit is contained in:
parent
44a014b8c6
commit
d9abcd0101
@ -1,4 +1,5 @@
|
||||
import { FeedCache } from "@snort/shared";
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
import { ReactNode, useEffect, useState, useSyncExternalStore } from "react";
|
||||
import { FormattedMessage, FormattedNumber } from "react-intl";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
@ -6,7 +7,6 @@ import { useNavigate } from "react-router-dom";
|
||||
import { GiftsCache, Relay, RelayMetrics } from "@/Cache";
|
||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||
import useLogin from "@/Hooks/useLogin";
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
|
||||
export function CacheSettings() {
|
||||
return (
|
||||
|
@ -13,7 +13,7 @@ import {
|
||||
UserMetadata,
|
||||
} from "@snort/system";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
import { useMemo } from "react";
|
||||
import { useEffect, useMemo } from "react";
|
||||
|
||||
import { useEmptyChatSystem } from "@/Hooks/useEmptyChatSystem";
|
||||
import useLogin from "@/Hooks/useLogin";
|
||||
@ -24,6 +24,7 @@ import { LoginSession } from "@/Utils/Login";
|
||||
import { Nip4Chats, Nip4ChatSystem } from "./nip4";
|
||||
import { Nip17Chats, Nip17ChatSystem } from "./nip17";
|
||||
import { Nip28Chats, Nip28ChatSystem } from "./nip28";
|
||||
import useEventPublisher from "@/Hooks/useEventPublisher";
|
||||
|
||||
export enum ChatType {
|
||||
DirectMessage = 1,
|
||||
@ -70,6 +71,11 @@ export interface ChatSystem {
|
||||
* Create a list of chats for a given pubkey and set of events
|
||||
*/
|
||||
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) {
|
||||
const login = useLogin();
|
||||
const {publisher} = useEventPublisher();
|
||||
const sub = useMemo(() => {
|
||||
return chat.subscription(login);
|
||||
}, [chat, login]);
|
||||
const data = useRequestBuilder(sub);
|
||||
const { isMuted } = useModeration();
|
||||
|
||||
useEffect(() => {
|
||||
if(publisher) {
|
||||
chat.processEvents(publisher, data);
|
||||
}
|
||||
}, [data, publisher]);
|
||||
|
||||
return useMemo(() => {
|
||||
if (login.publicKey) {
|
||||
return chat.listChats(
|
||||
@ -185,11 +198,11 @@ export function useChatSystem(chat: ChatSystem) {
|
||||
}
|
||||
|
||||
export function useChatSystems() {
|
||||
const nip4 = useChatSystem(Nip4Chats);
|
||||
//const nip4 = useChatSystem(Nip4Chats);
|
||||
const nip28 = useChatSystem(Nip28Chats);
|
||||
const nip17 = useChatSystem(Nip17Chats);
|
||||
|
||||
return [...nip4, ...nip28, ...nip17];
|
||||
return [...nip28, ...nip17];
|
||||
}
|
||||
|
||||
export function useChat(id: string) {
|
||||
|
@ -3,9 +3,11 @@ import {
|
||||
decodeTLV,
|
||||
encodeTLVEntries,
|
||||
EventKind,
|
||||
EventPublisher,
|
||||
NostrEvent,
|
||||
NostrPrefix,
|
||||
RequestBuilder,
|
||||
TaggedNostrEvent,
|
||||
TLVEntry,
|
||||
TLVEntryType,
|
||||
} from "@snort/system";
|
||||
@ -19,6 +21,7 @@ import { GetPowWorker } from "@/Utils/wasm";
|
||||
|
||||
export class Nip17ChatSystem extends ExternalStore<Array<Chat>> implements ChatSystem {
|
||||
#cache: GiftWrapCache;
|
||||
#seenEvents: Set<string> = new Set();
|
||||
|
||||
constructor(cache: GiftWrapCache) {
|
||||
super();
|
||||
@ -35,6 +38,12 @@ export class Nip17ChatSystem extends ExternalStore<Array<Chat>> implements ChatS
|
||||
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[] {
|
||||
const evs = this.#nip24Events();
|
||||
const messages = evs.filter(a => a.to === pk);
|
||||
@ -131,7 +140,7 @@ export class Nip17ChatSystem extends ExternalStore<Array<Chat>> implements ChatS
|
||||
}
|
||||
|
||||
#nip24Events() {
|
||||
const sn = this.#cache.takeSnapshot();
|
||||
const sn = this.#cache.snapshot();
|
||||
return sn.filter(a => a.inner.kind === EventKind.SealedRumor);
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,11 @@ export class Nip28ChatSystem implements ChatSystem {
|
||||
return rb;
|
||||
}
|
||||
|
||||
processEvents(): Promise<void> {
|
||||
// nothing to do
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
listChats(pk: string, evs: Array<TaggedNostrEvent>): Chat[] {
|
||||
const chats = this.#chatChannels(evs);
|
||||
const ret = Object.entries(chats).map(([k, v]) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user