extractBech32 -> extractIdentifier, support extracting nip05 names

This commit is contained in:
Alex Gleason
2024-08-07 16:22:10 -05:00
parent d3780037df
commit bc603188fa
4 changed files with 67 additions and 76 deletions

View File

@@ -8,9 +8,9 @@ import { getAuthor, getFollowedPubkeys } from '@/queries.ts';
import { booleanParamSchema, fileSchema } from '@/schema.ts';
import { Storages } from '@/storages.ts';
import { uploadFile } from '@/utils/upload.ts';
import { extractBech32, nostrNow } from '@/utils.ts';
import { nostrNow } from '@/utils.ts';
import { createEvent, paginated, parseBody, updateListEvent } from '@/utils/api.ts';
import { lookupAccount } from '@/utils/lookup.ts';
import { extractIdentifier, lookupAccount } from '@/utils/lookup.ts';
import { renderAccounts, renderEventAccounts, renderStatuses } from '@/views.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderRelationship } from '@/views/mastodon/relationships.ts';
@@ -125,11 +125,11 @@ const accountSearchController: AppController = async (c) => {
const query = decodeURIComponent(result.data.q);
const store = await Storages.search();
const bech32 = extractBech32(query);
const event = await lookupAccount(bech32 ?? query);
const lookup = extractIdentifier(query);
const event = await lookupAccount(lookup ?? query);
if (!event && bech32) {
const pubkey = bech32ToPubkey(bech32);
if (!event && lookup) {
const pubkey = bech32ToPubkey(lookup);
return c.json(pubkey ? [await accountFromPubkey(pubkey)] : []);
}

View File

@@ -6,14 +6,12 @@ import { AppController } from '@/app.ts';
import { booleanParamSchema } from '@/schema.ts';
import { Storages } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { bech32ToPubkey, extractBech32 } from '@/utils.ts';
import { bech32ToPubkey } from '@/utils.ts';
import { ACCT_REGEX, extractIdentifier } from '@/utils/lookup.ts';
import { nip05Cache } from '@/utils/nip05.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
/** Matches NIP-05 names with or without an @ in front. */
const ACCT_REGEX = /^@?(?:([\w.+-]+)@)?([\w.-]+)$/;
const searchQuerySchema = z.object({
q: z.string().transform(decodeURIComponent),
type: z.enum(['accounts', 'statuses', 'hashtags']).optional(),
@@ -34,11 +32,11 @@ const searchController: AppController = async (c) => {
}
const event = await lookupEvent(result.data, signal);
const bech32 = extractBech32(result.data.q);
const lookup = extractIdentifier(result.data.q);
// Render account from pubkey.
if (!event && bech32) {
const pubkey = bech32ToPubkey(bech32);
if (!event && lookup) {
const pubkey = bech32ToPubkey(lookup);
return c.json({
accounts: pubkey ? [await accountFromPubkey(pubkey)] : [],
statuses: [],
@@ -131,11 +129,10 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery, signal: Abort
return filters;
}
const bech32 = extractBech32(q);
if (bech32) {
const lookup = extractIdentifier(q);
if (lookup) {
try {
const result = nip19.decode(bech32);
const result = nip19.decode(lookup);
switch (result.type) {
case 'npub':
if (accounts) filters.push({ kinds: [0], authors: [result.data] });