From 8b797217258dadeecb3fb429fcf0f3f6cdfbc0b1 Mon Sep 17 00:00:00 2001 From: jack Date: Sun, 6 Jul 2025 22:47:02 +0200 Subject: [PATCH] Fix read receipts not being sent for existing messages - Added app activation observer to send read receipts when app becomes active - Send read receipts for all unread messages when receiving a new message while chat is open - Added import for NSApplication/UIApplication notifications - This ensures messages are marked as read even when opening chat from notifications or returning to app --- bitchat/ViewModels/ChatViewModel.swift | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index ac79629b..3c37a8f9 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -97,6 +97,23 @@ class ChatViewModel: ObservableObject { .sink { [weak self] (messageID, status) in self?.updateMessageDeliveryStatus(messageID, status: status) } + + // When app becomes active, send read receipts for visible messages + #if os(macOS) + NotificationCenter.default.addObserver( + self, + selector: #selector(appDidBecomeActive), + name: NSApplication.didBecomeActiveNotification, + object: nil + ) + #else + NotificationCenter.default.addObserver( + self, + selector: #selector(appDidBecomeActive), + name: UIApplication.didBecomeActiveNotification, + object: nil + ) + #endif } private func loadNickname() { @@ -902,6 +919,16 @@ class ChatViewModel: ObservableObject { selectedPrivateChatPeer = nil } + @objc private func appDidBecomeActive() { + // When app becomes active, send read receipts for visible private chat + if let peerID = selectedPrivateChatPeer { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { + print("[Delivery] App became active, checking for messages to mark as read in chat with \(peerID)") + self.markPrivateMessagesAsRead(from: peerID) + } + } + } + func markPrivateMessagesAsRead(from peerID: String) { guard let messages = privateChats[peerID] else { print("[Delivery] No messages found for peer \(peerID)") @@ -1842,6 +1869,11 @@ extension ChatViewModel: BitchatDelegate { ) meshService.sendReadReceipt(receipt, to: messageSenderID) print("[Delivery] Sending immediate read receipt for message \(message.id) from \(message.sender) to peer \(messageSenderID)") + + // Also check if there are other unread messages from this peer + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in + self?.markPrivateMessagesAsRead(from: peerID) + } } else { print("[Delivery] Cannot send read receipt - message has no senderPeerID") }