From f004f3e7ea079a062a60d44ba2eb6138a3e9d919 Mon Sep 17 00:00:00 2001 From: Islam <2553451+qalandarov@users.noreply.github.com> Date: Wed, 1 Oct 2025 12:38:28 +0100 Subject: [PATCH] PeerID 3/n: Remove dead code (#706) * PeerID 2/n: Count and hex conversion done with `bare` * ChatVM: remove dead code `registerPeerPublicKey` * ChatVM: Remove dead `handlePeerFavoritedUs` * ChatVM: remove dead functions * PrivateChatManager: Remove dead code * BLEService: Remove dead code --- bitchat/Services/BLEService.swift | 15 --- bitchat/Services/PrivateChatManager.swift | 123 ---------------------- bitchat/ViewModels/ChatViewModel.swift | 101 ------------------ 3 files changed, 239 deletions(-) diff --git a/bitchat/Services/BLEService.swift b/bitchat/Services/BLEService.swift index 2db1f85b..c2eceb36 100644 --- a/bitchat/Services/BLEService.swift +++ b/bitchat/Services/BLEService.swift @@ -539,21 +539,6 @@ final class BLEService: NSObject { } #endif - // MARK: - Helper Functions for Peripheral Management - - private func getConnectedPeripherals() -> [CBPeripheral] { - return peripherals.values - .filter { $0.isConnected } - .map { $0.peripheral } - } - - private func getPeripheral(for peerID: String) -> CBPeripheral? { - guard let uuid = peerToPeripheralUUID[peerID], - let state = peripherals[uuid], - state.isConnected else { return nil } - return state.peripheral - } - // MARK: - Core Public API func startServices() { diff --git a/bitchat/Services/PrivateChatManager.swift b/bitchat/Services/PrivateChatManager.swift index bcf52bf0..bc6e9ee6 100644 --- a/bitchat/Services/PrivateChatManager.swift +++ b/bitchat/Services/PrivateChatManager.swift @@ -53,87 +53,6 @@ final class PrivateChatManager: ObservableObject { selectedPeer = nil selectedPeerFingerprint = nil } - - /// Send a private message - func sendMessage(_ content: String, to peerID: String) { - guard let meshService = meshService, - let peerNickname = meshService.peerNickname(peerID: peerID) else { - return - } - - let messageID = UUID().uuidString - - // Create local message - let message = BitchatMessage( - id: messageID, - sender: meshService.myNickname, - content: content, - timestamp: Date(), - isRelay: false, - originalSender: nil, - isPrivate: true, - recipientNickname: peerNickname, - senderPeerID: meshService.myPeerID, - mentions: nil, - deliveryStatus: .sending - ) - - // Add to chat - if privateChats[peerID] == nil { privateChats[peerID] = [] } - privateChats[peerID]?.append(message) - // Enforce per-chat cap on local append - if var arr = privateChats[peerID], arr.count > privateChatCap { - let remove = arr.count - privateChatCap - arr.removeFirst(remove) - privateChats[peerID] = arr - } - - // Send via mesh service - meshService.sendPrivateMessage(content, to: peerID, recipientNickname: peerNickname, messageID: messageID) - } - - /// Handle incoming private message - func handleIncomingMessage(_ message: BitchatMessage) { - guard let senderPeerID = message.senderPeerID else { return } - - // Initialize chat if needed - if privateChats[senderPeerID] == nil { - privateChats[senderPeerID] = [] - } - - // Deduplicate by ID: replace existing message if present, else append - if let idx = privateChats[senderPeerID]?.firstIndex(where: { $0.id == message.id }) { - privateChats[senderPeerID]?[idx] = message - } else { - privateChats[senderPeerID]?.append(message) - } - - // Sanitize chat to avoid duplicate IDs and sort by timestamp - sanitizeChat(for: senderPeerID) - // Enforce cap after sanitize - if var arr = privateChats[senderPeerID], arr.count > privateChatCap { - let remove = arr.count - privateChatCap - arr.removeFirst(remove) - privateChats[senderPeerID] = arr - } - - // Mark as unread if not in this chat - if selectedPeer != senderPeerID { - unreadMessages.insert(senderPeerID) - - // Avoid notifying for messages already marked as read (dup/resubscribe cases) - if !sentReadReceipts.contains(message.id) { - NotificationService.shared.sendPrivateMessageNotification( - from: message.sender, - message: message.content, - peerID: senderPeerID - ) - } - } else { - // Send read receipt if viewing this chat - sendReadReceipt(for: message) - } - } /// Remove duplicate messages by ID and keep chronological order func sanitizeChat(for peerID: String) { @@ -173,48 +92,6 @@ final class PrivateChatManager: ObservableObject { } } - /// Update the selected peer if fingerprint matches (for reconnections) - func updateSelectedPeer(peers: [String: String]) { - guard let fingerprint = selectedPeerFingerprint else { return } - - // Find peer with matching fingerprint - for (peerID, _) in peers { - if meshService?.getFingerprint(for: peerID) == fingerprint { - selectedPeer = peerID - break - } - } - } - - /// Get chat messages for current context - func getCurrentMessages() -> [BitchatMessage] { - guard let peer = selectedPeer else { return [] } - return privateChats[peer] ?? [] - } - - /// Clear a private chat - func clearChat(with peerID: String) { - privateChats[peerID]?.removeAll() - } - - /// Handle delivery acknowledgment - func handleDeliveryAck(messageID: String, from peerID: String) { - guard privateChats[peerID] != nil else { return } - - if let index = privateChats[peerID]?.firstIndex(where: { $0.id == messageID }) { - privateChats[peerID]?[index].deliveryStatus = .delivered(to: "recipient", at: Date()) - } - } - - /// Handle read receipt - func handleReadReceipt(messageID: String, from peerID: String) { - guard privateChats[peerID] != nil else { return } - - if let index = privateChats[peerID]?.firstIndex(where: { $0.id == messageID }) { - privateChats[peerID]?[index].deliveryStatus = .read(by: "recipient", at: Date()) - } - } - // MARK: - Private Methods private func sendReadReceipt(for message: BitchatMessage) { diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index b24f64cb..69724081 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -1087,7 +1087,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { privateChats[convKey] = [] } privateChats[convKey]?.append(msg) - trimPrivateChatMessagesIfNeeded(for: convKey) // pared back: omit view-state log let isViewing = selectedPrivateChatPeer == convKey @@ -1295,57 +1294,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { // MARK: - Public Key and Identity Management - // Called when we receive a peer's public key - @MainActor - func registerPeerPublicKey(peerID: String, publicKeyData: Data) { - let fingerprintStr = publicKeyData.sha256Fingerprint() - - // Only register if not already registered - if peerIDToPublicKeyFingerprint[peerID] != fingerprintStr { - peerIDToPublicKeyFingerprint[peerID] = fingerprintStr - } - - // Update identity state manager with handshake completion - identityManager.updateHandshakeState(peerID: peerID, state: .completed(fingerprint: fingerprintStr)) - - // Update encryption status now that we have the fingerprint - updateEncryptionStatus(for: peerID) - - // Check if we have a claimed nickname for this peer - let peerNicknames = meshService.getPeerNicknames() - if let nickname = peerNicknames[peerID], nickname != "Unknown" && nickname != "anon\(peerID.prefix(4))" { - // Update or create social identity with the claimed nickname - if var identity = identityManager.getSocialIdentity(for: fingerprintStr) { - identity.claimedNickname = nickname - identityManager.updateSocialIdentity(identity) - } else { - let newIdentity = SocialIdentity( - fingerprint: fingerprintStr, - localPetname: nil, - claimedNickname: nickname, - trustLevel: .casual, - isFavorite: false, - isBlocked: false, - notes: nil - ) - identityManager.updateSocialIdentity(newIdentity) - } - } - - // Check if this peer is the one we're in a private chat with - updatePrivateChatPeerIfNeeded() - - // If we're in a private chat with this peer (by fingerprint), send pending read receipts - if let chatFingerprint = selectedPrivateChatFingerprint, - chatFingerprint == fingerprintStr { - // Send read receipts for any unread messages from this peer - // Use a small delay to ensure the connection is fully established - DispatchQueue.main.asyncAfter(deadline: .now() + TransportConfig.uiReadReceiptRetryLongSeconds) { [weak self] in - self?.markPrivateMessagesAsRead(from: peerID) - } - } - } - @MainActor func isPeerBlocked(_ peerID: String) -> Bool { return unifiedPeerService.isBlocked(peerID) @@ -1399,7 +1347,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { // Update all at once privateChats = chats // Trigger setter - trimPrivateChatMessagesIfNeeded(for: currentPeerID) } // Migrate unread status @@ -1837,7 +1784,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { privateChats[convKey] = [] } privateChats[convKey]?.append(msg) - trimPrivateChatMessagesIfNeeded(for: convKey) let isViewing = selectedPrivateChatPeer == convKey let wasReadBefore = sentReadReceipts.contains(messageId) @@ -2305,7 +2251,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { privateChats[peerID] = [] } privateChats[peerID]?.append(message) - trimPrivateChatMessagesIfNeeded(for: peerID) // Trigger UI update for sent message objectWillChange.send() @@ -2362,7 +2307,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { } privateChats[peerID]?.append(message) - trimPrivateChatMessagesIfNeeded(for: peerID) objectWillChange.send() // Resolve recipient hex from mapping @@ -2456,7 +2400,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { ) if privateChats[peerID] == nil { privateChats[peerID] = [] } privateChats[peerID]?.append(systemMessage) - trimPrivateChatMessagesIfNeeded(for: peerID) objectWillChange.send() } @@ -2951,7 +2894,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { } chats[peerID]?.append(localNotification) privateChats = chats // Trigger setter - trimPrivateChatMessagesIfNeeded(for: peerID) } else { // In public chat - send to active public channel @@ -4238,10 +4180,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { } } - private func trimPrivateChatMessagesIfNeeded(for peerID: String) { - // Handled by PrivateChatManager - } - // MARK: - Message Management private func addMessage(_ message: BitchatMessage) { @@ -4251,10 +4189,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { trimMessagesIfNeeded() } - private func addPrivateMessage(_ message: BitchatMessage, for peerID: String) { - // Deprecated - messages are now added directly in didReceiveMessage to avoid double processing - } - // Update encryption status in appropriate places, not during view updates @MainActor private func updateEncryptionStatus(for peerID: String) { @@ -4925,36 +4859,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { return Array(Set(mentions)) // Remove duplicates } - @MainActor - func handlePeerFavoritedUs(peerID: String, favorited: Bool, nickname: String, nostrNpub: String? = nil) { - // Get peer's noise public key - guard let noisePublicKey = Data(hexString: peerID) else { return } - - // Decode npub to hex if provided - var nostrPublicKey: String? = nil - if let npub = nostrNpub { - do { - let (hrp, data) = try Bech32.decode(npub) - if hrp == "npub" { - nostrPublicKey = data.hexEncodedString() - } - } catch { - SecureLogger.error("Failed to decode Nostr npub: \(error)", category: .session) - } - } - - // Update favorite status in persistence service - FavoritesPersistenceService.shared.updatePeerFavoritedUs( - peerNoisePublicKey: noisePublicKey, - favorited: favorited, - peerNickname: nickname, - peerNostrPublicKey: nostrPublicKey - ) - - // Update peer list to reflect the change - // UnifiedPeerService updates automatically via subscriptions - } - func isFavorite(fingerprint: String) -> Bool { return identityManager.isFavorite(fingerprint: fingerprint) } @@ -5343,7 +5247,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { } // Sanitize to avoid duplicate IDs privateChatManager.sanitizeChat(for: targetPeerID) - trimPrivateChatMessagesIfNeeded(for: targetPeerID) } @MainActor @@ -5362,7 +5265,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { privateChats[ephemeralPeerID]?[idx] = message } else { privateChats[ephemeralPeerID]?.append(message) - trimPrivateChatMessagesIfNeeded(for: ephemeralPeerID) } privateChatManager.sanitizeChat(for: ephemeralPeerID) } @@ -5637,7 +5539,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { privateChats[tempPeerID] = [] } privateChats[tempPeerID]?.append(message) - trimPrivateChatMessagesIfNeeded(for: tempPeerID) // For unknown senders (no Noise key), skip sending Nostr ACKs @@ -5885,7 +5786,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { // Initialize with migrated messages privateChats[peerID] = migratedMessages } - trimPrivateChatMessagesIfNeeded(for: peerID) privateChatManager.sanitizeChat(for: peerID) // Update selectedPrivateChatPeer if it was pointing to an old ID @@ -5971,7 +5871,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { var chats = privateChats chats[peerID]?.append(messageToStore) privateChats = chats - trimPrivateChatMessagesIfNeeded(for: peerID) // UI updates via @Published reassignment above