Let searchController look up accounts

This commit is contained in:
Alex Gleason 2023-05-13 14:45:13 -05:00
parent 017a34d5d4
commit a766449ba6
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 41 additions and 16 deletions

View File

@ -22,6 +22,7 @@ import {
} from './controllers/api/statuses.ts'; } from './controllers/api/statuses.ts';
import { requireAuth, setAuth } from './middleware/auth.ts'; import { requireAuth, setAuth } from './middleware/auth.ts';
import { indexController } from './controllers/site.ts'; import { indexController } from './controllers/site.ts';
import { searchController } from './controllers/api/search.ts';
interface AppEnv extends HonoEnv { interface AppEnv extends HonoEnv {
Variables: { Variables: {
@ -63,13 +64,14 @@ app.post('/api/v1/statuses', requireAuth, createStatusController);
app.get('/api/v1/timelines/home', requireAuth, homeController); app.get('/api/v1/timelines/home', requireAuth, homeController);
app.get('/api/v1/preferences', preferencesController); app.get('/api/v1/preferences', preferencesController);
app.get('/api/v1/search', searchController);
app.get('/api/v2/search', searchController);
// Not (yet) implemented. // Not (yet) implemented.
app.get('/api/v1/notifications', emptyArrayController); app.get('/api/v1/notifications', emptyArrayController);
app.get('/api/v1/bookmarks', emptyArrayController); app.get('/api/v1/bookmarks', emptyArrayController);
app.get('/api/v1/custom_emojis', emptyArrayController); app.get('/api/v1/custom_emojis', emptyArrayController);
app.get('/api/v1/accounts/search', 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/filters', emptyArrayController);
app.get('/api/v1/blocks', emptyArrayController); app.get('/api/v1/blocks', emptyArrayController);
app.get('/api/v1/mutes', emptyArrayController); app.get('/api/v1/mutes', emptyArrayController);

View File

@ -1,11 +1,8 @@
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { z } from '@/deps.ts'; import { z } from '@/deps.ts';
import { getAuthor, getFilter, getFollows } from '@/client.ts'; import { getAuthor, getFilter, getFollows } from '@/client.ts';
import { lookupNip05Cached } from '@/nip05.ts';
import { toAccount, toStatus } from '@/transmute.ts'; import { toAccount, toStatus } from '@/transmute.ts';
import { bech32ToPubkey, eventDateComparator } from '@/utils.ts'; import { eventDateComparator, lookupAccount } from '@/utils.ts';
import type { Event } from '@/event.ts';
const credentialsController: AppController = async (c) => { const credentialsController: AppController = async (c) => {
const pubkey = c.get('pubkey')!; const pubkey = c.get('pubkey')!;
@ -107,17 +104,6 @@ const accountStatusesController: AppController = async (c) => {
return c.json(statuses); return c.json(statuses);
}; };
/** Resolve a bech32 or NIP-05 identifier to an account. */
async function lookupAccount(value: string): Promise<Event<0> | undefined> {
console.log(`Looking up ${value}`);
const pubkey = bech32ToPubkey(value) || await lookupNip05Cached(value);
if (pubkey) {
return getAuthor(pubkey);
}
}
export { export {
accountController, accountController,
accountLookupController, accountLookupController,

View File

@ -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 };

View File

@ -1,5 +1,7 @@
import { getAuthor } from '@/client.ts';
import { Context, getPublicKey, nip19, nip21, parseFormData } from '@/deps.ts'; import { Context, getPublicKey, nip19, nip21, parseFormData } from '@/deps.ts';
import { type Event } from '@/event.ts'; import { type Event } from '@/event.ts';
import { lookupNip05Cached } from '@/nip05.ts';
/** Get the current time in Nostr format. */ /** Get the current time in Nostr format. */
const nostrNow = () => Math.floor(new Date().getTime() / 1000); 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<Event<0> | 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. */ /** Parse request body to JSON, depending on the content-type of the request. */
async function parseBody(req: Request): Promise<unknown> { async function parseBody(req: Request): Promise<unknown> {
switch (req.headers.get('content-type')?.split(';')[0]) { switch (req.headers.get('content-type')?.split(';')[0]) {
@ -92,6 +105,7 @@ export {
getKeys, getKeys,
isBech32, isBech32,
isNostrId, isNostrId,
lookupAccount,
type Nip05, type Nip05,
nostrNow, nostrNow,
parseBody, parseBody,