mirror of
https://github.com/dergigi/boris.git
synced 2026-01-21 15:54:30 +01:00
- Check actual relay connectivity before rebroadcasting
- Skip rebroadcast to all relays if no remote relays connected
- Still allows rebroadcast to local relays in flight mode
- Prevents unnecessary publish attempts to unreachable relays
- Logs: '✈️ Flight mode: skipping rebroadcast to remote relays'
This prevents the app from trying to rebroadcast fetched events to
remote relays when only local relays are connected (flight mode).
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { RelayPool } from 'applesauce-relay'
|
|
import { NostrEvent } from 'nostr-tools'
|
|
import { UserSettings } from './settingsService'
|
|
import { RELAYS } from '../config/relays'
|
|
import { isLocalRelay } from '../utils/helpers'
|
|
|
|
/**
|
|
* Rebroadcasts events to relays based on user settings
|
|
* @param events Events to rebroadcast
|
|
* @param relayPool The relay pool to use for publishing
|
|
* @param settings User settings to determine which relays to broadcast to
|
|
*/
|
|
export async function rebroadcastEvents(
|
|
events: NostrEvent[],
|
|
relayPool: RelayPool,
|
|
settings?: UserSettings
|
|
): Promise<void> {
|
|
if (!events || events.length === 0) {
|
|
return
|
|
}
|
|
|
|
// Check if any rebroadcast is enabled
|
|
const useLocalCache = settings?.useLocalRelayAsCache ?? true
|
|
const broadcastToAll = settings?.rebroadcastToAllRelays ?? false
|
|
|
|
if (!useLocalCache && !broadcastToAll) {
|
|
return // No rebroadcast enabled
|
|
}
|
|
|
|
// Check current relay connectivity - don't rebroadcast in flight mode
|
|
const connectedRelays = Array.from(relayPool.relays.values())
|
|
const connectedRemoteRelays = connectedRelays.filter(relay => relay.connected && !isLocalRelay(relay.url))
|
|
const hasRemoteConnection = connectedRemoteRelays.length > 0
|
|
|
|
// If we're in flight mode (only local relays connected) and user wants to broadcast to all relays, skip
|
|
if (broadcastToAll && !hasRemoteConnection) {
|
|
console.log('✈️ Flight mode: skipping rebroadcast to remote relays')
|
|
return
|
|
}
|
|
|
|
// Determine target relays based on settings
|
|
let targetRelays: string[] = []
|
|
|
|
if (broadcastToAll) {
|
|
// Broadcast to all relays (only if we have remote connection)
|
|
targetRelays = RELAYS
|
|
} else if (useLocalCache) {
|
|
// Only broadcast to local relays
|
|
targetRelays = RELAYS.filter(isLocalRelay)
|
|
}
|
|
|
|
if (targetRelays.length === 0) {
|
|
console.log('📡 No target relays for rebroadcast')
|
|
return
|
|
}
|
|
|
|
// Rebroadcast each event
|
|
const rebroadcastPromises = events.map(async (event) => {
|
|
try {
|
|
await relayPool.publish(targetRelays, event)
|
|
console.log('📡 Rebroadcast event', event.id?.slice(0, 8), 'to', targetRelays.length, 'relay(s)')
|
|
} catch (error) {
|
|
console.warn('⚠️ Failed to rebroadcast event', event.id?.slice(0, 8), error)
|
|
}
|
|
})
|
|
|
|
// Execute all rebroadcasts (don't block on completion)
|
|
Promise.all(rebroadcastPromises).catch((err) => {
|
|
console.warn('⚠️ Some rebroadcasts failed:', err)
|
|
})
|
|
|
|
console.log(`📡 Rebroadcasting ${events.length} event(s) to ${targetRelays.length} relay(s)`, {
|
|
broadcastToAll,
|
|
useLocalCache,
|
|
targetRelays
|
|
})
|
|
}
|
|
|