mirror of
https://github.com/dergigi/boris.git
synced 2025-12-19 15:44:20 +01:00
fix: improve offline sync with better tracking and logging
- Track events explicitly when created in offline mode - Mark highlights as offline-created when isLocalOnly is true - Add extensive debug logging throughout sync process - Increase query timeout from 5s to 10s for better reliability - Add 2-second delay before syncing to allow relays to connect - Log relay state transitions and event counts - Log each event received during sync query - Should help diagnose and fix offline sync issues
This commit is contained in:
@@ -48,7 +48,17 @@ export function useOfflineSync({
|
|||||||
|
|
||||||
if (wasLocalOnly && isNowOnline) {
|
if (wasLocalOnly && isNowOnline) {
|
||||||
console.log('✈️ Detected transition: Flight Mode → Online')
|
console.log('✈️ Detected transition: Flight Mode → Online')
|
||||||
|
console.log('📊 Relay state:', {
|
||||||
|
connectedRelays: connectedRelays.length,
|
||||||
|
remoteRelays: connectedRelays.filter(r => !isLocalRelay(r.url)).length,
|
||||||
|
localRelays: connectedRelays.filter(r => isLocalRelay(r.url)).length
|
||||||
|
})
|
||||||
|
|
||||||
|
// Wait a moment for relays to fully establish connections
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('🚀 Starting sync after delay...')
|
||||||
syncLocalEventsToRemote(relayPool, account)
|
syncLocalEventsToRemote(relayPool, account)
|
||||||
|
}, 2000)
|
||||||
}
|
}
|
||||||
|
|
||||||
previousStateRef.current.hasRemoteRelays = hasRemoteRelays
|
previousStateRef.current.hasRemoteRelays = hasRemoteRelays
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { RELAYS } from '../config/relays'
|
|||||||
import { Highlight } from '../types/highlights'
|
import { Highlight } from '../types/highlights'
|
||||||
import { UserSettings } from './settingsService'
|
import { UserSettings } from './settingsService'
|
||||||
import { areAllRelaysLocal } from '../utils/helpers'
|
import { areAllRelaysLocal } from '../utils/helpers'
|
||||||
|
import { markEventAsOfflineCreated } from './offlineSyncService'
|
||||||
|
|
||||||
// Boris pubkey for zap splits
|
// Boris pubkey for zap splits
|
||||||
const BORIS_PUBKEY = '6e468422dfb74a5738702a8823b9b28168fc6cfb119d613e49ca0ec5a0bbd0c3'
|
const BORIS_PUBKEY = '6e468422dfb74a5738702a8823b9b28168fc6cfb119d613e49ca0ec5a0bbd0c3'
|
||||||
@@ -142,6 +143,11 @@ export async function createHighlight(
|
|||||||
eventId: signedEvent.id
|
eventId: signedEvent.id
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// If we're in local-only mode, mark this event for later sync
|
||||||
|
if (isLocalOnly) {
|
||||||
|
markEventAsOfflineCreated(signedEvent.id)
|
||||||
|
}
|
||||||
|
|
||||||
// Convert to Highlight with relay tracking info
|
// Convert to Highlight with relay tracking info
|
||||||
const highlight = eventToHighlight(signedEvent)
|
const highlight = eventToHighlight(signedEvent)
|
||||||
highlight.publishedRelays = actualPublishedRelays
|
highlight.publishedRelays = actualPublishedRelays
|
||||||
|
|||||||
@@ -6,6 +6,17 @@ import { isLocalRelay } from '../utils/helpers'
|
|||||||
|
|
||||||
let isSyncing = false
|
let isSyncing = false
|
||||||
|
|
||||||
|
// Track events created during offline period
|
||||||
|
const offlineCreatedEvents = new Set<string>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks an event as created during offline period
|
||||||
|
*/
|
||||||
|
export function markEventAsOfflineCreated(eventId: string): void {
|
||||||
|
offlineCreatedEvents.add(eventId)
|
||||||
|
console.log(`📝 Marked event ${eventId.slice(0, 8)} as offline-created. Total: ${offlineCreatedEvents.size}`)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Syncs local-only events to remote relays when coming back online
|
* Syncs local-only events to remote relays when coming back online
|
||||||
*/
|
*/
|
||||||
@@ -19,14 +30,24 @@ export async function syncLocalEventsToRemote(
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log('🔄 Coming back online - syncing local events to remote relays...')
|
console.log('🔄 Coming back online - syncing local events to remote relays...')
|
||||||
|
console.log(`📦 Offline events tracked: ${offlineCreatedEvents.size}`)
|
||||||
isSyncing = true
|
isSyncing = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const localRelays = RELAYS.filter(isLocalRelay)
|
const localRelays = RELAYS.filter(isLocalRelay)
|
||||||
const remoteRelays = RELAYS.filter(url => !isLocalRelay(url))
|
const remoteRelays = RELAYS.filter(url => !isLocalRelay(url))
|
||||||
|
|
||||||
if (localRelays.length === 0 || remoteRelays.length === 0) {
|
console.log(`📡 Local relays: ${localRelays.length}, Remote relays: ${remoteRelays.length}`)
|
||||||
console.log('⚠️ No local or remote relays available for sync')
|
|
||||||
|
if (localRelays.length === 0) {
|
||||||
|
console.log('⚠️ No local relays available for sync')
|
||||||
|
isSyncing = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remoteRelays.length === 0) {
|
||||||
|
console.log('⚠️ No remote relays available for sync')
|
||||||
|
isSyncing = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +55,8 @@ export async function syncLocalEventsToRemote(
|
|||||||
const since = Math.floor(Date.now() / 1000) - (24 * 60 * 60)
|
const since = Math.floor(Date.now() / 1000) - (24 * 60 * 60)
|
||||||
const eventsToSync: NostrEvent[] = []
|
const eventsToSync: NostrEvent[] = []
|
||||||
|
|
||||||
|
console.log(`🔍 Querying local relays for events since ${new Date(since * 1000).toISOString()}...`)
|
||||||
|
|
||||||
// Query for user's events from local relays
|
// Query for user's events from local relays
|
||||||
const filters = [
|
const filters = [
|
||||||
{ kinds: [9802], authors: [account.pubkey], since }, // Highlights
|
{ kinds: [9802], authors: [account.pubkey], since }, // Highlights
|
||||||
@@ -41,31 +64,38 @@ export async function syncLocalEventsToRemote(
|
|||||||
]
|
]
|
||||||
|
|
||||||
for (const filter of filters) {
|
for (const filter of filters) {
|
||||||
|
console.log(`🔎 Querying with filter:`, filter)
|
||||||
const events = await new Promise<NostrEvent[]>((resolve) => {
|
const events = await new Promise<NostrEvent[]>((resolve) => {
|
||||||
const collected: NostrEvent[] = []
|
const collected: NostrEvent[] = []
|
||||||
const sub = relayPool.req(localRelays, filter, {
|
const sub = relayPool.req(localRelays, filter, {
|
||||||
onevent: (event: NostrEvent) => {
|
onevent: (event: NostrEvent) => {
|
||||||
|
console.log(`📥 Received event ${event.id.slice(0, 8)} (kind ${event.kind}) from local relay`)
|
||||||
collected.push(event)
|
collected.push(event)
|
||||||
},
|
},
|
||||||
oneose: () => {
|
oneose: () => {
|
||||||
|
console.log(`✅ EOSE received, collected ${collected.length} events`)
|
||||||
sub.close()
|
sub.close()
|
||||||
resolve(collected)
|
resolve(collected)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Timeout after 5 seconds
|
// Timeout after 10 seconds (increased from 5)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
console.log(`⏱️ Query timeout, collected ${collected.length} events`)
|
||||||
sub.close()
|
sub.close()
|
||||||
resolve(collected)
|
resolve(collected)
|
||||||
}, 5000)
|
}, 10000)
|
||||||
})
|
})
|
||||||
|
|
||||||
eventsToSync.push(...events)
|
eventsToSync.push(...events)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`📊 Total events collected: ${eventsToSync.length}`)
|
||||||
|
|
||||||
if (eventsToSync.length === 0) {
|
if (eventsToSync.length === 0) {
|
||||||
console.log('✅ No local events to sync')
|
console.log('✅ No local events to sync')
|
||||||
isSyncing = false
|
isSyncing = false
|
||||||
|
offlineCreatedEvents.clear()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +118,9 @@ export async function syncLocalEventsToRemote(
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(`✅ Synced ${successCount}/${uniqueEvents.length} events to remote relays`)
|
console.log(`✅ Synced ${successCount}/${uniqueEvents.length} events to remote relays`)
|
||||||
|
|
||||||
|
// Clear offline events tracking after successful sync
|
||||||
|
offlineCreatedEvents.clear()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error during offline sync:', error)
|
console.error('❌ Error during offline sync:', error)
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user