import type { ArticleDraft } from './articlePublisherTypes' import type { AlbyInvoice } from '@/types/alby' import type { Article } from '@/types/nostr' import { generatePublicationHashId } from './hashIdGenerator' import { buildObjectId } from './urlGenerator' export async function buildParsedArticleFromDraft(params: { draft: ArticleDraft invoice: AlbyInvoice authorPubkey: string }): Promise<{ article: Article; hash: string; version: number; index: number }> { const categoryTag = mapDraftCategoryToTag(params.draft.category) const hashId = await generatePublicationHashId(buildPublicationHashInput({ draft: params.draft, authorPubkey: params.authorPubkey, categoryTag })) const hash = hashId const version = 0 const index = 0 const id = buildObjectId(hash, index, version) const article: Article = { id, hash, version, index, pubkey: params.authorPubkey, title: params.draft.title, preview: params.draft.preview, content: '', description: params.draft.preview, contentDescription: params.draft.preview, createdAt: Math.floor(Date.now() / 1000), zapAmount: params.draft.zapAmount, paid: false, thumbnailUrl: params.draft.bannerUrl ?? '', invoice: params.invoice.invoice, ...(params.invoice.paymentHash ? { paymentHash: params.invoice.paymentHash } : {}), ...(params.draft.category ? { category: params.draft.category } : {}), ...(params.draft.seriesId ? { seriesId: params.draft.seriesId } : {}), ...(params.draft.bannerUrl ? { bannerUrl: params.draft.bannerUrl } : {}), ...(params.draft.pages && params.draft.pages.length > 0 ? { pages: params.draft.pages } : {}), kindType: 'article', } return { article, hash, version, index } } export function mapDraftCategoryToTag(category: ArticleDraft['category'] | undefined): 'sciencefiction' | 'research' { if (category === 'scientific-research') { return 'research' } return 'sciencefiction' } function buildPublicationHashInput(params: { draft: ArticleDraft authorPubkey: string categoryTag: 'sciencefiction' | 'research' }): Parameters[0] { return { pubkey: params.authorPubkey, title: params.draft.title, preview: params.draft.preview, category: params.categoryTag, ...(params.draft.seriesId ? { seriesId: params.draft.seriesId } : {}), ...(params.draft.bannerUrl ? { bannerUrl: params.draft.bannerUrl } : {}), zapAmount: params.draft.zapAmount, } }