From e7f5535bd2eda62d41d80f1ce57bbd3b11907d42 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Sun, 1 Sep 2024 19:15:22 +0530 Subject: [PATCH] handle event insertion errors with warning --- scripts/nostr-pull.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index 68766c8f..0556b64a 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -29,6 +29,18 @@ const importUsers = async ( const notes = new Set(); const { profilesOnly = false } = opts || {}; + const put = async (event: NostrEvent) => { + try { + await doEvent(event); + } catch (error) { + if (error.message.includes('violates unique constraint')) { + console.warn(`Skipping existing event ${event.id}...`); + } else { + console.error(error); + } + } + }; + await Promise.all(relays.map(async (relay) => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); @@ -49,7 +61,7 @@ const importUsers = async ( if (kind === 1 && !notes.has(event.id)) { // add the event to eventsDB only if it has not been found already. notes.add(event.id); - await doEvent(event); + await put(event); return; } @@ -64,7 +76,7 @@ const importUsers = async ( for (const user in profiles) { const profile = profiles[user]; for (const kind in profile) { - await doEvent(profile[kind]); + await put(profile[kind]); } let name = user;