story-research-zapwall/lib/nostrEventParsing.ts
2025-12-22 09:48:57 +01:00

47 lines
1.5 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 content = event.content
// Parse article metadata from tags
const titleTag = event.tags.find((tag) => tag[0] === 'title')
const previewTag = event.tags.find((tag) => tag[0] === 'preview')
const zapTag = event.tags.find((tag) => tag[0] === 'zap')
const invoiceTag = event.tags.find((tag) => tag[0] === 'invoice')
const paymentHashTag = event.tags.find((tag) => tag[0] === 'payment_hash')
const title = titleTag?.[1] || 'Untitled'
const preview = previewTag?.[1] || content.substring(0, 200)
const zapAmount = zapTag ? parseInt(zapTag[1] || '800') : 800
// Extract invoice information from tags
const invoice = invoiceTag?.[1] || undefined
const paymentHash = paymentHashTag?.[1] || undefined
// Split content: preview is in the note, full content is in private message
const lines = content.split('\n')
const previewContent = preview || lines[0] || content.substring(0, 200)
return {
id: event.id,
pubkey: event.pubkey,
title,
preview: previewContent,
content: '', // Full content will be loaded from private message
createdAt: event.created_at,
zapAmount,
paid: false,
invoice,
paymentHash,
}
} catch (e) {
console.error('Error parsing article:', e)
return null
}
}