From 304bf42c0dde225165df272aab344393f1711874 Mon Sep 17 00:00:00 2001 From: charles Date: Tue, 20 Dec 2022 17:27:26 +1100 Subject: [PATCH] Fix expiry info disappearance --- .../securesms/components/ConversationItemFooter.java | 4 ++-- .../expiration/ExpirationSettingsViewModel.kt | 5 +++++ .../conversation/v2/messages/VisibleMessageView.kt | 6 +++++- .../org/thoughtcrime/securesms/database/MmsDatabase.kt | 9 +++++++++ .../securesms/notifications/MarkReadReceiver.java | 4 ++-- .../securesms/service/ExpiringMessageManager.java | 10 ++++++---- .../messaging/sending_receiving/MessageSender.kt | 2 +- .../sending_receiving/ReceivedMessageHandler.kt | 2 +- .../org/session/libsession/utilities/SSKEnvironment.kt | 2 +- 9 files changed, 32 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/ConversationItemFooter.java b/app/src/main/java/org/thoughtcrime/securesms/components/ConversationItemFooter.java index 195c066d45..9d31053934 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/ConversationItemFooter.java +++ b/app/src/main/java/org/thoughtcrime/securesms/components/ConversationItemFooter.java @@ -117,8 +117,8 @@ public class ConversationItemFooter extends LinearLayout { long id = messageRecord.getId(); boolean mms = messageRecord.isMms(); - if (mms) DatabaseComponent.get(getContext()).mmsDatabase().markExpireStarted(id); - else DatabaseComponent.get(getContext()).smsDatabase().markExpireStarted(id); + if (mms) DatabaseComponent.get(getContext()).mmsDatabase().markExpireStarted(id, System.currentTimeMillis()); + else DatabaseComponent.get(getContext()).smsDatabase().markExpireStarted(id, System.currentTimeMillis()); expirationManager.scheduleDeletion(id, mms, messageRecord.getExpiresIn()); return null; diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt index 7d8cabc300..a0e42a3660 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt @@ -16,6 +16,7 @@ import org.session.libsession.messaging.messages.ExpirationConfiguration import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate import org.session.libsession.messaging.sending_receiving.MessageSender import org.session.libsession.utilities.SSKEnvironment.MessageExpirationManagerProtocol +import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.recipients.Recipient import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType import org.thoughtcrime.securesms.database.Storage @@ -26,6 +27,7 @@ class ExpirationSettingsViewModel( private val threadId: Long, private val afterReadOptions: List, private val afterSendOptions: List, + private val textSecurePreferences: TextSecurePreferences, private val messageExpirationManager: MessageExpirationManagerProtocol, private val threadDb: ThreadDatabase, private val storage: Storage @@ -110,6 +112,7 @@ class ExpirationSettingsViewModel( storage.setExpirationConfiguration(ExpirationConfiguration(threadId, expirationTimer, expiryType, expiryChangeTimestampMs)) val message = ExpirationTimerUpdate(expirationTimer) + message.sender = textSecurePreferences.getLocalNumber() message.recipient = address.serialize() message.sentTimestamp = expiryChangeTimestampMs messageExpirationManager.setExpirationTimer(message, expiryType) @@ -134,6 +137,7 @@ class ExpirationSettingsViewModel( @Assisted private val threadId: Long, @Assisted("afterRead") private val afterReadOptions: List, @Assisted("afterSend") private val afterSendOptions: List, + private val textSecurePreferences: TextSecurePreferences, private val messageExpirationManager: MessageExpirationManagerProtocol, private val threadDb: ThreadDatabase, private val storage: Storage @@ -144,6 +148,7 @@ class ExpirationSettingsViewModel( threadId, afterReadOptions, afterSendOptions, + textSecurePreferences, messageExpirationManager, threadDb, storage diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt index 0b4c1455fc..7162feb203 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt @@ -295,7 +295,11 @@ class VisibleMessageView : LinearLayout { val expirationManager = ApplicationContext.getInstance(context).expiringMessageManager val id = message.getId() val mms = message.isMms - if (mms) mmsDb.markExpireStarted(id) else smsDb.markExpireStarted(id) + if (mms) { + mmsDb.markExpireStarted(id, System.currentTimeMillis()) + } else { + smsDb.markExpireStarted(id, System.currentTimeMillis()) + } expirationManager.scheduleDeletion(id, mms, message.expiresIn) } } else { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt index ab81b3e9d1..32fe4d09db 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt @@ -19,6 +19,7 @@ package org.thoughtcrime.securesms.database import android.content.ContentValues import android.content.Context import android.database.Cursor +import android.provider.ContactsContract.CommonDataKinds.BaseTypes import com.annimon.stream.Stream import com.google.android.mms.pdu_alt.NotificationInd import com.google.android.mms.pdu_alt.PduHeaders @@ -645,6 +646,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa if (threadId == -1L) throw MmsException(e) } } + deleteExpirationTimerMessages(threadId) val contentValues = ContentValues() contentValues.put(DATE_SENT, retrieved.sentTimeMillis) contentValues.put(ADDRESS, retrieved.from.serialize()) @@ -737,6 +739,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa threadId = get(context).threadDatabase().getOrCreateThreadIdFor(retrieved.recipient) } } + deleteExpirationTimerMessages(threadId) val messageId = insertMessageOutbox(retrieved, threadId, false, null, serverTimestamp, runThreadUpdate) if (messageId == -1L) { return Optional.absent() @@ -1252,6 +1255,12 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa ) } + fun deleteExpirationTimerMessages(threadId: Long) { + val where = "$THREAD_ID = ? AND ($MESSAGE_BOX & ${MmsSmsColumns.Types.EXPIRATION_TIMER_UPDATE_BIT}) <> 0" + val updated = writableDatabase.delete(TABLE_NAME, where, arrayOf("$threadId")) + notifyConversationListeners(threadId) + } + object Status { const val DOWNLOAD_INITIALIZED = 1 const val DOWNLOAD_NO_CONNECTIVITY = 2 diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java index f1ec6d1887..e988fbe6c6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java @@ -95,8 +95,8 @@ public class MarkReadReceiver extends BroadcastReceiver { if (expirationInfo.getExpiresIn() > 0 && expirationInfo.getExpireStarted() <= 0) { ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager(); - if (expirationInfo.isMms()) DatabaseComponent.get(context).mmsDatabase().markExpireStarted(expirationInfo.getId()); - else DatabaseComponent.get(context).smsDatabase().markExpireStarted(expirationInfo.getId()); + if (expirationInfo.isMms()) DatabaseComponent.get(context).mmsDatabase().markExpireStarted(expirationInfo.getId(), System.currentTimeMillis()); + else DatabaseComponent.get(context).smsDatabase().markExpireStarted(expirationInfo.getId(), System.currentTimeMillis()); expirationManager.scheduleDeletion(expirationInfo.getId(), expirationInfo.isMms(), expirationInfo.getExpiresIn()); } 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 b76f94119e..6ee3ecfda2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.java +++ b/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.java @@ -81,7 +81,9 @@ public class ExpiringMessageManager implements SSKEnvironment.MessageExpirationM } else { insertIncomingExpirationTimerMessage(message, expireStartedAt); } - + if (message.getSentTimestamp() != null && senderPublicKey != null) { + startAnyExpiration(message.getSentTimestamp(), senderPublicKey, expireStartedAt); + } if (message.getId() != null) { DatabaseComponent.get(context).smsDatabase().deleteMessage(message.getId()); } @@ -148,16 +150,16 @@ public class ExpiringMessageManager implements SSKEnvironment.MessageExpirationM } @Override - public void startAnyExpiration(long timestamp, @NotNull String author) { + public void startAnyExpiration(long timestamp, @NotNull String author, long expireStartedAt) { MessageRecord messageRecord = DatabaseComponent.get(context).mmsSmsDatabase().getMessageFor(timestamp, author); if (messageRecord != null) { boolean mms = messageRecord.isMms(); ExpirationConfiguration config = DatabaseComponent.get(context).expirationConfigurationDatabase().getExpirationConfiguration(messageRecord.getThreadId()); if (config == null || !config.isEnabled()) return; if (mms) { - mmsDatabase.markExpireStarted(messageRecord.getId()); + mmsDatabase.markExpireStarted(messageRecord.getId(), expireStartedAt); } else { - smsDatabase.markExpireStarted(messageRecord.getId()); + smsDatabase.markExpireStarted(messageRecord.getId(), expireStartedAt); } scheduleDeletion(messageRecord.getId(), mms, config.getDurationSeconds() * 1000L); } 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 11099bf44a..3cb62c4bef 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 @@ -386,7 +386,7 @@ object MessageSender { storage.markUnidentified(message.sentTimestamp!!, userPublicKey) // Start the disappearing messages timer if needed if (message is VisibleMessage && !isSyncMessage) { - SSKEnvironment.shared.messageExpirationManager.startAnyExpiration(message.sentTimestamp!!, userPublicKey) + SSKEnvironment.shared.messageExpirationManager.startAnyExpiration(message.sentTimestamp!!, userPublicKey, System.currentTimeMillis()) } } ?: run { storage.updateReactionIfNeeded(message, message.sender?:userPublicKey, openGroupSentTimestamp) 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 8cc3a3ab85..675098ce1e 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 @@ -128,7 +128,7 @@ private fun MessageReceiver.handleSyncedExpiriesMessage(message: SyncedExpiriesM val config = storage.getExpirationConfiguration(storage.getOrCreateThreadIdFor(syncTarget)) ?: return@forEach syncedExpiries.forEach { syncedExpiry -> val startedAtMs = syncedExpiry.expirationTimestamp!! - config.durationSeconds * 1000 - SSKEnvironment.shared.messageExpirationManager.startAnyExpiration(startedAtMs, syncTarget) + SSKEnvironment.shared.messageExpirationManager.startAnyExpiration(startedAtMs, syncTarget, System.currentTimeMillis()) } } } diff --git a/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt b/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt index e3191fb177..4ddb81ca0f 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt @@ -37,7 +37,7 @@ class SSKEnvironment( interface MessageExpirationManagerProtocol { fun setExpirationTimer(message: ExpirationTimerUpdate, expiryType: Int) - fun startAnyExpiration(timestamp: Long, author: String) + fun startAnyExpiration(timestamp: Long, author: String, expireStartedAt: Long) } companion object {