|
|
|
@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
|
|
|
|
import '../../data/nostr/nostr_service.dart';
|
|
|
|
import '../../data/nostr/nostr_service.dart';
|
|
|
|
import '../../data/nostr/models/nostr_relay.dart';
|
|
|
|
import '../../data/nostr/models/nostr_relay.dart';
|
|
|
|
import '../../data/sync/sync_engine.dart';
|
|
|
|
import '../../data/sync/sync_engine.dart';
|
|
|
|
|
|
|
|
import '../../core/logger.dart';
|
|
|
|
|
|
|
|
|
|
|
|
/// Controller for managing Nostr relay UI state and operations.
|
|
|
|
/// Controller for managing Nostr relay UI state and operations.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
@ -49,7 +50,13 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
|
|
|
|
|
|
|
|
/// Loads relays from the Nostr service.
|
|
|
|
/// Loads relays from the Nostr service.
|
|
|
|
void _loadRelays() {
|
|
|
|
void _loadRelays() {
|
|
|
|
_relays = nostrService.getRelays();
|
|
|
|
// Create a new list with new relay objects to ensure Flutter detects the change
|
|
|
|
|
|
|
|
final serviceRelays = nostrService.getRelays();
|
|
|
|
|
|
|
|
_relays = serviceRelays.map((relay) => NostrRelay(
|
|
|
|
|
|
|
|
url: relay.url,
|
|
|
|
|
|
|
|
isConnected: relay.isConnected,
|
|
|
|
|
|
|
|
isEnabled: relay.isEnabled,
|
|
|
|
|
|
|
|
)).toList();
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -57,8 +64,10 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// [relayUrl] - The WebSocket URL of the relay.
|
|
|
|
/// [relayUrl] - The WebSocket URL of the relay.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Returns true if the relay was added successfully, false if it already exists.
|
|
|
|
/// Tests the connection and enables the relay if successful.
|
|
|
|
bool addRelay(String relayUrl) {
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Returns true if the relay was added successfully, false if it already exists or connection failed.
|
|
|
|
|
|
|
|
Future<bool> addRelay(String relayUrl) async {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
_error = null;
|
|
|
|
_error = null;
|
|
|
|
|
|
|
|
|
|
|
|
@ -69,9 +78,105 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add the relay (it will be disabled by default)
|
|
|
|
nostrService.addRelay(relayUrl);
|
|
|
|
nostrService.addRelay(relayUrl);
|
|
|
|
_loadRelays();
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test the connection
|
|
|
|
|
|
|
|
Logger.info('Testing connection to relay: $relayUrl');
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
final stream = await nostrService
|
|
|
|
|
|
|
|
.connectRelay(relayUrl)
|
|
|
|
|
|
|
|
.timeout(
|
|
|
|
|
|
|
|
const Duration(seconds: 3),
|
|
|
|
|
|
|
|
onTimeout: () {
|
|
|
|
|
|
|
|
Logger.warning('Connection timeout for relay: $relayUrl');
|
|
|
|
|
|
|
|
throw Exception('Connection timeout');
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
Logger.debug('WebSocket stream created for relay: $relayUrl');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait a bit to see if connection actually works (check for errors)
|
|
|
|
|
|
|
|
Logger.debug('Setting up stream listener for relay: $relayUrl');
|
|
|
|
|
|
|
|
final completer = Completer<bool>();
|
|
|
|
|
|
|
|
late StreamSubscription subscription;
|
|
|
|
|
|
|
|
bool gotError = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subscription = stream.listen(
|
|
|
|
|
|
|
|
(data) {
|
|
|
|
|
|
|
|
// Got data - connection is working
|
|
|
|
|
|
|
|
Logger.info('Received data from relay $relayUrl during add - connection confirmed');
|
|
|
|
|
|
|
|
if (!completer.isCompleted) {
|
|
|
|
|
|
|
|
completer.complete(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
onError: (error) {
|
|
|
|
|
|
|
|
// Connection error occurred
|
|
|
|
|
|
|
|
Logger.error('Stream error for relay $relayUrl during add', error);
|
|
|
|
|
|
|
|
gotError = true;
|
|
|
|
|
|
|
|
if (!completer.isCompleted) {
|
|
|
|
|
|
|
|
completer.complete(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
onDone: () {
|
|
|
|
|
|
|
|
// Stream closed - connection failed
|
|
|
|
|
|
|
|
Logger.warning('Stream closed for relay $relayUrl during add - connection failed');
|
|
|
|
|
|
|
|
if (!completer.isCompleted) {
|
|
|
|
|
|
|
|
completer.complete(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for either data (success) or error/timeout (failure)
|
|
|
|
|
|
|
|
Logger.debug('Waiting for connection confirmation for relay: $relayUrl (timeout: 2 seconds)');
|
|
|
|
|
|
|
|
final connected = await completer.future.timeout(
|
|
|
|
|
|
|
|
const Duration(seconds: 2),
|
|
|
|
|
|
|
|
onTimeout: () {
|
|
|
|
|
|
|
|
Logger.warning('Timeout waiting for connection confirmation for relay: $relayUrl during add');
|
|
|
|
|
|
|
|
subscription.cancel();
|
|
|
|
|
|
|
|
// If no error occurred but no data either, check if relay is marked as connected
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
final relay = _relays.firstWhere(
|
|
|
|
|
|
|
|
(r) => r.url == relayUrl,
|
|
|
|
|
|
|
|
orElse: () => throw Exception('Relay not found'),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
Logger.debug('Relay $relayUrl connection state during add: isConnected=${relay.isConnected}, isEnabled=${relay.isEnabled}');
|
|
|
|
|
|
|
|
return relay.isConnected;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subscription.cancel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (connected && !gotError) {
|
|
|
|
|
|
|
|
// Connection successful - enable the relay
|
|
|
|
|
|
|
|
Logger.info('Connection successful for relay: $relayUrl - enabling relay');
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, true);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Connection failed - leave it disabled
|
|
|
|
|
|
|
|
Logger.warning('Connection failed for relay: $relayUrl (connected=$connected, gotError=$gotError) - leaving disabled');
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
_error = 'Failed to connect to relay';
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
// Connection test failed - leave relay disabled
|
|
|
|
|
|
|
|
Logger.error('Exception during connection test for relay: $relayUrl', e);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore disconnect errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_error = 'Failed to connect to relay: ${e.toString().replaceAll('Exception: ', '')}';
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
_error = 'Failed to add relay: $e';
|
|
|
|
_error = 'Failed to add relay: $e';
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
@ -141,10 +246,13 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
stream.listen(null).cancel();
|
|
|
|
stream.listen(null).cancel();
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
// Connection failed - disconnect to mark as unhealthy
|
|
|
|
// Connection failed - disconnect and disable the relay
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
// Reload and notify to update UI
|
|
|
|
_loadRelays();
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
} catch (_) {
|
|
|
|
} catch (_) {
|
|
|
|
// Ignore disconnect errors
|
|
|
|
// Ignore disconnect errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -155,12 +263,113 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
/// Toggles a relay on/off (enables/disables it).
|
|
|
|
/// Toggles a relay on/off (enables/disables it).
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// [relayUrl] - The URL of the relay to toggle.
|
|
|
|
/// [relayUrl] - The URL of the relay to toggle.
|
|
|
|
void toggleRelay(String relayUrl) {
|
|
|
|
///
|
|
|
|
|
|
|
|
/// When enabling, automatically attempts to connect to the relay.
|
|
|
|
|
|
|
|
/// If connection fails, automatically disables the relay (toggle moves back to OFF).
|
|
|
|
|
|
|
|
/// Always attempts to reconnect when toggling on, even if previously failed.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// The toggle responds immediately for better UX, then reverts if connection fails.
|
|
|
|
|
|
|
|
Future<void> toggleRelay(String relayUrl) async {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
_error = null;
|
|
|
|
_error = null;
|
|
|
|
final relay = _relays.firstWhere((r) => r.url == relayUrl);
|
|
|
|
final relay = _relays.firstWhere((r) => r.url == relayUrl);
|
|
|
|
nostrService.setRelayEnabled(relayUrl, !relay.isEnabled);
|
|
|
|
final newEnabledState = !relay.isEnabled;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If disabling, just disconnect (no test needed) - update UI immediately
|
|
|
|
|
|
|
|
if (!newEnabledState) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore disconnect errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If enabling, update UI immediately (optimistic update)
|
|
|
|
|
|
|
|
// Disconnect first to ensure a fresh connection attempt
|
|
|
|
|
|
|
|
Logger.debug('Disconnecting existing connection for relay: $relayUrl (if any)');
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore if not connected
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enable the relay immediately and update UI (optimistic update)
|
|
|
|
|
|
|
|
Logger.info('Toggling relay ON: $relayUrl - updating UI immediately');
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update UI immediately by updating the relay in our list directly
|
|
|
|
|
|
|
|
// This bypasses the auto-disable logic in getRelays() during connection attempt
|
|
|
|
|
|
|
|
final relayIndex = _relays.indexWhere((r) => r.url == relayUrl);
|
|
|
|
|
|
|
|
if (relayIndex != -1) {
|
|
|
|
|
|
|
|
_relays[relayIndex] = NostrRelay(
|
|
|
|
|
|
|
|
url: relayUrl,
|
|
|
|
|
|
|
|
isConnected: _relays[relayIndex].isConnected,
|
|
|
|
|
|
|
|
isEnabled: true,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
notifyListeners(); // Update UI immediately
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Now attempt to connect in the background
|
|
|
|
|
|
|
|
// If connection fails, we'll toggle it back to OFF
|
|
|
|
|
|
|
|
Logger.info('Attempting to connect to relay: $relayUrl (background)');
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
final stream = await nostrService
|
|
|
|
|
|
|
|
.connectRelay(relayUrl)
|
|
|
|
|
|
|
|
.timeout(
|
|
|
|
|
|
|
|
const Duration(seconds: 5),
|
|
|
|
|
|
|
|
onTimeout: () {
|
|
|
|
|
|
|
|
Logger.warning('Connection timeout for relay: $relayUrl (5 seconds)');
|
|
|
|
|
|
|
|
throw Exception('Connection timeout');
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
Logger.debug('WebSocket stream created for relay: $relayUrl');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait a short time to see if connection is established (no errors)
|
|
|
|
|
|
|
|
// The service marks connection as established after 500ms if no errors occur
|
|
|
|
|
|
|
|
Logger.debug('Waiting for connection confirmation for relay: $relayUrl (timeout: 1 second)');
|
|
|
|
|
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Cancel the stream subscription to clean up
|
|
|
|
|
|
|
|
stream.listen(null).cancel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if connection was established (no errors occurred)
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
final updatedRelay = _relays.firstWhere(
|
|
|
|
|
|
|
|
(r) => r.url == relayUrl,
|
|
|
|
|
|
|
|
orElse: () => throw Exception('Relay not found'),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Logger.debug('Relay $relayUrl connection state: isConnected=${updatedRelay.isConnected}, isEnabled=${updatedRelay.isEnabled}');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!updatedRelay.isConnected) {
|
|
|
|
|
|
|
|
// Connection failed - toggle back to OFF
|
|
|
|
|
|
|
|
Logger.warning('Connection failed for relay: $relayUrl - toggling back to OFF');
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
_error = 'Failed to connect to relay';
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Connection successful - keep it enabled and connected
|
|
|
|
|
|
|
|
Logger.info('Connection successful for relay: $relayUrl - keeping enabled');
|
|
|
|
_loadRelays();
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
// Connection failed - toggle back to OFF
|
|
|
|
|
|
|
|
Logger.error('Exception during toggle connection for relay: $relayUrl', e);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
_error = 'Failed to connect to relay: ${e.toString().replaceAll('Exception: ', '')}';
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore disconnect errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
_error = 'Failed to toggle relay: $e';
|
|
|
|
_error = 'Failed to toggle relay: $e';
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
@ -168,13 +377,140 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Toggles all relays on/off.
|
|
|
|
/// Toggles all relays on/off.
|
|
|
|
void toggleAllRelays() {
|
|
|
|
///
|
|
|
|
|
|
|
|
/// When enabling, automatically attempts to connect to all relays.
|
|
|
|
|
|
|
|
/// Always attempts to reconnect when toggling on, even if previously failed.
|
|
|
|
|
|
|
|
Future<void> toggleAllRelays() async {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
_error = null;
|
|
|
|
_error = null;
|
|
|
|
final allEnabled = _relays.every((r) => r.isEnabled);
|
|
|
|
final allEnabled = _relays.every((r) => r.isEnabled);
|
|
|
|
nostrService.setAllRelaysEnabled(!allEnabled);
|
|
|
|
final newEnabledState = !allEnabled;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If disabling, just disconnect all (no test needed)
|
|
|
|
|
|
|
|
if (!newEnabledState) {
|
|
|
|
|
|
|
|
Logger.info('Toggling all relays OFF');
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.setAllRelaysEnabled(false);
|
|
|
|
|
|
|
|
for (final relay in _relays) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relay.url);
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore disconnect errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If enabling, first ensure all are enabled, then attempt to reconnect
|
|
|
|
|
|
|
|
Logger.info('Toggling all relays ON - attempting to connect to all');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Disconnect all first to ensure fresh connection attempts
|
|
|
|
|
|
|
|
final currentRelayUrls = _relays.map((r) => r.url).toList();
|
|
|
|
|
|
|
|
for (final relayUrl in currentRelayUrls) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore if not connected
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enable all relays immediately and update UI (optimistic update)
|
|
|
|
|
|
|
|
nostrService.setAllRelaysEnabled(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update UI immediately by updating relays in our list directly
|
|
|
|
|
|
|
|
// This bypasses the auto-disable logic in getRelays() during connection attempts
|
|
|
|
|
|
|
|
for (var i = 0; i < _relays.length; i++) {
|
|
|
|
|
|
|
|
_relays[i] = NostrRelay(
|
|
|
|
|
|
|
|
url: _relays[i].url,
|
|
|
|
|
|
|
|
isConnected: _relays[i].isConnected,
|
|
|
|
|
|
|
|
isEnabled: true,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners(); // Update UI immediately
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Capture relay URLs before starting connections (list might change)
|
|
|
|
|
|
|
|
final relayUrls = _relays.map((r) => r.url).toList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Now attempt to connect to all relays in parallel
|
|
|
|
|
|
|
|
final futures = <Future<void>>[];
|
|
|
|
|
|
|
|
for (final relayUrl in relayUrls) {
|
|
|
|
|
|
|
|
futures.add(
|
|
|
|
|
|
|
|
Future<void>(() async {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Logger.info('Attempting to connect to relay: $relayUrl');
|
|
|
|
|
|
|
|
final stream = await nostrService
|
|
|
|
|
|
|
|
.connectRelay(relayUrl)
|
|
|
|
|
|
|
|
.timeout(
|
|
|
|
|
|
|
|
const Duration(seconds: 5),
|
|
|
|
|
|
|
|
onTimeout: () {
|
|
|
|
|
|
|
|
Logger.warning('Connection timeout for relay: $relayUrl (5 seconds)');
|
|
|
|
|
|
|
|
throw Exception('Connection timeout');
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
Logger.debug('WebSocket stream created for relay: $relayUrl');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait a short time to see if connection is established (no errors)
|
|
|
|
|
|
|
|
// The service marks connection as established after 500ms if no errors occur
|
|
|
|
|
|
|
|
Logger.debug('Waiting for connection confirmation for relay: $relayUrl (timeout: 1 second)');
|
|
|
|
|
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Cancel the stream subscription to clean up
|
|
|
|
|
|
|
|
stream.listen(null).cancel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if connection was established
|
|
|
|
_loadRelays();
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
final updatedRelay = _relays.firstWhere(
|
|
|
|
|
|
|
|
(r) => r.url == relayUrl,
|
|
|
|
|
|
|
|
orElse: () => throw Exception('Relay not found'),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Logger.debug('Relay $relayUrl connection state: isConnected=${updatedRelay.isConnected}, isEnabled=${updatedRelay.isEnabled}');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!updatedRelay.isConnected) {
|
|
|
|
|
|
|
|
// Connection failed - disable the relay
|
|
|
|
|
|
|
|
Logger.warning('Connection failed for relay: $relayUrl');
|
|
|
|
|
|
|
|
throw Exception('Connection failed');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Connection successful
|
|
|
|
|
|
|
|
Logger.info('Connection successful for relay: $relayUrl');
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
// Connection failed - automatically disable the relay
|
|
|
|
|
|
|
|
Logger.error('Exception during connection for relay: $relayUrl', e);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore disconnect errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).catchError((error) {
|
|
|
|
|
|
|
|
// Final safety net - ensure no exceptions escape
|
|
|
|
|
|
|
|
Logger.error('Error in toggleAllRelays for relay: $relayUrl', error);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
nostrService.disconnectRelay(relayUrl);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relayUrl, false);
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// Ignore all errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Future<void>.value();
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for all connection attempts to complete (or fail gracefully)
|
|
|
|
|
|
|
|
Logger.info('Waiting for all ${futures.length} relay connection attempts to complete');
|
|
|
|
|
|
|
|
await Future.wait(futures, eagerError: false);
|
|
|
|
|
|
|
|
Logger.info('All relay connection attempts completed');
|
|
|
|
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
Logger.error('Failed to toggle all relays', e);
|
|
|
|
_error = 'Failed to toggle all relays: $e';
|
|
|
|
_error = 'Failed to toggle all relays: $e';
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -213,10 +549,13 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
// Cancel the stream subscription to clean up
|
|
|
|
// Cancel the stream subscription to clean up
|
|
|
|
stream.listen(null).cancel();
|
|
|
|
stream.listen(null).cancel();
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
// Connection failed - disconnect to mark as unhealthy
|
|
|
|
// Connection failed - disconnect and disable to mark as unhealthy
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
nostrService.disconnectRelay(relay.url);
|
|
|
|
nostrService.disconnectRelay(relay.url);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relay.url, false);
|
|
|
|
|
|
|
|
// Reload and notify to update UI
|
|
|
|
_loadRelays();
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
} catch (_) {
|
|
|
|
} catch (_) {
|
|
|
|
// Ignore disconnect errors
|
|
|
|
// Ignore disconnect errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -225,10 +564,13 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
// Catch all exceptions - connection failures are expected in tests
|
|
|
|
// Catch all exceptions - connection failures are expected in tests
|
|
|
|
// Disconnect relay to mark as unhealthy
|
|
|
|
// Disconnect and disable relay to mark as unhealthy
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
nostrService.disconnectRelay(relay.url);
|
|
|
|
nostrService.disconnectRelay(relay.url);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relay.url, false);
|
|
|
|
|
|
|
|
// Reload and notify to update UI
|
|
|
|
_loadRelays();
|
|
|
|
_loadRelays();
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
} catch (_) {
|
|
|
|
} catch (_) {
|
|
|
|
// Ignore disconnect errors
|
|
|
|
// Ignore disconnect errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -238,6 +580,7 @@ class RelayManagementController extends ChangeNotifier {
|
|
|
|
// Final safety net - ensure no exceptions escape
|
|
|
|
// Final safety net - ensure no exceptions escape
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
nostrService.disconnectRelay(relay.url);
|
|
|
|
nostrService.disconnectRelay(relay.url);
|
|
|
|
|
|
|
|
nostrService.setRelayEnabled(relay.url, false);
|
|
|
|
_loadRelays();
|
|
|
|
_loadRelays();
|
|
|
|
} catch (_) {
|
|
|
|
} catch (_) {
|
|
|
|
// Ignore all errors
|
|
|
|
// Ignore all errors
|
|
|
|
|