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 ed5c4f2ad6..157e50eb22 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 @@ -147,12 +147,15 @@ class ExpirationSettingsViewModel( override fun onSetClick() = viewModelScope.launch { val state = _state.value - val mode = (state.expiryMode ?: ExpiryMode.NONE).let { - if (it is ExpiryMode.Legacy) ExpiryMode.AfterRead(it.expirySeconds) - else it + val mode = state.expiryMode.let { + when { + it !is ExpiryMode.Legacy -> it + state.isGroup -> ExpiryMode.AfterSend(it.expirySeconds) + else -> ExpiryMode.AfterRead(it.expirySeconds) + } ?: ExpiryMode.NONE } val address = state.address - if (address == null || expirationConfig?.expiryMode == mode) { + if (address == null) { _event.send(Event.FAIL) return@launch } 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 0419b3c384..c357464d79 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -645,7 +645,7 @@ open class Storage( } else { val mode = if (group.disappearingTimer == 0L) ExpiryMode.NONE - else ExpiryMode.AfterRead(group.disappearingTimer) + else ExpiryMode.AfterSend(group.disappearingTimer) val newConfig = ExpirationConfiguration( conversationThreadId, mode, messageTimestamp ) @@ -1698,36 +1698,21 @@ open class Storage( 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.isLocalNumber) { - configFactory.user?.getNtsExpiry()?.let { mode -> - ExpirationConfiguration( - threadId, - mode, - dbExpirationMetadata.updatedTimestampMs - ) + return when { + recipient.isLocalNumber -> configFactory.user?.getNtsExpiry() + recipient.isContactRecipient -> { + // read it from contacts config if exists + recipient.address.serialize().takeIf { it.startsWith(IdPrefix.STANDARD.value) } + ?.let { configFactory.contacts?.get(it)?.expiryMode } } - } else 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, - dbExpirationMetadata.updatedTimestampMs - ) + recipient.isClosedGroupRecipient -> { + // read it from group config if exists + GroupUtil.doubleDecodeGroupId(recipient.address.serialize()) + .let { configFactory.userGroups?.getLegacyGroupInfo(it) } + ?.run { disappearingTimer.takeIf { it != 0L }?.let(ExpiryMode::AfterSend) ?: ExpiryMode.NONE } } - } else if (recipient.isClosedGroupRecipient) { - // read it from group config if exists - val groupPublicKey = GroupUtil.doubleDecodeGroupId(recipient.address.serialize()) - 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 + else -> null + }?.let { ExpirationConfiguration(threadId, it, dbExpirationMetadata.updatedTimestampMs) } } override fun setExpirationConfiguration(config: ExpirationConfiguration) { 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 f93a97dd58..d0e5011c3d 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 @@ -4,9 +4,9 @@ import kotlin.time.Duration.Companion.seconds sealed class ExpiryMode(val expirySeconds: Long) { object NONE: ExpiryMode(0) - class Legacy(seconds: Long): ExpiryMode(seconds) // after read - class AfterSend(seconds: Long): ExpiryMode(seconds) - class AfterRead(seconds: Long): ExpiryMode(seconds) + data class Legacy(private val seconds: Long): ExpiryMode(seconds) // after read + data class AfterSend(private val seconds: Long): ExpiryMode(seconds) + data class AfterRead(private val seconds: Long): ExpiryMode(seconds) val duration get() = expirySeconds.seconds }