story-research-zapwall/lib/articlePublisherPresentationHelpers.ts
2026-01-13 14:49:19 +01:00

92 lines
3.0 KiB
TypeScript

import type { AuthorPresentationDraft } from './articlePublisherTypes'
import { buildPresentationEvent } from './articlePublisherHelpers'
import { generateAuthorHashId } from './hashIdGenerator'
import { buildObjectId } from './urlGenerator'
import { extractAuthorNameFromTitle, parseAuthorPresentationDraft } from './authorPresentationParsing'
import type { EventTemplate } from 'nostr-tools'
export async function buildPresentationPublishContext(params: {
draft: AuthorPresentationDraft
authorPubkey: string
}): Promise<{
hash: string
version: number
index: number
parsedAuthor: import('@/types/nostr').AuthorPresentationArticle
eventTemplate: EventTemplate
}> {
const authorName = extractAuthorNameFromTitle(params.draft.title)
const { presentation, contentDescription } = parseAuthorPresentationDraft(params.draft)
const category = 'sciencefiction'
const version = 0
const index = 0
const hash = await generateAuthorHashId({
pubkey: params.authorPubkey,
authorName,
presentation,
contentDescription,
mainnetAddress: params.draft.mainnetAddress ?? undefined,
pictureUrl: params.draft.pictureUrl ?? undefined,
category,
})
const id = buildObjectId(hash, index, version)
const parsedAuthor = buildParsedAuthorPresentation({
draft: params.draft,
authorPubkey: params.authorPubkey,
id,
hash,
version,
index,
presentation,
contentDescription,
})
const eventTemplate = await buildPresentationEvent({ draft: params.draft, authorPubkey: params.authorPubkey, authorName, category, version, index })
return { hash, version, index, parsedAuthor, eventTemplate }
}
function buildParsedAuthorPresentation(params: {
draft: AuthorPresentationDraft
authorPubkey: string
id: string
hash: string
version: number
index: number
presentation: string
contentDescription: string
}): import('@/types/nostr').AuthorPresentationArticle {
return {
id: params.id,
hash: params.hash,
version: params.version,
index: params.index,
pubkey: params.authorPubkey,
title: params.draft.title,
preview: params.draft.preview,
content: params.draft.content,
description: params.presentation,
contentDescription: params.contentDescription,
thumbnailUrl: params.draft.pictureUrl ?? '',
createdAt: Math.floor(Date.now() / 1000),
zapAmount: 0,
paid: true,
category: 'author-presentation',
isPresentation: true,
mainnetAddress: params.draft.mainnetAddress ?? '',
totalSponsoring: 0,
originalCategory: 'science-fiction',
...(params.draft.pictureUrl ? { bannerUrl: params.draft.pictureUrl } : {}),
}
}
export async function getActiveRelaysOrPrimary(): Promise<string[]> {
const { relaySessionManager } = await import('./relaySessionManager')
const activeRelays = await relaySessionManager.getActiveRelays()
if (activeRelays.length > 0) {
return activeRelays
}
const { getPrimaryRelay } = await import('./config')
return [await getPrimaryRelay()]
}