From 19083e2ce75b113406ea051c46d5af64c87d9bed Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Fri, 28 Jul 2023 11:39:12 +1000 Subject: [PATCH] refactor: use the shared config expiry mode types instead of custom protobufs so we can remove a variable and simplify checks across two variables --- .../ExpirationConfigurationDatabase.kt | 10 +++---- .../securesms/database/Storage.kt | 18 +++++++++---- .../notifications/RemoteReplyReceiver.java | 26 +++++++++++++++---- .../messages/ExpirationConfiguration.kt | 9 +++---- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ExpirationConfigurationDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/ExpirationConfigurationDatabase.kt index 8ba0a1f3f2..c47fbc6ea8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ExpirationConfigurationDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ExpirationConfigurationDatabase.kt @@ -4,7 +4,7 @@ import android.content.ContentValues import android.content.Context import android.database.Cursor import org.session.libsession.messaging.messages.ExpirationConfiguration -import org.session.libsession.messaging.messages.ExpirationDatabaseConfiguration +import org.session.libsession.messaging.messages.ExpirationDatabaseMetadata import org.session.libsession.utilities.GroupUtil.CLOSED_GROUP_PREFIX import org.session.libsession.utilities.GroupUtil.OPEN_GROUP_INBOX_PREFIX import org.session.libsession.utilities.GroupUtil.OPEN_GROUP_PREFIX @@ -43,19 +43,19 @@ class ExpirationConfigurationDatabase(context: Context, helper: SQLCipherOpenHel AND EXISTS (SELECT ${RecipientDatabase.EXPIRE_MESSAGES} FROM ${RecipientDatabase.TABLE_NAME} WHERE ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ADDRESS} = ${RecipientDatabase.TABLE_NAME}.${RecipientDatabase.ADDRESS} AND ${RecipientDatabase.EXPIRE_MESSAGES} > 0) """.trimIndent() - private fun readExpirationConfiguration(cursor: Cursor): ExpirationDatabaseConfiguration { - return ExpirationDatabaseConfiguration( + private fun readExpirationConfiguration(cursor: Cursor): ExpirationDatabaseMetadata { + return ExpirationDatabaseMetadata( threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID)), updatedTimestampMs = cursor.getLong(cursor.getColumnIndexOrThrow(UPDATED_TIMESTAMP_MS)) ) } } - fun getExpirationConfiguration(threadId: Long): ExpirationDatabaseConfiguration? { + fun getExpirationConfiguration(threadId: Long): ExpirationDatabaseMetadata? { val query = "$THREAD_ID = ?" val args = arrayOf("$threadId") - val configurations: MutableList = mutableListOf() + val configurations: MutableList = mutableListOf() readableDatabase.query(TABLE_NAME, null, query, args, null, null, null).use { cursor -> while (cursor.moveToNext()) { 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 74801bb1be..ad78cc96d6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -1672,21 +1672,29 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co override fun getExpirationConfiguration(threadId: Long): ExpirationConfiguration? { val recipient = getRecipientForThread(threadId) ?: return null + val dbExpirationMetadata = DatabaseComponent.get(context).expirationConfigurationDatabase().getExpirationConfiguration(threadId) ?: return null return if (recipient.isContactRecipient && recipient.address.serialize().startsWith(IdPrefix.STANDARD.value)) { // read it from contacts config if exists configFactory.contacts?.get(recipient.address.serialize())?.let { contact -> val mode = contact.expiryMode - ExpirationConfiguration(threadId, mode.expirySeconds,) - contact.expiryMode + ExpirationConfiguration( + threadId, + mode, + dbExpirationMetadata.updatedTimestampMs + ) } } else if (recipient.isClosedGroupRecipient) { // read it from group config if exists val groupPublicKey = GroupUtil.doubleDecodeGroupId(recipient.address.serialize()) - configFactory.userGroups?.getLegacyGroupInfo(groupPublicKey)?.let { - + configFactory.userGroups?.getLegacyGroupInfo(groupPublicKey)?.let { group -> + val expiry = group.disappearingTimer + ExpirationConfiguration( + threadId, + if (expiry != 0L) ExpiryMode.AfterRead(expiry) else ExpiryMode.NONE, + dbExpirationMetadata.updatedTimestampMs + ) } } else null - return DatabaseComponent.get(context).expirationConfigurationDatabase().getExpirationConfiguration(threadId) } override fun setExpirationConfiguration(config: ExpirationConfiguration) { 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 a7c34d14e1..2875631f7f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java @@ -38,16 +38,23 @@ import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType; import org.session.libsignal.utilities.Log; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo; +import org.thoughtcrime.securesms.database.MmsDatabase; +import org.thoughtcrime.securesms.database.SmsDatabase; +import org.thoughtcrime.securesms.database.Storage; import org.thoughtcrime.securesms.database.ThreadDatabase; -import org.thoughtcrime.securesms.dependencies.DatabaseComponent; import org.thoughtcrime.securesms.mms.MmsException; import java.util.Collections; import java.util.List; +import javax.inject.Inject; + +import dagger.hilt.android.AndroidEntryPoint; + /** * Get the response text from the Wearable Device and sends an message as a reply */ +@AndroidEntryPoint public class RemoteReplyReceiver extends BroadcastReceiver { public static final String TAG = RemoteReplyReceiver.class.getSimpleName(); @@ -55,6 +62,15 @@ public class RemoteReplyReceiver extends BroadcastReceiver { public static final String ADDRESS_EXTRA = "address"; public static final String REPLY_METHOD = "reply_method"; + @Inject + ThreadDatabase threadDatabase; + @Inject + MmsDatabase mmsDatabase; + @Inject + SmsDatabase smsDatabase; + @Inject + Storage storage; + @SuppressLint("StaticFieldLeak") @Override public void onReceive(final Context context, Intent intent) { @@ -76,19 +92,19 @@ public class RemoteReplyReceiver extends BroadcastReceiver { @Override protected Void doInBackground(Void... params) { Recipient recipient = Recipient.from(context, address, false); - ThreadDatabase threadDatabase = DatabaseComponent.get(context).threadDatabase(); long threadId = threadDatabase.getOrCreateThreadIdFor(recipient); VisibleMessage message = new VisibleMessage(); message.setSentTimestamp(SnodeAPI.getNowWithOffset()); message.setText(responseText.toString()); - ExpirationConfiguration config = DatabaseComponent.get(context).expirationConfigurationDatabase().getExpirationConfiguration(threadId); + ExpirationConfiguration config = storage.getExpirationConfiguration(threadId); + long expiresInMillis = config == null ? 0 : config.getDurationSeconds() * 1000L; long expireStartedAt = config.getExpirationType() == ExpirationType.DELETE_AFTER_SEND ? message.getSentTimestamp() : 0L; switch (replyMethod) { case GroupMessage: { OutgoingMediaMessage reply = OutgoingMediaMessage.from(message, recipient, Collections.emptyList(), null, null, expiresInMillis, 0); try { - DatabaseComponent.get(context).mmsDatabase().insertMessageOutbox(reply, threadId, false, null, true); + mmsDatabase.insertMessageOutbox(reply, threadId, false, null, true); MessageSender.send(message, address); } catch (MmsException e) { Log.w(TAG, e); @@ -97,7 +113,7 @@ public class RemoteReplyReceiver extends BroadcastReceiver { } case SecureMessage: { OutgoingTextMessage reply = OutgoingTextMessage.from(message, recipient, expiresInMillis, expireStartedAt); - DatabaseComponent.get(context).smsDatabase().insertMessageOutbox(threadId, reply, false, System.currentTimeMillis(), null, true); + smsDatabase.insertMessageOutbox(threadId, reply, false, System.currentTimeMillis(), null, true); MessageSender.send(message, address); break; } diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/ExpirationConfiguration.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/ExpirationConfiguration.kt index ea2baf6c59..e2b9e9d917 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/ExpirationConfiguration.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/ExpirationConfiguration.kt @@ -1,14 +1,13 @@ package org.session.libsession.messaging.messages -import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType +import network.loki.messenger.libsession_util.util.ExpiryMode data class ExpirationConfiguration( val threadId: Long = -1, - val durationSeconds: Int = 0, - val expirationType: ExpirationType? = null, + val expirationType: ExpiryMode? = null, val updatedTimestampMs: Long = 0 ) { - val isEnabled = durationSeconds > 0 && expirationType != null + val isEnabled = expirationType != null && expirationType.expirySeconds > 0 companion object { val isNewConfigEnabled = false /* TODO: System.currentTimeMillis() > 1_676_851_200_000 // 13/02/2023 */ @@ -16,7 +15,7 @@ data class ExpirationConfiguration( } } -data class ExpirationDatabaseConfiguration( +data class ExpirationDatabaseMetadata( val threadId: Long = -1, val updatedTimestampMs: Long ) \ No newline at end of file