From c6313d29515d7fe71328bad9679313aa8d046703 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 21 Feb 2026 12:12:11 +0000 Subject: [PATCH] Hook sending messages back up --- autotests/actionstest.cpp | 5 +- autotests/chatbarcachetest.cpp | 19 +++--- src/libneochat/blockcache.cpp | 67 +++++++++++++++++++ src/libneochat/blockcache.h | 5 ++ src/libneochat/chatbarcache.cpp | 26 +++---- src/libneochat/chatbarcache.h | 14 ---- src/libneochat/chattextitemhelper.cpp | 1 - src/libneochat/chattextitemhelper.h | 1 - .../models/chatbarmessagecontentmodel.cpp | 53 --------------- .../models/chatbarmessagecontentmodel.h | 1 - 10 files changed, 94 insertions(+), 98 deletions(-) diff --git a/autotests/actionstest.cpp b/autotests/actionstest.cpp index 2273d6e47..5137b6107 100644 --- a/autotests/actionstest.cpp +++ b/autotests/actionstest.cpp @@ -7,7 +7,9 @@ #include #include "accountmanager.h" +#include "blockcache.h" #include "chatbarcache.h" +#include "enums/messagecomponenttype.h" #include "models/actionsmodel.h" #include "server.h" @@ -89,8 +91,9 @@ void ActionsTest::testActions() QFETCH(std::optional, type); auto cache = new ChatBarCache(this); - cache->setText(command); + cache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(command)}; auto result = ActionsModel::handleAction(room, cache); + qWarning() << result << resultText; QCOMPARE(resultText, std::get>(result)); QCOMPARE(type, std::get>(result)); } diff --git a/autotests/chatbarcachetest.cpp b/autotests/chatbarcachetest.cpp index 5f635c827..a31cd931a 100644 --- a/autotests/chatbarcachetest.cpp +++ b/autotests/chatbarcachetest.cpp @@ -14,6 +14,7 @@ #include #include "accountmanager.h" +#include "blockcache.h" #include "chatbarcache.h" #include "neochatroom.h" @@ -77,7 +78,7 @@ void ChatBarCacheTest::empty() { QScopedPointer chatBarCache(new ChatBarCache(room)); - QCOMPARE(chatBarCache->text(), QString()); + QCOMPARE(chatBarCache->cache().toString(), QString()); QCOMPARE(chatBarCache->isReplying(), false); QCOMPARE(chatBarCache->replyId(), QString()); QCOMPARE(chatBarCache->isEditing(), false); @@ -123,11 +124,11 @@ void ChatBarCacheTest::badParent() void ChatBarCacheTest::reply() { QScopedPointer chatBarCache(new ChatBarCache(room)); - chatBarCache->setText(u"some text"_s); + chatBarCache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(u"some text"_s)}; chatBarCache->setAttachmentPath(u"some/path"_s); chatBarCache->setReplyId(eventId); - QCOMPARE(chatBarCache->text(), u"some text"_s); + QCOMPARE(chatBarCache->cache().toString(), u"some text"_s); QCOMPARE(chatBarCache->isReplying(), true); QCOMPARE(chatBarCache->replyId(), eventId); QCOMPARE(chatBarCache->isEditing(), false); @@ -141,11 +142,11 @@ void ChatBarCacheTest::reply() void ChatBarCacheTest::replyMissingUser() { QScopedPointer chatBarCache(new ChatBarCache(room)); - chatBarCache->setText(u"some text"_s); + chatBarCache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(u"some text"_s)}; chatBarCache->setAttachmentPath(u"some/path"_s); chatBarCache->setReplyId(eventId); - QCOMPARE(chatBarCache->text(), u"some text"_s); + QCOMPARE(chatBarCache->cache().toString(), u"some text"_s); QCOMPARE(chatBarCache->isReplying(), true); QCOMPARE(chatBarCache->replyId(), eventId); QCOMPARE(chatBarCache->isEditing(), false); @@ -172,7 +173,7 @@ void ChatBarCacheTest::edit() { QScopedPointer chatBarCache(new ChatBarCache(room)); - chatBarCache->setText(u"some text"_s); + chatBarCache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(u"some text"_s)}; chatBarCache->setAttachmentPath(u"some/path"_s); connect(chatBarCache.get(), &ChatBarCache::relationIdChanged, this, [this](const QString &oldEventId, const QString &newEventId) { QCOMPARE(oldEventId, QString()); @@ -180,7 +181,7 @@ void ChatBarCacheTest::edit() }); chatBarCache->setEditId(eventId); - QCOMPARE(chatBarCache->text(), u"some text"_s); + QCOMPARE(chatBarCache->cache().toString(), u"some text"_s); QCOMPARE(chatBarCache->isReplying(), false); QCOMPARE(chatBarCache->replyId(), QString()); QCOMPARE(chatBarCache->isEditing(), true); @@ -193,11 +194,11 @@ void ChatBarCacheTest::edit() void ChatBarCacheTest::attachment() { QScopedPointer chatBarCache(new ChatBarCache(room)); - chatBarCache->setText(u"some text"_s); + chatBarCache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(u"some text"_s)}; chatBarCache->setEditId(eventId); chatBarCache->setAttachmentPath(u"some/path"_s); - QCOMPARE(chatBarCache->text(), u"some text"_s); + QCOMPARE(chatBarCache->cache().toString(), u"some text"_s); QCOMPARE(chatBarCache->isReplying(), false); QCOMPARE(chatBarCache->replyId(), QString()); QCOMPARE(chatBarCache->isEditing(), false); diff --git a/src/libneochat/blockcache.cpp b/src/libneochat/blockcache.cpp index 68c671bae..13f7e5ec5 100644 --- a/src/libneochat/blockcache.cpp +++ b/src/libneochat/blockcache.cpp @@ -3,10 +3,65 @@ #include "blockcache.h" +#include + #include "chattextitemhelper.h" using namespace Block; +inline QString formatQuote(const QString &input) +{ + QString stringOut; + auto splitString = input.split(u"\n\n"_s, Qt::SkipEmptyParts); + for (auto &string : splitString) { + if (string.startsWith(u'*')) { + string.removeFirst(); + } + if (string.startsWith(u'\"')) { + string.removeFirst(); + } + if (string.endsWith(u'*')) { + string.removeLast(); + } + if (string.endsWith(u'\"')) { + string.removeLast(); + } + if (!stringOut.isEmpty()) { + stringOut += u"\n"_s; + } + stringOut += u"> "_s + string; + } + return stringOut; +} + +inline QString formatCode(const QString &input) +{ + return u"```\n%1\n```"_s.arg(input).replace(u"\n\n"_s, u"\n"_s); +} + +inline QString trim(QString string) +{ + while (string.startsWith(u"\n"_s)) { + string.removeFirst(); + } + while (string.endsWith(u"\n"_s)) { + string.removeLast(); + } + return string; +} + +QString CacheItem::toString() const +{ + auto newText = trim(content.toMarkdown(QTextDocument::MarkdownDialectGitHub)); + newText.replace(QRegularExpression(u"(? components) { std::ranges::for_each(components, [this](const MessageComponent &component) { @@ -23,3 +78,15 @@ void Cache::fill(QList components) }); }); } + +QString Cache::toString() const +{ + QString text; + std::ranges::for_each(constBegin(), constEnd(), [&text](const CacheItem &item) { + if (!text.isEmpty()) { + text += u"\n\n"_s; + } + text += item.toString(); + }); + return text; +} diff --git a/src/libneochat/blockcache.h b/src/libneochat/blockcache.h index e1a694f1d..b229806ff 100644 --- a/src/libneochat/blockcache.h +++ b/src/libneochat/blockcache.h @@ -14,6 +14,8 @@ namespace Block struct CacheItem { MessageComponentType::Type type = MessageComponentType::Other; QTextDocumentFragment content; + + QString toString() const; }; class Cache : private QList @@ -22,7 +24,10 @@ public: using QList::constBegin, QList::constEnd; using QList::isEmpty; using QList::clear; + using QList::append, QList::operator+=, QList::operator<<; void fill(QList components); + + QString toString() const; }; } diff --git a/src/libneochat/chatbarcache.cpp b/src/libneochat/chatbarcache.cpp index 475f36497..48c9d5922 100644 --- a/src/libneochat/chatbarcache.cpp +++ b/src/libneochat/chatbarcache.cpp @@ -38,29 +38,16 @@ Block::Cache &ChatBarCache::cache() return m_cache; } -QString ChatBarCache::text() const -{ - return m_text; -} - -void ChatBarCache::setText(const QString &text) -{ - if (text == m_text) { - return; - } - m_text = text; - Q_EMIT textChanged(); -} - QString ChatBarCache::sendText() const { + const auto cacheText = m_cache.toString(); if (!attachmentPath().isEmpty()) { QUrl url(attachmentPath()); auto path = url.isLocalFile() ? url.toLocalFile() : url.toString(); - return text().isEmpty() ? path.mid(path.lastIndexOf(u'/') + 1) : text(); + return cacheText.isEmpty() ? path.mid(path.lastIndexOf(u'/') + 1) : cacheText; } - return text(); + return cacheText; } bool ChatBarCache::isReplying() const @@ -267,13 +254,16 @@ void ChatBarCache::postMessage() auto content = std::make_unique(sendText, u"text/html"_s); - room->post(text(), *std::get>(result), std::move(content), relatesTo); + room->post(m_cache.toString(), + *std::get>(result), + std::move(content), + relatesTo); clearCache(); } void ChatBarCache::clearCache() { - setText({}); + m_cache.clear(); m_savedText = QString(); clearRelations(); } diff --git a/src/libneochat/chatbarcache.h b/src/libneochat/chatbarcache.h index 8e57c54e3..cda6793a5 100644 --- a/src/libneochat/chatbarcache.h +++ b/src/libneochat/chatbarcache.h @@ -39,14 +39,6 @@ class ChatBarCache : public QObject QML_ELEMENT QML_UNCREATABLE("") - /** - * @brief The text in the chat bar. - * - * Due to problems with QTextDocument, unlike the other properties here, - * text is *not* used to store the text when switching rooms. - */ - Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) - /** * @brief Whether the chat bar is currently replying to a message. */ @@ -139,10 +131,7 @@ public: explicit ChatBarCache(QObject *parent = nullptr); Block::Cache &cache(); - - QString text() const; QString sendText() const; - void setText(const QString &text); bool isReplying() const; QString replyId() const; @@ -189,7 +178,6 @@ public: Q_INVOKABLE void drop(QList urls, const QString &transferPortal); Q_SIGNALS: - void textChanged(); void relationIdChanged(const QString &oldEventId, const QString &newEventId); void threadIdChanged(const QString &oldThreadId, const QString &newThreadId); void attachmentPathChanged(); @@ -199,8 +187,6 @@ Q_SIGNALS: private: Block::Cache m_cache; - QString m_text = QString(); - QString m_relationId = QString(); RelationType m_relationType = RelationType::None; QString m_threadId = QString(); diff --git a/src/libneochat/chattextitemhelper.cpp b/src/libneochat/chattextitemhelper.cpp index b80ca4304..069928a15 100644 --- a/src/libneochat/chattextitemhelper.cpp +++ b/src/libneochat/chattextitemhelper.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include diff --git a/src/libneochat/chattextitemhelper.h b/src/libneochat/chattextitemhelper.h index 6c707de06..3595dbf1e 100644 --- a/src/libneochat/chattextitemhelper.h +++ b/src/libneochat/chattextitemhelper.h @@ -6,7 +6,6 @@ #include #include #include -#include #include "enums/chatbartype.h" #include "enums/richformat.h" diff --git a/src/messagecontent/models/chatbarmessagecontentmodel.cpp b/src/messagecontent/models/chatbarmessagecontentmodel.cpp index 814d9361a..1443950a1 100644 --- a/src/messagecontent/models/chatbarmessagecontentmodel.cpp +++ b/src/messagecontent/models/chatbarmessagecontentmodel.cpp @@ -596,59 +596,6 @@ void ChatBarMessageContentModel::updateCache() const m_room->cacheForType(m_type)->cache().fill(m_components); } -inline QString formatQuote(const QString &input) -{ - QString stringOut; - auto splitString = input.split(u"\n\n"_s, Qt::SkipEmptyParts); - for (auto &string : splitString) { - if (string.startsWith(u'*')) { - string.removeFirst(); - } - if (string.startsWith(u'\"')) { - string.removeFirst(); - } - if (string.endsWith(u'*')) { - string.removeLast(); - } - if (string.endsWith(u'\"')) { - string.removeLast(); - } - if (!stringOut.isEmpty()) { - stringOut += u"\n"_s; - } - stringOut += u"> "_s + string; - } - return stringOut; -} - -inline QString formatCode(const QString &input) -{ - return u"```\n%1\n```"_s.arg(input).replace(u"\n\n"_s, u"\n"_s); -} - -QString ChatBarMessageContentModel::messageText() const -{ - QString text; - for (const auto &component : m_components) { - if (MessageComponentType::isTextType(component.type)) { - if (const auto textItem = textItemForComponent(component)) { - auto newText = textItem->markdownText(); - newText.replace(QRegularExpression(u"(?