From ff5b9721392be8edb74ec4a0fde0900179bf52bb Mon Sep 17 00:00:00 2001 From: hydrogen Date: Mon, 30 Nov 2020 23:24:01 +0200 Subject: [PATCH] fix: stop crash by showing error message fix #898 The segmentation fault occured because the RPC response returned json with an error message as oppossed to the usual data required to update the chat. Since the section of the code didn't handle this error message it caused the app to crash. I've handled this error to show an error alert box by emitting a event --- src/app/chat/event_handling.nim | 4 ++++ src/app/chat/view.nim | 2 ++ src/status/chat.nim | 9 ++++++--- src/status/chat/utils.nim | 4 ++-- ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml | 12 ++++++++++++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/app/chat/event_handling.nim b/src/app/chat/event_handling.nim index a83d7a030a..028066fb9b 100644 --- a/src/app/chat/event_handling.nim +++ b/src/app/chat/event_handling.nim @@ -66,6 +66,10 @@ proc handleChatEvents(self: ChatController) = self.status.messages.trackMessage(msg.id, msg.channel) self.view.sendingMessage() + self.status.events.on("sendingMessageFailed") do(e:Args): + var msg = MessageArgs(e) + self.view.sendingMessageFailed() + self.status.events.on("messageSent") do(e:Args): var msg = MessageSentArgs(e) self.view.markMessageAsSent(msg.chatId, msg.id) diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 86c198f0c7..bb9307bf7a 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -233,6 +233,8 @@ QtObject: proc appReady*(self: ChatsView) {.signal.} + proc sendingMessageFailed*(self: ChatsView) {.signal.} + proc alias*(self: ChatsView, pubKey: string): string {.slot.} = generateAlias(pubKey) diff --git a/src/status/chat.nim b/src/status/chat.nim index dc44621fac..04e2a8d210 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -198,9 +198,12 @@ proc setActiveChannel*(self: ChatModel, chatId: string) = proc processMessageUpdateAfterSend(self: ChatModel, response: string): (seq[Chat], seq[Message]) = result = self.processChatUpdate(parseJson(response)) var (chats, messages) = result - self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats, contacts: @[])) - for msg in messages: - self.events.emit("sendingMessage", MessageArgs(id: msg.id, channel: msg.chatId)) + if chats.len == 0 or messages.len == 0: + self.events.emit("sendingMessageFailed", MessageArgs()) + else: + self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats, contacts: @[])) + for msg in messages: + self.events.emit("sendingMessage", MessageArgs(id: msg.id, channel: msg.chatId)) proc sendMessage*(self: ChatModel, chatId: string, msg: string, replyTo: string = "", contentType: int = ContentType.Message.int) = var response = status_chat.sendChatMessage(chatId, msg, replyTo, contentType) diff --git a/src/status/chat/utils.nim b/src/status/chat/utils.nim index cf06ab1da5..4371d18d7d 100644 --- a/src/status/chat/utils.nim +++ b/src/status/chat/utils.nim @@ -12,10 +12,10 @@ proc formatChatUpdate(response: JsonNode): (seq[Chat], seq[Message]) = proc processChatUpdate(self: ChatModel, response: JsonNode): (seq[Chat], seq[Message]) = var chats: seq[Chat] = @[] var messages: seq[Message] = @[] - if response["result"]{"chats"} != nil: + if response{"result"}{"chats"} != nil: for jsonMsg in response["result"]["messages"]: messages.add(jsonMsg.toMessage) - if response["result"]{"chats"} != nil: + if response{"result"}{"chats"} != nil: for jsonChat in response["result"]["chats"]: let chat = jsonChat.toChat self.channels[chat.id] = chat diff --git a/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml b/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml index 66ac1a23c3..9ef3f9b871 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml @@ -3,6 +3,7 @@ import QtQuick.Controls 2.13 import QtQuick.Layouts 1.13 import QtQml.Models 2.13 import QtGraphicalEffects 1.13 +import QtQuick.Dialogs 1.3 import "../../../../shared" import "../../../../imports" import "../components" @@ -132,6 +133,10 @@ ScrollView { chatLogView.scrollToBottom(true) } + onSendingMessageFailed: { + sendingMsgFailedPopup.open(); + } + onNewMessagePushed: { if (!chatLogView.scrollToBottom()) { root.newMessages++ @@ -172,6 +177,13 @@ ScrollView { section.criteria: ViewSection.FullString } + MessageDialog { + id: sendingMsgFailedPopup + standardButtons: StandardButton.Ok + text: qsTr("Failed to send message.") + icon: StandardIcon.Critical + } + DelegateModel { id: messageListDelegate property var lessThan: [