2023-07-05 09:32:02 +00:00
|
|
|
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";
|
2023-07-05 09:32:02 +00:00
|
|
|
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
|
|
|
|
2023-07-05 09:32:02 +00:00
|
|
|
syncEvent(id: NoteID) {
|
2023-07-16 15:04:23 +00:00
|
|
|
for (const e of this.db.events) {
|
|
|
|
if (e.id == id.hex) {
|
|
|
|
return e;
|
|
|
|
}
|
2023-07-05 09:32:02 +00:00
|
|
|
}
|
|
|
|
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() {
|
|
|
|
}
|
2023-07-05 09:32:02 +00:00
|
|
|
}
|