session-ios/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ReadReceipts.swift
Morgan Pretty 08b1e9a131 Started caching pending ReadReceipt messages to resolve an edge-case
Fixed an issue where read receipts could be sent for already read messages
Fixed an issue where the read state change might not update the UI
2023-02-15 15:11:57 +11:00

36 lines
1.3 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
extension MessageReceiver {
internal static func handleReadReceipt(
_ db: Database,
message: ReadReceipt,
serverExpirationTimestamp: TimeInterval?
) throws {
guard let sender: String = message.sender else { return }
guard let timestampMsValues: [Int64] = message.timestamps?.map({ Int64($0) }) else { return }
guard let readTimestampMs: Int64 = message.receivedTimestamp.map({ Int64($0) }) else { return }
let pendingTimestampMs: Set<Int64> = try Interaction.markAsRead(
db,
recipientId: sender,
timestampMsValues: timestampMsValues,
readTimestampMs: readTimestampMs
)
guard !pendingTimestampMs.isEmpty else { return }
// We have some pending read receipts so store them in the database
try pendingTimestampMs.forEach { timestampMs in
try PendingReadReceipt(
threadId: sender,
interactionTimestampMs: timestampMs,
readTimestampMs: readTimestampMs,
serverExpirationTimestamp: (serverExpirationTimestamp ?? 0)
).save(db)
}
}
}