Clean up chatbarcache

This commit is contained in:
Tobias Fella
2026-02-20 13:18:43 +01:00
parent 23c0bdb647
commit 0375ef99a5
5 changed files with 33 additions and 75 deletions

View File

@@ -90,7 +90,7 @@ void ActionsTest::testActions()
QFETCH(std::optional<QString>, resultText); QFETCH(std::optional<QString>, resultText);
QFETCH(std::optional<Quotient::RoomMessageEvent::MsgType>, type); QFETCH(std::optional<Quotient::RoomMessageEvent::MsgType>, type);
auto cache = new ChatBarCache(this); auto cache = new ChatBarCache(room);
cache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(command)}; cache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(command)};
auto result = ActionsModel::handleAction(room, cache); auto result = ActionsModel::handleAction(room, cache);
QCOMPARE(resultText, std::get<std::optional<QString>>(result)); QCOMPARE(resultText, std::get<std::optional<QString>>(result));

View File

@@ -37,8 +37,6 @@ private Q_SLOTS:
void initTestCase(); void initTestCase();
void empty(); void empty();
void noRoom();
void badParent();
void reply(); void reply();
void replyMissingUser(); void replyMissingUser();
void edit(); void edit();
@@ -88,39 +86,6 @@ void ChatBarCacheTest::empty()
QCOMPARE(chatBarCache->attachmentPath(), QString()); QCOMPARE(chatBarCache->attachmentPath(), QString());
} }
void ChatBarCacheTest::noRoom()
{
QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.");
QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache());
chatBarCache->setReplyId(eventId);
// These should return empty even though a reply ID has been set because the
// ChatBarCache has no parent.
QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.");
QCOMPARE(chatBarCache->relationAuthor(), Quotient::RoomMember());
QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.");
QCOMPARE(chatBarCache->relationMessage(), QString());
}
void ChatBarCacheTest::badParent()
{
QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.");
QScopedPointer<QObject> badParent(new QObject());
QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(badParent.get()));
chatBarCache->setReplyId(eventId);
// These should return empty even though a reply ID has been set because the
// ChatBarCache has no parent.
QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.");
QCOMPARE(chatBarCache->relationAuthor(), Quotient::RoomMember());
QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.");
QCOMPARE(chatBarCache->relationMessage(), QString());
}
void ChatBarCacheTest::reply() void ChatBarCacheTest::reply()
{ {
QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(room)); QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(room));

View File

@@ -60,6 +60,13 @@ target_sources(LibNeoChat PRIVATE
models/widgetmodel.h models/widgetmodel.h
) )
ecm_qt_declare_logging_category(LibNeoChat
HEADER "chatbarlogging.h"
IDENTIFIER "ChatBar"
CATEGORY_NAME "org.kde.neochat.chatbar"
DEFAULT_SEVERITY Info
)
if (TARGET KF6::KIOWidgets) if (TARGET KF6::KIOWidgets)
target_compile_definitions(LibNeoChat PUBLIC -DHAVE_KIO) target_compile_definitions(LibNeoChat PUBLIC -DHAVE_KIO)
endif() endif()

View File

