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<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)};
auto result = ActionsModel::handleAction(room, cache);
QCOMPARE(resultText, std::get<std::optional<QString>>(result));

View File

@@ -37,8 +37,6 @@ private Q_SLOTS:
void initTestCase();
void empty();
void noRoom();
void badParent();
void reply();
void replyMissingUser();
void edit();
@@ -88,39 +86,6 @@ void ChatBarCacheTest::empty()
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()
{
QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(room));

View File

@@ -60,6 +60,13 @@ target_sources(LibNeoChat PRIVATE
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)
target_compile_definitions(LibNeoChat PUBLIC -DHAVE_KIO)
endif()

View File

@@ -14,20 +14,15 @@
#include "neochatroom.h"
#include "texthandler.h"
#include "chatbarlogging.h"
using namespace Qt::StringLiterals;
ChatBarCache::ChatBarCache(QObject *parent)
: QObject(parent)
ChatBarCache::ChatBarCache(NeoChatRoom *room)
: QObject(room)
, m_room(room)
{
if (parent == nullptr) {
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;
}
Q_ASSERT(room);
connect(room, &NeoChatRoom::memberLeft, this, &ChatBarCache::relationAuthorIsPresentChanged);
connect(room, &NeoChatRoom::memberJoined, 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
{
if (parent() == nullptr) {
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.";
if (!m_room) {
qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted";
return {};
}
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) {
return room->member(event->senderId());
return m_room->member(event->senderId());
}
qWarning() << "Failed to find relation" << m_relationId << "in timeline?";
return room->member(QString());
qCWarning(ChatBar) << "Failed to find relation" << m_relationId << "in timeline?";
return m_room->member(QString());
}
bool ChatBarCache::relationAuthorIsPresent() const
@@ -137,20 +127,14 @@ bool ChatBarCache::relationAuthorIsPresent() const
QString ChatBarCache::relationMessage() const
{
if (parent() == nullptr) {
qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.";
if (!m_room) {
qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted";
return {};
}
if (m_relationId.isEmpty()) {
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 {};
}
if (auto [event, _] = room->getEvent(m_relationId); event != nullptr) {
if (auto [event, _] = m_room->getEvent(m_relationId); event != nullptr) {
return EventHandler::markdownBody(event);
}
return {};
@@ -216,9 +200,8 @@ void ChatBarCache::setSavedText(const QString &savedText)
void ChatBarCache::postMessage()
{
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.";
if (!m_room) {
qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted";
return;
}
@@ -234,12 +217,12 @@ void ChatBarCache::postMessage()
}
if (!attachmentPath().isEmpty()) {
room->uploadFile(QUrl(attachmentPath()), sendText(), relatesTo);
m_room->uploadFile(QUrl(attachmentPath()), sendText(), relatesTo);
clearCache();
return;
}
const auto result = ActionsModel::handleAction(room, this);
const auto result = ActionsModel::handleAction(m_room, this);
if (!result.second.has_value()) {
return;
}
@@ -254,7 +237,7 @@ void ChatBarCache::postMessage()
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::move(content),
relatesTo);

View File

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