From 036fd4e8e286c8ca8852be098b5ce4f7e9442e93 Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Tue, 1 Oct 2024 15:31:33 +1000 Subject: [PATCH] Multi message handling Sharing admin logic --- .../conversation/v2/ConversationActivityV2.kt | 6 ++ .../conversation/v2/ConversationAdapter.kt | 1 + .../conversation/v2/ConversationViewModel.kt | 68 ++++++++++--------- .../menus/ConversationActionModeCallback.kt | 3 + 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 483a445ead..255ea08a0b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -649,6 +649,12 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe recyclerScrollState = newState } }) + + lifecycleScope.launch { + viewModel.isAdmin.collect{ + adapter.isAdmin = it + } + } } private fun scrollToMostRecentMessageIfWeShould() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt index 880dacb070..83e7dcfa92 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt @@ -44,6 +44,7 @@ class ConversationAdapter( private val messageDB by lazy { DatabaseComponent.get(context).mmsSmsDatabase() } private val contactDB by lazy { DatabaseComponent.get(context).sessionContactDatabase() } var selectedItems = mutableSetOf() + var isAdmin: Boolean = false private var searchQuery: String? = null var visibleMessageViewDelegate: VisibleMessageViewDelegate? = null diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index 5c240445ca..ea984af37b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -65,8 +65,41 @@ class ConversationViewModel( private val _dialogsState = MutableStateFlow(DialogsState()) val dialogsState: StateFlow = _dialogsState + private val _isAdmin = MutableStateFlow(false) + val isAdmin: StateFlow = _isAdmin + private var _recipient: RetrieveOnce = RetrieveOnce { - repository.maybeGetRecipientForThreadId(threadId) + val conversation = repository.maybeGetRecipientForThreadId(threadId) + + // set admin from current conversation + val conversationType = conversation?.getType() + // Determining is the current user is an admin will depend on the kind of conversation we are in + _isAdmin.value = when(conversationType) { + // for Groups V2 + MessageType.GROUPS_V2 -> { + //todo GROUPS V2 add logic where code is commented to determine if user is an admin + false // FANCHAO - properly set up admin for groups v2 here + } + + // for legacy groups, check if the user created the group + MessageType.LEGACY_GROUP -> { + // for legacy groups, we check if the current user is the one who created the group + run { + val localUserAddress = + textSecurePreferences.getLocalNumber() ?: return@run false + val group = storage.getGroup(conversation.address.toGroupString()) + group?.admins?.contains(fromSerialized(localUserAddress)) ?: false + } + } + + // for communities the the `isUserModerator` field + MessageType.COMMUNITY -> isUserCommunityManager() + + // false in other cases + else -> false + } + + conversation } val expirationConfiguration: ExpirationConfiguration? get() = storage.getExpirationConfiguration(threadId) @@ -206,9 +239,7 @@ class ConversationViewModel( //todo DELETION handle multi select scenarios - //todo DELETION check that the unread status works as expected when deleting a message - - //todo DELETION handle deleting messages not fully sent yet (failed or sending states) + //todo DELETION reactions are still visible on "marked as deleted" messages viewModelScope.launch(Dispatchers.IO) { val allSentByCurrentUser = messages.all { it.isOutgoing } @@ -222,31 +253,6 @@ class ConversationViewModel( it.isMms ) != null } - // Determining is the current user is an admin will depend on the kind of conversation we are in - val isAdmin = when(conversationType) { - // for Groups V2 - MessageType.GROUPS_V2 -> { - //todo GROUPS V2 add logic where code is commented to determine if user is an admin - false // FANCHAO - properly set up admin for groups v2 here - } - - // for legacy groups, check if the user created the group - MessageType.LEGACY_GROUP -> { - // for legacy groups, we check if the current user is the one who created the group - run { - val localUserAddress = - textSecurePreferences.getLocalNumber() ?: return@run false - val group = storage.getGroup(conversation.address.toGroupString()) - group?.admins?.contains(fromSerialized(localUserAddress)) ?: false - } - } - - // for communities the the `isUserModerator` field - MessageType.COMMUNITY -> isUserCommunityManager() - - // false in other cases - else -> false - } // There are three types of dialogs for deletion: // 1- Delete on device only OR all devices - Used for Note to self @@ -266,12 +272,12 @@ class ConversationViewModel( } // If the user is an admin or is interacting with their own message And are allowed to delete for everyone - (isAdmin || allSentByCurrentUser) && canDeleteForEveryone -> { + (isAdmin.value || allSentByCurrentUser) && canDeleteForEveryone -> { _dialogsState.update { it.copy( deleteEveryone = DeleteForEveryoneDialogData( messages = messages, - defaultToEveryone = isAdmin, + defaultToEveryone = isAdmin.value, messageType = when{ conversation.isLocalNumber -> DeleteForEveryoneMessageType.NoteToSelf conversation.isCommunityRecipient -> DeleteForEveryoneMessageType.Community diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationActionModeCallback.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationActionModeCallback.kt index 29956e3db9..abadc06335 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationActionModeCallback.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationActionModeCallback.kt @@ -44,6 +44,9 @@ class ConversationActionModeCallback(private val adapter: ConversationAdapter, p // Embedded function fun userCanDeleteSelectedItems(): Boolean { + // admin can delete all combinations + if(adapter.isAdmin) return true + val allSentByCurrentUser = selectedItems.all { it.isOutgoing } val allReceivedByCurrentUser = selectedItems.all { !it.isOutgoing } if (openGroup == null) { return allSentByCurrentUser || allReceivedByCurrentUser }