diff options
| author | Adam Soltys <[email protected]> | 2024-10-09 12:51:00 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-10 12:17:05 -0700 |
| commit | d734c424f19bbec89274d828c3a2b70dd3ef221a (patch) | |
| tree | 8ec38336b345539c5ea110c2800eff79c677b19d | |
| parent | c0bfa4a8a45415e16ef335219c84632f4c04f97d (diff) | |
| download | coinos-server-d734c424f19bbec89274d828c3a2b70dd3ef221a.tar.gz coinos-server-d734c424f19bbec89274d828c3a2b70dd3ef221a.zip | |
follows
| -rw-r--r-- | lib/nostr.ts | 39 | ||||
| -rw-r--r-- | routes/nostr.ts | 28 | ||||
| -rw-r--r-- | routes/users.ts | 10 |
3 files changed, 47 insertions, 30 deletions
diff --git a/lib/nostr.ts b/lib/nostr.ts index ca09649c..e4d87b42 100644 --- a/lib/nostr.ts +++ b/lib/nostr.ts @@ -10,7 +10,10 @@ export const serverPubkey = getPublicKey( nip19.decode(config.nostrKey).data as Uint8Array, ); -let alwaysTrue: any = (t: Event) => { t[Symbol("verified")] = true; return true; }; +let alwaysTrue: any = (t: Event) => { + t[Symbol("verified")] = true; + return true; +}; export let pool = new AbstractSimplePool({ verifyEvent: alwaysTrue }); export async function send(ev, url = config.nostr) { @@ -107,14 +110,36 @@ export let getRelays = async (pubkey): Promise<any> => { }; export let getProfile = async (pubkey) => { - if (await db.sIsMember("noprofile", pubkey)) return; + let noprofile = await db.sIsMember("noprofile", pubkey); + if (noprofile) return; + let pubkeys = [pubkey]; let relays = [config.nostr]; - let filter = { authors: [pubkey], kinds: [0] }; + let filter: any = { authors: pubkeys, kinds: [0] }; + let ev = await pool.get(relays, filter); + + if (!ev) { + filter = { cache: ["user_infos", { pubkeys }] }; + ev = (await pool.querySync([config.cache], filter)).find( + (e) => e.kind === 0, + ); + } + + if (!ev) { + ({ write: relays } = await getRelays(pubkey)); + ev = await pool.get(relays, filter); + } + if (ev) { + send(ev); return JSON.parse(ev.content); - } else - getRelays(pubkey).then(({ write: relays }) => - pool.get(relays, filter).then(send), - ); + } else return anon(pubkey); }; + +export let anon = (pubkey) => ({ + name: pubkey.substr(0, 6), + pubkey, + anon: true, + follows: [], + followers: [], +}); diff --git a/routes/nostr.ts b/routes/nostr.ts index 3f801677..573424e3 100644 --- a/routes/nostr.ts +++ b/routes/nostr.ts @@ -1,15 +1,7 @@ import config from "$config"; import { bail, chunk, getUser, uniq } from "$lib/utils"; import { g, s, db } from "$lib/db"; -import { send, getRelays, serverPubkey, pool } from "$lib/nostr"; - -let anon = (pubkey) => ({ - username: pubkey.substr(0, 6), - pubkey, - anon: true, - follows: [], - followers: [], -}); +import { anon, send, getRelays, serverPubkey, pool } from "$lib/nostr"; export default { async event(req, res) { @@ -32,7 +24,6 @@ export default { query: { tagsonly }, } = req; try { - let filter = { kinds: [3], authors: [pubkey] }; let ev = await pool.get([config.nostr], filter); @@ -53,19 +44,14 @@ export default { let cache = await g(`${pubkey}:follows`); if (cache && cache.t >= created_at) ({ follows } = cache); else { - let profiles = []; let pubkeys = tags.map((t) => t[1]).filter((p) => p.length === 64); - for (let authors of chunk(pubkeys, 100)) { - let filter = { authors, kinds: [0] }; - profiles.push(...(await pool.querySync([config.nostr], filter))); - } - let filter: any = { cache: ["user_infos", { pubkeys }] }; - let followers = JSON.parse( - (await pool.querySync([config.cache], filter)).find( - (e) => e.kind === 10000133, - ).content, - ); + let infos = await pool.querySync([config.cache], filter); + let profiles = infos.filter((e) => e.kind === 0); + + let f = infos.find((e) => e.kind === 10000133); + let followers = {}; + if (f) followers = JSON.parse(f.content); for (let p of profiles) { let { content, pubkey } = p; diff --git a/routes/users.ts b/routes/users.ts index f907340e..e1971e78 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -87,11 +87,17 @@ export default { if (key.length === 64) { let nostr: any = await getProfile(key); if (nostr) { - nostr.username = nostr.name; + nostr.username = nostr.name || key.substr(0, 6); nostr.display = nostr.display_name || nostr.displayName; + delete nostr.display_name; + delete nostr.displayName; + delete nostr.name; } - if (user) user.anon = false; + if (user) { + user.anon = false; + delete nostr.display; + } user = { ...nostr, |
