mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
search: fix nip05 lookups
This commit is contained in:
parent
bc603188fa
commit
8f5ec50a25
2 changed files with 45 additions and 41 deletions
|
|
@ -6,8 +6,7 @@ import { AppController } from '@/app.ts';
|
||||||
import { booleanParamSchema } from '@/schema.ts';
|
import { booleanParamSchema } from '@/schema.ts';
|
||||||
import { Storages } from '@/storages.ts';
|
import { Storages } from '@/storages.ts';
|
||||||
import { hydrateEvents } from '@/storages/hydrate.ts';
|
import { hydrateEvents } from '@/storages/hydrate.ts';
|
||||||
import { bech32ToPubkey } from '@/utils.ts';
|
import { extractIdentifier, lookupPubkey } from '@/utils/lookup.ts';
|
||||||
import { ACCT_REGEX, extractIdentifier } from '@/utils/lookup.ts';
|
|
||||||
import { nip05Cache } from '@/utils/nip05.ts';
|
import { nip05Cache } from '@/utils/nip05.ts';
|
||||||
import { accountFromPubkey, 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';
|
||||||
|
|
@ -36,7 +35,7 @@ const searchController: AppController = async (c) => {
|
||||||
|
|
||||||
// Render account from pubkey.
|
// Render account from pubkey.
|
||||||
if (!event && lookup) {
|
if (!event && lookup) {
|
||||||
const pubkey = bech32ToPubkey(lookup);
|
const pubkey = await lookupPubkey(lookup);
|
||||||
return c.json({
|
return c.json({
|
||||||
accounts: pubkey ? [await accountFromPubkey(pubkey)] : [],
|
accounts: pubkey ? [await accountFromPubkey(pubkey)] : [],
|
||||||
statuses: [],
|
statuses: [],
|
||||||
|
|
@ -120,51 +119,55 @@ async function lookupEvent(query: SearchQuery, signal: AbortSignal): Promise<Nos
|
||||||
|
|
||||||
/** Get filters to lookup the input value. */
|
/** Get filters to lookup the input value. */
|
||||||
async function getLookupFilters({ q, type, resolve }: SearchQuery, signal: AbortSignal): Promise<NostrFilter[]> {
|
async function getLookupFilters({ q, type, resolve }: SearchQuery, signal: AbortSignal): Promise<NostrFilter[]> {
|
||||||
const filters: NostrFilter[] = [];
|
|
||||||
|
|
||||||
const accounts = !type || type === 'accounts';
|
const accounts = !type || type === 'accounts';
|
||||||
const statuses = !type || type === 'statuses';
|
const statuses = !type || type === 'statuses';
|
||||||
|
|
||||||
if (!resolve || type === 'hashtags') {
|
if (!resolve || type === 'hashtags') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n.id().safeParse(q).success) {
|
||||||
|
const filters: NostrFilter[] = [];
|
||||||
|
if (accounts) filters.push({ kinds: [0], authors: [q] });
|
||||||
|
if (statuses) filters.push({ kinds: [1], ids: [q] });
|
||||||
return filters;
|
return filters;
|
||||||
}
|
}
|
||||||
|
|
||||||
const lookup = extractIdentifier(q);
|
const lookup = extractIdentifier(q);
|
||||||
if (lookup) {
|
if (!lookup) return [];
|
||||||
try {
|
|
||||||
const result = nip19.decode(lookup);
|
try {
|
||||||
switch (result.type) {
|
const result = nip19.decode(lookup);
|
||||||
case 'npub':
|
const filters: NostrFilter[] = [];
|
||||||
if (accounts) filters.push({ kinds: [0], authors: [result.data] });
|
switch (result.type) {
|
||||||
break;
|
case 'npub':
|
||||||
case 'nprofile':
|
if (accounts) filters.push({ kinds: [0], authors: [result.data] });
|
||||||
if (accounts) filters.push({ kinds: [0], authors: [result.data.pubkey] });
|
break;
|
||||||
break;
|
case 'nprofile':
|
||||||
case 'note':
|
if (accounts) filters.push({ kinds: [0], authors: [result.data.pubkey] });
|
||||||
if (statuses) filters.push({ kinds: [1], ids: [result.data] });
|
break;
|
||||||
break;
|
case 'note':
|
||||||
case 'nevent':
|
if (statuses) filters.push({ kinds: [1], ids: [result.data] });
|
||||||
if (statuses) filters.push({ kinds: [1], ids: [result.data.id] });
|
break;
|
||||||
break;
|
case 'nevent':
|
||||||
}
|
if (statuses) filters.push({ kinds: [1], ids: [result.data.id] });
|
||||||
} catch {
|
break;
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
} else if (n.id().safeParse(q).success) {
|
|
||||||
if (accounts) filters.push({ kinds: [0], authors: [q] });
|
|
||||||
if (statuses) filters.push({ kinds: [1], ids: [q] });
|
|
||||||
} else if (accounts && ACCT_REGEX.test(q)) {
|
|
||||||
try {
|
|
||||||
const { pubkey } = await nip05Cache.fetch(q, { signal });
|
|
||||||
if (pubkey) {
|
|
||||||
filters.push({ kinds: [0], authors: [pubkey] });
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// do nothing
|
|
||||||
}
|
}
|
||||||
|
return filters;
|
||||||
|
} catch {
|
||||||
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
return filters;
|
try {
|
||||||
|
const { pubkey } = await nip05Cache.fetch(lookup, { signal });
|
||||||
|
if (pubkey) {
|
||||||
|
return [{ kinds: [0], authors: [pubkey] }];
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
export { searchController };
|
export { searchController };
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,6 @@ import { bech32ToPubkey } from '@/utils.ts';
|
||||||
import { nip05Cache } from '@/utils/nip05.ts';
|
import { nip05Cache } from '@/utils/nip05.ts';
|
||||||
import { Stickynotes } from '@soapbox/stickynotes';
|
import { Stickynotes } from '@soapbox/stickynotes';
|
||||||
|
|
||||||
/** Matches NIP-05 names with or without an @ in front. */
|
|
||||||
export const ACCT_REGEX = /^@?(?:([\w.+-]+)@)?([\w.-]+)$/;
|
|
||||||
|
|
||||||
/** Resolve a bech32 or NIP-05 identifier to an account. */
|
/** Resolve a bech32 or NIP-05 identifier to an account. */
|
||||||
export async function lookupAccount(
|
export async function lookupAccount(
|
||||||
value: string,
|
value: string,
|
||||||
|
|
@ -43,6 +40,8 @@ export async function lookupPubkey(value: string, signal?: AbortSignal): Promise
|
||||||
|
|
||||||
/** Extract an acct or bech32 identifier out of a URL or of itself. */
|
/** Extract an acct or bech32 identifier out of a URL or of itself. */
|
||||||
export function extractIdentifier(value: string): string | undefined {
|
export function extractIdentifier(value: string): string | undefined {
|
||||||
|
value = value.trim();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const uri = new URL(value);
|
const uri = new URL(value);
|
||||||
switch (uri.protocol) {
|
switch (uri.protocol) {
|
||||||
|
|
@ -79,11 +78,13 @@ export function extractIdentifier(value: string): string | undefined {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
value = value.replace(/^@/, '');
|
||||||
|
|
||||||
if (n.bech32().safeParse(value).success) {
|
if (n.bech32().safeParse(value).success) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ACCT_REGEX.test(value)) {
|
if (NIP05.regex().test(value)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue