parent
9be92003bb
commit
10d7c26470
@ -6,6 +6,7 @@ import { Chats } from "Cache";
|
||||
import { findTag, unixNow } from "SnortUtils";
|
||||
import { Nip29ChatSystem } from "./nip29";
|
||||
import useModeration from "Hooks/useModeration";
|
||||
import useLogin from "Hooks/useLogin";
|
||||
|
||||
export enum ChatType {
|
||||
DirectMessage = 1,
|
||||
@ -31,7 +32,7 @@ export interface ChatSystem {
|
||||
subscription(id: string): RequestBuilder | undefined;
|
||||
onEvent(evs: Array<NostrEvent>): Promise<void> | void;
|
||||
|
||||
listChats(): Array<Chat>;
|
||||
listChats(pk: string): Array<Chat>;
|
||||
}
|
||||
|
||||
export const Nip4Chats = new Nip4ChatSystem(Chats);
|
||||
@ -58,6 +59,10 @@ export function inChatWith(e: NostrEvent, myPk: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export function selfChat(e: NostrEvent, myPk: string) {
|
||||
return chatTo(e) === myPk && e.pubkey === myPk;
|
||||
}
|
||||
|
||||
export function lastReadInChat(id: string) {
|
||||
const k = `dm:seen:${id}`;
|
||||
return parseInt(window.localStorage.getItem(k) ?? "0");
|
||||
@ -70,9 +75,10 @@ export function setLastReadIn(id: string) {
|
||||
}
|
||||
|
||||
export function useNip4Chat() {
|
||||
const { publicKey } = useLogin();
|
||||
return useSyncExternalStore(
|
||||
c => Nip4Chats.hook(c),
|
||||
() => Nip4Chats.snapshot()
|
||||
() => Nip4Chats.snapshot(publicKey)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { ExternalStore, FeedCache, dedupe } from "@snort/shared";
|
||||
import { EventKind, NostrEvent, RequestBuilder, SystemInterface } from "@snort/system";
|
||||
import { Chat, ChatSystem, ChatType, chatTo, inChatWith, lastReadInChat } from "chat";
|
||||
import { Chat, ChatSystem, ChatType, inChatWith, lastReadInChat, selfChat } from "chat";
|
||||
import { debug } from "debug";
|
||||
|
||||
export class Nip4ChatSystem extends ExternalStore<Array<Chat>> implements ChatSystem {
|
||||
@ -34,14 +34,16 @@ export class Nip4ChatSystem extends ExternalStore<Array<Chat>> implements ChatSy
|
||||
return rb;
|
||||
}
|
||||
|
||||
takeSnapshot() {
|
||||
return this.listChats();
|
||||
takeSnapshot(p: string) {
|
||||
return this.listChats(p);
|
||||
}
|
||||
|
||||
listChats(): Chat[] {
|
||||
listChats(pk: string): Chat[] {
|
||||
const myDms = this.#nip4Events();
|
||||
return dedupe(myDms.map(a => chatTo(a))).map(a => {
|
||||
const messages = myDms.filter(b => chatTo(b) === a || b.pubkey === a);
|
||||
return dedupe(myDms.map(a => inChatWith(a, pk))).map(a => {
|
||||
const messages = myDms.filter(
|
||||
b => (a === pk && selfChat(b, pk)) || (!selfChat(b, pk) && inChatWith(b, pk) === a)
|
||||
);
|
||||
return Nip4ChatSystem.createChatObj(a, messages);
|
||||
});
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ export abstract class ExternalStore<TSnapshot> {
|
||||
};
|
||||
}
|
||||
|
||||
snapshot() {
|
||||
snapshot(p?: any) {
|
||||
if (this.#changed) {
|
||||
this.#snapshot = this.takeSnapshot();
|
||||
this.#snapshot = this.takeSnapshot(p);
|
||||
this.#changed = false;
|
||||
}
|
||||
return this.#snapshot;
|
||||
@ -39,5 +39,5 @@ export abstract class ExternalStore<TSnapshot> {
|
||||
}
|
||||
}
|
||||
|
||||
abstract takeSnapshot(): TSnapshot;
|
||||
abstract takeSnapshot(p?: any): TSnapshot;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user