From cd9eb66ebb1e604633cbb05484810227b985af7c Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 9 Jan 2025 22:28:29 +0000 Subject: [PATCH] ui: remove support for inline moderation (#5495) * android: remove support for inline moderation * ios: emove support for inline moderation * fix prefix on preview for ios * unused * final pass * ios: should not be able to assign moderator * button label --------- Co-authored-by: Evgeny Poberezkin --- apps/ios/Shared/Views/Chat/ChatView.swift | 205 ++------------- .../Chat/Group/GroupMemberInfoView.swift | 32 +-- .../Views/ChatList/ChatPreviewView.swift | 2 +- apps/ios/SimpleXChat/ChatTypes.swift | 13 +- .../chat/simplex/common/model/ChatModel.kt | 13 - .../simplex/common/views/chat/ChatView.kt | 72 +++--- .../views/chat/group/GroupMemberInfoView.kt | 16 +- .../common/views/chat/item/ChatItemView.kt | 238 ++---------------- .../commonMain/resources/MR/base/strings.xml | 9 +- 9 files changed, 106 insertions(+), 494 deletions(-) diff --git a/apps/ios/Shared/Views/Chat/ChatView.swift b/apps/ios/Shared/Views/Chat/ChatView.swift index b74cbfbc81..3444fd0723 100644 --- a/apps/ios/Shared/Views/Chat/ChatView.swift +++ b/apps/ios/Shared/Views/Chat/ChatView.swift @@ -1284,20 +1284,11 @@ struct ChatView: View { @ViewBuilder private func menu(_ ci: ChatItem, _ range: ClosedRange?, live: Bool) -> some View { - if let groupInfo = chat.chatInfo.groupInfo, ci.isReport, ci.meta.itemDeleted == nil { - if ci.chatDir == .groupSnd { - deleteButton(ci) - } else { + if case let .group(gInfo) = chat.chatInfo, ci.isReport, ci.meta.itemDeleted == nil { + if ci.chatDir != .groupSnd, gInfo.membership.memberRole >= .moderator { archiveReportButton(ci) - if let qi = ci.quotedItem { - moderateReportedButton(qi, ci, groupInfo) - if let rMember = qi.memberToModerate(chat.chatInfo) { - if !rMember.blockedByAdmin, rMember.canBlockForAll(groupInfo: groupInfo) { - blockMemberButton(rMember, groupInfo, qi, ci) - } - } - } } + deleteButton(ci, label: "Delete report") } else if let mc = ci.content.msgContent, !ci.isReport, ci.meta.itemDeleted == nil || revealed { if chat.chatInfo.featureEnabled(.reactions) && ci.allowAddReaction, availableReactions.count > 0 { @@ -1351,7 +1342,7 @@ struct ChatView: View { if ci.chatDir != .groupSnd { if let (groupInfo, _) = ci.memberToModerate(chat.chatInfo) { moderateButton(ci, groupInfo) - } else if ci.meta.itemDeleted == nil, case let .group(gInfo) = chat.chatInfo, gInfo.membership.memberRole < .moderator, !live, composeState.voiceMessageRecordingState == .noRecording { + } else if ci.meta.itemDeleted == nil, case let .group(gInfo) = chat.chatInfo, gInfo.membership.memberRole == .member, !live, composeState.voiceMessageRecordingState == .noRecording { reportButton(ci) } } @@ -1627,7 +1618,7 @@ struct ChatView: View { } } - private func deleteButton(_ ci: ChatItem) -> Button { + private func deleteButton(_ ci: ChatItem, label: LocalizedStringKey = "Delete") -> Button { Button(role: .destructive) { if !revealed, let currIndex = m.getChatItemIndex(ci), @@ -1649,10 +1640,7 @@ struct ChatView: View { deletingItem = ci } } label: { - Label( - NSLocalizedString("Delete", comment: "chat item action"), - systemImage: "trash" - ) + Label(label, systemImage: "trash") } } @@ -1668,31 +1656,19 @@ struct ChatView: View { private func moderateButton(_ ci: ChatItem, _ groupInfo: GroupInfo) -> Button { Button(role: .destructive) { - showModerateMessageAlert(groupInfo) { - deletingItem = ci - deleteMessage(.cidmBroadcast, moderate: true) - } - } label: { - Label( - NSLocalizedString("Moderate", comment: "chat item action"), - systemImage: "flag" - ) - } - } - - private func moderateReportedButton(_ rItem: CIQuote, _ reportItem: ChatItem, _ groupInfo: GroupInfo) -> Button { - Button(role: .destructive) { - showModerateMessageAlert(groupInfo) { - Task { - let deleted = await deleteReportedMessage(rItem, reportItem.id, groupInfo) - if deleted != nil { - await MainActor.run { - deletingItem = reportItem - deleteMessage(.cidmInternalMark, moderate: false) - } - } - } - } + AlertManager.shared.showAlert(Alert( + title: Text("Delete member message?"), + message: Text( + groupInfo.fullGroupPreferences.fullDelete.on + ? "The message will be deleted for all members." + : "The message will be marked as moderated for all members." + ), + primaryButton: .destructive(Text("Delete")) { + deletingItem = ci + deleteMessage(.cidmBroadcast, moderate: true) + }, + secondaryButton: .cancel() + )) } label: { Label( NSLocalizedString("Moderate", comment: "chat item action"), @@ -1715,74 +1691,7 @@ struct ChatView: View { ) ) } label: { - Label( - NSLocalizedString("Archive", comment: "chat item action"), - systemImage: "archivebox" - ) - } - } - - private func blockMemberButton(_ member: GroupMember, _ groupInfo: GroupInfo, _ rItem: CIQuote, _ report: ChatItem) -> Button { - Button(role: .destructive) { - actionSheet = SomeActionSheet( - actionSheet: ActionSheet( - title: Text("Block and moderate?"), - buttons: [ - .destructive(Text("Block and moderate")) { - AlertManager.shared.showAlert( - Alert( - title: Text("Delete member message and block?"), - message: Text( - NSLocalizedString( - groupInfo.fullGroupPreferences.fullDelete.on - ? "The message will be deleted for all members.\nAll new messages from \(member.chatViewName) will be hidden!" - : "The message will be marked as moderated for all members.\n All new messages from \(member.chatViewName) will be hidden!" - , comment: "block and moderate action" - ) - ), - primaryButton: .destructive(Text("Delete and block")) { - Task { - let deleted = await deleteReportedMessage(rItem, report.id, groupInfo) - if deleted != nil { - let blocked = await blockMemberForAll(groupInfo, member, true) - - if blocked != nil { - await MainActor.run { - deletingItem = report - deleteMessage(.cidmInternalMark, moderate: false) - } - } - } - } - }, - secondaryButton: .cancel() - ) - ) - }, - .destructive(Text("Only block")) { - Task { - if (await getLocalIdForReportedMessage(rItem, report.id, groupInfo)) != nil { - AlertManager.shared.showAlert( - blockForAllAlert(groupInfo, member) { - deletingItem = report - deleteMessage(.cidmInternalMark, moderate: false) - } - ) - } else { - showNoMessageMessageAlert() - } - } - }, - .cancel() - ] - ), - id: "blockMember" - ) - } label: { - Label( - NSLocalizedString("Block member", comment: "chat item action"), - systemImage: "hand.raised" - ) + Label("Archive report", systemImage: "archivebox") } } @@ -1886,60 +1795,6 @@ struct ChatView: View { itemIds.forEach { selectedChatItems?.remove($0) } } } - - private func deleteReportedMessage(_ rItem: CIQuote, _ reportId: Int64, _ groupInfo: GroupInfo) async -> ChatItemDeletion? { - do { - let itemId = await getLocalIdForReportedMessage(rItem, reportId, groupInfo) - - if let itemId = itemId { - let deletedItem = try await apiDeleteMemberChatItems( - groupId: groupInfo.apiId, - itemIds: [itemId] - ).first - - if let di = deletedItem { - await MainActor.run { - if let toItem = di.toChatItem { - _ = m.upsertChatItem(chat.chatInfo, toItem.chatItem) - } else { - m.removeChatItem(chat.chatInfo, di.deletedChatItem.chatItem) - } - } - - return di - } - } else { - showNoMessageMessageAlert() - } - } catch { - logger.error("ChatView.deleteReportedMessage error: \(error)") - AlertManager.shared.showAlertMsg(title: LocalizedStringKey("Error"), message: LocalizedStringKey("Failed to delete reported message")) - } - - return nil - } - - private func getLocalIdForReportedMessage(_ rItem: CIQuote, _ reportId: Int64, _ groupInfo: GroupInfo) async -> Int64? { - do { - if let itemId = rItem.itemId { - return itemId - } else { - let reportItem = try await apiGetChatItems( - type: chat.chatInfo.chatType, - id: chat.chatInfo.apiId, - pagination: .around(chatItemId: reportId, count: 0) - ).first - - if let itemId = reportItem?.quotedItem?.itemId { - return itemId - } - } - } catch { - logger.error("ChatView.getLocalIdForReportedMessage error: \(error)") - } - - return nil - } private func deleteMessage(_ mode: CIDeleteMode, moderate: Bool) { logger.debug("ChatView deleteMessage") @@ -2014,26 +1869,6 @@ struct ChatView: View { } } -private func showModerateMessageAlert(_ groupInfo: GroupInfo, _ onModerate: @escaping () -> Void) { - AlertManager.shared.showAlert(Alert( - title: Text("Delete member message?"), - message: Text( - groupInfo.fullGroupPreferences.fullDelete.on - ? "The message will be deleted for all members." - : "The message will be marked as moderated for all members." - ), - primaryButton: .destructive(Text("Delete"), action: onModerate), - secondaryButton: .cancel() - )) -} - -private func showNoMessageMessageAlert() { - AlertManager.shared.showAlertMsg( - title: LocalizedStringKey("No message"), - message: LocalizedStringKey("This message was deleted or not received yet.") - ) -} - private func broadcastDeleteButtonText(_ chat: Chat) -> LocalizedStringKey { chat.chatInfo.featureEnabled(.fullDelete) ? "Delete for everyone" : "Mark deleted for everyone" } diff --git a/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift b/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift index 58e22e63a2..78ea394caf 100644 --- a/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift +++ b/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift @@ -764,18 +764,12 @@ func updateMemberSettings(_ gInfo: GroupInfo, _ member: GroupMember, _ memberSet } } -func blockForAllAlert(_ gInfo: GroupInfo, _ mem: GroupMember, _ onBlocked: (() -> Void)? = nil) -> Alert { +func blockForAllAlert(_ gInfo: GroupInfo, _ mem: GroupMember) -> Alert { Alert( title: Text("Block member for all?"), message: Text("All new messages from \(mem.chatViewName) will be hidden!"), primaryButton: .destructive(Text("Block for all")) { - Task { - let uMember = await blockMemberForAll(gInfo, mem, true) - - if uMember != nil { - onBlocked?() - } - } + blockMemberForAll(gInfo, mem, true) }, secondaryButton: .cancel() ) @@ -786,25 +780,23 @@ func unblockForAllAlert(_ gInfo: GroupInfo, _ mem: GroupMember) -> Alert { title: Text("Unblock member for all?"), message: Text("Messages from \(mem.chatViewName) will be shown!"), primaryButton: .default(Text("Unblock for all")) { - Task { - await blockMemberForAll(gInfo, mem, false) - } + blockMemberForAll(gInfo, mem, false) }, secondaryButton: .cancel() ) } -func blockMemberForAll(_ gInfo: GroupInfo, _ member: GroupMember, _ blocked: Bool) async -> GroupMember? { - do { - let updatedMember = try await apiBlockMemberForAll(gInfo.groupId, member.groupMemberId, blocked) - await MainActor.run { - _ = ChatModel.shared.upsertGroupMember(gInfo, updatedMember) +func blockMemberForAll(_ gInfo: GroupInfo, _ member: GroupMember, _ blocked: Bool) { + Task { + do { + let updatedMember = try await apiBlockMemberForAll(gInfo.groupId, member.groupMemberId, blocked) + await MainActor.run { + _ = ChatModel.shared.upsertGroupMember(gInfo, updatedMember) + } + } catch let error { + logger.error("apiBlockMemberForAll error: \(responseError(error))") } - return updatedMember - } catch let error { - logger.error("apiBlockMemberForAll error: \(responseError(error))") } - return nil } struct GroupMemberInfoView_Previews: PreviewProvider { diff --git a/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift b/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift index a311db7d50..ff5fb2986b 100644 --- a/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift +++ b/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift @@ -277,7 +277,7 @@ struct ChatPreviewView: View { func prefix() -> Text { switch cItem.content.msgContent { - case let .report(text, reason): return Text(!text.isEmpty ? "\(reason.text): " : reason.text).italic().foregroundColor(Color.red) + case let .report(_, reason): return Text(!itemText.isEmpty ? "\(reason.text): " : reason.text).italic().foregroundColor(Color.red) default: return Text("") } } diff --git a/apps/ios/SimpleXChat/ChatTypes.swift b/apps/ios/SimpleXChat/ChatTypes.swift index 2638c56776..f64a1076a5 100644 --- a/apps/ios/SimpleXChat/ChatTypes.swift +++ b/apps/ios/SimpleXChat/ChatTypes.swift @@ -2078,7 +2078,7 @@ public struct GroupMember: Identifiable, Decodable, Hashable { public func canChangeRoleTo(groupInfo: GroupInfo) -> [GroupMemberRole]? { if !canBeRemoved(groupInfo: groupInfo) { return nil } let userRole = groupInfo.membership.memberRole - return GroupMemberRole.allCases.filter { $0 <= userRole && $0 != .author } + return GroupMemberRole.supportedRoles.filter { $0 <= userRole } } public func canBlockForAll(groupInfo: GroupInfo) -> Bool { @@ -3337,17 +3337,6 @@ public struct CIQuote: Decodable, ItemContent, Hashable { } return CIQuote(chatDir: chatDir, itemId: itemId, sentAt: sentAt, content: mc) } - - public func memberToModerate(_ chatInfo: ChatInfo) -> GroupMember? { - switch (chatInfo, chatDir) { - case let (.group(groupInfo), .groupRcv(groupMember)): - let m = groupInfo.membership - return m.memberRole >= .admin && m.memberRole >= groupMember.memberRole - ? groupMember - : nil - default: return nil - } - } } public struct CIReactionCount: Decodable, Hashable { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index 5b05d033c8..96f12b9ce9 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -2958,19 +2958,6 @@ class CIQuote ( null -> null } - fun memberToModerate(chatInfo: ChatInfo): GroupMember? { - return if (chatInfo is ChatInfo.Group && chatDir is CIDirection.GroupRcv) { - val m = chatInfo.groupInfo.membership - if (m.memberRole >= GroupMemberRole.Moderator && m.memberRole >= chatDir.groupMember.memberRole) { - chatDir.groupMember - } else { - null - } - } else { - null - } - } - companion object { fun getSample(itemId: Long?, sentAt: Instant, text: String, chatDir: CIDirection?): CIQuote = CIQuote(chatDir = chatDir, itemId = itemId, sentAt = sentAt, content = MsgContent.MCText(text)) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt index a488b66c8b..64b7cfe9a1 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt @@ -301,41 +301,41 @@ fun ChatView(staleChatId: State, onComposed: suspend (chatId: String) - } }, deleteMessage = { itemId, mode -> - val toDeleteItem = chatModel.chatItems.value.firstOrNull { it.id == itemId } - val toModerate = toDeleteItem?.memberToModerate(chatInfo) - val groupInfo = toModerate?.first - val groupMember = toModerate?.second - val deletedChatItem: ChatItem? - val toChatItem: ChatItem? - val r = if (mode == CIDeleteMode.cidmBroadcast && groupInfo != null && groupMember != null) { - chatModel.controller.apiDeleteMemberChatItems( - chatRh, - groupId = groupInfo.groupId, - itemIds = listOf(itemId) - ) - } else { - chatModel.controller.apiDeleteChatItems( - chatRh, - type = chatInfo.chatType, - id = chatInfo.apiId, - itemIds = listOf(itemId), - mode = mode - ) - } - val deleted = r?.firstOrNull() - if (deleted != null) { - deletedChatItem = deleted.deletedChatItem.chatItem - toChatItem = deleted.toChatItem?.chatItem - withChats { - if (toChatItem != null) { - upsertChatItem(chatRh, chatInfo, toChatItem) - } else { - removeChatItem(chatRh, chatInfo, deletedChatItem) + withBGApi { + val toDeleteItem = chatModel.chatItems.value.firstOrNull { it.id == itemId } + val toModerate = toDeleteItem?.memberToModerate(chatInfo) + val groupInfo = toModerate?.first + val groupMember = toModerate?.second + val deletedChatItem: ChatItem? + val toChatItem: ChatItem? + val r = if (mode == CIDeleteMode.cidmBroadcast && groupInfo != null && groupMember != null) { + chatModel.controller.apiDeleteMemberChatItems( + chatRh, + groupId = groupInfo.groupId, + itemIds = listOf(itemId) + ) + } else { + chatModel.controller.apiDeleteChatItems( + chatRh, + type = chatInfo.chatType, + id = chatInfo.apiId, + itemIds = listOf(itemId), + mode = mode + ) + } + val deleted = r?.firstOrNull() + if (deleted != null) { + deletedChatItem = deleted.deletedChatItem.chatItem + toChatItem = deleted.toChatItem?.chatItem + withChats { + if (toChatItem != null) { + upsertChatItem(chatRh, chatInfo, toChatItem) + } else { + removeChatItem(chatRh, chatInfo, deletedChatItem) + } } } } - - deleted }, deleteMessages = { itemIds -> deleteMessages(chatRh, chatInfo, itemIds, false, moderate = false) }, receiveFile = { fileId -> @@ -599,7 +599,7 @@ fun ChatLayout( info: () -> Unit, showMemberInfo: (GroupInfo, GroupMember) -> Unit, loadMessages: suspend (ChatId, ChatPagination, ActiveChatState, visibleItemIndexesNonReversed: () -> IntRange) -> Unit, - deleteMessage: suspend (Long, CIDeleteMode) -> ChatItemDeletion?, + deleteMessage: (Long, CIDeleteMode) -> Unit, deleteMessages: (List) -> Unit, receiveFile: (Long) -> Unit, cancelFile: (Long) -> Unit, @@ -946,7 +946,7 @@ fun BoxScope.ChatItemsList( showMemberInfo: (GroupInfo, GroupMember) -> Unit, showChatInfo: () -> Unit, loadMessages: suspend (ChatId, ChatPagination, ActiveChatState, visibleItemIndexesNonReversed: () -> IntRange) -> Unit, - deleteMessage: suspend (Long, CIDeleteMode) -> ChatItemDeletion?, + deleteMessage: (Long, CIDeleteMode) -> Unit, deleteMessages: (List) -> Unit, receiveFile: (Long) -> Unit, cancelFile: (Long) -> Unit, @@ -2438,7 +2438,7 @@ fun PreviewChatLayout() { info = {}, showMemberInfo = { _, _ -> }, loadMessages = { _, _, _, _ -> }, - deleteMessage = { _, _ -> null }, + deleteMessage = { _, _ -> }, deleteMessages = { _ -> }, receiveFile = { _ -> }, cancelFile = {}, @@ -2511,7 +2511,7 @@ fun PreviewGroupChatLayout() { info = {}, showMemberInfo = { _, _ -> }, loadMessages = { _, _, _, _ -> }, - deleteMessage = { _, _ -> null }, + deleteMessage = { _, _ -> }, deleteMessages = {}, receiveFile = { _ -> }, cancelFile = {}, diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt index a064058533..760f340851 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt @@ -747,13 +747,13 @@ fun updateMemberSettings(rhId: Long?, gInfo: GroupInfo, member: GroupMember, mem } } -fun blockForAllAlert(rhId: Long?, gInfo: GroupInfo, mem: GroupMember, blockMember: () -> Unit = { withBGApi { blockMemberForAll(rhId, gInfo, mem, true) } }) { +fun blockForAllAlert(rhId: Long?, gInfo: GroupInfo, mem: GroupMember) { AlertManager.shared.showAlertDialog( title = generalGetString(MR.strings.block_for_all_question), text = generalGetString(MR.strings.block_member_desc).format(mem.chatViewName), confirmText = generalGetString(MR.strings.block_for_all), onConfirm = { - blockMember() + blockMemberForAll(rhId, gInfo, mem, true) }, destructive = true, ) @@ -765,15 +765,17 @@ fun unblockForAllAlert(rhId: Long?, gInfo: GroupInfo, mem: GroupMember) { text = generalGetString(MR.strings.unblock_member_desc).format(mem.chatViewName), confirmText = generalGetString(MR.strings.unblock_for_all), onConfirm = { - withBGApi { blockMemberForAll(rhId, gInfo, mem, false) } + blockMemberForAll(rhId, gInfo, mem, false) }, ) } -suspend fun blockMemberForAll(rhId: Long?, gInfo: GroupInfo, member: GroupMember, blocked: Boolean) { - val updatedMember = ChatController.apiBlockMemberForAll(rhId, gInfo.groupId, member.groupMemberId, blocked) - withChats { - upsertGroupMember(rhId, gInfo, updatedMember) +fun blockMemberForAll(rhId: Long?, gInfo: GroupInfo, member: GroupMember, blocked: Boolean) { + withBGApi { + val updatedMember = ChatController.apiBlockMemberForAll(rhId, gInfo.groupId, member.groupMemberId, blocked) + withChats { + upsertGroupMember(rhId, gInfo, updatedMember) + } } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt index 1a094f613a..58e4a31840 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt @@ -27,12 +27,9 @@ import androidx.compose.ui.unit.* import chat.simplex.common.model.* import chat.simplex.common.model.ChatModel.controller import chat.simplex.common.model.ChatModel.currentUser -import chat.simplex.common.model.ChatModel.withChats import chat.simplex.common.platform.* import chat.simplex.common.ui.theme.* import chat.simplex.common.views.chat.* -import chat.simplex.common.views.chat.group.blockForAllAlert -import chat.simplex.common.views.chat.group.blockMemberForAll import chat.simplex.common.views.helpers.* import chat.simplex.res.MR import kotlinx.datetime.Clock @@ -77,7 +74,7 @@ fun ChatItemView( selectedChatItems: MutableState?>, fillMaxWidth: Boolean = true, selectChatItem: () -> Unit, - deleteMessage: suspend (Long, CIDeleteMode) -> ChatItemDeletion?, + deleteMessage: (Long, CIDeleteMode) -> Unit, deleteMessages: (List) -> Unit, receiveFile: (Long) -> Unit, cancelFile: (Long) -> Unit, @@ -113,12 +110,6 @@ fun ChatItemView( val onLinkLongClick = { _: String -> showMenu.value = true } val live = remember { derivedStateOf { composeState.value.liveMessage != null } }.value - val deleteMessageAsync: (Long, CIDeleteMode) -> Unit = { id, mode -> - withBGApi { - deleteMessage(id, mode) - } - } - Box( modifier = if (fillMaxWidth) Modifier.fillMaxWidth() else Modifier, contentAlignment = alignment, @@ -293,7 +284,7 @@ fun ChatItemView( @Composable fun DeleteItemMenu() { DefaultDropdownMenu(showMenu) { - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -308,31 +299,12 @@ fun ChatItemView( // cItem.id check is a special case for live message chat item which has negative ID while not sent yet cItem.isReport && cItem.meta.itemDeleted == null && cInfo is ChatInfo.Group -> { DefaultDropdownMenu(showMenu) { - if (cItem.chatDir is CIDirection.GroupSnd) { - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) - } else { - ArchiveReportItemAction(cItem, showMenu, deleteMessageAsync) - val qItem = cItem.quotedItem - if (qItem != null) { - ModerateReportItemAction(rhId, cInfo, cItem, qItem, showMenu, deleteMessage) - val rMember = qItem.memberToModerate(cInfo) - if (rMember != null && !rMember.blockedByAdmin && rMember.canBlockForAll(cInfo.groupInfo)) { - BlockMemberAction( - rhId, - chatInfo = cInfo, - groupInfo = cInfo.groupInfo, - cItem = cItem, - reportedItem = qItem, - member = rMember, - showMenu = showMenu, - deleteMessage = deleteMessage - ) - } - } - - Divider() - SelectItemAction(showMenu, selectChatItem) + if (cItem.chatDir !is CIDirection.GroupSnd && cInfo.groupInfo.membership.memberRole >= GroupMemberRole.Moderator) { + ArchiveReportItemAction(cItem, showMenu, deleteMessage) } + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages, buttonText = stringResource(MR.strings.delete_report)) + Divider() + SelectItemAction(showMenu, selectChatItem) } } cItem.content.msgContent != null && cItem.id >= 0 && !cItem.isReport -> { @@ -421,13 +393,13 @@ fun ChatItemView( CancelFileItemAction(cItem.file.fileId, showMenu, cancelFile = cancelFile, cancelAction = cItem.file.cancelAction) } if (!(live && cItem.meta.isLive) && !preview) { - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) } if (cItem.chatDir !is CIDirection.GroupSnd) { val groupInfo = cItem.memberToModerate(cInfo)?.first if (groupInfo != null) { - ModerateItemAction(cItem, questionText = moderateMessageQuestionText(cInfo.featureEnabled(ChatFeature.FullDelete), 1), showMenu, deleteMessageAsync) - } else if (cItem.meta.itemDeleted == null && cInfo is ChatInfo.Group && cInfo.groupInfo.membership.memberRole < GroupMemberRole.Moderator && !live) { + ModerateItemAction(cItem, questionText = moderateMessageQuestionText(cInfo.featureEnabled(ChatFeature.FullDelete), 1), showMenu, deleteMessage) + } else if (cItem.meta.itemDeleted == null && cInfo is ChatInfo.Group && cInfo.groupInfo.membership.memberRole == GroupMemberRole.Member && !live) { ReportItemAction(cItem, composeState, showMenu) } } @@ -447,7 +419,7 @@ fun ChatItemView( ExpandItemAction(revealed, showMenu, reveal) } ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -457,7 +429,7 @@ fun ChatItemView( cItem.isDeletedContent -> { DefaultDropdownMenu(showMenu) { ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -471,7 +443,7 @@ fun ChatItemView( } else { ExpandItemAction(revealed, showMenu, reveal) } - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -480,7 +452,7 @@ fun ChatItemView( } else -> { DefaultDropdownMenu(showMenu) { - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (selectedChatItems.value == null) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -497,7 +469,7 @@ fun ChatItemView( RevealItemAction(revealed, showMenu, reveal) } ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -531,7 +503,7 @@ fun ChatItemView( DeletedItemView(cItem, cInfo.timedMessagesTTL, showViaProxy = showViaProxy, showTimestamp = showTimestamp) DefaultDropdownMenu(showMenu) { ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -588,7 +560,7 @@ fun ChatItemView( MarkedDeletedItemView(cItem, cInfo.timedMessagesTTL, revealed, showViaProxy = showViaProxy, showTimestamp = showTimestamp) DefaultDropdownMenu(showMenu) { ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(cItem, revealed, showMenu, questionText = generalGetString(MR.strings.delete_message_cannot_be_undone_warning), deleteMessageAsync, deleteMessages) + DeleteItemAction(cItem, revealed, showMenu, questionText = generalGetString(MR.strings.delete_message_cannot_be_undone_warning), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -772,9 +744,10 @@ fun DeleteItemAction( questionText: String, deleteMessage: (Long, CIDeleteMode) -> Unit, deleteMessages: (List) -> Unit, + buttonText: String = stringResource(MR.strings.delete_verb), ) { ItemAction( - stringResource(MR.strings.delete_verb), + buttonText, painterResource(MR.images.ic_delete), onClick = { showMenu.value = false @@ -822,7 +795,7 @@ fun ModerateItemAction( painterResource(MR.images.ic_flag), onClick = { showMenu.value = false - moderateMessageAlertDialog(cItem.id, questionText, deleteMessage = deleteMessage) + moderateMessageAlertDialog(cItem, questionText, deleteMessage = deleteMessage) }, color = Color.Red ) @@ -937,120 +910,10 @@ private fun ReportItemAction( ) } -@Composable -private fun ModerateReportItemAction( - rhId: Long?, - chatInfo: ChatInfo, - cItem: ChatItem, - reportedItem: CIQuote, - showMenu: MutableState, - deleteMessage: suspend (Long, CIDeleteMode) -> ChatItemDeletion? -) { - ItemAction( - stringResource(MR.strings.moderate_verb), - painterResource(MR.images.ic_flag), - onClick = { - withBGApi { - val reportedMessageId = getLocalIdForReportedMessage(rhId, chatInfo, reportedItem, cItem.id) - if (reportedMessageId != null) { - moderateMessageAlertDialog( - reportedMessageId, - questionText = moderateMessageQuestionText(chatInfo.featureEnabled(ChatFeature.FullDelete), 1), - deleteMessage = { id, m -> - withApi { - val deleted = deleteMessage(id, m) - if (deleted != null) { - deleteMessage(cItem.id, CIDeleteMode.cidmInternalMark) - } - } - }, - ) - } - } - showMenu.value = false - }, - color = Color.Red - ) -} - -@Composable -private fun BlockMemberAction( - rhId: Long?, - chatInfo: ChatInfo, - groupInfo: GroupInfo, - cItem: ChatItem, - reportedItem: CIQuote, - member: GroupMember, - showMenu: MutableState, - deleteMessage: suspend (Long, CIDeleteMode) -> ChatItemDeletion? -) { - ItemAction( - stringResource(MR.strings.block_member_button), - painterResource(MR.images.ic_back_hand), - onClick = { - AlertManager.shared.showAlertDialogButtonsColumn( - title = generalGetString(MR.strings.report_block_and_moderate_title), - buttons = { - SectionItemView({ - AlertManager.shared.hideAlert() - withBGApi { - val reportedMessageId = getLocalIdForReportedMessage(rhId, chatInfo, reportedItem, cItem.id) - if (reportedMessageId != null) { - blockAndModerateAlertDialog( - rhId, - reportedMessageId = reportedMessageId, - reportId = cItem.id, - gInfo = groupInfo, - mem = member, - deleteMessage = deleteMessage, - ) - } - } - }) { - Text(generalGetString(MR.strings.report_block_and_moderate_block_and_moderate_action), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.error) - } - SectionItemView({ - AlertManager.shared.hideAlert() - withBGApi { - val reportedMessageId = getLocalIdForReportedMessage(rhId, chatInfo, reportedItem, cItem.id) - if (reportedMessageId != null) { - blockForAllAlert(rhId, gInfo = groupInfo, mem = member, blockMember = { - withBGApi { - try { - blockMemberForAll( - rhId, - gInfo = groupInfo, - member = member, - blocked = true - ) - deleteMessage(reportedMessageId, CIDeleteMode.cidmInternalMark) - } catch (ex: Exception) { - Log.e(TAG, "BlockMemberAction block and moderate ${ex.message}") - } - } - }) - } - } - }) { - Text(generalGetString(MR.strings.report_block_and_moderate_only_block_action), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.error) - } - SectionItemView({ - AlertManager.shared.hideAlert() - }) { - Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) - } - } - ) - showMenu.value = false - }, - color = Color.Red - ) -} - @Composable private fun ArchiveReportItemAction(cItem: ChatItem, showMenu: MutableState, deleteMessage: (Long, CIDeleteMode) -> Unit) { ItemAction( - stringResource(MR.strings.archive_verb), + stringResource(MR.strings.archive_report), painterResource(MR.images.ic_inventory_2), onClick = { AlertManager.shared.showAlertDialog( @@ -1401,14 +1264,14 @@ fun moderateMessageQuestionText(fullDeleteAllowed: Boolean, count: Int): String } } -fun moderateMessageAlertDialog(chatItemId: Long, questionText: String, deleteMessage: (Long, CIDeleteMode) -> Unit) { +fun moderateMessageAlertDialog(chatItem: ChatItem, questionText: String, deleteMessage: (Long, CIDeleteMode) -> Unit) { AlertManager.shared.showAlertDialog( title = generalGetString(MR.strings.delete_member_message__question), text = questionText, confirmText = generalGetString(MR.strings.delete_verb), destructive = true, onConfirm = { - deleteMessage(chatItemId, CIDeleteMode.cidmBroadcast) + deleteMessage(chatItem.id, CIDeleteMode.cidmBroadcast) } ) } @@ -1423,59 +1286,8 @@ fun moderateMessagesAlertDialog(itemIds: List, questionText: String, delet ) } -private fun blockAndModerateAlertDialog( - rhId: Long?, - reportedMessageId: Long, - reportId: Long, - gInfo: GroupInfo, - mem: GroupMember, - deleteMessage: suspend (Long, CIDeleteMode) -> ChatItemDeletion? -) { - AlertManager.shared.showAlertDialog( - title = generalGetString(MR.strings.report_block_and_moderate_confirmation_title), - text = generalGetString( - if (gInfo.fullGroupPreferences.fullDelete.on) MR.strings.report_block_and_moderate_confirmation_desc_full_delete else MR.strings.report_block_and_moderate_confirmation_desc_full_delete).format(mem.chatViewName), - confirmText = generalGetString(MR.strings.report_block_and_moderate_confirmation_ok), - onConfirm = { - withBGApi { - try { - val deleted = deleteMessage(reportedMessageId, CIDeleteMode.cidmBroadcast) - if (deleted != null) { - blockMemberForAll(rhId, gInfo, mem, true) - deleteMessage(reportId, CIDeleteMode.cidmInternalMark) - } - } catch (ex: Exception) { - Log.e(TAG, "blockAndModerateAlertDialog block and moderate ${ex.message}") - } - } - }, - destructive = true, - ) -} - expect fun copyItemToClipboard(cItem: ChatItem, clipboard: ClipboardManager) -private suspend fun getLocalIdForReportedMessage( - rhId: Long?, - chatInfo: ChatInfo, - reportedItem: CIQuote, - itemId: Long): Long? { - if (reportedItem.itemId != null) { - return reportedItem.itemId - } - val item = apiLoadSingleMessage(rhId, chatInfo.chatType, chatInfo.apiId, itemId) - - if (item?.quotedItem?.itemId != null) { - withChats { - updateChatItem(chatInfo, item) - } - return item.quotedItem.itemId - } else { - showQuotedItemDoesNotExistAlert() - return null - } -} - @Preview @Composable fun PreviewChatItemView( @@ -1493,7 +1305,7 @@ fun PreviewChatItemView( range = remember { mutableStateOf(0..1) }, selectedChatItems = remember { mutableStateOf(setOf()) }, selectChatItem = {}, - deleteMessage = { _, _ -> null }, + deleteMessage = { _, _ -> }, deleteMessages = { _ -> }, receiveFile = { _ -> }, cancelFile = {}, @@ -1539,7 +1351,7 @@ fun PreviewChatItemViewDeletedContent() { range = remember { mutableStateOf(0..1) }, selectedChatItems = remember { mutableStateOf(setOf()) }, selectChatItem = {}, - deleteMessage = { _, _ -> null }, + deleteMessage = { _, _ -> }, deleteMessages = { _ -> }, receiveFile = { _ -> }, cancelFile = {}, diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml index 860975a414..c9c0f00555 100644 --- a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml +++ b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml @@ -305,13 +305,6 @@ Report reason? Archive report? The report will be archived for you. - Block and moderate? - Block and moderate - Only block - Delete member message and block? - The message will be deleted for all members.\nAll new messages from %1$s will be hidden! - The message will be marked as moderated for all members.\nAll new messages from %1$s will be hidden! - Delete and block Error: %1$s @@ -338,6 +331,8 @@ Info Search Archive + Archive report + Delete report Sent message Received message History