74 lines
2.8 KiB
TypeScript
74 lines
2.8 KiB
TypeScript
import type { Event } from 'nostr-tools'
|
|
import { extractTagsFromEvent } from '../nostrTagSystem'
|
|
import { generateSeriesHashId } from '../hashIdGenerator'
|
|
import type { ExtractedSeries } from './types'
|
|
import { getMetadataFromEvent } from './metadataJson'
|
|
import { firstString } from './utils'
|
|
|
|
export async function extractSeriesFromEvent(event: Event): Promise<ExtractedSeries | null> {
|
|
const tags = extractTagsFromEvent(event)
|
|
if (tags.type !== 'series') {
|
|
return null
|
|
}
|
|
const metadata = getMetadataFromEvent(event)
|
|
if (metadata?.type === 'series') {
|
|
const seriesData = buildSeriesDataFromMetadata({ event, tags, metadata })
|
|
const id = await generateSeriesHashId(seriesData)
|
|
return buildExtractedSeries({ eventId: event.id, id, data: seriesData })
|
|
}
|
|
|
|
if (!tags.title || !tags.description) {
|
|
return null
|
|
}
|
|
const base = {
|
|
pubkey: event.pubkey,
|
|
title: tags.title,
|
|
description: tags.description,
|
|
preview: (tags.preview as string) ?? event.content.substring(0, 200),
|
|
category: tags.category ?? 'sciencefiction',
|
|
...(tags.coverUrl ? { coverUrl: tags.coverUrl } : {}),
|
|
}
|
|
const id = await generateSeriesHashId(base)
|
|
return buildExtractedSeries({ eventId: event.id, id, data: base })
|
|
}
|
|
|
|
function buildSeriesDataFromMetadata(params: {
|
|
event: Event
|
|
tags: ReturnType<typeof extractTagsFromEvent>
|
|
metadata: Record<string, unknown>
|
|
}): { pubkey: string; title: string; description: string; preview: string; coverUrl?: string; category: string } {
|
|
const title = firstString(params.metadata.title, params.tags.title) ?? ''
|
|
const preview = firstString(params.metadata.preview, params.tags.preview) ?? params.event.content.substring(0, 200)
|
|
const pubkey = firstString(params.metadata.pubkey, params.event.pubkey) ?? params.event.pubkey
|
|
const coverUrl = firstString(params.metadata.coverUrl, params.tags.coverUrl)
|
|
const result: { pubkey: string; title: string; description: string; preview: string; coverUrl?: string; category: string } = {
|
|
pubkey,
|
|
title,
|
|
description: firstString(params.metadata.description) ?? '',
|
|
preview,
|
|
category: firstString(params.metadata.category, params.tags.category, 'sciencefiction') ?? 'sciencefiction',
|
|
}
|
|
if (coverUrl) {
|
|
result.coverUrl = coverUrl
|
|
}
|
|
return result
|
|
}
|
|
|
|
function buildExtractedSeries(params: {
|
|
eventId: string
|
|
id: string
|
|
data: { pubkey: string; title: string; description: string; preview: string; coverUrl?: string; category: string }
|
|
}): ExtractedSeries {
|
|
return {
|
|
type: 'series',
|
|
id: params.id,
|
|
pubkey: params.data.pubkey,
|
|
title: params.data.title,
|
|
description: params.data.description,
|
|
category: params.data.category,
|
|
eventId: params.eventId,
|
|
...(params.data.coverUrl ? { coverUrl: params.data.coverUrl } : {}),
|
|
...(params.data.preview ? { preview: params.data.preview } : {}),
|
|
}
|
|
}
|