|
| 1 | +// ABOUTME: Enriches Funnelcake feed videos with ProofMode data via WebSocket |
| 2 | +// ABOUTME: Queries relay for full events to extract verification tags missing from REST API |
| 3 | + |
| 4 | +import { useMemo } from 'react'; |
| 5 | +import { useNostr } from '@nostrify/react'; |
| 6 | +import { useQuery } from '@tanstack/react-query'; |
| 7 | +import type { ParsedVideoData } from '@/types/video'; |
| 8 | +import type { ProofModeData } from '@/types/video'; |
| 9 | +import { getProofModeData } from '@/lib/videoParser'; |
| 10 | +import { debugLog } from '@/lib/debug'; |
| 11 | + |
| 12 | +const BATCH_SIZE = 20; |
| 13 | +const STALE_TIME = 5 * 60 * 1000; // 5 minutes |
| 14 | + |
| 15 | +/** |
| 16 | + * Hook that enriches videos with ProofMode data from WebSocket queries |
| 17 | + * |
| 18 | + * Feed videos from Funnelcake REST API don't include event tags, |
| 19 | + * so ProofMode data is always undefined. This hook fetches the full |
| 20 | + * events via WebSocket to extract verification tags. |
| 21 | + * |
| 22 | + * Returns the same videos array with proofMode populated where available. |
| 23 | + */ |
| 24 | +export function useProofModeEnrichment(videos: ParsedVideoData[]): ParsedVideoData[] { |
| 25 | + const { nostr } = useNostr(); |
| 26 | + |
| 27 | + // Find videos that need enrichment (no proofMode and have an ID) |
| 28 | + const videosNeedingEnrichment = useMemo( |
| 29 | + () => videos.filter(v => !v.proofMode && v.id).slice(0, BATCH_SIZE), |
| 30 | + [videos] |
| 31 | + ); |
| 32 | + |
| 33 | + const videoIds = useMemo( |
| 34 | + () => videosNeedingEnrichment.map(v => v.id), |
| 35 | + [videosNeedingEnrichment] |
| 36 | + ); |
| 37 | + |
| 38 | + // Query relay for full events by ID |
| 39 | + const { data: proofModeMap } = useQuery<Map<string, ProofModeData>>({ |
| 40 | + queryKey: ['proofmode-enrichment', ...videoIds], |
| 41 | + queryFn: async ({ signal }) => { |
| 42 | + if (videoIds.length === 0) return new Map(); |
| 43 | + |
| 44 | + debugLog(`[ProofModeEnrichment] Fetching ${videoIds.length} events for ProofMode data`); |
| 45 | + |
| 46 | + const events = await nostr.query( |
| 47 | + [{ ids: videoIds, limit: BATCH_SIZE }], |
| 48 | + { signal: AbortSignal.any([signal, AbortSignal.timeout(10000)]) }, |
| 49 | + ); |
| 50 | + |
| 51 | + debugLog(`[ProofModeEnrichment] Got ${events.length} events`); |
| 52 | + |
| 53 | + const map = new Map<string, ProofModeData>(); |
| 54 | + for (const event of events) { |
| 55 | + const proofMode = getProofModeData(event); |
| 56 | + if (proofMode) { |
| 57 | + map.set(event.id, proofMode); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + debugLog(`[ProofModeEnrichment] Found ${map.size} videos with ProofMode data`); |
| 62 | + return map; |
| 63 | + }, |
| 64 | + enabled: videoIds.length > 0, |
| 65 | + staleTime: STALE_TIME, |
| 66 | + gcTime: STALE_TIME * 2, |
| 67 | + }); |
| 68 | + |
| 69 | + // Merge ProofMode data into videos |
| 70 | + return useMemo(() => { |
| 71 | + if (!proofModeMap || proofModeMap.size === 0) return videos; |
| 72 | + |
| 73 | + return videos.map(video => { |
| 74 | + if (video.proofMode) return video; // Already has data |
| 75 | + const proofMode = proofModeMap.get(video.id); |
| 76 | + if (!proofMode) return video; |
| 77 | + return { ...video, proofMode }; |
| 78 | + }); |
| 79 | + }, [videos, proofModeMap]); |
| 80 | +} |
0 commit comments