83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
/**
|
|
* Synchronize user content (profile, series, publications) to IndexedDB cache
|
|
* Called after key import to ensure all user content is cached locally
|
|
*/
|
|
|
|
import { nostrService } from './nostr'
|
|
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
|
|
import type { SyncProgress } from './helpers/syncProgressHelper'
|
|
import { initializeSyncProgress, finalizeSync } from './helpers/syncProgressHelper'
|
|
import {
|
|
syncAuthorProfile,
|
|
syncSeries,
|
|
syncPublications,
|
|
syncPurchases,
|
|
syncSponsoring,
|
|
syncReviewTips,
|
|
syncPaymentNotes,
|
|
} from './sync/userContentSyncSteps'
|
|
|
|
export type { SyncProgress }
|
|
|
|
/**
|
|
* Synchronize all user content to IndexedDB cache
|
|
* Fetches profile, series, publications, purchases, sponsoring, review tips, and payment notes and caches them
|
|
* @param userPubkey - The user's public key
|
|
* @param onProgress - Optional callback to report progress (currentStep, totalSteps, completed)
|
|
*/
|
|
export async function syncUserContentToCache(
|
|
userPubkey: string,
|
|
onProgress?: (progress: SyncProgress) => void
|
|
): Promise<void> {
|
|
try {
|
|
const pool = nostrService.getPool()
|
|
if (!pool) {
|
|
const errorMsg = 'Pool not initialized, cannot sync user content'
|
|
console.warn(errorMsg)
|
|
throw new Error(errorMsg)
|
|
}
|
|
|
|
const poolWithSub = pool as unknown as SimplePoolWithSub
|
|
const { getCurrentTimestamp } = await import('./syncStorage')
|
|
const currentTimestamp = getCurrentTimestamp()
|
|
|
|
const { updateProgress } = await initializeSyncProgress(onProgress)
|
|
|
|
await executeSyncSteps(poolWithSub, userPubkey, updateProgress)
|
|
|
|
await finalizeSync(currentTimestamp)
|
|
} catch (syncError) {
|
|
console.error('Error syncing user content to cache:', syncError)
|
|
throw syncError
|
|
}
|
|
}
|
|
|
|
async function executeSyncSteps(
|
|
poolWithSub: SimplePoolWithSub,
|
|
userPubkey: string,
|
|
updateProgress: (step: number, completed?: boolean) => void
|
|
): Promise<void> {
|
|
let currentStep = 0
|
|
|
|
await syncAuthorProfile(poolWithSub, userPubkey)
|
|
updateProgress(++currentStep)
|
|
|
|
await syncSeries(poolWithSub, userPubkey)
|
|
updateProgress(++currentStep)
|
|
|
|
await syncPublications(poolWithSub, userPubkey)
|
|
updateProgress(++currentStep)
|
|
|
|
await syncPurchases(poolWithSub, userPubkey)
|
|
updateProgress(++currentStep)
|
|
|
|
await syncSponsoring(poolWithSub, userPubkey)
|
|
updateProgress(++currentStep)
|
|
|
|
await syncReviewTips(poolWithSub, userPubkey)
|
|
updateProgress(++currentStep)
|
|
|
|
await syncPaymentNotes(poolWithSub, userPubkey)
|
|
updateProgress(++currentStep, true)
|
|
}
|