Merge branch 'feat-search-account-with-no-kind-0' into 'main'

Render account from pubkey if kind 0 is not present in database

See merge request soapbox-pub/ditto!173
This commit is contained in:
Alex Gleason 2024-04-21 01:44:16 +00:00
commit ed08ac7c17
2 changed files with 28 additions and 5 deletions

View File

@ -100,6 +100,17 @@ const accountSearchController: AppController = async (c) => {
signal: c.req.raw.signal, signal: c.req.raw.signal,
}); });
if ((results.length < 1) && query.match(/npub1\w+/)) {
const possibleNpub = query;
try {
const npubHex = nip19.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))); const accounts = await Promise.all(results.map((event) => renderAccount(event)));
return c.json(accounts); return c.json(accounts);
}; };

View File

@ -6,7 +6,7 @@ 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';
@ -47,18 +47,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 = nip19.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,
statuses: statuses.filter(Boolean), statuses,
hashtags: [], hashtags: [],
}); });
}; };