From ca81d35936f6cb24d45c6dd21ed698d4e7d0a16d Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 1 Dec 2024 19:05:31 +0000 Subject: [PATCH] Post Message Refactor 2 Remove the need for NeoChat to have overloaded functions for posting messages and just use what quotient gives --- src/chatbarcache.cpp | 21 ++++++++++++- src/models/actionsmodel.cpp | 41 ++++++++++++------------- src/neochatroom.cpp | 59 ------------------------------------ src/neochatroom.h | 34 --------------------- src/notificationsmanager.cpp | 4 ++- 5 files changed, 42 insertions(+), 117 deletions(-) diff --git a/src/chatbarcache.cpp b/src/chatbarcache.cpp index e87d27294..24879d75a 100644 --- a/src/chatbarcache.cpp +++ b/src/chatbarcache.cpp @@ -11,6 +11,8 @@ #include "neochatroom.h" #include "texthandler.h" +using namespace Qt::StringLiterals; + ChatBarCache::ChatBarCache(QObject *parent) : QObject(parent) { @@ -319,7 +321,24 @@ void ChatBarCache::postMessage() return; } - room->postMessage(text(), sendText, *std::get>(result), replyId(), editId(), threadId()); + bool isReply = !replyId().isEmpty(); + const auto replyIt = room->findInTimeline(replyId()); + if (replyIt == room->historyEdge()) { + isReply = false; + } + + auto content = std::make_unique(sendText, u"text/html"_s); + std::optional relatesTo = std::nullopt; + + if (!threadId().isEmpty()) { + relatesTo = Quotient::EventRelation::replyInThread(threadId(), !isReply, isReply ? replyId() : threadId()); + } else if (!editId().isEmpty()) { + relatesTo = Quotient::EventRelation::replace(editId()); + } else if (isReply) { + relatesTo = Quotient::EventRelation::replyTo(replyId()); + } + + room->post(text(), *std::get>(result), std::move(content), relatesTo); clearCache(); } diff --git a/src/models/actionsmodel.cpp b/src/models/actionsmodel.cpp index d7e6b8a3f..6eaff75e2 100644 --- a/src/models/actionsmodel.cpp +++ b/src/models/actionsmodel.cpp @@ -109,11 +109,10 @@ QList actions{ rainbowText += QStringLiteral("%3").arg(rainbowColors[i % rainbowColors.length()], text.at(i)); } // Ideally, we would just return rainbowText and let that do the rest, but the colors don't survive markdownToHTML. - room->postMessage(QStringLiteral("/rainbow %1").arg(text), - rainbowText, - RoomMessageEvent::MsgType::Text, - chatBarCache->replyId(), - chatBarCache->editId()); + auto content = std::make_unique(rainbowText, u"text/html"_s); + EventRelation relatesTo = + chatBarCache->isReplying() ? EventRelation::replyTo(chatBarCache->replyId()) : EventRelation::replace(chatBarCache->editId()); + room->post("/rainbow %1"_L1.arg(text), MessageEventType::Text, std::move(content), relatesTo); return QString(); }, false, @@ -129,11 +128,10 @@ QList actions{ rainbowText += QStringLiteral("%3").arg(rainbowColors[i % rainbowColors.length()], text.at(i)); } // Ideally, we would just return rainbowText and let that do the rest, but the colors don't survive markdownToHTML. - room->postMessage(QStringLiteral("/rainbow %1").arg(text), - rainbowText, - RoomMessageEvent::MsgType::Emote, - chatBarCache->replyId(), - chatBarCache->editId()); + auto content = std::make_unique(rainbowText, u"text/html"_s); + EventRelation relatesTo = + chatBarCache->isReplying() ? EventRelation::replyTo(chatBarCache->replyId()) : EventRelation::replace(chatBarCache->editId()); + room->post(u"/rainbow %1"_s.arg(text), MessageEventType::Text, std::move(content), relatesTo); return QString(); }, false, @@ -144,7 +142,7 @@ QList actions{ Action{ QStringLiteral("plain"), [](const QString &text, NeoChatRoom *room, ChatBarCache *) { - room->postMessage(text, text.toHtmlEscaped(), RoomMessageEvent::MsgType::Text, {}, {}); + room->postPlainText(text.toHtmlEscaped()); return QString(); }, false, @@ -156,11 +154,10 @@ QList actions{ QStringLiteral("spoiler"), [](const QString &text, NeoChatRoom *room, ChatBarCache *chatBarCache) { // Ideally, we would just return rainbowText and let that do the rest, but the colors don't survive markdownToHTML. - room->postMessage(QStringLiteral("/spoiler %1").arg(text), - QStringLiteral("%1").arg(text), - RoomMessageEvent::MsgType::Text, - chatBarCache->replyId(), - chatBarCache->editId()); + auto content = std::make_unique(u"%1"_s.arg(text), u"text/html"_s); + EventRelation relatesTo = + chatBarCache->isReplying() ? EventRelation::replyTo(chatBarCache->replyId()) : EventRelation::replace(chatBarCache->editId()); + room->post(u"/spoiler %1"_s.arg(text), MessageEventType::Text, std::move(content), relatesTo); return QString(); }, false, @@ -605,15 +602,15 @@ bool ActionsModel::handleQuickEditAction(NeoChatRoom *room, const QString &messa if (eventRelation && eventRelation->type == "m.replace"_L1) { replaceId = eventRelation->eventId; } + + std::unique_ptr content = nullptr; if (flags == "/g"_L1) { - room->postHtmlMessage(messageText, originalString.replace(regex, replacement), event->msgtype(), {}, replaceId); + content = std::make_unique(originalString.replace(regex, replacement), u"text/html"_s); } else { - room->postHtmlMessage(messageText, - originalString.replace(originalString.indexOf(regex), regex.size(), replacement), - event->msgtype(), - {}, - replaceId); + content = std::make_unique(originalString.replace(regex, replacement), u"text/html"_s); } + Quotient::EventRelation relatesTo = Quotient::EventRelation::replace(replaceId); + room->post(messageText, event->msgtype(), std::move(content), relatesTo); return true; } } diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index f833e34f7..a4f49dcb0 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -484,65 +484,6 @@ QString msgTypeToString(MessageEventType msgType) } } -void NeoChatRoom::postMessage(const QString &rawText, - const QString &text, - MessageEventType type, - const QString &replyEventId, - const QString &relateToEventId, - const QString &threadRootId, - const QString &fallbackId) -{ - postHtmlMessage(rawText, text, type, replyEventId, relateToEventId, threadRootId, fallbackId); -} - -void NeoChatRoom::postHtmlMessage(const QString &text, - const QString &html, - MessageEventType type, - const QString &replyEventId, - const QString &relateToEventId, - const QString &threadRootId, - const QString &fallbackId) -{ - bool isReply = !replyEventId.isEmpty(); - bool isEdit = !relateToEventId.isEmpty(); - bool isThread = !threadRootId.isEmpty(); - const auto replyIt = findInTimeline(replyEventId); - if (replyIt == historyEdge()) { - isReply = false; - } - - auto content = std::make_unique(html, u"text/html"_s); - std::optional relatesTo = std::nullopt; - - if (isThread) { - bool isFallingBack = !fallbackId.isEmpty(); - QString replyEventId = isFallingBack ? fallbackId : QString(); - if (isReply) { - isFallingBack = false; - replyEventId = replyIt->event()->displayId(); - } - - // If we are not replying and there is no fallback ID it means a new thread - // is being created. - if (!isFallingBack && !isReply) { - isFallingBack = true; - replyEventId = threadRootId; - } - - relatesTo = EventRelation::replyInThread(threadRootId, isFallingBack, replyEventId); - } - - if (isEdit) { - relatesTo = EventRelation::replace(relateToEventId); - } - - if (isReply) { - relatesTo = EventRelation::replyTo(replyEventId); - } - - post(text, type, std::move(content), relatesTo); -} - void NeoChatRoom::toggleReaction(const QString &eventId, const QString &reaction) { if (eventId.isEmpty() || reaction.isEmpty()) { diff --git a/src/neochatroom.h b/src/neochatroom.h index 4e2d5d373..1e037844c 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -691,40 +691,6 @@ public Q_SLOTS: */ void sendTypingNotification(bool isTyping); - /** - * @brief Send a message to the room. - * - * @param rawText the text as it was typed. - * @param cleanedText the text marked up as html. - * @param type the type of message being sent. - * @param replyEventId the id of the message being replied to if a reply. - * @param relateToEventId the id of the message being edited if an edit. - */ - void postMessage(const QString &rawText, - const QString &cleanedText, - Quotient::MessageEventType type = Quotient::MessageEventType::Text, - const QString &replyEventId = QString(), - const QString &relateToEventId = QString(), - const QString &threadRootId = QString(), - const QString &fallbackId = QString()); - - /** - * @brief Send an html message to the room. - * - * @param text the text as it was typed. - * @param html the text marked up as html. - * @param type the type of message being sent. - * @param replyEventId the id of the message being replied to if a reply. - * @param relateToEventId the id of the message being edited if an edit. - */ - void postHtmlMessage(const QString &text, - const QString &html, - Quotient::MessageEventType type = Quotient::MessageEventType::Text, - const QString &replyEventId = QString(), - const QString &relateToEventId = QString(), - const QString &threadRootId = QString(), - const QString &fallbackId = QString()); - /** * @brief Set the room avatar. */ diff --git a/src/notificationsmanager.cpp b/src/notificationsmanager.cpp index ebe17f2a4..39bd3e30c 100644 --- a/src/notificationsmanager.cpp +++ b/src/notificationsmanager.cpp @@ -248,7 +248,9 @@ void NotificationsManager::postNotification(NeoChatRoom *room, connect(replyAction.get(), &KNotificationReplyAction::replied, this, [room, replyEventId](const QString &text) { TextHandler textHandler; textHandler.setData(text); - room->postMessage(text, textHandler.handleSendText(), RoomMessageEvent::MsgType::Text, replyEventId, QString()); + auto content = std::make_unique(textHandler.handleSendText(), u"text/html"_s); + EventRelation relatesTo = EventRelation::replyTo(replyEventId); + room->post(text, MessageEventType::Text, std::move(content), relatesTo); }); notification->setReplyAction(std::move(replyAction)); }