From 7f4db64a5ad95ec35f6c9511e0187ff9aae23fd9 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Thu, 27 Jul 2023 17:40:59 +1000 Subject: [PATCH] refactor: refactor ExpirationConfigurationDatabase to return a specific db config after removing config values that are in shared lib --- .../expiration/ExpirationSettingsViewModel.kt | 4 ++-- .../ExpirationConfigurationDatabase.kt | 21 +++++++------------ .../messages/ExpirationConfiguration.kt | 9 ++++++-- 3 files changed, 16 insertions(+), 18 deletions(-) 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 e0c06054c8..a891abecc8 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 @@ -111,7 +111,7 @@ class ExpirationSettingsViewModel( if (typeValue == 0) { typeValue = ExpirationType.DELETE_AFTER_READ_VALUE } - val expiryType = typeValue.expiry() + val expiryType = typeValue.expiryType() val expirationTimer = _selectedExpirationTimer.value?.value?.toIntOrNull() ?: 0 val address = recipient.value?.address if (address == null || (expirationConfig?.typeRadioIndex() == typeValue && expirationConfig?.durationSeconds == expirationTimer)) { @@ -188,7 +188,7 @@ fun ExpirationConfiguration?.typeRadioIndex(): Int { return if (expirationType == ) } -fun Int.expiry(): ExpirationType? { +fun Int.expiryType(): ExpirationType? { if (this == -1) return null TODO() } \ No newline at end of file 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 3f8c73aba4..8ba0a1f3f2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ExpirationConfigurationDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ExpirationConfigurationDatabase.kt @@ -4,6 +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.utilities.GroupUtil.CLOSED_GROUP_PREFIX import org.session.libsession.utilities.GroupUtil.OPEN_GROUP_INBOX_PREFIX import org.session.libsession.utilities.GroupUtil.OPEN_GROUP_PREFIX @@ -14,23 +15,19 @@ class ExpirationConfigurationDatabase(context: Context, helper: SQLCipherOpenHel companion object { const val TABLE_NAME = "expiration_configuration" const val THREAD_ID = "thread_id" - const val DURATION_SECONDS = "duration_seconds" - const val EXPIRATION_TYPE = "expiration_type" const val UPDATED_TIMESTAMP_MS = "updated_timestamp_ms" @JvmField val CREATE_EXPIRATION_CONFIGURATION_TABLE_COMMAND = """ CREATE TABLE $TABLE_NAME ( $THREAD_ID INTEGER NOT NULL PRIMARY KEY ON CONFLICT REPLACE, - $DURATION_SECONDS INTEGER NOT NULL, - $EXPIRATION_TYPE INTEGER DEFAULT NULL, $UPDATED_TIMESTAMP_MS INTEGER DEFAULT NULL ) """.trimIndent() @JvmField val MIGRATE_GROUP_CONVERSATION_EXPIRY_TYPE_COMMAND = """ - INSERT INTO $TABLE_NAME ($THREAD_ID, $DURATION_SECONDS, $EXPIRATION_TYPE) SELECT ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ID}, ${RecipientDatabase.EXPIRE_MESSAGES}, 1 + INSERT INTO $TABLE_NAME ($THREAD_ID) SELECT ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ID}, ${RecipientDatabase.EXPIRE_MESSAGES}, 1 FROM ${ThreadDatabase.TABLE_NAME}, ${RecipientDatabase.TABLE_NAME} WHERE ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ADDRESS} LIKE '$CLOSED_GROUP_PREFIX%' 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) @@ -38,7 +35,7 @@ class ExpirationConfigurationDatabase(context: Context, helper: SQLCipherOpenHel @JvmField val MIGRATE_ONE_TO_ONE_CONVERSATION_EXPIRY_TYPE_COMMAND = """ - INSERT INTO $TABLE_NAME ($THREAD_ID, $DURATION_SECONDS, $EXPIRATION_TYPE) SELECT ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ID}, ${RecipientDatabase.EXPIRE_MESSAGES}, 2 + INSERT INTO $TABLE_NAME ($THREAD_ID) SELECT ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ID}, ${RecipientDatabase.EXPIRE_MESSAGES}, 2 FROM ${ThreadDatabase.TABLE_NAME}, ${RecipientDatabase.TABLE_NAME} WHERE ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ADDRESS} NOT LIKE '$CLOSED_GROUP_PREFIX%' AND ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ADDRESS} NOT LIKE '$OPEN_GROUP_PREFIX%' @@ -46,21 +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): ExpirationConfiguration { - return ExpirationConfiguration( + private fun readExpirationConfiguration(cursor: Cursor): ExpirationDatabaseConfiguration { + return ExpirationDatabaseConfiguration( threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID)), - durationSeconds = cursor.getInt(cursor.getColumnIndexOrThrow(DURATION_SECONDS)), - expirationTypeValue = cursor.getInt(cursor.getColumnIndexOrThrow(EXPIRATION_TYPE)), updatedTimestampMs = cursor.getLong(cursor.getColumnIndexOrThrow(UPDATED_TIMESTAMP_MS)) ) } } - fun getExpirationConfiguration(threadId: Long): ExpirationConfiguration? { + fun getExpirationConfiguration(threadId: Long): ExpirationDatabaseConfiguration? { 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()) { @@ -76,8 +71,6 @@ class ExpirationConfigurationDatabase(context: Context, helper: SQLCipherOpenHel try { val values = ContentValues().apply { put(THREAD_ID, configuration.threadId) - put(DURATION_SECONDS, configuration.durationSeconds) - put(EXPIRATION_TYPE, configuration.expirationTypeValue) put(UPDATED_TIMESTAMP_MS, configuration.updatedTimestampMs) } 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 0b5e81a7a0..ea2baf6c59 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 @@ -2,7 +2,7 @@ package org.session.libsession.messaging.messages import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType -class ExpirationConfiguration( +data class ExpirationConfiguration( val threadId: Long = -1, val durationSeconds: Int = 0, val expirationType: ExpirationType? = null, @@ -14,4 +14,9 @@ class ExpirationConfiguration( val isNewConfigEnabled = false /* TODO: System.currentTimeMillis() > 1_676_851_200_000 // 13/02/2023 */ const val LAST_READ_TEST = 1673587663000L } -} \ No newline at end of file +} + +data class ExpirationDatabaseConfiguration( + val threadId: Long = -1, + val updatedTimestampMs: Long +) \ No newline at end of file