From 5f0b1ffe79956d2693cdb6f8068f6b2b144d50d2 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 04:56:38 +0530 Subject: [PATCH 1/4] fetch profiles separately from notes --- scripts/nostr-pull.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index 03e389cb..d215e435 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -32,9 +32,11 @@ const importUsers = async ( await Promise.all(relays.map(async (relay) => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); - const kinds = [0, 3]; - if (!profilesOnly) kinds.push(1); - const matched = await conn.query([{ kinds, authors, limit: 1000 }]); + + const matched = [ + ...await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]), + ...(!profilesOnly ? [] : await conn.query([{ kinds: [1], authors, limit: 1000 }])), + ]; await conn.close(); await Promise.all( matched.map(async (event) => { From f1e9eb4f4c87e14f840b6bb7d3bece1db9a2b9cf Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 04:58:06 +0530 Subject: [PATCH 2/4] fetch each author's notes separately --- scripts/nostr-pull.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index d215e435..ae5b0183 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -35,8 +35,11 @@ const importUsers = async ( const matched = [ ...await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]), - ...(!profilesOnly ? [] : await conn.query([{ kinds: [1], authors, limit: 1000 }])), + ...(!profilesOnly ? [] : await conn.query( + authors.map((author) => ({ kinds: [1], authors: [author], limit: 200 })), + )), ]; + await conn.close(); await Promise.all( matched.map(async (event) => { From d2df6721bdcf53092e4470dfa923e923fc7d97f7 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 05:01:16 +0530 Subject: [PATCH 3/4] fix kind 1 querying logic --- scripts/nostr-pull.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index ae5b0183..c3cbb67b 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -32,13 +32,14 @@ const importUsers = async ( await Promise.all(relays.map(async (relay) => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); - - const matched = [ - ...await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]), - ...(!profilesOnly ? [] : await conn.query( - authors.map((author) => ({ kinds: [1], authors: [author], limit: 200 })), - )), - ]; + const matched = await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]); + if (!profilesOnly) { + matched.push( + ...await conn.query( + authors.map((author) => ({ kinds: [1], authors: [author], limit: 200 })), + ), + ); + } await conn.close(); await Promise.all( From 757e5c9baaa75fbce45822a06c1bd2be89fe776c Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 05:21:16 +0530 Subject: [PATCH 4/4] print name as import acknowledgement --- scripts/nostr-pull.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index c3cbb67b..1a236f8f 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -1,5 +1,6 @@ /** - * Script to import a user/list of users into Ditto given their npub/pubkey by looking them up on a list of relays. + * Script to import a user/list of users into Ditto given their npub/pubkey + * by looking them up on a list of relays. */ import { NostrEvent, NRelay1, NSchema } from '@nostrify/nostrify'; @@ -26,13 +27,13 @@ const importUsers = async ( const profiles: Record> = {}; // Kind 1s. const notes = new Set(); - const { profilesOnly = false } = opts || {}; await Promise.all(relays.map(async (relay) => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); const matched = await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]); + if (!profilesOnly) { matched.push( ...await conn.query( @@ -65,6 +66,20 @@ const importUsers = async ( for (const kind in profile) { await doEvent(profile[kind]); } + + let name = user; + // kind 0, not first idx + const event = profile[0]; + if (event) { + // if event exists, print name + const parsed = JSON.parse(event.content); + name = parsed.nip05 || parsed.name || name; + } + if (NSchema.id().safeParse(name).success) { + // if no kind 0 found and this is a pubkey, encode as npub + name = nip19.npubEncode(name); + } + console.info(`Imported user ${name}${profilesOnly ? "'s profile" : ''}.`); } }; @@ -85,6 +100,7 @@ if (import.meta.main) { if (optionsEnd) { console.error('Option encountered after end of options section.'); showUsage(); + Deno.exit(1); } switch (arg) { case '-p':