Only allow admins to change disappearing messages config

This commit is contained in:
charles 2022-12-22 18:20:13 +11:00
parent 1f0a0760ba
commit cdbf76c0f1
3 changed files with 19 additions and 5 deletions

View File

@ -168,18 +168,21 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() {
RadioOption( RadioOption(
value = "-1", value = "-1",
title = getString(R.string.expiration_off), title = getString(R.string.expiration_off),
enabled = viewModel.uiState.value.isSelfAdmin,
contentDescription = getString(R.string.AccessibilityId_disable_disappearing_messages) contentDescription = getString(R.string.AccessibilityId_disable_disappearing_messages)
), ),
RadioOption( RadioOption(
value = ExpirationType.DELETE_AFTER_READ_VALUE.toString(), value = ExpirationType.DELETE_AFTER_READ_VALUE.toString(),
title = getString(R.string.expiration_type_disappear_after_read), title = getString(R.string.expiration_type_disappear_after_read),
subtitle = getString(R.string.expiration_type_disappear_after_read_description), subtitle = getString(R.string.expiration_type_disappear_after_read_description),
enabled = viewModel.uiState.value.isSelfAdmin,
contentDescription = getString(R.string.AccessibilityId_disappear_after_read_option) contentDescription = getString(R.string.AccessibilityId_disappear_after_read_option)
), ),
RadioOption( RadioOption(
value = ExpirationType.DELETE_AFTER_SEND_VALUE.toString(), value = ExpirationType.DELETE_AFTER_SEND_VALUE.toString(),
title = getString(R.string.expiration_type_disappear_after_send), title = getString(R.string.expiration_type_disappear_after_send),
subtitle = getString(R.string.expiration_type_disappear_after_send_description), subtitle = getString(R.string.expiration_type_disappear_after_send_description),
enabled = viewModel.uiState.value.isSelfAdmin,
contentDescription = getString(R.string.AccessibilityId_disappear_after_send_option) contentDescription = getString(R.string.AccessibilityId_disappear_after_send_option)
) )
) )

View File

@ -20,6 +20,7 @@ import org.session.libsession.utilities.SSKEnvironment.MessageExpirationManagerP
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType
import org.thoughtcrime.securesms.database.GroupDatabase
import org.thoughtcrime.securesms.database.Storage import org.thoughtcrime.securesms.database.Storage
import org.thoughtcrime.securesms.database.ThreadDatabase import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.preferences.RadioOption import org.thoughtcrime.securesms.preferences.RadioOption
@ -31,6 +32,7 @@ class ExpirationSettingsViewModel(
private val textSecurePreferences: TextSecurePreferences, private val textSecurePreferences: TextSecurePreferences,
private val messageExpirationManager: MessageExpirationManagerProtocol, private val messageExpirationManager: MessageExpirationManagerProtocol,
private val threadDb: ThreadDatabase, private val threadDb: ThreadDatabase,
private val groupDb: GroupDatabase,
private val storage: Storage private val storage: Storage
) : ViewModel() { ) : ViewModel() {
@ -56,8 +58,14 @@ class ExpirationSettingsViewModel(
expirationConfig = storage.getExpirationConfiguration(threadId) expirationConfig = storage.getExpirationConfiguration(threadId)
val recipient = threadDb.getRecipientForThreadId(threadId) val recipient = threadDb.getRecipientForThreadId(threadId)
_recipient.value = recipient _recipient.value = recipient
_uiState.update { val groupInfo = if (recipient?.isClosedGroupRecipient == true) {
it.copy(showExpirationTypeSelector = !ExpirationConfiguration.isNewConfigEnabled || (recipient?.isContactRecipient == true && !recipient.isLocalNumber)) groupDb.getGroup(recipient.address.toGroupString()).orNull()
} else null
_uiState.update { currentUiState ->
currentUiState.copy(
isSelfAdmin = groupInfo?.admins?.any{ it.serialize() == textSecurePreferences.getLocalNumber() } == true,
showExpirationTypeSelector = !ExpirationConfiguration.isNewConfigEnabled || (recipient?.isContactRecipient == true && !recipient.isLocalNumber)
)
} }
_selectedExpirationType.value = if (ExpirationConfiguration.isNewConfigEnabled) { _selectedExpirationType.value = if (ExpirationConfiguration.isNewConfigEnabled) {
if (recipient?.isLocalNumber == true || recipient?.isClosedGroupRecipient == true) { if (recipient?.isLocalNumber == true || recipient?.isClosedGroupRecipient == true) {
@ -82,9 +90,9 @@ class ExpirationSettingsViewModel(
} }
}.onEach { options -> }.onEach { options ->
_expirationTimerOptions.value = if (ExpirationConfiguration.isNewConfigEnabled && (recipient.value?.isLocalNumber == true || recipient.value?.isClosedGroupRecipient == true)) { _expirationTimerOptions.value = if (ExpirationConfiguration.isNewConfigEnabled && (recipient.value?.isLocalNumber == true || recipient.value?.isClosedGroupRecipient == true)) {
options options.map { it.copy(enabled = _uiState.value.isSelfAdmin) }
} else { } else {
options.slice(1 until options.size) options.slice(1 until options.size).map { it.copy(enabled = _uiState.value.isSelfAdmin) }
} }
}.launchIn(viewModelScope) }.launchIn(viewModelScope)
} }
@ -141,6 +149,7 @@ class ExpirationSettingsViewModel(
private val textSecurePreferences: TextSecurePreferences, private val textSecurePreferences: TextSecurePreferences,
private val messageExpirationManager: MessageExpirationManagerProtocol, private val messageExpirationManager: MessageExpirationManagerProtocol,
private val threadDb: ThreadDatabase, private val threadDb: ThreadDatabase,
private val groupDb: GroupDatabase,
private val storage: Storage private val storage: Storage
) : ViewModelProvider.Factory { ) : ViewModelProvider.Factory {
@ -152,6 +161,7 @@ class ExpirationSettingsViewModel(
textSecurePreferences, textSecurePreferences,
messageExpirationManager, messageExpirationManager,
threadDb, threadDb,
groupDb,
storage storage
) as T ) as T
} }
@ -159,6 +169,7 @@ class ExpirationSettingsViewModel(
} }
data class ExpirationSettingsUiState( data class ExpirationSettingsUiState(
val isSelfAdmin: Boolean = false,
val showExpirationTypeSelector: Boolean = false, val showExpirationTypeSelector: Boolean = false,
val settingsSaved: Boolean? = null val settingsSaved: Boolean? = null
) )

View File

@ -12,6 +12,6 @@ class ExpirationConfiguration(
val expirationType: ExpirationType? = ExpirationType.valueOf(expirationTypeValue) val expirationType: ExpirationType? = ExpirationType.valueOf(expirationTypeValue)
companion object { companion object {
val isNewConfigEnabled = true /* TODO: System.currentTimeMillis() > 1_674_000_000_000 // 18/01/2023 */ val isNewConfigEnabled = true /* TODO: System.currentTimeMillis() > 1_676_851_200_000 // 13/02/2023 */
} }
} }