From 96afa8d23f1ccec406fd60df77f5a396f5efced3 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 5 Sep 2023 14:07:41 +0930 Subject: [PATCH] Fix expiry timer millis conversion --- .../conversation/ConversationActionBarView.kt | 2 +- .../conversation/v2/ConversationActivityV2.kt | 4 ++-- .../org/thoughtcrime/securesms/database/Storage.kt | 10 +++++----- .../notifications/AndroidAutoReplyReceiver.java | 2 +- .../securesms/notifications/RemoteReplyReceiver.java | 2 +- .../securesms/repository/ConversationRepository.kt | 2 +- .../securesms/service/ExpiringMessageManager.java | 2 +- .../messenger/libsession_util/util/ExpiryMode.kt | 2 ++ .../session/libsession/messaging/messages/Message.kt | 2 +- .../messaging/sending_receiving/MessageSender.kt | 6 +++--- .../sending_receiving/ReceivedMessageHandler.kt | 12 ++++-------- 11 files changed, 22 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActionBarView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActionBarView.kt index 59ac8bb2bb..00b29fe393 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActionBarView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActionBarView.kt @@ -118,7 +118,7 @@ class ConversationActionBarView : LinearLayout { } settings.add( ConversationSetting( - "$prefix - ${ExpirationUtil.getExpirationAbbreviatedDisplayValue(context, config.expiryMode?.expirySeconds!!)}" , + "$prefix - ${ExpirationUtil.getExpirationAbbreviatedDisplayValue(context, config.expiryMode.expirySeconds)}" , ConversationSettingType.EXPIRATION, R.drawable.ic_timer ) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 3f24592472..9ed9deec25 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -1577,7 +1577,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe val message = VisibleMessage() message.sentTimestamp = sentTimestamp message.text = text - val expiresInMillis = (viewModel.expirationConfiguration?.expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMillis = viewModel.expirationConfiguration?.expiryMode?.expiryMillis ?: 0 val expireStartedAt = if (viewModel.expirationConfiguration?.expiryMode is ExpiryMode.AfterSend) { message.sentTimestamp!! } else 0 @@ -1620,7 +1620,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe else it.individualRecipient.address quote?.copy(author = sender) } - val expiresInMs = (viewModel.expirationConfiguration?.expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMs = viewModel.expirationConfiguration?.expiryMode?.expiryMillis ?: 0 val expireStartedAtMs = if (viewModel.expirationConfiguration?.expiryMode is ExpiryMode.AfterSend) { sentTimestamp } else 0 diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index c357464d79..9d3baefc50 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -329,7 +329,7 @@ open class Storage( } val expirationConfig = getExpirationConfiguration(message.threadID ?: -1) val expiryMode = expirationConfig?.expiryMode - val expiresInMillis = (expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMillis = expiryMode?.expiryMillis ?: 0 val expireStartedAt = if (expiryMode is ExpiryMode.AfterSend) message.sentTimestamp!! else 0 if (message.isMediaMessage() || attachments.isNotEmpty()) { val quote: Optional = if (quotes != null) Optional.of(quotes) else Optional.absent() @@ -973,7 +973,7 @@ open class Storage( val threadId = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) val expirationConfig = getExpirationConfiguration(threadId) val expiryMode = expirationConfig?.expiryMode - val expiresInMillis = (expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMillis = expiryMode?.expiryMillis ?: 0 val expireStartedAt = if (expiryMode is ExpiryMode.AfterSend) sentTimestamp else 0 val m = IncomingTextMessage(fromSerialized(senderPublicKey), 1, sentTimestamp, "", Optional.of(group), expiresInMillis, expireStartedAt, true, false) val updateData = UpdateMessageData.buildGroupUpdate(type, name, members)?.toJSON() @@ -991,7 +991,7 @@ open class Storage( val threadId = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) val expirationConfig = getExpirationConfiguration(threadId) val expiryMode = expirationConfig?.expiryMode - val expiresInMillis = (expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMillis = expiryMode?.expiryMillis ?: 0 val expireStartedAt = if (expiryMode is ExpiryMode.AfterSend) sentTimestamp else 0 val updateData = UpdateMessageData.buildGroupUpdate(type, name, members)?.toJSON() ?: "" val infoMessage = OutgoingGroupMediaMessage(recipient, updateData, groupID, null, sentTimestamp, expiresInMillis, expireStartedAt, true, null, listOf(), listOf()) @@ -1397,7 +1397,7 @@ open class Storage( val threadId = getThreadId(recipient) ?: return val expirationConfig = getExpirationConfiguration(threadId) val expiryMode = expirationConfig?.expiryMode - val expiresInMillis = (expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMillis = expiryMode?.expiryMillis ?: 0 val expireStartedAt = if (expiryMode is ExpiryMode.AfterSend) sentTimestamp else 0 val mediaMessage = IncomingMediaMessage( address, @@ -1551,7 +1551,7 @@ open class Storage( val threadId = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) val expirationConfig = getExpirationConfiguration(threadId) val expiryMode = expirationConfig?.expiryMode - val expiresInMillis = (expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMillis = expiryMode?.expiryMillis ?: 0 val expireStartedAt = if (expiryMode is ExpiryMode.AfterSend) sentTimestamp else 0 val callMessage = IncomingTextMessage.fromCallInfo(callMessageType, address, Optional.absent(), sentTimestamp, expiresInMillis, expireStartedAt) database.insertCallMessage(callMessage) diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/AndroidAutoReplyReceiver.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/AndroidAutoReplyReceiver.java index 9b24dc6feb..fb10a4bd73 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/AndroidAutoReplyReceiver.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/AndroidAutoReplyReceiver.java @@ -90,7 +90,7 @@ public class AndroidAutoReplyReceiver extends BroadcastReceiver { MessageSender.send(message, recipient.getAddress()); ExpirationConfiguration config = DatabaseComponent.get(context).storage().getExpirationConfiguration(threadId); ExpiryMode expiryMode = config == null ? null : config.getExpiryMode(); - long expiresInMillis = expiryMode == null ? 0 : expiryMode.getExpirySeconds() * 1000L; + long expiresInMillis = expiryMode == null ? 0 : expiryMode.getExpiryMillis(); long expireStartedAt = expiryMode instanceof ExpiryMode.AfterSend ? message.getSentTimestamp() : 0L; if (recipient.isGroupRecipient()) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java index 20a72afe0e..95ef513cd6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java @@ -99,7 +99,7 @@ public class RemoteReplyReceiver extends BroadcastReceiver { ExpirationConfiguration config = storage.getExpirationConfiguration(threadId); ExpiryMode expiryMode = config == null ? null : config.getExpiryMode(); - long expiresInMillis = expiryMode == null ? 0 : expiryMode.getExpirySeconds() * 1000L; + long expiresInMillis = expiryMode == null ? 0 : expiryMode.getExpiryMillis(); long expireStartedAt = expiryMode instanceof ExpiryMode.AfterSend ? message.getSentTimestamp() : 0L; switch (replyMethod) { case GroupMessage: { diff --git a/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt index 8e87c50395..af7295eed9 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt @@ -149,7 +149,7 @@ class DefaultConversationRepository @Inject constructor( openGroupInvitation.url = openGroup.joinURL message.openGroupInvitation = openGroupInvitation val expirationConfig = storage.getExpirationConfiguration(threadId) - val expiresInMillis = (expirationConfig?.expiryMode?.expirySeconds ?: 0) * 1000L + val expiresInMillis = (expirationConfig?.expiryMode?.expiryMillis ?: 0) val expireStartedAt = if (expirationConfig?.expiryMode is ExpiryMode.AfterSend) message.sentTimestamp!! else 0 val outgoingTextMessage = OutgoingTextMessage.fromOpenGroupInvitation( openGroupInvitation, diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.java b/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.java index 525f2f0dd1..662f7b8d4b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.java +++ b/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.java @@ -179,7 +179,7 @@ public class ExpiringMessageManager implements SSKEnvironment.MessageExpirationM } else { smsDatabase.markExpireStarted(messageRecord.getId(), expireStartedAt); } - scheduleDeletion(messageRecord.getId(), mms, expireStartedAt, (mode != null ? mode.getExpirySeconds() : 0) * 1000L); + scheduleDeletion(messageRecord.getId(), mms, expireStartedAt, (mode != null ? mode.getExpiryMillis() : 0)); } } diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt index d0e5011c3d..dded5d6524 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt @@ -9,4 +9,6 @@ sealed class ExpiryMode(val expirySeconds: Long) { data class AfterRead(private val seconds: Long): ExpiryMode(seconds) val duration get() = expirySeconds.seconds + + val expiryMillis get() = expirySeconds * 1000L } diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt index 646c744e38..013e8ff2ee 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt @@ -62,7 +62,7 @@ abstract class Message { expirationTimer = 0 return this } - if (config.isEnabled && config.expiryMode != null) { + if (config.isEnabled) { expirationTimer = config.expiryMode.expirySeconds.toInt() lastDisappearingMessageChangeTimestamp = config.updatedTimestampMs if (ExpirationConfiguration.isNewConfigEnabled) { diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index eb5f598777..f404c41973 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -256,10 +256,10 @@ object MessageSender { val address = if (isSyncMessage && message is VisibleMessage) message.syncTarget else message.recipient storage.getThreadId(Address.fromSerialized(address!!)) ?: return null } - val config = storage.getExpirationConfiguration(threadId) ?: return null + val config = storage.getExpirationConfiguration(threadId)?.takeIf { it.isEnabled } ?: return null val expiryMode = config.expiryMode - return if (config.isEnabled && (expiryMode is ExpiryMode.AfterSend || isSyncMessage)) { - (expiryMode?.expirySeconds ?: 0L) * 1000L + return if (expiryMode is ExpiryMode.AfterSend || isSyncMessage) { + expiryMode.expiryMillis } else null } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt index 4fd9d7427c..e5a9c32a06 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt @@ -303,12 +303,8 @@ fun MessageReceiver.updateExpiryIfNeeded( proto.lastDisappearingMessageChangeTimestamp ) - val (shouldUpdateConfig, configToUse) = - if (localConfig != null && localConfig.updatedTimestampMs > proto.lastDisappearingMessageChangeTimestamp) { - false to localConfig - } else { - true to remoteConfig - } + val configToUse = localConfig?.takeIf { it.updatedTimestampMs > proto.lastDisappearingMessageChangeTimestamp } ?: remoteConfig + val shouldUpdateConfig = configToUse == remoteConfig // don't update any values for open groups if (recipient.isOpenGroupRecipient && type != null) throw MessageReceiver.Error.InvalidMessage @@ -325,12 +321,12 @@ fun MessageReceiver.updateExpiryIfNeeded( // handle a delete after send expired fetch if (type == ExpirationType.DELETE_AFTER_SEND - && sentTime + configToUse.expiryMode.expirySeconds <= SnodeAPI.nowWithOffset) { + && sentTime + configToUse.expiryMode.expiryMillis <= SnodeAPI.nowWithOffset) { throw MessageReceiver.Error.ExpiredMessage } // handle a delete after read last known config value if (type == ExpirationType.DELETE_AFTER_READ - && sentTime + configToUse.expiryMode.expirySeconds <= storage.getLastSeen(threadID)) { + && sentTime + configToUse.expiryMode.expiryMillis <= storage.getLastSeen(threadID)) { throw MessageReceiver.Error.ExpiredMessage }