From b0f6568ff5b0c9bf263b2d63d7893558d7da7513 Mon Sep 17 00:00:00 2001 From: jack Date: Thu, 11 Sep 2025 12:29:19 +0200 Subject: [PATCH] On launch, queue Nostr subscriptions without initiating connects; let centralized connect handle it - In subscribe(), if no connections exist, just list relays and queue subs - Avoids early send/cancel churn before connect() runs post-Tor-ready --- bitchat/Nostr/NostrRelayManager.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/bitchat/Nostr/NostrRelayManager.swift b/bitchat/Nostr/NostrRelayManager.swift index 49ce5c04..07b3d99d 100644 --- a/bitchat/Nostr/NostrRelayManager.swift +++ b/bitchat/Nostr/NostrRelayManager.swift @@ -244,6 +244,22 @@ class NostrRelayManager: ObservableObject { // Target specific relays if provided; else default. Filter permanently failed relays. let baseUrls = relayUrls ?? Self.defaultRelays let urls = baseUrls.filter { !isPermanentlyFailed($0) } + // If no connections exist yet (e.g., app launch), avoid initiating connections + // from subscribe. Just ensure relays are listed and queue subs to flush later. + if connections.isEmpty { + let existingSet = Set(relays.map { $0.url }) + for url in urls where !existingSet.contains(url) { + relays.append(Relay(url: url)) + } + for url in urls { + var map = self.pendingSubscriptions[url] ?? [:] + map[id] = messageString + self.pendingSubscriptions[url] = map + } + SecureLogger.log("📋 Queued subscription id=\(id) for \(urls.count) relay(s)", + category: SecureLogger.session, level: .debug) + return + } ensureConnections(to: urls) let targets: [(String, URLSessionWebSocketTask)] = urls.compactMap { url in connections[url].map { (url, $0) }