From 60a1ff7adce1e65a59c9b35aa8490bc7d27b05e8 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sat, 20 Apr 2024 20:58:12 -0300 Subject: [PATCH 1/3] feat: render account from pubkey if no kind 0 exists - /v1/accounts/search --- src/controllers/api/accounts.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 9e250fa..c689899 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -16,6 +16,7 @@ import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderRelationship } from '@/views/mastodon/relationships.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; +import { decode } from 'npm:nostr-tools@^2.5.0/nip19'; const usernameSchema = z .string().min(1).max(30) @@ -100,6 +101,17 @@ const accountSearchController: AppController = async (c) => { signal: c.req.raw.signal, }); + if ((results.length < 1) && query.match(/npub1\w+/)) { + const possibleNpub = query; + try { + const npubHex = decode(possibleNpub); + return c.json([await accountFromPubkey(String(npubHex.data))]); + } catch (e) { + console.log(e); + return c.json([]); + } + } + const accounts = await Promise.all(results.map((event) => renderAccount(event))); return c.json(accounts); }; From 49f06869a3acad50bac67b11e938abbb3cb9efd8 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sat, 20 Apr 2024 21:26:05 -0300 Subject: [PATCH 2/3] feat: render account from pubkey if no kind 0 exists - /v2/search --- src/controllers/api/search.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index 5fa268a..c95ea45 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -6,9 +6,10 @@ import { nostrIdSchema } from '@/schemas/nostr.ts'; import { searchStore } from '@/storages.ts'; import { dedupeEvents } from '@/utils.ts'; import { nip05Cache } from '@/utils/nip05.ts'; -import { renderAccount } from '@/views/mastodon/accounts.ts'; +import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; +import { decode } from 'npm:nostr-tools@^2.5.0/nip19'; /** Matches NIP-05 names with or without an @ in front. */ const ACCT_REGEX = /^@?(?:([\w.+-]+)@)?([\w.-]+)$/; @@ -47,18 +48,30 @@ const searchController: AppController = async (c) => { Promise.all( results .filter((event) => event.kind === 0) - .map((event) => renderAccount(event)), + .map((event) => renderAccount(event)) + .filter(Boolean), ), Promise.all( results .filter((event) => event.kind === 1) - .map((event) => renderStatus(event, { viewerPubkey: c.get('pubkey') })), + .map((event) => renderStatus(event, { viewerPubkey: c.get('pubkey') })) + .filter(Boolean), ), ]); + if ((result.data.type === 'accounts') && (accounts.length < 1) && (result.data.q.match(/npub1\w+/))) { + const possibleNpub = result.data.q; + try { + const npubHex = decode(possibleNpub); + accounts.push(await accountFromPubkey(String(npubHex.data))); + } catch (e) { + console.log(e); + } + } + return c.json({ - accounts: accounts.filter(Boolean), - statuses: statuses.filter(Boolean), + accounts: accounts, + statuses: statuses, hashtags: [], }); }; From 12b030c8aaa7f2ce5e62b3a0f00b317c6e621064 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sat, 20 Apr 2024 21:52:36 -0300 Subject: [PATCH 3/3] refactor: decode pubkey with right import in search account --- src/controllers/api/accounts.ts | 3 +-- src/controllers/api/search.ts | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index c689899..472c9e5 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -16,7 +16,6 @@ import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderRelationship } from '@/views/mastodon/relationships.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; -import { decode } from 'npm:nostr-tools@^2.5.0/nip19'; const usernameSchema = z .string().min(1).max(30) @@ -104,7 +103,7 @@ const accountSearchController: AppController = async (c) => { if ((results.length < 1) && query.match(/npub1\w+/)) { const possibleNpub = query; try { - const npubHex = decode(possibleNpub); + const npubHex = nip19.decode(possibleNpub); return c.json([await accountFromPubkey(String(npubHex.data))]); } catch (e) { console.log(e); diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index c95ea45..628bb3a 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -9,7 +9,6 @@ import { nip05Cache } from '@/utils/nip05.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; -import { decode } from 'npm:nostr-tools@^2.5.0/nip19'; /** Matches NIP-05 names with or without an @ in front. */ const ACCT_REGEX = /^@?(?:([\w.+-]+)@)?([\w.-]+)$/; @@ -62,7 +61,7 @@ const searchController: AppController = async (c) => { if ((result.data.type === 'accounts') && (accounts.length < 1) && (result.data.q.match(/npub1\w+/))) { const possibleNpub = result.data.q; try { - const npubHex = decode(possibleNpub); + const npubHex = nip19.decode(possibleNpub); accounts.push(await accountFromPubkey(String(npubHex.data))); } catch (e) { console.log(e); @@ -70,8 +69,8 @@ const searchController: AppController = async (c) => { } return c.json({ - accounts: accounts, - statuses: statuses, + accounts, + statuses, hashtags: [], }); };