diff --git a/Session/Calls/Call Management/SessionCall.swift b/Session/Calls/Call Management/SessionCall.swift index e0b92096e..853712fe0 100644 --- a/Session/Calls/Call Management/SessionCall.swift +++ b/Session/Calls/Call Management/SessionCall.swift @@ -235,6 +235,7 @@ public final class SessionCall: CurrentCallProtocol, WebRTCSessionDelegate { body: String(data: messageInfoData, encoding: .utf8), timestampMs: timestampMs ) + .withDisappearingMessagesConfiguration(db) .inserted(db) self.callInteractionId = interaction?.id diff --git a/Session/Notifications/PushRegistrationManager.swift b/Session/Notifications/PushRegistrationManager.swift index 0b9aa560b..af85f50d4 100644 --- a/Session/Notifications/PushRegistrationManager.swift +++ b/Session/Notifications/PushRegistrationManager.swift @@ -315,7 +315,10 @@ public enum PushRegistrationError: Error { variant: .infoCall, body: messageInfoString, timestampMs: timestampMs - ).inserted(db) + ) + .withDisappearingMessagesConfiguration(db) + .inserted(db) + call.callInteractionId = interaction.id return call diff --git a/SessionMessagingKit/Database/Models/Interaction.swift b/SessionMessagingKit/Database/Models/Interaction.swift index 3ed3f307c..3c0c7e992 100644 --- a/SessionMessagingKit/Database/Models/Interaction.swift +++ b/SessionMessagingKit/Database/Models/Interaction.swift @@ -476,6 +476,20 @@ public extension Interaction { openGroupWhisperTo: self.openGroupWhisperTo ) } + + func withDisappearingMessagesConfiguration( + _ db: Database, + disappearingMessagesConfiguration: DisappearingMessagesConfiguration? = nil + ) -> Interaction { + if let config = disappearingMessagesConfiguration ?? (try? DisappearingMessagesConfiguration.fetchOne(db, id: self.threadId)) { + return self.with( + expiresInSeconds: config.durationSeconds, + expiresStartedAtMs: (config.type == .disappearAfterSend ? Double(self.timestampMs) : nil) + ) + } + + return self + } } // MARK: - GRDB Interactions diff --git a/SessionMessagingKit/Messages/Control Messages/CallMessage.swift b/SessionMessagingKit/Messages/Control Messages/CallMessage.swift index f37d0c37f..4e6cf8a70 100644 --- a/SessionMessagingKit/Messages/Control Messages/CallMessage.swift +++ b/SessionMessagingKit/Messages/Control Messages/CallMessage.swift @@ -190,7 +190,7 @@ public final class CallMessage: ControlMessage { let contentProto = SNProtoContent.builder() // DisappearingMessagesConfiguration - setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId) + setDisappearingMessagesConfigurationIfNeeded(on: contentProto) do { contentProto.setCallMessage(try callMessageProto.build()) diff --git a/SessionMessagingKit/Messages/Control Messages/DataExtractionNotification.swift b/SessionMessagingKit/Messages/Control Messages/DataExtractionNotification.swift index a36d27059..331ce7fa9 100644 --- a/SessionMessagingKit/Messages/Control Messages/DataExtractionNotification.swift +++ b/SessionMessagingKit/Messages/Control Messages/DataExtractionNotification.swift @@ -98,7 +98,7 @@ public final class DataExtractionNotification: ControlMessage { let contentProto = SNProtoContent.builder() contentProto.setDataExtractionNotification(try dataExtractionNotification.build()) // DisappearingMessagesConfiguration - setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId) + setDisappearingMessagesConfigurationIfNeeded(on: contentProto) return try contentProto.build() } catch { SNLog("Couldn't construct data extraction notification proto from: \(self).") diff --git a/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift b/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift index ec0947b0c..f959909ac 100644 --- a/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift +++ b/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift @@ -78,7 +78,7 @@ public final class ExpirationTimerUpdate: ControlMessage { let contentProto = SNProtoContent.builder() // DisappearingMessagesConfiguration - setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId) + setDisappearingMessagesConfigurationIfNeeded(on: contentProto) do { contentProto.setDataMessage(try dataMessageProto.build()) diff --git a/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift b/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift index 5e52a3e96..e143d2946 100644 --- a/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift +++ b/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift @@ -65,7 +65,7 @@ public final class ReadReceipt: ControlMessage { do { contentProto.setReceiptMessage(try receiptProto.build()) // DisappearingMessagesConfiguration - setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId) + setDisappearingMessagesConfigurationIfNeeded(on: contentProto) return try contentProto.build() } catch { SNLog("Couldn't construct read receipt proto from: \(self).") diff --git a/SessionMessagingKit/Messages/Message.swift b/SessionMessagingKit/Messages/Message.swift index 483f813c5..e04573d13 100644 --- a/SessionMessagingKit/Messages/Message.swift +++ b/SessionMessagingKit/Messages/Message.swift @@ -66,17 +66,19 @@ public class Message: Codable { preconditionFailure("toProto(_:) is abstract and must be overridden.") } - public func setDisappearingMessagesConfigurationIfNeeded(_ db: Database, on proto: SNProtoContent.SNProtoContentBuilder, threadId: String) { - guard let disappearingMessagesConfiguration = try? DisappearingMessagesConfiguration.fetchOne(db, id: threadId) else { + public func setDisappearingMessagesConfigurationIfNeeded(on proto: SNProtoContent.SNProtoContentBuilder) { + if let expiresInSeconds = self.expiresInSeconds { + proto.setExpirationTimer(UInt32(expiresInSeconds)) + } else { proto.setExpirationTimer(0) + proto.setExpirationType(.unknown) return } - let expireTimer: UInt32 = disappearingMessagesConfiguration.isEnabled ? UInt32(disappearingMessagesConfiguration.durationSeconds) : 0 - proto.setExpirationTimer(expireTimer) - - if disappearingMessagesConfiguration.isEnabled, let type = disappearingMessagesConfiguration.type { - proto.setExpirationType(type.toProto()) + if let expiresStartedAtMs = self.expiresStartedAtMs, UInt64(expiresStartedAtMs) == self.sentTimestamp { + proto.setExpirationType(.deleteAfterSend) + } else { + proto.setExpirationType(.deleteAfterRead) } } diff --git a/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift b/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift index c1462e727..a61e53228 100644 --- a/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift +++ b/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift @@ -185,7 +185,7 @@ public final class VisibleMessage: Message { } // DisappearingMessagesConfiguration - setDisappearingMessagesConfigurationIfNeeded(db, on: proto, threadId: threadId) + setDisappearingMessagesConfigurationIfNeeded(on: proto) // Sync target if let syncTarget = syncTarget { diff --git a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+Calls.swift b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+Calls.swift index 784b91437..185c7c3da 100644 --- a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+Calls.swift +++ b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+Calls.swift @@ -228,7 +228,9 @@ extension MessageReceiver { timestampMs: (messageSentTimestamp * 1000), userPublicKey: getUserHexEncodedPublicKey(db), openGroup: nil - ) + ), + expiresInSeconds: message.expiresInSeconds, + expiresStartedAtMs: message.expiresStartedAtMs ) .inserted(db)