@@ -14,20 +14,15 @@
#include "neochatroom.h" #include "neochatroom.h"
#include "texthandler.h" #include "texthandler.h"
#include "chatbarlogging.h"
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;
ChatBarCache::ChatBarCache(QObject *parent) ChatBarCache::ChatBarCache(NeoChatRoom *room)
: QObject(parent) : QObject(room)
, m_room(room)
{ {
if (parent == nullptr) { Q_ASSERT(room);
qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.";
return;
}
auto room = dynamic_cast<NeoChatRoom *>(parent);
if (room == nullptr) {
qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.";
return;
}
connect(room, &NeoChatRoom::memberLeft, this, &ChatBarCache::relationAuthorIsPresentChanged); connect(room, &NeoChatRoom::memberLeft, this, &ChatBarCache::relationAuthorIsPresentChanged);
connect(room, &NeoChatRoom::memberJoined, this, &ChatBarCache::relationAuthorIsPresentChanged); connect(room, &NeoChatRoom::memberJoined, this, &ChatBarCache::relationAuthorIsPresentChanged);
connect(this, &ChatBarCache::relationIdChanged, this, &ChatBarCache::relationAuthorIsPresentChanged); connect(this, &ChatBarCache::relationIdChanged, this, &ChatBarCache::relationAuthorIsPresentChanged);
@@ -110,24 +105,19 @@ void ChatBarCache::setEditId(const QString &editId)
Quotient::RoomMember ChatBarCache::relationAuthor() const Quotient::RoomMember ChatBarCache::relationAuthor() const
{ {
if (parent() == nullptr) { if (!m_room) {
qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."; qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted";
return {};
}
auto room = dynamic_cast<NeoChatRoom *>(parent());
if (room == nullptr) {
qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.";
return {}; return {};
} }
if (m_relationId.isEmpty()) { if (m_relationId.isEmpty()) {
return room->member(QString()); return m_room->member({});
} }
const auto [event, _] = room->getEvent(m_relationId); const auto [event, _] = m_room->getEvent(m_relationId);
if (event != nullptr) { if (event != nullptr) {
return room->member(event->senderId()); return m_room->member(event->senderId());
} }
qWarning() << "Failed to find relation" << m_relationId << "in timeline?"; qCWarning(ChatBar) << "Failed to find relation" << m_relationId << "in timeline?";
return room->member(QString()); return m_room->member(QString());
} }
bool ChatBarCache::relationAuthorIsPresent() const bool ChatBarCache::relationAuthorIsPresent() const
@@ -137,20 +127,14 @@ bool ChatBarCache::relationAuthorIsPresent() const
QString ChatBarCache::relationMessage() const QString ChatBarCache::relationMessage() const
{ {
if (parent() == nullptr) { if (!m_room) {
qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."; qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted";
return {}; return {};
} }
if (m_relationId.isEmpty()) { if (m_relationId.isEmpty()) {
return {}; return {};
} }
auto room = dynamic_cast<NeoChatRoom *>(parent()); if (auto [event, _] = m_room->getEvent(m_relationId); event != nullptr) {
if (room == nullptr) {
qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.";
return {};
}
if (auto [event, _] = room->getEvent(m_relationId); event != nullptr) {
return EventHandler::markdownBody(event); return EventHandler::markdownBody(event);
} }
return {}; return {};
@@ -216,9 +200,8 @@ void ChatBarCache::setSavedText(const QString &savedText)
void ChatBarCache::postMessage() void ChatBarCache::postMessage()
{ {
auto room = dynamic_cast<NeoChatRoom *>(parent()); if (!m_room) {
if (room == nullptr) { qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted";
qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.";
return; return;
} }
@@ -234,12 +217,12 @@ void ChatBarCache::postMessage()
} }
if (!attachmentPath().isEmpty()) { if (!attachmentPath().isEmpty()) {
room->uploadFile(QUrl(attachmentPath()), sendText(), relatesTo); m_room->uploadFile(QUrl(attachmentPath()), sendText(), relatesTo);
clearCache(); clearCache();
return; return;
} }
const auto result = ActionsModel::handleAction(room, this); const auto result = ActionsModel::handleAction(m_room, this);
if (!result.second.has_value()) { if (!result.second.has_value()) {
return; return;
} }
@@ -254,7 +237,7 @@ void ChatBarCache::postMessage()
auto content = std::make_unique<Quotient::EventContent::TextContent>(sendText, u"text/html"_s); auto content = std::make_unique<Quotient::EventContent::TextContent>(sendText, u"text/html"_s);
room->post<Quotient::RoomMessageEvent>(m_cache.toString(), m_room->post<Quotient::RoomMessageEvent>(m_cache.toString(),
*std::get<std::optional<Quotient::RoomMessageEvent::MsgType>>(result), *std::get<std::optional<Quotient::RoomMessageEvent::MsgType>>(result),
std::move(content), std::move(content),
relatesTo); relatesTo);

View File

@@ -15,6 +15,8 @@ namespace Quotient
class RoomMember; class RoomMember;
} }
class NeoChatRoom;
/** /**
* @class ChatBarCache * @class ChatBarCache
* *
@@ -128,7 +130,7 @@ public:
}; };
Q_ENUM(RelationType) Q_ENUM(RelationType)
explicit ChatBarCache(QObject *parent = nullptr); explicit ChatBarCache(NeoChatRoom *room);
Block::Cache &cache(); Block::Cache &cache();
QString sendText() const; QString sendText() const;
@@ -186,6 +188,7 @@ Q_SIGNALS:
private: private:
Block::Cache m_cache; Block::Cache m_cache;
QPointer<NeoChatRoom> m_room;
QString m_relationId = QString(); QString m_relationId = QString();
RelationType m_relationType = RelationType::None; RelationType m_relationType = RelationType::None;