From e757c07c967e2c0de671165c89814040e89f9276 Mon Sep 17 00:00:00 2001 From: Khushboo Mehta Date: Thu, 13 Jan 2022 16:14:34 +0100 Subject: [PATCH] fix(@desktop/chat): delete message not working fixes #4411 --- .../chat_section/chat_content/controller.nim | 9 +++- .../chat_content/messages/controller.nim | 11 ++++- .../messages/controller_interface.nim | 5 +- .../chat_content/messages/module.nim | 4 ++ .../module_view_delegate_interface.nim | 8 +++- .../chat_content/messages/view.nim | 5 +- .../modules/shared_models/message_model.nim | 4 +- src/app_service/service/message/service.nim | 39 ++++++++++++++- .../AppLayouts/Chat/stores/MessageStore.qml | 6 +++ ui/app/AppLayouts/Chat/stores/RootStore.qml | 5 -- .../AppLayouts/Chat/views/ChatContentView.qml | 4 ++ .../views/chat/MessageContextMenuView.qml | 48 +++++++++++-------- ui/imports/shared/views/chat/MessageView.qml | 12 ----- 13 files changed, 116 insertions(+), 44 deletions(-) diff --git a/src/app/modules/main/chat_section/chat_content/controller.nim b/src/app/modules/main/chat_section/chat_content/controller.nim index 1ea7d7b62b..b8c6cb5ab5 100644 --- a/src/app/modules/main/chat_section/chat_content/controller.nim +++ b/src/app/modules/main/chat_section/chat_content/controller.nim @@ -99,6 +99,13 @@ method init*(self: Controller) = var args = ContactArgs(e) self.delegate.onContactDetailsUpdated(args.contactId) + self.events.on(SIGNAL_MESSAGE_DELETION) do(e: Args): + let args = MessageDeletedArgs(e) + if(self.chatId != args.chatId): + return + # remove from pinned messages model + self.delegate.onUnpinMessage(args.messageId) + method getMyChatId*(self: Controller): string = return self.chatId @@ -152,4 +159,4 @@ method getCurrentFleet*(self: Controller): string = return self.settingsService.getFleetAsString() method getRenderedText*(self: Controller, parsedTextArray: seq[ParsedText]): string = - return self.messageService.getRenderedText(parsedTextArray) \ No newline at end of file + return self.messageService.getRenderedText(parsedTextArray) diff --git a/src/app/modules/main/chat_section/chat_content/messages/controller.nim b/src/app/modules/main/chat_section/chat_content/messages/controller.nim index 2f3308604a..e4797d59e0 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/controller.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/controller.nim @@ -102,6 +102,12 @@ method init*(self: Controller) = var args = ContactArgs(e) self.delegate.updateContactDetails(args.contactId) + self.events.on(SIGNAL_MESSAGE_DELETION) do(e: Args): + let args = MessageDeletedArgs(e) + if(self.chatId != args.chatId): + return + self.delegate.onMessageDeleted(args.messageId) + method getMySectionId*(self: Controller): string = return self.sectionId @@ -146,4 +152,7 @@ method getRenderedText*(self: Controller, parsedTextArray: seq[ParsedText]): str method getMessageDetails*(self: Controller, messageId: string): tuple[message: MessageDto, reactions: seq[ReactionDto], error: string] = - return self.messageService.getDetailsForMessage(self.chatId, messageId) \ No newline at end of file + return self.messageService.getDetailsForMessage(self.chatId, messageId) + +method deleteMessage*(self: Controller, messageId: string) = + self.messageService.deleteMessage(messageId) diff --git a/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim b/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim index 151b3d59fa..42d01e8cdb 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim @@ -58,4 +58,7 @@ method getRenderedText*(self: AccessInterface, parsedTextArray: seq[ParsedText]) method getMessageDetails*(self: AccessInterface, messageId: string): tuple[message: MessageDto, reactions: seq[ReactionDto], error: string] {.base.} = - raise newException(ValueError, "No implementation available") \ No newline at end of file + raise newException(ValueError, "No implementation available") + +method deleteMessage*(self: AccessInterface, messageId: string) {.base.} = + raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/chat_section/chat_content/messages/module.nim b/src/app/modules/main/chat_section/chat_content/messages/module.nim index 3ac033d782..4e10e68287 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/module.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/module.nim @@ -213,4 +213,8 @@ method updateContactDetails*(self: Module, contactId: string) = item.messageText = self.controller.getRenderedText(m.parsedText) item.messageContainsMentions = m.containsContactMentions() +method deleteMessage*(self: Module, messageId: string) = + self.controller.deleteMessage(messageId) +method onMessageDeleted*(self: Module, messageId: string) = + self.view.model().removeItem(messageId) diff --git a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim index 144c70cb8d..ac8438c9a7 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim @@ -20,4 +20,10 @@ method amIChatAdmin*(self: AccessInterface): bool {.base.} = raise newException(ValueError, "No implementation available") method getNumberOfPinnedMessages*(self: AccessInterface): int {.base.} = - raise newException(ValueError, "No implementation available") \ No newline at end of file + raise newException(ValueError, "No implementation available") + +method deleteMessage*(self: AccessInterface, messageId: string) {.base.} = + raise newException(ValueError, "No implementation available") + +method onMessageDeleted*(self: AccessInterface, messageId: string) {.base.} = + raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/chat_section/chat_content/messages/view.nim b/src/app/modules/main/chat_section/chat_content/messages/view.nim index 79695e5de7..7461a15d36 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/view.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/view.nim @@ -105,4 +105,7 @@ QtObject: proc sendingMessageFailed*(self: View) {.signal.} proc emitSendingMessageErrorSignal*(self: View) = - self.sendingMessageFailed() \ No newline at end of file + self.sendingMessageFailed() + + proc deleteMessage*(self: View, messageId: string) {.slot.} = + self.delegate.deleteMessage(messageId) diff --git a/src/app/modules/shared_models/message_model.nim b/src/app/modules/shared_models/message_model.nim index a13b9c95fc..5010c87919 100644 --- a/src/app/modules/shared_models/message_model.nim +++ b/src/app/modules/shared_models/message_model.nim @@ -187,6 +187,8 @@ QtObject: self.countChanged() proc appendItem*(self: Model, item: Item) = + if(self.findIndexForMessageId(item.id) != -1): + return let parentModelIndex = newQModelIndex() defer: parentModelIndex.delete @@ -292,4 +294,4 @@ QtObject: if(roles.len > 0): let index = self.createIndex(i, 0, nil) - self.dataChanged(index, index, roles) \ No newline at end of file + self.dataChanged(index, index, roles) diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index 8c3b0cf90f..8b08aac0fc 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -30,6 +30,7 @@ const SIGNAL_SEARCH_MESSAGES_LOADED* = "new-searchMessagesLoaded" const SIGNAL_MESSAGES_MARKED_AS_READ* = "new-messagesMarkedAsRead" const SIGNAL_MESSAGE_REACTION_ADDED* = "new-messageReactionAdded" const SIGNAL_MESSAGE_REACTION_REMOVED* = "new-messageReactionRemoved" +const SIGNAL_MESSAGE_DELETION* = "new-messageDeleted" include async_tasks @@ -63,6 +64,10 @@ type emojiId*: int reactionId*: string + MessageDeletedArgs* = ref object of Args + chatId*: string + messageId*: string + QtObject: type Service* = ref object of QObject events: EventEmitter @@ -143,6 +148,12 @@ QtObject: self.numOfPinnedMessagesPerChat[pm.chatId] = self.numOfPinnedMessagesPerChat[pm.chatId] - 1 self.events.emit(SIGNAL_MESSAGE_UNPINNED, data) + # Handling deleted messages updates + if (receivedData.deletedMessages.len > 0): + for dm in receivedData.deletedMessages: + let data = MessageDeletedArgs(chatId: dm.chatId, messageId: dm.messageId) + self.events.emit(SIGNAL_MESSAGE_DELETION, data) + proc initialMessagesFetched(self: Service, chatId: string): bool = return self.msgCursor.hasKey(chatId) @@ -542,4 +553,30 @@ proc getRenderedText*(self: Service, parsedTextArray: seq[ParsedText]): string = "") of PARSED_TEXT_TYPE_CODEBLOCK: result = result & "" & escape_html(parsedText.literal) & "" - result = result.strip() \ No newline at end of file + result = result.strip() + +proc deleteMessage*(self: Service, messageId: string) = + try: + let response = status_go.deleteMessageAndSend(messageId) + + var deletesMessagesObj: JsonNode + if(not response.result.getProp("removedMessages", deletesMessagesObj) or deletesMessagesObj.kind != JArray): + error "error: ", methodName="deleteMessage", errDesription = "no messages deleted or it's not an array" + return + + let deletedMessagesArr = deletesMessagesObj.getElems() + if(deletedMessagesArr.len == 0): # an array is returned + error "error: ", methodName="deleteMessage", errDesription = "array has no message to delete" + return + + let deletedMessageObj = deletedMessagesArr[0] + var chat_Id, message_Id: string + if not deletedMessageObj.getProp("chatId", chat_Id) or not deletedMessageObj.getProp("messageId", message_Id): + error "error: ", methodName="deleteMessage", errDesription = "there is no set chat id or message id in response" + return + + let data = MessageDeletedArgs(chatId: chat_Id, messageId: message_Id) + self.events.emit(SIGNAL_MESSAGE_DELETION, data) + + except Exception as e: + error "error: ", methodName="deleteMessage", errName = e.name, errDesription = e.msg diff --git a/ui/app/AppLayouts/Chat/stores/MessageStore.qml b/ui/app/AppLayouts/Chat/stores/MessageStore.qml index df0fc78ab4..55e8c311d3 100644 --- a/ui/app/AppLayouts/Chat/stores/MessageStore.qml +++ b/ui/app/AppLayouts/Chat/stores/MessageStore.qml @@ -152,4 +152,10 @@ QtObject { } return tooltip } + + function deleteMessage(messageId) { + if(!messageModule) + return + messageModule.deleteMessage(messageId) + } } diff --git a/ui/app/AppLayouts/Chat/stores/RootStore.qml b/ui/app/AppLayouts/Chat/stores/RootStore.qml index 8840f45cf5..7ecd6959ec 100644 --- a/ui/app/AppLayouts/Chat/stores/RootStore.qml +++ b/ui/app/AppLayouts/Chat/stores/RootStore.qml @@ -102,11 +102,6 @@ QtObject { globalUtils.copyToClipboard(text) } - function deleteMessage(messageId) { - // Not Refactored Yet -// chatsModelInst.messageView.deleteMessage(messageId); - } - function getCommunity(communityId) { // Not Refactored Yet // try { diff --git a/ui/app/AppLayouts/Chat/views/ChatContentView.qml b/ui/app/AppLayouts/Chat/views/ChatContentView.qml index e0f30b233f..d5635e365b 100644 --- a/ui/app/AppLayouts/Chat/views/ChatContentView.qml +++ b/ui/app/AppLayouts/Chat/views/ChatContentView.qml @@ -335,6 +335,10 @@ ColumnLayout { onOpenProfileClicked: { Global.openProfilePopup(publicKey) } + + onDeleteMessage: { + messageStore.deleteMessage(messageId) + } } ColumnLayout { diff --git a/ui/imports/shared/views/chat/MessageContextMenuView.qml b/ui/imports/shared/views/chat/MessageContextMenuView.qml index e7881088cc..de53bf4078 100644 --- a/ui/imports/shared/views/chat/MessageContextMenuView.qml +++ b/ui/imports/shared/views/chat/MessageContextMenuView.qml @@ -60,6 +60,7 @@ StatusPopupMenu { signal createOneToOneChat(string chatId, string ensName) signal showReplyArea() signal toggleReaction(string messageId, int emojiId) + signal deleteMessage(string messageId) onHeightChanged: { root.y = setYPosition() @@ -328,27 +329,11 @@ StatusPopupMenu { text: qsTrId("delete-message") onTriggered: { if (!localAccountSensitiveSettings.showDeleteMessageWarning) { - // Not Refactored Yet -// return root.store.chatsModelInst.messageView.deleteMessage(messageId) + deleteMessage(messageId) + } + else { + Global.openPopup(deleteMessageConfirmationDialogComponent) } - - let confirmationDialog = openPopup(genericConfirmationDialog, { - //% "Confirm deleting this message" - title: qsTrId("confirm-deleting-this-message"), - //% "Are you sure you want to delete this message? Be aware that other clients are not guaranteed to delete the message as well." - confirmationText: qsTrId("are-you-sure-you-want-to-delete-this-message--be-aware-that-other-clients-are-not-guaranteed-to-delete-the-message-as-well-"), - height: 260, - "checkbox.visible": true, - executeConfirm: function () { - if (confirmationDialog.checkbox.checked) { - localAccountSensitiveSettings.showDeleteMessageWarning = false - } - - confirmationDialog.close() - // Not Refactored Yet -// root.store.chatsModelInst.messageView.deleteMessage(messageId) - } - }) } icon.name: "delete" type: StatusMenuItem.Type.Danger @@ -380,4 +365,27 @@ StatusPopupMenu { fileDialog.close() } } + + Component { + id: deleteMessageConfirmationDialogComponent + ConfirmationDialog { + //% "Confirm deleting this message" + header.title: qsTrId("confirm-deleting-this-message") + //% "Are you sure you want to delete this message? Be aware that other clients are not guaranteed to delete the message as well." + confirmationText: qsTrId("are-you-sure-you-want-to-delete-this-message--be-aware-that-other-clients-are-not-guaranteed-to-delete-the-message-as-well-") + height: 260 + checkbox.visible: true + executeConfirm: function () { + if (checkbox.checked) { + localAccountSensitiveSettings.showDeleteMessageWarning = false + } + + close() + root.deleteMessage(messageId) + } + onClosed: { + destroy() + } + } + } } diff --git a/ui/imports/shared/views/chat/MessageView.qml b/ui/imports/shared/views/chat/MessageView.qml index 18fa4fa628..adc4916f90 100644 --- a/ui/imports/shared/views/chat/MessageView.qml +++ b/ui/imports/shared/views/chat/MessageView.qml @@ -216,18 +216,6 @@ Column { // } // } // } -// } - -// Connections { -// enabled: !!root.rootStore -// target: !!root.rootStore ? root.chatsModel.messageView : null -// onHideMessage: { -// // This hack is used because message_list deleteMessage sometimes does not remove the messages (there might be an issue with the delegate model) -// if(mId === messageId){ -// root.visible = 0; -// root.height = 0; -// } -// } // } Loader {