story-research-zapwall/lib/nostrZapVerification.ts
Nicolas Cantu f5d9033183 Refactor to use cache-first architecture with background sync
**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
2026-01-06 15:04:14 +01:00

92 lines
2.3 KiB
TypeScript

import type { Event } from 'nostr-tools'
import { SimplePool } from 'nostr-tools'
import { createSubscription } from '@/types/nostr-tools-extended'
import { getPrimaryRelaySync } from './config'
function createZapFilters(targetPubkey: string, targetEventId: string, userPubkey: string): Array<{
kinds: number[]
'#p': string[]
'#e': string[]
authors: string[]
}> {
return [
{
kinds: [9735], // Zap receipt
'#p': [targetPubkey],
'#e': [targetEventId],
authors: [userPubkey], // Filter by the payer's pubkey
},
]
}
async function isValidZapReceipt(
event: Event,
targetEventId: string,
targetPubkey: string,
userPubkey: string,
amount: number
): Promise<boolean> {
// Import verification service dynamically to avoid circular dependencies
const { zapVerificationService } = await import('./zapVerification')
return zapVerificationService.verifyZapReceiptForArticle(event, targetEventId, targetPubkey, userPubkey, amount)
}
/**
* Check if user has paid for an article by looking for zap receipts
*/
function handleZapReceiptEvent(
event: Event,
targetEventId: string,
targetPubkey: string,
userPubkey: string,
amount: number,
finalize: (value: boolean) => void,
resolved: { current: boolean }
): void {
if (resolved.current) {
return
}
void isValidZapReceipt(event, targetEventId, targetPubkey, userPubkey, amount).then((isValid) => {
if (isValid) {
finalize(true)
}
})
}
export function checkZapReceipt(
pool: SimplePool,
targetPubkey: string,
targetEventId: string,
amount: number,
userPubkey: string
): Promise<boolean> {
if (!pool) {
return Promise.resolve(false)
}
return new Promise((resolve) => {
let resolved = false
const relayUrl = getPrimaryRelaySync()
const sub = createSubscription(pool, [relayUrl], createZapFilters(targetPubkey, targetEventId, userPubkey))
const finalize = (value: boolean): void => {
if (resolved) {
return
}
resolved = true
sub.unsub()
resolve(value)
}
const resolvedRef = { current: resolved }
sub.on('event', (event: Event): void => {
handleZapReceiptEvent(event, targetEventId, targetPubkey, userPubkey, amount, finalize, resolvedRef)
})
const end = () => finalize(false)
sub.on('eose', end)
setTimeout(end, 3000)
})
}