profile-query.ts (1825B)
1 import { getProfileContent } from 'applesauce-core/helpers'; 2 import { canonicalPublicKey } from '../contracts/public-key.ts'; 3 import { 4 PUBLIC_QUERY_BUDGETS, 5 PUBLIC_SEARCH_BUDGETS 6 } from '../config/budgets.ts'; 7 import { 8 verifiedEnvelopeSnapshot, 9 type VerifiedEnvelope 10 } from './verified-envelope.ts'; 11 import type { PublicFilter } from './exports.ts'; 12 export function publisherProfileQuery( 13 authors: readonly string[] 14 ): readonly PublicFilter[] { 15 if (authors.length > PUBLIC_SEARCH_BUDGETS.publisherAuthorsPerPage) 16 throw new Error('publisher_page_limit'); 17 const unique = new Map<string, true>(); 18 for (const author of authors) { 19 if (canonicalPublicKey(author) === undefined) 20 throw new Error('publisher_key_invalid'); 21 unique.set(author, true); 22 } 23 return unique.size 24 ? [ 25 { 26 kinds: [0], 27 authors: Array.from(unique.keys()), 28 limit: PUBLIC_QUERY_BUDGETS.requestedPerRelay 29 } 30 ] 31 : []; 32 } 33 // Parse a fresh genuine envelope through the qualified generic SDK. Cached SDK 34 // symbols, profile URLs and claimed identity fields never become authority. 35 export function assertedProfileName( 36 proof: VerifiedEnvelope 37 ): string | undefined { 38 const event = verifiedEnvelopeSnapshot(proof); 39 if (!event || event.kind !== 0) return undefined; 40 try { 41 const profile = getProfileContent(event); 42 if (!profile || typeof profile !== 'object' || Array.isArray(profile)) 43 return undefined; 44 for (const value of [ 45 profile.display_name, 46 profile.displayName, 47 profile.name 48 ]) 49 if ( 50 typeof value === 'string' && 51 value.trim() !== '' && 52 value.isWellFormed() 53 ) 54 return value; 55 } catch { 56 /* Unsupported metadata leaves the selected head at key fallback. */ 57 } 58 return undefined; 59 }