From a766449ba6302a5f04dc0b2f1a15db9654f6b49c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 13 May 2023 14:45:13 -0500 Subject: [PATCH] Let searchController look up accounts --- src/app.ts | 4 +++- src/controllers/api/accounts.ts | 16 +--------------- src/controllers/api/search.ts | 23 +++++++++++++++++++++++ src/utils.ts | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 src/controllers/api/search.ts diff --git a/src/app.ts b/src/app.ts index 26f0925e..16e74703 100644 --- a/src/app.ts +++ b/src/app.ts @@ -22,6 +22,7 @@ import { } from './controllers/api/statuses.ts'; import { requireAuth, setAuth } from './middleware/auth.ts'; import { indexController } from './controllers/site.ts'; +import { searchController } from './controllers/api/search.ts'; interface AppEnv extends HonoEnv { Variables: { @@ -63,13 +64,14 @@ app.post('/api/v1/statuses', requireAuth, createStatusController); app.get('/api/v1/timelines/home', requireAuth, homeController); app.get('/api/v1/preferences', preferencesController); +app.get('/api/v1/search', searchController); +app.get('/api/v2/search', searchController); // Not (yet) implemented. app.get('/api/v1/notifications', emptyArrayController); app.get('/api/v1/bookmarks', emptyArrayController); app.get('/api/v1/custom_emojis', emptyArrayController); app.get('/api/v1/accounts/search', emptyArrayController); -app.get('/api/v2/search', (c) => c.json({ accounts: [], statuses: [], hashtags: [] })); app.get('/api/v1/filters', emptyArrayController); app.get('/api/v1/blocks', emptyArrayController); app.get('/api/v1/mutes', emptyArrayController); diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 7e5cea8b..589c5c04 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -1,11 +1,8 @@ import { type AppController } from '@/app.ts'; import { z } from '@/deps.ts'; import { getAuthor, getFilter, getFollows } from '@/client.ts'; -import { lookupNip05Cached } from '@/nip05.ts'; import { toAccount, toStatus } from '@/transmute.ts'; -import { bech32ToPubkey, eventDateComparator } from '@/utils.ts'; - -import type { Event } from '@/event.ts'; +import { eventDateComparator, lookupAccount } from '@/utils.ts'; const credentialsController: AppController = async (c) => { const pubkey = c.get('pubkey')!; @@ -107,17 +104,6 @@ const accountStatusesController: AppController = async (c) => { return c.json(statuses); }; -/** Resolve a bech32 or NIP-05 identifier to an account. */ -async function lookupAccount(value: string): Promise | undefined> { - console.log(`Looking up ${value}`); - - const pubkey = bech32ToPubkey(value) || await lookupNip05Cached(value); - - if (pubkey) { - return getAuthor(pubkey); - } -} - export { accountController, accountLookupController, diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts new file mode 100644 index 00000000..d6988ea1 --- /dev/null +++ b/src/controllers/api/search.ts @@ -0,0 +1,23 @@ +import { AppController } from '@/app.ts'; +import { lookupAccount } from '../../utils.ts'; +import { toAccount } from '../../transmute.ts'; + +const searchController: AppController = async (c) => { + const q = c.req.query('q'); + + if (!q) { + return c.json({ error: 'Missing `q` query parameter.' }, 422); + } + + // For now, only support looking up accounts. + // TODO: Support searching statuses and hashtags. + const event = await lookupAccount(decodeURIComponent(q)); + + return c.json({ + accounts: event ? [await toAccount(event)] : [], + statuses: [], + hashtags: [], + }); +}; + +export { searchController }; diff --git a/src/utils.ts b/src/utils.ts index e8acb842..4f821702 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,7 @@ +import { getAuthor } from '@/client.ts'; import { Context, getPublicKey, nip19, nip21, parseFormData } from '@/deps.ts'; import { type Event } from '@/event.ts'; +import { lookupNip05Cached } from '@/nip05.ts'; /** Get the current time in Nostr format. */ const nostrNow = () => Math.floor(new Date().getTime() / 1000); @@ -75,6 +77,17 @@ function parseNip05(value: string): Nip05 { }; } +/** Resolve a bech32 or NIP-05 identifier to an account. */ +async function lookupAccount(value: string): Promise | undefined> { + console.log(`Looking up ${value}`); + + const pubkey = bech32ToPubkey(value) || await lookupNip05Cached(value); + + if (pubkey) { + return getAuthor(pubkey); + } +} + /** Parse request body to JSON, depending on the content-type of the request. */ async function parseBody(req: Request): Promise { switch (req.headers.get('content-type')?.split(';')[0]) { @@ -92,6 +105,7 @@ export { getKeys, isBech32, isNostrId, + lookupAccount, type Nip05, nostrNow, parseBody,