story-research-zapwall/lib/sync/userContentSyncSteps.ts
2026-01-07 03:10:40 +01:00

206 lines
5.9 KiB
TypeScript

/**
* Individual sync steps for user content
* Each function handles fetching and caching a specific type of content
*/
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
import type { Event } from 'nostr-tools'
import { fetchAuthorPresentationFromPool } from '../articlePublisherHelpersPresentation'
import { buildTagFilter } from '../nostrTagSystemFilter'
import { PLATFORM_SERVICE } from '../platformConfig'
import { extractTagsFromEvent } from '../nostrTagSystem'
import { createSyncSubscription } from '../helpers/syncSubscriptionHelper'
import { cachePublicationsByHash, cacheSeriesByHash } from '../helpers/syncContentCacheHelpers'
import { cachePurchases, cacheSponsoring, cacheReviewTips } from '../helpers/syncCacheHelpers'
import { buildPaymentNoteFilters, createPaymentNoteSubscriptions, processPaymentNoteEvents } from '../helpers/paymentNoteSyncHelper'
export async function syncAuthorProfile(
poolWithSub: SimplePoolWithSub,
userPubkey: string
): Promise<void> {
console.warn('[Sync] Step 1/7: Fetching author profile...')
await fetchAuthorPresentationFromPool(poolWithSub, userPubkey)
console.warn('[Sync] Step 1/7: Author profile fetch completed')
}
export async function syncSeries(
poolWithSub: SimplePoolWithSub,
userPubkey: string
): Promise<void> {
console.warn('[Sync] Step 2/7: Fetching series...')
const { getLastSyncDate } = await import('../syncStorage')
const lastSyncDate = await getLastSyncDate()
const filters = [
{
...buildTagFilter({
type: 'series',
authorPubkey: userPubkey,
service: PLATFORM_SERVICE,
}),
since: lastSyncDate,
limit: 1000,
},
]
const result = await createSyncSubscription({
pool: poolWithSub,
filters,
eventFilter: (event: Event): boolean => {
const tags = extractTagsFromEvent(event)
return tags.type === 'series' && !tags.hidden
},
timeout: 10000,
onEvent: (event: Event): void => {
console.warn('[Sync] Received series event:', event.id)
},
onComplete: (events: Event[]): void => {
console.warn(`[Sync] EOSE for series, received ${events.length} events`)
},
})
await cacheSeriesByHash(result.events)
}
export async function syncPublications(
poolWithSub: SimplePoolWithSub,
userPubkey: string
): Promise<void> {
console.warn('[Sync] Step 3/7: Fetching publications...')
const { getLastSyncDate } = await import('../syncStorage')
const lastSyncDate = await getLastSyncDate()
const filters = [
{
...buildTagFilter({
type: 'publication',
authorPubkey: userPubkey,
service: PLATFORM_SERVICE,
}),
since: lastSyncDate,
limit: 1000,
},
]
const result = await createSyncSubscription({
pool: poolWithSub,
filters,
eventFilter: (event: Event): boolean => {
const tags = extractTagsFromEvent(event)
return tags.type === 'publication' && !tags.hidden
},
timeout: 10000,
})
await cachePublicationsByHash(result.events)
}
export async function syncPurchases(
poolWithSub: SimplePoolWithSub,
userPubkey: string
): Promise<void> {
console.warn('[Sync] Step 4/7: Fetching purchases...')
const { getLastSyncDate } = await import('../syncStorage')
const lastSyncDate = await getLastSyncDate()
const filters = [
{
kinds: [9735],
authors: [userPubkey],
'#kind_type': ['purchase'],
since: lastSyncDate,
limit: 1000,
},
]
await createSyncSubscription({
pool: poolWithSub,
filters,
timeout: 10000,
onEvent: (event: Event): void => {
console.warn('[Sync] Received purchase event:', event.id)
},
onComplete: async (events: Event[]): Promise<void> => {
console.warn(`[Sync] EOSE for purchases, received ${events.length} events`)
await cachePurchases(events)
},
})
}
export async function syncSponsoring(
poolWithSub: SimplePoolWithSub,
userPubkey: string
): Promise<void> {
console.warn('[Sync] Step 5/7: Fetching sponsoring...')
const { getLastSyncDate } = await import('../syncStorage')
const lastSyncDate = await getLastSyncDate()
const filters = [
{
kinds: [9735],
'#p': [userPubkey],
'#kind_type': ['sponsoring'],
since: lastSyncDate,
limit: 1000,
},
]
await createSyncSubscription({
pool: poolWithSub,
filters,
timeout: 10000,
onEvent: (event: Event): void => {
console.warn('[Sync] Received sponsoring event:', event.id)
},
onComplete: async (events: Event[]): Promise<void> => {
console.warn(`[Sync] EOSE for sponsoring, received ${events.length} events`)
await cacheSponsoring(events)
},
})
}
export async function syncReviewTips(
poolWithSub: SimplePoolWithSub,
userPubkey: string
): Promise<void> {
console.warn('[Sync] Step 6/7: Fetching review tips...')
const { getLastSyncDate } = await import('../syncStorage')
const lastSyncDate = await getLastSyncDate()
const filters = [
{
kinds: [9735],
'#p': [userPubkey],
'#kind_type': ['review_tip'],
since: lastSyncDate,
limit: 1000,
},
]
await createSyncSubscription({
pool: poolWithSub,
filters,
timeout: 10000,
onEvent: (event: Event): void => {
console.warn('[Sync] Received review tip event:', event.id)
},
onComplete: async (events: Event[]): Promise<void> => {
console.warn(`[Sync] EOSE for review tips, received ${events.length} events`)
await cacheReviewTips(events)
},
})
}
export async function syncPaymentNotes(
poolWithSub: SimplePoolWithSub,
userPubkey: string
): Promise<void> {
console.warn('[Sync] Step 7/7: Fetching payment notes...')
const { getLastSyncDate } = await import('../syncStorage')
const lastSyncDate = await getLastSyncDate()
const filters = buildPaymentNoteFilters(userPubkey, lastSyncDate)
const subscriptions = await createPaymentNoteSubscriptions(poolWithSub, filters)
await processPaymentNoteEvents(subscriptions)
}