2026-01-13 15:56:14 +01:00

179 lines
5.6 KiB
TypeScript

import type { Event, EventTemplate } from 'nostr-tools'
import type { Sponsoring } from '@/types/nostr'
import { buildTags } from '../nostrTagSystem'
import { PLATFORM_SERVICE } from '../platformConfig'
import { generateSponsoringHashId } from '../hashIdGenerator'
import { buildObjectId } from '../urlGenerator'
import { buildPaymentNoteTemplate, mapPaymentCategory, type PaymentCategoryTag, publishPaymentNoteToRelays } from './shared'
export async function publishSponsoringNote(params: {
authorPubkey: string
payerPubkey: string
amount: number
paymentHash: string
category?: 'science-fiction' | 'scientific-research'
seriesId?: string
articleId?: string
text?: string
transactionId?: string
payerPrivateKey: string
}): Promise<Event | null> {
const category = mapPaymentCategory(params.category)
const payload = await buildSponsoringNotePayload({ ...params, category })
return publishPaymentNoteToRelays({
payerPrivateKey: params.payerPrivateKey,
objectType: 'sponsoring',
hash: payload.hashId,
eventTemplate: payload.eventTemplate,
parsed: payload.parsedSponsoring,
version: 0,
hidden: false,
index: 0,
})
}
async function buildSponsoringNotePayload(params: {
authorPubkey: string
payerPubkey: string
amount: number
paymentHash: string
category: PaymentCategoryTag
seriesId?: string
articleId?: string
text?: string
transactionId?: string
}): Promise<{ hashId: string; eventTemplate: EventTemplate; parsedSponsoring: Sponsoring }> {
const sponsoringData = buildSponsoringHashInput(params)
const hashId = await generateSponsoringHashId(sponsoringData)
const id = buildObjectId(hashId, 0, 0)
const tags = buildSponsoringNoteTags({ ...params, hashId })
tags.push(['json', buildSponsoringPaymentJson(buildSponsoringPaymentJsonInput({ id, hashId, sponsoringData, text: params.text, transactionId: params.transactionId }))])
const parsedSponsoring = buildParsedSponsoring({ ...params, id, hashId })
return { hashId, eventTemplate: buildSponsoringEventTemplate({ tags, content: buildSponsoringNoteContent(params) }), parsedSponsoring }
}
function buildSponsoringHashInput(params: {
payerPubkey: string
authorPubkey: string
amount: number
paymentHash: string
seriesId?: string
articleId?: string
}): Parameters<typeof generateSponsoringHashId>[0] {
return {
payerPubkey: params.payerPubkey,
authorPubkey: params.authorPubkey,
amount: params.amount,
paymentHash: params.paymentHash,
...(params.seriesId ? { seriesId: params.seriesId } : {}),
...(params.articleId ? { articleId: params.articleId } : {}),
}
}
function buildParsedSponsoring(params: {
authorPubkey: string
payerPubkey: string
amount: number
paymentHash: string
seriesId?: string
articleId?: string
text?: string
id: string
hashId: string
}): Sponsoring {
return {
id: params.id,
hash: params.hashId,
version: 0,
index: 0,
payerPubkey: params.payerPubkey,
authorPubkey: params.authorPubkey,
amount: params.amount,
paymentHash: params.paymentHash,
createdAt: Math.floor(Date.now() / 1000),
...(params.seriesId ? { seriesId: params.seriesId } : {}),
...(params.articleId ? { articleId: params.articleId } : {}),
...(params.text ? { text: params.text } : {}),
kindType: 'sponsoring',
}
}
function buildSponsoringEventTemplate(params: { tags: string[][]; content: string }): EventTemplate {
return buildPaymentNoteTemplate(params.tags, params.content)
}
function buildSponsoringNoteContent(params: { authorPubkey: string; amount: number; text?: string }): string {
const prefix = `Sponsoring confirmed: ${params.amount} sats for author ${params.authorPubkey.substring(0, 16)}...`
return params.text ? `${prefix}\n\n${params.text}` : prefix
}
function buildSponsoringNoteTags(params: {
authorPubkey: string
payerPubkey: string
amount: number
paymentHash: string
category: PaymentCategoryTag
seriesId?: string
articleId?: string
text?: string
transactionId?: string
hashId: string
}): string[][] {
const tags = buildTags({
type: 'payment',
category: params.category,
id: params.hashId,
service: PLATFORM_SERVICE,
version: 0,
hidden: false,
payment: true,
paymentType: 'sponsoring',
amount: params.amount,
payerPubkey: params.payerPubkey,
recipientPubkey: params.authorPubkey,
paymentHash: params.paymentHash,
...(params.seriesId ? { seriesId: params.seriesId } : {}),
...(params.articleId ? { articleId: params.articleId } : {}),
...(params.text ? { text: params.text } : {}),
})
if (params.transactionId) {
tags.push(['transaction_id', params.transactionId])
}
return tags
}
function buildSponsoringPaymentJson(params: {
id: string
hashId: string
sponsoringData: Record<string, unknown>
text?: string
transactionId?: string
}): string {
return JSON.stringify({
type: 'sponsoring',
id: params.id,
hash: params.hashId,
version: 0,
index: 0,
...params.sponsoringData,
...(params.text ? { text: params.text } : {}),
...(params.transactionId ? { transactionId: params.transactionId } : {}),
})
}
function buildSponsoringPaymentJsonInput(params: {
id: string
hashId: string
sponsoringData: Record<string, unknown>
text: string | undefined
transactionId: string | undefined
}): { id: string; hashId: string; sponsoringData: Record<string, unknown>; text?: string; transactionId?: string } {
return {
id: params.id,
hashId: params.hashId,
sponsoringData: params.sponsoringData,
...(params.text ? { text: params.text } : {}),
...(params.transactionId ? { transactionId: params.transactionId } : {}),
}
}