blowater/UI/event_syncer.ts

49 lines
1.6 KiB
TypeScript
Raw Normal View History

import {
ConnectionPool,
SubscriptionAlreadyExist,
} from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/relay.ts";
2023-07-11 09:49:58 +00:00
import { Database_Contextual_View } from "../database.ts";
import { NoteID } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nip19.ts";
import { verifyEvent } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nostr.ts";
export class EventSyncer {
2023-07-11 09:49:58 +00:00
constructor(private readonly pool: ConnectionPool, private readonly db: Database_Contextual_View) {}
2023-07-30 06:39:53 +00:00
syncEvent(id: NoteID) {
for (const e of this.db.events) {
if (e.id == id.hex) {
return e;
}
}
return (async () => {
let events = await this.pool.newSub("EventSyncer", {
ids: [id.hex],
});
if (events instanceof SubscriptionAlreadyExist) {
events = await this.pool.updateSub("EventSyncer", {
ids: [id.hex],
});
}
if (events instanceof Error) {
return events;
}
for await (const { res, url } of events) {
if (res.type == "EOSE") {
continue;
}
const ok = await verifyEvent(res.event);
if (!ok) {
console.warn(res.event, url, "not valid");
continue;
}
await this.db.addEvent(res.event);
return; // just need to read from 1 relay
}
})();
}
2023-07-30 06:39:53 +00:00
// todo
syncSocial() {
}
}