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
This commit is contained in:
jack
2025-07-06 22:47:02 +02:00
parent e93d02afd0
commit 8b79721725
+32
View File
@@ -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")
}