63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import type { Event } from 'nostr-tools'
|
|
import type { Series } from '@/types/nostr'
|
|
import { extractTagsFromEvent } from '../nostrTagSystem'
|
|
import { buildObjectId } from '../urlGenerator'
|
|
import { generateHashId } from '../hashIdGenerator'
|
|
import { mapNostrCategoryToLegacy, readSeriesInput, resolveObjectIdParts, type ExtractedTags } from './shared'
|
|
|
|
export async function parseSeriesFromEvent(event: Event): Promise<Series | null> {
|
|
try {
|
|
const tags = extractTagsFromEvent(event)
|
|
const input = readSeriesInput({ tags, eventContent: event.content })
|
|
if (!input) {
|
|
return null
|
|
}
|
|
const category = mapNostrCategoryToLegacy(tags.category) ?? 'science-fiction'
|
|
const { hash, version, index } = await resolveObjectIdParts({
|
|
...(input.idTag ? { idTag: input.idTag } : {}),
|
|
defaultVersion: input.defaultVersion,
|
|
defaultIndex: 0,
|
|
generateHash: async (): Promise<string> =>
|
|
generateHashId({
|
|
type: 'series',
|
|
pubkey: event.pubkey,
|
|
title: input.title,
|
|
description: input.description,
|
|
category: input.categoryTag,
|
|
coverUrl: input.coverUrl,
|
|
}),
|
|
})
|
|
return buildSeriesFromParsed({ event, input, hash, version, index, category, tags })
|
|
} catch (e) {
|
|
console.error('Error parsing series:', e)
|
|
return null
|
|
}
|
|
}
|
|
|
|
function buildSeriesFromParsed(params: {
|
|
event: Event
|
|
input: { title: string; description: string; preview: string; coverUrl: string }
|
|
hash: string
|
|
version: number
|
|
index: number
|
|
category: Series['category']
|
|
tags: ExtractedTags
|
|
}): Series {
|
|
const id = buildObjectId(params.hash, params.index, params.version)
|
|
const series: Series = {
|
|
id,
|
|
hash: params.hash,
|
|
version: params.version,
|
|
index: params.index,
|
|
pubkey: params.event.pubkey,
|
|
title: params.input.title,
|
|
description: params.input.description,
|
|
preview: params.input.preview,
|
|
thumbnailUrl: params.input.coverUrl,
|
|
category: params.category,
|
|
...(params.input.coverUrl ? { coverUrl: params.input.coverUrl } : {}),
|
|
}
|
|
series.kindType = 'series'
|
|
return series
|
|
}
|