From 70f69b80f3c3114d26a749ebef475d020783bb03 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 29 Apr 2023 16:28:53 -0500 Subject: [PATCH] Add account search controller, which does basically the same thing as account lookup for now --- src/api/accounts.ts | 20 +++++++++++++++++++- src/app.ts | 3 ++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/api/accounts.ts b/src/api/accounts.ts index 3b422f4..1e7406d 100644 --- a/src/api/accounts.ts +++ b/src/api/accounts.ts @@ -54,4 +54,22 @@ const accountLookupController: AppController = async (c) => { return c.json({ error: 'Could not find user.' }, 404); }; -export { accountController, accountLookupController, credentialsController }; +const accountSearchController: AppController = async (c) => { + const q = c.req.query('q'); + + if (!q) { + return c.json({ error: 'Missing `q` query parameter.' }, 422); + } + + const pubkey = bech32ToPubkey(q); + if (pubkey) { + const event = await getAuthor(pubkey); + if (event) { + return c.json([toAccount(event)]); + } + } + + return c.json([]); +}; + +export { accountController, accountSearchController, accountLookupController, credentialsController }; diff --git a/src/app.ts b/src/app.ts index cdb1c85..c1d5091 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,6 @@ import { type Context, cors, type Handler, Hono, type HonoEnv, type MiddlewareHandler } from '@/deps.ts'; -import { accountController, accountLookupController, credentialsController } from './api/accounts.ts'; +import { accountController, accountLookupController, accountSearchController, credentialsController } from './api/accounts.ts'; import { appCredentialsController, createAppController } from './api/apps.ts'; import { emptyArrayController, emptyObjectController } from './api/fallback.ts'; import homeController from './api/home.ts'; @@ -33,6 +33,7 @@ app.post('/oauth/token', createTokenController); app.post('/oauth/revoke', emptyObjectController); app.get('/api/v1/accounts/verify_credentials', requireAuth, credentialsController); +app.get('/api/v1/accounts/search', accountSearchController); app.get('/api/v1/accounts/lookup', accountLookupController); app.get('/api/v1/accounts/:id', accountController);