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 78c33673b9..44c4d0ba46 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 @@ -242,7 +242,9 @@ val defaultTimes = listOf(12.hours, 1.days, 7.days, 14.days) val afterSendTimes = defaultTimes val afterSendModes = afterSendTimes.map { it.inWholeSeconds }.map(ExpiryMode::AfterSend) +val legacyModes = afterSendTimes.map { it.inWholeSeconds }.map(ExpiryMode::Legacy) fun afterSendOptions(state: State) = afterSendModes.map { timeOption(it, state) } +fun legacyOptions(state: State) = legacyModes.map { timeOption(it, state) } val afterReadTimes = buildList { add(5.minutes) @@ -252,16 +254,26 @@ val afterReadTimes = buildList { val afterReadModes = afterReadTimes.map { it.inWholeSeconds }.map(ExpiryMode::AfterRead) fun afterReadOptions(state: State) = afterReadModes.map { timeOption(it, state) } -private fun timeOptions(state: State): List? = - if (state.typeOptionsHidden) timeOptionsAfterSend(state) - else when (state.expiryMode) { - is ExpiryMode.Legacy, is ExpiryMode.AfterRead -> debugOptions(state) + afterReadOptions(state) - is ExpiryMode.AfterSend -> debugOptions(state) + afterSendOptions(state) - else -> null - } +private fun timeOptions( + state: State +): List? { + val type = state.takeUnless { + it.typeOptionsHidden + }?.expiryType ?: if (state.isNewConfigEnabled) ExpiryType.AFTER_SEND else ExpiryType.LEGACY -private fun timeOptionsAfterSend(state: State): List = - listOf(offTypeOption(state)) + debugOptions(state) + afterSendOptions(state) + return when (type) { + ExpiryType.AFTER_READ -> afterReadOptions(state) + ExpiryType.AFTER_SEND -> afterSendOptions(state) + ExpiryType.LEGACY -> legacyOptions(state) + else -> null + }?.let { + buildList { + if (state.typeOptionsHidden) add(offTypeOption(state)) + addAll(debugOptions(state)) + addAll(it) + } + } +} fun timeOption( mode: ExpiryMode, diff --git a/app/src/test/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModelTest.kt b/app/src/test/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModelTest.kt index 8c7bfb938f..4c38f5c917 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModelTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModelTest.kt @@ -13,12 +13,16 @@ import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito import org.mockito.junit.MockitoJUnitRunner +import org.mockito.kotlin.any import org.mockito.kotlin.whenever import org.session.libsession.messaging.messages.ExpirationConfiguration import org.session.libsession.utilities.Address +import org.session.libsession.utilities.GroupRecord +import org.session.libsession.utilities.GroupUtil import org.session.libsession.utilities.SSKEnvironment import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.recipients.Recipient +import org.session.libsignal.utilities.guava.Optional import org.thoughtcrime.securesms.MainCoroutineRule import org.thoughtcrime.securesms.database.GroupDatabase import org.thoughtcrime.securesms.database.Storage @@ -32,6 +36,8 @@ import kotlin.time.Duration.Companion.minutes private const val THREAD_ID = 1L private const val LOCAL_NUMBER = "05---local---address" private val LOCAL_ADDRESS = Address.fromSerialized(LOCAL_NUMBER) +private const val GROUP_NUMBER = "${GroupUtil.OPEN_GROUP_PREFIX}4133" +private val GROUP_ADDRESS = Address.fromSerialized(GROUP_NUMBER) @OptIn(ExperimentalCoroutinesApi::class) @RunWith(MockitoJUnitRunner::class) @@ -48,6 +54,7 @@ class ExpirationSettingsViewModelTest { @Mock lateinit var groupDb: GroupDatabase @Mock lateinit var storage: Storage @Mock lateinit var recipient: Recipient + @Mock lateinit var groupRecord: GroupRecord @Test fun `note to self, off, new config`() = runTest { @@ -120,10 +127,50 @@ class ExpirationSettingsViewModelTest { CardModel( R.string.activity_expiration_settings_timer, typeOption(ExpiryMode.NONE, selected = true), - timeOption(ExpiryType.AFTER_SEND, 12.hours), - timeOption(ExpiryType.AFTER_SEND, 1.days), - timeOption(ExpiryType.AFTER_SEND, 7.days), - timeOption(ExpiryType.AFTER_SEND, 14.days) + timeOption(ExpiryType.LEGACY, 12.hours), + timeOption(ExpiryType.LEGACY, 1.days), + timeOption(ExpiryType.LEGACY, 7.days), + timeOption(ExpiryType.LEGACY, 14.days) + ) + ) + ) + } + + @Test + fun `group, off, not admin, new config`() = runTest { + mockGroup(ExpiryMode.NONE) + + val viewModel = createViewModel() + + advanceUntilIdle() + + assertThat( + viewModel.state.value + ).isEqualTo( + State( + isGroup = true, + isSelfAdmin = false, + address = GROUP_ADDRESS, + isNoteToSelf = false, + expiryMode = ExpiryMode.NONE, + isNewConfigEnabled = true, + persistedMode = ExpiryMode.NONE, + showDebugOptions = false + ) + ) + + assertThat( + viewModel.uiState.value + ).isEqualTo( + UiState( + showGroupFooter = true, + CardModel( + R.string.activity_expiration_settings_timer, + typeOption(ExpiryMode.NONE, enabled = false, selected = true), + timeOption(ExpiryType.AFTER_SEND, 12.hours, enabled = false), + timeOption(ExpiryType.AFTER_SEND, 1.days, enabled = false), + timeOption(ExpiryType.AFTER_SEND, 7.days, enabled = false), + timeOption(ExpiryType.AFTER_SEND, 14.days, enabled = false) ) ) ) @@ -254,10 +301,10 @@ class ExpirationSettingsViewModelTest { ), CardModel( GetString(R.string.activity_expiration_settings_timer), - timeOption(ExpiryType.AFTER_SEND, 12.hours, selected = true), - timeOption(ExpiryType.AFTER_SEND, 1.days), - timeOption(ExpiryType.AFTER_SEND, 7.days), - timeOption(ExpiryType.AFTER_SEND, 14.days) + timeOption(ExpiryType.LEGACY, 12.hours, selected = true), + timeOption(ExpiryType.LEGACY, 1.days), + timeOption(ExpiryType.LEGACY, 7.days), + timeOption(ExpiryType.LEGACY, 14.days) ) ) ) @@ -435,7 +482,7 @@ class ExpirationSettingsViewModelTest { mock1on1(ExpiryType.AFTER_SEND.mode(time), someAddress) } - private fun mock1on1(mode: ExpiryMode, someAddress: Address, new: Boolean = true) { + private fun mock1on1(mode: ExpiryMode, someAddress: Address) { val config = config(mode) whenever(threadDb.getRecipientForThreadId(Mockito.anyLong())).thenReturn(recipient) @@ -445,6 +492,19 @@ class ExpirationSettingsViewModelTest { whenever(recipient.address).thenReturn(someAddress) } + private fun mockGroup(mode: ExpiryMode) { + val config = config(mode) + + whenever(threadDb.getRecipientForThreadId(Mockito.anyLong())).thenReturn(recipient) + whenever(storage.getExpirationConfiguration(Mockito.anyLong())).thenReturn(config) + whenever(textSecurePreferences.getLocalNumber()).thenReturn(LOCAL_NUMBER) + whenever(recipient.isClosedGroupRecipient).thenReturn(false) + whenever(recipient.address).thenReturn(GROUP_ADDRESS) + whenever(recipient.isClosedGroupRecipient).thenReturn(true) + whenever(groupDb.getGroup(any())).thenReturn(Optional.of(groupRecord)) + whenever(groupRecord.admins).thenReturn(listOf()) + } + private fun config(mode: ExpiryMode) = ExpirationConfiguration( threadId = THREAD_ID, expiryMode = mode,