story-research-zapwall/lib/nostrEventParsing.ts
Nicolas Cantu 3000872dbc refactoring
- **Motivations :** Assurer passage du lint strict et clarifier la logique paiements/publications.

- **Root causes :** Fonctions trop longues, promesses non gérées et typages WebLN/Nostr incomplets.

- **Correctifs :** Refactor PaymentModal (handlers void), extraction helpers articlePublisher, simplification polling sponsoring/zap, corrections curly et awaits.

- **Evolutions :** Nouveau module articlePublisherHelpers pour présentation/aiguillage contenu privé.

- **Page affectées :** components/PaymentModal.tsx, lib/articlePublisher.ts, lib/articlePublisherHelpers.ts, lib/paymentPolling.ts, lib/sponsoring.ts, lib/nostrZapVerification.ts et dépendances liées.
2025-12-22 17:56:00 +01:00

59 lines
1.9 KiB
TypeScript

import type { Event } from 'nostr-tools'
import type { Article } from '@/types/nostr'
/**
* Parse article metadata from Nostr event
*/
export function parseArticleFromEvent(event: Event): Article | null {
try {
const tags = extractTags(event)
const { previewContent } = getPreviewContent(event.content, tags.preview)
return buildArticle(event, tags, previewContent)
} catch (e) {
console.error('Error parsing article:', e)
return null
}
}
function extractTags(event: Event) {
const findTag = (key: string) => event.tags.find((tag) => tag[0] === key)?.[1]
return {
title: findTag('title') ?? 'Untitled',
preview: findTag('preview'),
zapAmount: parseInt(findTag('zap') ?? '800', 10),
invoice: findTag('invoice'),
paymentHash: findTag('payment_hash'),
category: findTag('category') as import('@/types/nostr').ArticleCategory | undefined,
isPresentation: findTag('presentation') === 'true',
mainnetAddress: findTag('mainnet_address'),
totalSponsoring: parseInt(findTag('total_sponsoring') ?? '0', 10),
authorPresentationId: findTag('author_presentation_id'),
}
}
function getPreviewContent(content: string, previewTag?: string) {
const lines = content.split('\n')
const previewContent = previewTag ?? lines[0] ?? content.substring(0, 200)
return { previewContent }
}
function buildArticle(event: Event, tags: ReturnType<typeof extractTags>, preview: string): Article {
return {
id: event.id,
pubkey: event.pubkey,
title: tags.title,
preview,
content: '',
createdAt: event.created_at,
zapAmount: tags.zapAmount,
paid: false,
invoice: tags.invoice,
paymentHash: tags.paymentHash,
category: tags.category,
isPresentation: tags.isPresentation,
mainnetAddress: tags.mainnetAddress,
totalSponsoring: tags.totalSponsoring,
authorPresentationId: tags.authorPresentationId,
}
}