import { nip19 } from 'nostr-tools' import type { AuthorPresentationDraft } from '../articlePublisher' import { buildTags } from '../nostrTagSystem' import { PLATFORM_SERVICE } from '../platformConfig' import { generateAuthorHashId } from '../hashIdGenerator' import { generateObjectUrl } from '../urlGenerator' import { parseAuthorPresentationDraft } from '../authorPresentationParsing' interface BuildPresentationEventParams { draft: AuthorPresentationDraft authorPubkey: string authorName: string category?: 'sciencefiction' | 'research' version?: number index?: number } export async function buildPresentationEvent( params: BuildPresentationEventParams ): Promise<{ kind: 1; created_at: number; tags: string[][]; content: string }> { const { category, version, index } = normalizeBuildPresentationEventParams(params) const parsedDraft = parseAuthorPresentationDraft(params.draft) const hashId = await generateAuthorHashIdForPresentation({ authorPubkey: params.authorPubkey, authorName: params.authorName, parsedDraft, mainnetAddress: params.draft.mainnetAddress, pictureUrl: params.draft.pictureUrl, category, }) const profileUrl = generateObjectUrl('author', hashId, index, version) const visibleContent = buildPresentationVisibleContent({ profileUrl, authorName: params.authorName, pictureUrl: params.draft.pictureUrl, presentation: parsedDraft.presentation, contentDescription: parsedDraft.contentDescription, }) const profileJson = buildPresentationProfileJson({ authorPubkey: params.authorPubkey, authorName: params.authorName, profileUrl, presentation: parsedDraft.presentation, contentDescription: parsedDraft.contentDescription, mainnetAddress: params.draft.mainnetAddress, pictureUrl: params.draft.pictureUrl, category, version, index, }) const tags = buildPresentationTags({ draft: params.draft, category, hashId, version, profileJson }) return { kind: 1 as const, created_at: Math.floor(Date.now() / 1000), tags, content: visibleContent } } function normalizeBuildPresentationEventParams(params: BuildPresentationEventParams): { category: 'sciencefiction' | 'research'; version: number; index: number } { return { category: params.category ?? 'sciencefiction', version: params.version ?? 0, index: params.index ?? 0 } } async function generateAuthorHashIdForPresentation(params: { authorPubkey: string authorName: string parsedDraft: { presentation: string; contentDescription: string } mainnetAddress: string | undefined pictureUrl: string | undefined category: 'sciencefiction' | 'research' }): Promise { return generateAuthorHashId({ pubkey: params.authorPubkey, authorName: params.authorName, presentation: params.parsedDraft.presentation, contentDescription: params.parsedDraft.contentDescription, mainnetAddress: params.mainnetAddress ?? undefined, pictureUrl: params.pictureUrl ?? undefined, category: params.category, }) } function buildPresentationVisibleContent(params: { profileUrl: string authorName: string pictureUrl: string | undefined presentation: string contentDescription: string }): string { const linkWithPreview = buildProfileLink({ profileUrl: params.profileUrl, authorName: params.authorName, pictureUrl: params.pictureUrl }) const lines = [ 'Nouveau profil auteur publié sur zapwall.fr (plateforme de publications scientifiques)', linkWithPreview, `Présentation personnelle : ${params.presentation}`, ] if (params.contentDescription) { lines.push(`Description de votre contenu : ${params.contentDescription}`) } return lines.join('\n') } function buildPresentationProfileJson(params: { authorPubkey: string authorName: string profileUrl: string presentation: string contentDescription: string mainnetAddress: string | undefined pictureUrl: string | undefined category: 'sciencefiction' | 'research' version: number index: number }): string { const npub = nip19.npubEncode(params.authorPubkey) return JSON.stringify({ authorName: params.authorName, npub, pubkey: params.authorPubkey, presentation: params.presentation, contentDescription: params.contentDescription, mainnetAddress: params.mainnetAddress, pictureUrl: params.pictureUrl, category: params.category, url: params.profileUrl, version: params.version, index: params.index, }) } function buildPresentationTags(params: { draft: AuthorPresentationDraft; category: 'sciencefiction' | 'research'; hashId: string; version: number; profileJson: string }): string[][] { const tags = buildTags({ type: 'author', category: params.category, id: params.hashId, service: PLATFORM_SERVICE, version: params.version, hidden: false, paywall: false, title: params.draft.title, preview: params.draft.preview, mainnetAddress: params.draft.mainnetAddress, ...(params.draft.pictureUrl ? { pictureUrl: params.draft.pictureUrl } : {}), }) tags.push(['json', params.profileJson]) return tags } function buildProfileLink(params: { profileUrl: string; authorName: string; pictureUrl: string | undefined }): string { if (params.pictureUrl) { return `[![${params.authorName}](${params.pictureUrl})](${params.profileUrl})` } return params.profileUrl }