From 9c2e0669f6ac1783f289b7148fabe232dd10ea59 Mon Sep 17 00:00:00 2001 From: James Graham Date: Tue, 14 Jun 2022 13:47:13 +0000 Subject: [PATCH] Fixe Reply or Edit from Chatbar Fixes it so that the cleaned text is shown when using the shortcuts to reply or edit from the chatbar. Also ensures that the correct eventids are passed when the message is an edit. This also fixes the issue of having html pasted into the chatbar when editing and edit. Fixes network/neochat#448 BUG: 455016 --- imports/NeoChat/Page/RoomPage.qml | 4 +-- src/messageeventmodel.cpp | 52 +++++++++++++++---------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/imports/NeoChat/Page/RoomPage.qml b/imports/NeoChat/Page/RoomPage.qml index ec3b2a345..50d7381b8 100644 --- a/imports/NeoChat/Page/RoomPage.qml +++ b/imports/NeoChat/Page/RoomPage.qml @@ -492,14 +492,14 @@ Kirigami.ScrollablePage { onEditLastUserMessage: { const targetMessage = messageEventModel.getLastLocalUserMessageEventId(); if (targetMessage) { - ChatBoxHelper.edit(targetMessage["body"], targetMessage["body"], targetMessage["event_id"]); + ChatBoxHelper.edit(targetMessage["message"], targetMessage["formattedBody"], targetMessage["event_id"]); chatBox.focusInputField(); } } onReplyPreviousUserMessage: { const replyResponse = messageEventModel.getLatestMessageFromIndex(0); if (replyResponse && replyResponse["event_id"]) { - ChatBoxHelper.replyToMessage(replyResponse["event_id"], replyResponse["event"], replyResponse["sender_id"]); + ChatBoxHelper.replyToMessage(replyResponse["event_id"], replyResponse["message"], replyResponse["sender_id"]); } } } diff --git a/src/messageeventmodel.cpp b/src/messageeventmodel.cpp index c1bfc4d88..d6601c1af 100644 --- a/src/messageeventmodel.cpp +++ b/src/messageeventmodel.cpp @@ -823,17 +823,21 @@ QVariant MessageEventModel::getLastLocalUserMessageEventId() if ((*it)->senderId() == m_currentRoom->localUser()->id()) { auto content = (*it)->contentJson(); - if (content.contains("m.relates_to")) { - // the message has been edited once - // so we have to return the id of the related' message instead - targetMessage.insert("event_id", content["m.relates_to"].toObject()["event_id"].toString()); - targetMessage.insert("body", content["formatted_body"].toString()); - return targetMessage; - } + if (e->msgtype() != MessageEventType::Unknown) { + QString eventId; + if (content.contains("m.new_content")) { + // The message has been edited so we have to return the id of the original message instead of the replacement + eventId = content["m.relates_to"].toObject()["event_id"].toString(); + } else { + // For any message that isn't an edit return the id of the current message + eventId = (*it)->id(); + } + targetMessage.insert("event_id", eventId); + targetMessage.insert("formattedBody", content["formatted_body"].toString()); + // Need to get the message from the original eventId or body will have * on the front + QModelIndex idx = index(eventIDToIndex(eventId), 0); + targetMessage.insert("message", idx.data(Qt::UserRole + 2)); - if (e->msgtype() == MessageEventType::Text) { - targetMessage.insert("event_id", (*it)->id()); - targetMessage.insert("body", content["body"].toString()); return targetMessage; } } @@ -856,23 +860,19 @@ QVariant MessageEventModel::getLatestMessageFromIndex(const int baseline) auto content = (*it)->contentJson(); - if (content.contains("m.relates_to")) { - auto relatedContent = content["m.relates_to"].toObject(); - - if (!relatedContent.contains("m.in_reply_to")) { - // the message has been edited once - // so we have to return the id of the related' message instead - replyResponse.insert("event_id", relatedContent["event_id"].toString()); - replyResponse.insert("event", content["m.formatted_body"].toString()); - replyResponse.insert("sender_id", QVariant::fromValue(m_currentRoom->getUser((*it)->senderId()))); - replyResponse.insert("at", -it->index()); - return replyResponse; - } - } - if (e->msgtype() != MessageEventType::Unknown) { - replyResponse.insert("event_id", (*it)->id()); - replyResponse.insert("event", content["body"].toString()); + QString eventId; + if (content.contains("m.new_content")) { + // The message has been edited so we have to return the id of the original message instead of the replacement + eventId = content["m.relates_to"].toObject()["event_id"].toString(); + } else { + // For any message that isn't an edit return the id of the current message + eventId = (*it)->id(); + } + replyResponse.insert("event_id", eventId); + // Need to get the message from the original eventId or body will have * on the front + QModelIndex idx = index(eventIDToIndex(eventId), 0); + replyResponse.insert("message", idx.data(Qt::UserRole + 2)); replyResponse.insert("sender_id", QVariant::fromValue(m_currentRoom->getUser((*it)->senderId()))); replyResponse.insert("at", -it->index()); return replyResponse;