Make Nostr connect single-sourced; defer DM subscription until connected

- Remove duplicate connect call from ChatViewModel; let scene-based flow connect
- Setup DM subscription once on first connection via  sink
- Reduce early subscription send/cancel noise after Tor restarts
This commit is contained in:
jack
2025-09-11 12:24:36 +02:00
parent ca10724e64
commit f7b78452d0
+8 -3
View File
@@ -367,6 +367,8 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
// Track whether a Tor restart is pending so we only announce // Track whether a Tor restart is pending so we only announce
// "tor restarted" after an actual restart, not the first launch. // "tor restarted" after an actual restart, not the first launch.
private var torRestartPending: Bool = false private var torRestartPending: Bool = false
// Ensure we set up DM subscription only once per app session
private var nostrHandlersSetup: Bool = false
// MARK: - Caches // MARK: - Caches
@@ -565,9 +567,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
} }
nostrRelayManager = NostrRelayManager.shared nostrRelayManager = NostrRelayManager.shared
SecureLogger.log("Initializing Nostr relay connections", category: SecureLogger.session, level: .debug) SecureLogger.log("Initializing Nostr relay connections", category: SecureLogger.session, level: .debug)
nostrRelayManager?.connect() // Connect is managed centrally on scene activation; avoid duplicate connects here
// Set up Nostr message handling directly once Tor is ready
setupNostrMessageHandling()
// Attempt to flush any queued outbox after Nostr comes online // Attempt to flush any queued outbox after Nostr comes online
messageRouter.flushAllOutbox() messageRouter.flushAllOutbox()
@@ -622,6 +622,11 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
guard let self = self else { return } guard let self = self else { return }
if connected { if connected {
Task { @MainActor in Task { @MainActor in
// Set up DM handler once on first connect
if !self.nostrHandlersSetup {
self.setupNostrMessageHandling()
self.nostrHandlersSetup = true
}
self.resubscribeCurrentGeohash() self.resubscribeCurrentGeohash()
// Re-init sampling for regional + bookmarked geohashes after reconnect // Re-init sampling for regional + bookmarked geohashes after reconnect
let regional = LocationChannelManager.shared.availableChannels.map { $0.geohash } let regional = LocationChannelManager.shared.availableChannels.map { $0.geohash }