65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { Event } from 'nostr-tools'
|
|
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
|
|
import { getPrimaryRelaySync } from './config'
|
|
import type { ContentDeliveryTracking } from './platformTrackingTypes'
|
|
import { getTrackingKind } from './platformTrackingEvents'
|
|
|
|
export function parseTrackingEvent(event: Event): ContentDeliveryTracking | null {
|
|
try {
|
|
const data = JSON.parse(event.content) as ContentDeliveryTracking
|
|
const zapReceiptTag = event.tags.find((tag) => tag[0] === 'zap_receipt')?.[1]
|
|
const authorAmountTag = event.tags.find((tag) => tag[0] === 'author_amount')?.[1]
|
|
const platformCommissionTag = event.tags.find((tag) => tag[0] === 'platform_commission')?.[1]
|
|
|
|
const delivery: ContentDeliveryTracking = {
|
|
...data,
|
|
}
|
|
|
|
if (authorAmountTag) {
|
|
delivery.authorAmount = parseInt(authorAmountTag, 10)
|
|
}
|
|
|
|
if (platformCommissionTag) {
|
|
delivery.platformCommission = parseInt(platformCommissionTag, 10)
|
|
}
|
|
|
|
if (zapReceiptTag) {
|
|
delivery.zapReceiptId = zapReceiptTag
|
|
}
|
|
|
|
return delivery
|
|
} catch (error) {
|
|
console.error('Error parsing tracking event', {
|
|
eventId: event.id,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
})
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function createArticleDeliveriesSubscription(pool: SimplePoolWithSub, articleId: string, platformPubkey: string) {
|
|
const filters = [
|
|
{
|
|
kinds: [getTrackingKind()],
|
|
'#p': [platformPubkey],
|
|
'#article': [articleId],
|
|
limit: 100,
|
|
},
|
|
]
|
|
const relayUrl = getPrimaryRelaySync()
|
|
return pool.sub([relayUrl], filters)
|
|
}
|
|
|
|
export function createRecipientDeliveriesSubscription(pool: SimplePoolWithSub, recipientPubkey: string, platformPubkey: string) {
|
|
const filters = [
|
|
{
|
|
kinds: [getTrackingKind()],
|
|
'#p': [platformPubkey],
|
|
'#recipient': [recipientPubkey],
|
|
limit: 100,
|
|
},
|
|
]
|
|
const relayUrl = getPrimaryRelaySync()
|
|
return pool.sub([relayUrl], filters)
|
|
}
|