Chat system refactor

This commit is contained in:
2023-06-20 14:15:33 +01:00
parent 4365fac9b5
commit 234c1c092d
22 changed files with 397 additions and 263 deletions

View File

@ -9,6 +9,7 @@ enum EventKind {
Repost = 6, // NIP-18
Reaction = 7, // NIP-25
BadgeAward = 8, // NIP-58
SimpleChatMessage = 9, // NIP-29
SnortSubscriptions = 1000, // NIP-XX
Polls = 6969, // NIP-69
GiftWrap = 1059, // NIP-59
@ -23,6 +24,7 @@ enum EventKind {
ProfileBadges = 30008, // NIP-58
LiveEvent = 30311, // NIP-102
ZapstrTrack = 31337,
SimpleChatMetadata = 39_000, // NIP-29
ZapRequest = 9734, // NIP 57
ZapReceipt = 9735, // NIP 57
HttpAuthentication = 27235, // NIP XX - HTTP Authentication

View File

@ -38,19 +38,21 @@ export type ReqCommand = [cmd: "REQ", id: string, ...filters: Array<ReqFilter>];
* Raw REQ filter object
*/
export interface ReqFilter {
ids?: u256[];
authors?: u256[];
kinds?: number[];
"#e"?: u256[];
"#p"?: u256[];
"#t"?: string[];
"#d"?: string[];
"#r"?: string[];
"#a"?: string[];
search?: string;
since?: number;
until?: number;
limit?: number;
ids?: u256[]
authors?: u256[]
kinds?: number[]
"#e"?: u256[]
"#p"?: u256[]
"#t"?: string[]
"#d"?: string[]
"#r"?: string[]
"#a"?: string[]
"#g"?: string[]
search?: string
since?: number
until?: number
limit?: number
[key: string]: Array<string> | Array<number> | string | number | undefined
}
/**

View File

@ -142,7 +142,7 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
try {
const addr = unwrap(sanitizeRelayUrl(address));
if (!this.#sockets.has(addr)) {
const c = new Connection(addr, { read: true, write: false }, this.#handleAuth?.bind(this), true);
const c = new Connection(addr, { read: true, write: true }, this.#handleAuth?.bind(this), true);
this.#sockets.set(addr, c);
c.OnEvent = (s, e) => this.OnEvent(s, e);
c.OnEose = s => this.OnEndOfStoredEvents(c, s);
@ -252,18 +252,27 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
* Write an event to a relay then disconnect
*/
async WriteOnceToRelay(address: string, ev: NostrEvent) {
return new Promise<void>((resolve, reject) => {
const c = new Connection(address, { write: true, read: false }, this.#handleAuth?.bind(this), true);
const addrClean = sanitizeRelayUrl(address);
if (!addrClean) {
throw new Error("Invalid relay address");
}
const t = setTimeout(reject, 5_000);
c.OnConnected = async () => {
clearTimeout(t);
await c.SendAsync(ev);
c.Close();
resolve();
};
c.Connect();
});
if (this.#sockets.has(addrClean)) {
await this.#sockets.get(addrClean)?.SendAsync(ev);
} else {
return await new Promise<void>((resolve, reject) => {
const c = new Connection(address, { write: true, read: true }, this.#handleAuth?.bind(this), true);
const t = setTimeout(reject, 5_000);
c.OnConnected = async () => {
clearTimeout(t);
await c.SendAsync(ev);
c.Close();
resolve();
};
c.Connect();
});
}
}
takeSnapshot(): SystemSnapshot {

View File

@ -22,7 +22,7 @@ export class ProfileLoaderService {
/**
* List of pubkeys to fetch metadata for
*/
WantsMetadata: Set<HexKey> = new Set();
#wantsMetadata: Set<HexKey> = new Set();
readonly #log = debug("ProfileCache");
@ -42,7 +42,7 @@ export class ProfileLoaderService {
TrackMetadata(pk: HexKey | Array<HexKey>) {
const bufferNow = [];
for (const p of Array.isArray(pk) ? pk : [pk]) {
if (p.length > 0 && this.WantsMetadata.add(p)) {
if (p.length === 64 && this.#wantsMetadata.add(p)) {
bufferNow.push(p);
}
}
@ -55,7 +55,7 @@ export class ProfileLoaderService {
UntrackMetadata(pk: HexKey | Array<HexKey>) {
for (const p of Array.isArray(pk) ? pk : [pk]) {
if (p.length > 0) {
this.WantsMetadata.delete(p);
this.#wantsMetadata.delete(p);
}
}
}
@ -68,10 +68,10 @@ export class ProfileLoaderService {
}
async #FetchMetadata() {
const missingFromCache = await this.#cache.buffer([...this.WantsMetadata]);
const missingFromCache = await this.#cache.buffer([...this.#wantsMetadata]);
const expire = unixNowMs() - ProfileCacheExpire;
const expired = [...this.WantsMetadata]
const expired = [...this.#wantsMetadata]
.filter(a => !missingFromCache.includes(a))
.filter(a => (this.#cache.getFromCache(a)?.loaded ?? 0) < expire);
const missing = new Set([...missingFromCache, ...expired]);

View File

@ -70,6 +70,13 @@ export class RequestBuilder {
return this.#options;
}
/**
* Add another request builders filters to this one
*/
add(other: RequestBuilder) {
this.#builders.push(...other.#builders);
}
withFilter() {
const ret = new RequestFilterBuilder();
this.#builders.push(ret);
@ -203,7 +210,7 @@ export class RequestFilterBuilder {
return this;
}
tag(key: "e" | "p" | "d" | "t" | "r" | "a", value?: Array<string>) {
tag(key: "e" | "p" | "d" | "t" | "r" | "a" | "g", value?: Array<string>) {
if (!value) return this;
this.#filter[`#${key}`] = appendDedupe(this.#filter[`#${key}`], value);
return this;