**Motivations:** - total_sponsoring should be calculated from cache, not stored in tags - Author searches should use cache first, not query Nostr directly - All elements should be read/written from/to database - Background sync should scan all notes with service='zapwall.fr' tag and update cache - Sync should run at startup and resume on each page navigation **Root causes:** - total_sponsoring was stored in tags which required updates on every sponsoring payment - Author queries were querying Nostr directly instead of using cache - No background sync service to keep cache up to date with new notes **Correctifs:** - Removed totalSponsoring from AuthorTags interface and buildAuthorTags - Modified getAuthorSponsoring to calculate from cache (sponsoring queries) instead of tags - Modified parsePresentationEvent to set totalSponsoring to 0 (calculated on demand from cache) - Modified fetchAuthorByHashId and fetchAuthorPresentationFromPool to use cache first and calculate totalSponsoring from cache - Created platformSyncService that scans all notes with service='zapwall.fr' tag and caches them - Modified _app.tsx to start continuous sync on mount and resume on page navigation - All author presentations now calculate totalSponsoring from cache when loaded **Evolutions:** - Cache-first architecture: all queries check cache before querying Nostr - Background sync service keeps cache up to date automatically - totalSponsoring is always calculated from actual sponsoring data in cache - Better performance: cache queries are faster than Nostr queries - Non-blocking sync: background sync doesn't block UI **Pages affectées:** - lib/nostrTagSystemTypes.ts - lib/nostrTagSystemBuild.ts - lib/articlePublisherHelpersPresentation.ts - lib/sponsoring.ts - lib/authorQueries.ts - lib/platformSync.ts (new) - pages/_app.tsx
217 lines
6.4 KiB
TypeScript
217 lines
6.4 KiB
TypeScript
/**
|
|
* Background sync service that scans all notes with service='zapwall.fr' tag
|
|
* and caches them in IndexedDB
|
|
* Runs in background (non-blocking) and updates cache when new notes are published
|
|
*/
|
|
|
|
import type { Event } from 'nostr-tools'
|
|
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
|
|
import { nostrService } from './nostr'
|
|
import { getPrimaryRelaySync } from './config'
|
|
import { PLATFORM_SERVICE, MIN_EVENT_DATE } from './platformConfig'
|
|
import { buildTagFilter, extractTagsFromEvent } from './nostrTagSystem'
|
|
import { objectCache } from './objectCache'
|
|
import { parsePresentationEvent } from './articlePublisherHelpersPresentation'
|
|
import { parseArticleFromEvent, parseSeriesFromEvent, parseReviewFromEvent, parsePurchaseFromEvent, parseReviewTipFromEvent, parseSponsoringFromEvent } from './nostrEventParsing'
|
|
|
|
class PlatformSyncService {
|
|
private syncInProgress = false
|
|
private syncSubscription: { unsub: () => void } | null = null
|
|
private lastSyncTime: number = 0
|
|
private readonly SYNC_INTERVAL_MS = 60000 // Sync every minute
|
|
private readonly SYNC_TIMEOUT_MS = 30000 // 30 seconds timeout per sync
|
|
|
|
/**
|
|
* Start background sync
|
|
* Scans all notes with service='zapwall.fr' and caches them
|
|
*/
|
|
async startSync(): Promise<void> {
|
|
if (this.syncInProgress) {
|
|
return
|
|
}
|
|
|
|
const pool = nostrService.getPool()
|
|
if (!pool) {
|
|
console.warn('Pool not initialized, cannot start platform sync')
|
|
return
|
|
}
|
|
|
|
this.syncInProgress = true
|
|
|
|
try {
|
|
await this.performSync(pool as unknown as SimplePoolWithSub)
|
|
} catch (error) {
|
|
console.error('Error in platform sync:', error)
|
|
} finally {
|
|
this.syncInProgress = false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Perform a sync operation
|
|
* Scans all notes with service='zapwall.fr' tag
|
|
*/
|
|
private async performSync(pool: SimplePoolWithSub): Promise<void> {
|
|
const filters = [
|
|
{
|
|
...buildTagFilter({
|
|
service: PLATFORM_SERVICE,
|
|
}),
|
|
since: MIN_EVENT_DATE,
|
|
limit: 1000, // Get up to 1000 events per sync
|
|
},
|
|
]
|
|
|
|
return new Promise((resolve) => {
|
|
const relayUrl = getPrimaryRelaySync()
|
|
const { createSubscription } = require('@/types/nostr-tools-extended')
|
|
const sub = createSubscription(pool, [relayUrl], filters)
|
|
|
|
const events: Event[] = []
|
|
let resolved = false
|
|
|
|
const finalize = async (): Promise<void> => {
|
|
if (resolved) {
|
|
return
|
|
}
|
|
resolved = true
|
|
sub.unsub()
|
|
this.syncSubscription = null
|
|
|
|
// Process all events and cache them
|
|
await this.processAndCacheEvents(events)
|
|
|
|
this.lastSyncTime = Date.now()
|
|
resolve()
|
|
}
|
|
|
|
sub.on('event', (event: Event): void => {
|
|
// Only process events with service='zapwall.fr'
|
|
const tags = extractTagsFromEvent(event)
|
|
if (tags.service === PLATFORM_SERVICE) {
|
|
events.push(event)
|
|
}
|
|
})
|
|
|
|
sub.on('eose', async (): Promise<void> => {
|
|
await finalize()
|
|
})
|
|
|
|
// Timeout after SYNC_TIMEOUT_MS
|
|
setTimeout(async (): Promise<void> => {
|
|
await finalize()
|
|
}, this.SYNC_TIMEOUT_MS).unref?.()
|
|
|
|
this.syncSubscription = sub
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Process events and cache them by type
|
|
*/
|
|
private async processAndCacheEvents(events: Event[]): Promise<void> {
|
|
for (const event of events) {
|
|
try {
|
|
await this.processEvent(event)
|
|
} catch (error) {
|
|
console.error('Error processing event:', error, event.id)
|
|
// Continue processing other events even if one fails
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Process a single event and cache it
|
|
*/
|
|
private async processEvent(event: Event): Promise<void> {
|
|
const tags = extractTagsFromEvent(event)
|
|
|
|
// Skip hidden events
|
|
if (tags.hidden) {
|
|
return
|
|
}
|
|
|
|
// Try to parse and cache by type
|
|
if (tags.type === 'author') {
|
|
const parsed = await parsePresentationEvent(event)
|
|
if (parsed && parsed.hash) {
|
|
await objectCache.set('author', parsed.hash, event, parsed, tags.version ?? 0, tags.hidden, parsed.index)
|
|
}
|
|
} else if (tags.type === 'series') {
|
|
const parsed = await parseSeriesFromEvent(event)
|
|
if (parsed && parsed.hash) {
|
|
await objectCache.set('series', parsed.hash, event, parsed, tags.version ?? 0, tags.hidden, parsed.index)
|
|
}
|
|
} else if (tags.type === 'publication') {
|
|
const parsed = await parseArticleFromEvent(event)
|
|
if (parsed && parsed.hash) {
|
|
await objectCache.set('publication', parsed.hash, event, parsed, tags.version ?? 0, tags.hidden, parsed.index)
|
|
}
|
|
} else if (tags.type === 'quote') {
|
|
const parsed = await parseReviewFromEvent(event)
|
|
if (parsed && parsed.hash) {
|
|
await objectCache.set('review', parsed.hash, event, parsed, tags.version ?? 0, tags.hidden, parsed.index)
|
|
}
|
|
} else if (event.kind === 9735) {
|
|
// Zap receipts (kind 9735) can be sponsoring, purchase, or review_tip
|
|
const sponsoring = await parseSponsoringFromEvent(event)
|
|
if (sponsoring && sponsoring.hash) {
|
|
await objectCache.set('sponsoring', sponsoring.hash, event, sponsoring, 0, false, sponsoring.index)
|
|
} else {
|
|
const purchase = await parsePurchaseFromEvent(event)
|
|
if (purchase && purchase.hash) {
|
|
await objectCache.set('purchase', purchase.hash, event, purchase, 0, false, purchase.index)
|
|
} else {
|
|
const reviewTip = await parseReviewTipFromEvent(event)
|
|
if (reviewTip && reviewTip.hash) {
|
|
await objectCache.set('review_tip', reviewTip.hash, event, reviewTip, 0, false, reviewTip.index)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Start continuous sync (runs periodically)
|
|
*/
|
|
startContinuousSync(): void {
|
|
// Start initial sync
|
|
void this.startSync()
|
|
|
|
// Schedule periodic syncs
|
|
setInterval(() => {
|
|
if (!this.syncInProgress) {
|
|
void this.startSync()
|
|
}
|
|
}, this.SYNC_INTERVAL_MS)
|
|
}
|
|
|
|
/**
|
|
* Stop sync
|
|
*/
|
|
stopSync(): void {
|
|
if (this.syncSubscription) {
|
|
this.syncSubscription.unsub()
|
|
this.syncSubscription = null
|
|
}
|
|
this.syncInProgress = false
|
|
}
|
|
|
|
/**
|
|
* Check if sync is in progress
|
|
*/
|
|
isSyncing(): boolean {
|
|
return this.syncInProgress
|
|
}
|
|
|
|
/**
|
|
* Get last sync time
|
|
*/
|
|
getLastSyncTime(): number {
|
|
return this.lastSyncTime
|
|
}
|
|
}
|
|
|
|
export const platformSyncService = new PlatformSyncService()
|
|
|