From 9379475d1cb1c8337d5c859c27a402a9f66017d2 Mon Sep 17 00:00:00 2001 From: Gigi Date: Thu, 30 Oct 2025 20:41:24 +0100 Subject: [PATCH] feat: implement proper relay response tracking for flight mode - Use pool.publish() which returns individual relay responses - Track which relays actually accepted the event (response.ok === true) - Set isLocalOnly = true only when only local relays accepted the event - This provides accurate flight mode detection based on actual publishing success - Debug logging shows all relay responses for troubleshooting --- src/services/highlightCreationService.ts | 70 +++++++++++++----------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/src/services/highlightCreationService.ts b/src/services/highlightCreationService.ts index 04428993..8c10a623 100644 --- a/src/services/highlightCreationService.ts +++ b/src/services/highlightCreationService.ts @@ -120,50 +120,54 @@ export async function createHighlight( // Store the event in EventStore first for immediate UI display eventStore.add(signedEvent) - // Get all configured relays and determine which ones are connected + // Publish to all relays and get individual responses const allRelays = RELAYS - const connectedRelays = Array.from(relayPool.relays.values()) - .filter(relay => relay.connected) - .map(relay => relay.url) + let publishResponses: { ok: boolean; message?: string; from: string }[] = [] + let isLocalOnly = false - const connectedLocalRelays = connectedRelays.filter(url => isLocalRelay(url)) - const connectedRemoteRelays = connectedRelays.filter(url => !isLocalRelay(url)) + try { + // Publish to all relays and wait for responses + publishResponses = await relayPool.publish(allRelays, signedEvent) + + // Determine which relays successfully accepted the event + const successfulRelays = publishResponses + .filter(response => response.ok) + .map(response => response.from) + + const successfulLocalRelays = successfulRelays.filter(url => isLocalRelay(url)) + const successfulRemoteRelays = successfulRelays.filter(url => !isLocalRelay(url)) + + // isLocalOnly is true if only local relays accepted the event + isLocalOnly = successfulLocalRelays.length > 0 && successfulRemoteRelays.length === 0 - // Always try to publish to ALL relays, but track which ones are actually connected - const targetRelays = allRelays - const actuallyConnectedRelays = connectedRelays - - // isLocalOnly is true if we only have local relays connected (flight mode) - const isLocalOnly = connectedLocalRelays.length > 0 && connectedRemoteRelays.length === 0 - - console.log('🔍 Highlight creation debug:', { - allRelays, - connectedRelays, - connectedLocalRelays, - connectedRemoteRelays, - targetRelays, - actuallyConnectedRelays, - isLocalOnly - }) - - // Publish to all relays (fire-and-forget) - relayPool.publish(targetRelays, signedEvent) - .then(() => { - console.log('✅ Highlight published successfully to all relays') - }) - .catch((error) => { - console.warn('⚠️ Failed to publish highlight to some relays:', error) + console.log('🔍 Highlight creation debug:', { + allRelays, + publishResponses, + successfulRelays, + successfulLocalRelays, + successfulRemoteRelays, + isLocalOnly }) - // Mark for offline sync if we're in local-only mode - if (isLocalOnly) { + // Mark for offline sync if we're in local-only mode + if (isLocalOnly) { + const { markEventAsOfflineCreated } = await import('./offlineSyncService') + markEventAsOfflineCreated(signedEvent.id) + } + + } catch (error) { + console.warn('⚠️ Failed to publish highlight to relays:', error) + // If publishing fails completely, assume local-only mode + isLocalOnly = true const { markEventAsOfflineCreated } = await import('./offlineSyncService') markEventAsOfflineCreated(signedEvent.id) } // Convert to Highlight with relay tracking info and return IMMEDIATELY const highlight = eventToHighlight(signedEvent) - highlight.publishedRelays = actuallyConnectedRelays + highlight.publishedRelays = publishResponses + .filter(response => response.ok) + .map(response => response.from) highlight.isLocalOnly = isLocalOnly return highlight