mirror of
https://github.com/BlowaterNostr/blowater.git
synced 2024-10-18 15:43:20 +00:00
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { ConnectionPool, SubscriptionAlreadyExist } from "../lib/nostr-ts/relay.ts";
|
|
import { Database_Contextual_View } from "../database.ts";
|
|
import { NostrFilters, NostrKind, RelayResponse_REQ_Message, verifyEvent } from "../lib/nostr-ts/nostr.ts";
|
|
import { Channel } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
|
|
import { NoteID } from "../lib/nostr-ts/nip19.ts";
|
|
|
|
export class EventSyncer {
|
|
constructor(private readonly pool: ConnectionPool, private readonly db: Database_Contextual_View) {}
|
|
|
|
syncEvent(id: NoteID) {
|
|
const subID = EventSyncer.name + ":syncEvent";
|
|
for (const e of this.db.events) {
|
|
if (e.id == id.hex) {
|
|
return e;
|
|
}
|
|
}
|
|
return (async () => {
|
|
await this.pool.closeSub(subID);
|
|
let events = await this.pool.newSub(subID, { ids: [id.hex] });
|
|
if (events instanceof Error) {
|
|
return events;
|
|
}
|
|
for await (const { res, url } of events.chan) {
|
|
if (res.type != "EVENT") {
|
|
continue;
|
|
}
|
|
await this.db.addEvent(res.event);
|
|
return; // just need to read from 1 relay
|
|
}
|
|
})();
|
|
}
|
|
|
|
async syncEvents(filter: NostrFilters) {
|
|
await this.pool.closeSub(EventSyncer.name);
|
|
let events = await this.pool.newSub(EventSyncer.name, filter);
|
|
if (events instanceof Error) {
|
|
return events;
|
|
}
|
|
for await (const { res, url } of events.chan) {
|
|
if (res.type != "EVENT") {
|
|
continue;
|
|
}
|
|
await this.db.addEvent(res.event);
|
|
}
|
|
}
|
|
}
|