feat: render account from pubkey if no kind 0 exists - /v2/search

This commit is contained in:
P. Reis 2024-04-20 21:26:05 -03:00
parent 60a1ff7adc
commit 49f06869a3
1 changed files with 18 additions and 5 deletions

View File

@ -6,9 +6,10 @@ import { nostrIdSchema } from '@/schemas/nostr.ts';
import { searchStore } from '@/storages.ts'; import { searchStore } from '@/storages.ts';
import { dedupeEvents } from '@/utils.ts'; import { dedupeEvents } from '@/utils.ts';
import { nip05Cache } from '@/utils/nip05.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 { renderStatus } from '@/views/mastodon/statuses.ts';
import { hydrateEvents } from '@/storages/hydrate.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. */ /** Matches NIP-05 names with or without an @ in front. */
const ACCT_REGEX = /^@?(?:([\w.+-]+)@)?([\w.-]+)$/; const ACCT_REGEX = /^@?(?:([\w.+-]+)@)?([\w.-]+)$/;
@ -47,18 +48,30 @@ const searchController: AppController = async (c) => {
Promise.all( Promise.all(
results results
.filter((event) => event.kind === 0) .filter((event) => event.kind === 0)
.map((event) => renderAccount(event)), .map((event) => renderAccount(event))
.filter(Boolean),
), ),
Promise.all( Promise.all(
results results
.filter((event) => event.kind === 1) .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({ return c.json({
accounts: accounts.filter(Boolean), accounts: accounts,
statuses: statuses.filter(Boolean), statuses: statuses,
hashtags: [], hashtags: [],
}); });
}; };