diff --git a/Session/Conversations/Settings/ThreadDisappearingMessagesViewModel.swift b/Session/Conversations/Settings/ThreadDisappearingMessagesViewModel.swift index 5c25678dd..7e2901cd8 100644 --- a/Session/Conversations/Settings/ThreadDisappearingMessagesViewModel.swift +++ b/Session/Conversations/Settings/ThreadDisappearingMessagesViewModel.swift @@ -275,7 +275,7 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel 0 else { return "YOU_DISABLED_DISAPPEARING_MESSAGES_CONFIGURATION".localized() } @@ -107,13 +110,23 @@ public extension DisappearingMessagesConfiguration { } guard isEnabled, durationSeconds > 0 else { - return String(format: "OTHER_DISABLED_DISAPPEARING_MESSAGES_CONFIGURATION".localized(), senderName) + return String(format: "DISAPPERING_MESSAGES_INFO_DISABLE".localized(), senderName) + } + + guard isPreviousOff == true else { + return String( + format: "DISAPPERING_MESSAGES_INFO_UPDATE".localized(), + senderName, + floor(durationSeconds).formatted(format: .long), + (type == .disappearAfterRead ? "MESSAGE_STATE_READ".localized() : "MESSAGE_STATE_SENT".localized()) + ) } return String( - format: "OTHER_UPDATED_DISAPPEARING_MESSAGES_CONFIGURATION".localized(), + format: "DISAPPERING_MESSAGES_INFO_ENABLE".localized(), senderName, - floor(durationSeconds).formatted(format: .long) + floor(durationSeconds).formatted(format: .long), + (type == .disappearAfterRead ? "MESSAGE_STATE_READ".localized() : "MESSAGE_STATE_SENT".localized()) ) } } @@ -122,11 +135,13 @@ public extension DisappearingMessagesConfiguration { floor(durationSeconds).formatted(format: .long) } - func messageInfoString(with senderName: String?) -> String? { + func messageInfoString(with senderName: String?, isPreviousOff: Bool) -> String? { let messageInfo: MessageInfo = DisappearingMessagesConfiguration.MessageInfo( senderName: senderName, isEnabled: isEnabled, - durationSeconds: durationSeconds + durationSeconds: durationSeconds, + type: type, + isPreviousOff: isPreviousOff ) guard let messageInfoData: Data = try? JSONEncoder().encode(messageInfo) else { return nil } @@ -232,47 +247,4 @@ public class SMKDisappearingMessagesConfiguration: NSObject { return floor(durationSeconds).formatted(format: .long) } - - @objc(update:isEnabled:durationIndex:) - public static func update(_ threadId: String, isEnabled: Bool, durationIndex: Int) { - let durationSeconds: TimeInterval = ( - durationIndex >= 0 && durationIndex < DisappearingMessagesConfiguration.validDurationsSeconds.count ? - DisappearingMessagesConfiguration.validDurationsSeconds[durationIndex] : - DisappearingMessagesConfiguration.validDurationsSeconds[0] - ) - - Storage.shared.write { db in - guard let thread: SessionThread = try SessionThread.fetchOne(db, id: threadId) else { - return - } - - let config: DisappearingMessagesConfiguration = try DisappearingMessagesConfiguration - .fetchOne(db, id: threadId) - .defaulting(to: DisappearingMessagesConfiguration.defaultWith(threadId)) - .with( - isEnabled: isEnabled, - durationSeconds: durationSeconds - ) - .saved(db) - - let interaction: Interaction = try Interaction( - threadId: threadId, - authorId: getUserHexEncodedPublicKey(db), - variant: .infoDisappearingMessagesUpdate, - body: config.messageInfoString(with: nil), - timestampMs: Int64(floor(Date().timeIntervalSince1970 * 1000)) - ) - .inserted(db) - - try MessageSender.send( - db, - message: ExpirationTimerUpdate( - syncTarget: nil, - duration: UInt32(floor(isEnabled ? durationSeconds : 0)) - ), - interactionId: interaction.id, - in: thread - ) - } - } } diff --git a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift index bb5f3bfaa..381b68a73 100644 --- a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift +++ b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift @@ -40,7 +40,8 @@ extension MessageReceiver { with: (sender != getUserHexEncodedPublicKey(db) ? Profile.displayName(db, id: sender) : nil - ) + ), + isPreviousOff: false ), timestampMs: Int64(message.sentTimestamp ?? 0) // Default to `0` if not set ).inserted(db) diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift index ddf0ebfc5..351e2b126 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift @@ -355,7 +355,8 @@ public enum MessageReceiver { with: (sender != getUserHexEncodedPublicKey(db) ? Profile.displayName(db, id: sender) : nil - ) + ), + isPreviousOff: !localConfig.isEnabled ), timestampMs: protoLastChangeTimestampMs ).inserted(db)