From 9391e44e4b7f68d78ed68e74ab7aee03b1256e31 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 23 Nov 2024 14:21:13 +0000 Subject: [PATCH] EventHandler Cleanup Remove functions from eventhandler and replace with now uplifted functions in libquotient --- autotests/eventhandlertest.cpp | 66 ----------------------------- src/eventhandler.cpp | 67 ------------------------------ src/eventhandler.h | 36 ---------------- src/models/messagecontentmodel.cpp | 17 ++++---- src/models/messageeventmodel.cpp | 22 +++++++--- src/models/searchmodel.cpp | 12 ++++-- src/models/threadmodel.cpp | 4 +- src/neochatroom.cpp | 2 +- 8 files changed, 38 insertions(+), 188 deletions(-) diff --git a/autotests/eventhandlertest.cpp b/autotests/eventhandlertest.cpp index 225a89934..412c78b5a 100644 --- a/autotests/eventhandlertest.cpp +++ b/autotests/eventhandlertest.cpp @@ -32,8 +32,6 @@ class EventHandlerTest : public QObject private Q_SLOTS: void initTestCase(); - void eventId(); - void nullEventId(); void authorDisplayName(); void nullAuthorDisplayName(); void singleLineSidplayName(); @@ -56,14 +54,8 @@ private Q_SLOTS: void nullSubtitle(); void mediaInfo(); void nullMediaInfo(); - void hasReply(); - void nullHasReply(); - void replyId(); - void nullReplyId(); void replyAuthor(); void nullReplyAuthor(); - void thread(); - void nullThread(); void location(); void nullLocation(); }; @@ -74,17 +66,6 @@ void EventHandlerTest::initTestCase() room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-eventhandler-sync.json")); } -void EventHandlerTest::eventId() -{ - QCOMPARE(EventHandler::id(room->messageEvents().at(0).get()), QStringLiteral("$153456789:example.org")); -} - -void EventHandlerTest::nullEventId() -{ - QTest::ignoreMessage(QtWarningMsg, "id called with event set to nullptr."); - QCOMPARE(EventHandler::id(nullptr), QString()); -} - void EventHandlerTest::authorDisplayName() { QCOMPARE(EventHandler::authorDisplayName(room, room->messageEvents().at(1).get()), QStringLiteral("before")); @@ -295,30 +276,6 @@ void EventHandlerTest::nullMediaInfo() QCOMPARE(EventHandler::mediaInfo(room, nullptr), QVariantMap()); } -void EventHandlerTest::hasReply() -{ - QCOMPARE(EventHandler::hasReply(room->messageEvents().at(5).get()), true); - QCOMPARE(EventHandler::hasReply(room->messageEvents().at(0).get()), false); -} - -void EventHandlerTest::nullHasReply() -{ - QTest::ignoreMessage(QtWarningMsg, "hasReply called with event set to nullptr."); - QCOMPARE(EventHandler::hasReply(nullptr), false); -} - -void EventHandlerTest::replyId() -{ - QCOMPARE(EventHandler::replyId(room->messageEvents().at(5).get()), QStringLiteral("$153456789:example.org")); - QCOMPARE(EventHandler::replyId(room->messageEvents().at(0).get()), QStringLiteral("")); -} - -void EventHandlerTest::nullReplyId() -{ - QTest::ignoreMessage(QtWarningMsg, "replyId called with event set to nullptr."); - QCOMPARE(EventHandler::replyId(nullptr), QString()); -} - void EventHandlerTest::replyAuthor() { auto replyEvent = room->messageEvents().at(0).get(); @@ -344,29 +301,6 @@ void EventHandlerTest::nullReplyAuthor() QCOMPARE(EventHandler::replyAuthor(room, nullptr), RoomMember()); } -void EventHandlerTest::thread() -{ - QCOMPARE(EventHandler::isThreaded(room->messageEvents().at(0).get()), false); - QCOMPARE(EventHandler::threadRoot(room->messageEvents().at(0).get()), QString()); - - QCOMPARE(EventHandler::isThreaded(room->messageEvents().at(9).get()), true); - QCOMPARE(EventHandler::threadRoot(room->messageEvents().at(9).get()), QStringLiteral("$threadroot:example.org")); - QCOMPARE(EventHandler::replyId(room->messageEvents().at(9).get()), QStringLiteral("$threadroot:example.org")); - - QCOMPARE(EventHandler::isThreaded(room->messageEvents().at(10).get()), true); - QCOMPARE(EventHandler::threadRoot(room->messageEvents().at(10).get()), QStringLiteral("$threadroot:example.org")); - QCOMPARE(EventHandler::replyId(room->messageEvents().at(10).get()), QStringLiteral("$threadmessage1:example.org")); -} - -void EventHandlerTest::nullThread() -{ - QTest::ignoreMessage(QtWarningMsg, "isThreaded called with event set to nullptr."); - QCOMPARE(EventHandler::isThreaded(nullptr), false); - - QTest::ignoreMessage(QtWarningMsg, "threadRoot called with event set to nullptr."); - QCOMPARE(EventHandler::threadRoot(nullptr), QString()); -} - void EventHandlerTest::location() { QCOMPARE(EventHandler::latitude(room->messageEvents().at(7).get()), QStringLiteral("51.7035").toFloat()); diff --git a/src/eventhandler.cpp b/src/eventhandler.cpp index 59c0183e3..1d3e8bed3 100644 --- a/src/eventhandler.cpp +++ b/src/eventhandler.cpp @@ -49,16 +49,6 @@ Q_DECLARE_FLAGS(MemberChanges, MemberChange) Q_DECLARE_OPERATORS_FOR_FLAGS(MemberChanges) }; -QString EventHandler::id(const Quotient::RoomEvent *event) -{ - if (event == nullptr) { - qCWarning(EventHandling) << "id called with event set to nullptr."; - return {}; - } - - return !event->id().isEmpty() ? event->id() : event->transactionId(); -} - QString EventHandler::authorDisplayName(const NeoChatRoom *room, const Quotient::RoomEvent *event, bool isPending) { if (room == nullptr) { @@ -834,31 +824,6 @@ QVariantMap EventHandler::getMediaInfoFromTumbnail(const NeoChatRoom *room, cons return thumbnailInfo; } -bool EventHandler::hasReply(const Quotient::RoomEvent *event, bool showFallbacks) -{ - if (event == nullptr) { - qCWarning(EventHandling) << "hasReply called with event set to nullptr."; - return false; - } - - const auto relations = event->contentPart("m.relates_to"_ls); - if (!relations.isEmpty()) { - const bool hasReplyRelation = relations.contains("m.in_reply_to"_ls); - bool isFallingBack = relations["is_falling_back"_ls].toBool(); - return hasReplyRelation && (showFallbacks ? true : !isFallingBack); - } - return false; -} - -QString EventHandler::replyId(const Quotient::RoomEvent *event) -{ - if (event == nullptr) { - qCWarning(EventHandling) << "replyId called with event set to nullptr."; - return {}; - } - return event->contentJson()["m.relates_to"_ls].toObject()["m.in_reply_to"_ls].toObject()["event_id"_ls].toString(); -} - Quotient::RoomMember EventHandler::replyAuthor(const NeoChatRoom *room, const Quotient::RoomEvent *event) { if (room == nullptr) { @@ -877,38 +842,6 @@ Quotient::RoomMember EventHandler::replyAuthor(const NeoChatRoom *room, const Qu } } -bool EventHandler::isThreaded(const Quotient::RoomEvent *event) -{ - if (event == nullptr) { - qCWarning(EventHandling) << "isThreaded called with event set to nullptr."; - return false; - } - - return (event->contentPart("m.relates_to"_ls).contains("rel_type"_ls) - && event->contentPart("m.relates_to"_ls)["rel_type"_ls].toString() == "m.thread"_ls) - || (!event->unsignedPart("m.relations"_ls).isEmpty() && event->unsignedPart("m.relations"_ls).contains("m.thread"_ls)); -} - -QString EventHandler::threadRoot(const Quotient::RoomEvent *event) -{ - if (event == nullptr) { - qCWarning(EventHandling) << "threadRoot called with event set to nullptr."; - return {}; - } - - // Get the thread root ID from m.relates_to if it exists. - if (event->contentPart("m.relates_to"_ls).contains("rel_type"_ls) - && event->contentPart("m.relates_to"_ls)["rel_type"_ls].toString() == "m.thread"_ls) { - return event->contentPart("m.relates_to"_ls)["event_id"_ls].toString(); - } - // For thread root events they have an m.relations in the unsigned part with a m.thread object. - // If so return the event ID as it is the root. - if (!event->unsignedPart("m.relations"_ls).isEmpty() && event->unsignedPart("m.relations"_ls).contains("m.thread"_ls)) { - return id(event); - } - return {}; -} - float EventHandler::latitude(const Quotient::RoomEvent *event) { if (event == nullptr) { diff --git a/src/eventhandler.h b/src/eventhandler.h index 350d850af..1220d43c9 100644 --- a/src/eventhandler.h +++ b/src/eventhandler.h @@ -37,14 +37,6 @@ class NeoChatRoom; class EventHandler { public: - /** - * @brief Return the ID of the event. - * - * Returns the transaction ID if the Matrix ID is empty, which may be the case - * for a pending event. - */ - static QString id(const Quotient::RoomEvent *event); - /** * @brief Get the display name of the event author. * @@ -220,20 +212,6 @@ class EventHandler */ static QVariantMap mediaInfo(const NeoChatRoom *room, const Quotient::RoomEvent *event); - /** - * @brief Whether the event is a reply to another in the timeline. - * - * @param showFallbacks whether message that have is_falling_back set true should - * show the fallback reply. Leave true for non-threaded - * timelines. - */ - static bool hasReply(const Quotient::RoomEvent *event, bool showFallbacks = true); - - /** - * @brief Return the Matrix ID of the event replied to. - */ - static QString replyId(const Quotient::RoomEvent *event); - /** * @brief Get the author of the event replied to in context of the room. * @@ -249,20 +227,6 @@ class EventHandler */ static Quotient::RoomMember replyAuthor(const NeoChatRoom *room, const Quotient::RoomEvent *event); - /** - * @brief Whether the message is part of a thread. - * - * i.e. There is a rel_type of m.thread. - */ - static bool isThreaded(const Quotient::RoomEvent *event); - - /** - * @brief Return the Matrix ID of the thread's root message. - * - * Empty if this not part of a thread. - */ - static QString threadRoot(const Quotient::RoomEvent *event); - /** * @brief Return the latitude for the event. * diff --git a/src/models/messagecontentmodel.cpp b/src/models/messagecontentmodel.cpp index 67fbf5c82..1d06f7434 100644 --- a/src/models/messagecontentmodel.cpp +++ b/src/models/messagecontentmodel.cpp @@ -305,7 +305,7 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const return component.attributes; } if (role == EventIdRole) { - return EventHandler::id(event.first); + return event.first->displayId(); } if (role == TimeRole) { const auto pendingIt = std::find_if(m_room->pendingEvents().cbegin(), m_room->pendingEvents().cend(), [event](const PendingEventItem &pendingEvent) { @@ -348,7 +348,9 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const return QVariant::fromValue(m_room->poll(m_eventId)); } if (role == ReplyEventIdRole) { - return EventHandler::replyId(event.first); + if (const auto roomMessageEvent = eventCast(event.first)) { + return roomMessageEvent->replyEventId(); + } } if (role == ReplyAuthorRole) { return QVariant::fromValue(EventHandler::replyAuthor(m_room, event.first)); @@ -456,8 +458,8 @@ QList MessageContentModel::messageContentComponents(bool isEdi QList newComponents; - if (eventCast(event.first) - && eventCast(event.first)->rawMsgtype() == QStringLiteral("m.key.verification.request")) { + const auto roomMessageEvent = eventCast(event.first); + if (roomMessageEvent && roomMessageEvent->rawMsgtype() == QStringLiteral("m.key.verification.request")) { newComponents += MessageComponent{MessageComponentType::Verification, QString(), {}}; return newComponents; } @@ -482,7 +484,7 @@ QList MessageContentModel::messageContentComponents(bool isEdi } // If the event is already threaded the ThreadModel will handle displaying a chat bar. - if (isThreading && !EventHandler::isThreaded(event.first)) { + if (isThreading && roomMessageEvent && roomMessageEvent->isThreaded()) { newComponents += MessageComponent{MessageComponentType::ChatBar, QString(), {}}; } @@ -496,7 +498,8 @@ void MessageContentModel::updateReplyModel() return; } - if (!EventHandler::hasReply(event.first) || (EventHandler::isThreaded(event.first) && NeoChatConfig::self()->threads())) { + const auto roomMessageEvent = eventCast(event.first); + if (!roomMessageEvent->isReply() || (roomMessageEvent && roomMessageEvent->isThreaded() && NeoChatConfig::self()->threads())) { if (m_replyModel) { delete m_replyModel; } @@ -507,7 +510,7 @@ void MessageContentModel::updateReplyModel() return; } - m_replyModel = new MessageContentModel(m_room, EventHandler::replyId(event.first), true, false, this); + m_replyModel = new MessageContentModel(m_room, roomMessageEvent->replyEventId(), true, false, this); connect(m_replyModel, &MessageContentModel::eventUpdated, this, [this]() { Q_EMIT dataChanged(index(0), index(0), {ReplyAuthorRole}); diff --git a/src/models/messageeventmodel.cpp b/src/models/messageeventmodel.cpp index 188db292e..d6ba98a5b 100644 --- a/src/models/messageeventmodel.cpp +++ b/src/models/messageeventmodel.cpp @@ -501,7 +501,8 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const return EventStatus::Hidden; } - if (EventHandler::isThreaded(&evt) && EventHandler::threadRoot(&evt) != EventHandler::id(&evt) && NeoChatConfig::threads()) { + auto roomMessageEvent = eventCast(&evt); + if (roomMessageEvent && roomMessageEvent->isThreaded() && roomMessageEvent->threadRootEventId() != evt.id() && NeoChatConfig::threads()) { return EventStatus::Hidden; } @@ -509,7 +510,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == EventIdRole) { - return EventHandler::id(&evt); + return evt.displayId(); } if (role == ProgressInfoRole) { @@ -534,11 +535,18 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == IsThreadedRole) { - return EventHandler::isThreaded(&evt); + if (auto roomMessageEvent = eventCast(&evt)) { + return roomMessageEvent->isThreaded(); + } + return {}; } if (role == ThreadRootRole) { - return EventHandler::threadRoot(&evt); + auto roomMessageEvent = eventCast(&evt); + if (roomMessageEvent && roomMessageEvent->isThreaded()) { + return roomMessageEvent->threadRootEventId(); + } + return {}; } if (role == ShowSectionRole) { @@ -654,8 +662,10 @@ void MessageEventModel::createEventObjects(const Quotient::RoomEvent *event, boo } } - if (EventHandler::isThreaded(event) && !m_threadModels.contains(EventHandler::threadRoot(event))) { - m_threadModels[EventHandler::threadRoot(event)] = QSharedPointer(new ThreadModel(EventHandler::threadRoot(event), m_currentRoom)); + const auto roomMessageEvent = eventCast(event); + if (roomMessageEvent && roomMessageEvent->isThreaded() && !m_threadModels.contains(roomMessageEvent->threadRootEventId())) { + m_threadModels[roomMessageEvent->threadRootEventId()] = + QSharedPointer(new ThreadModel(roomMessageEvent->threadRootEventId(), m_currentRoom)); } // ReadMarkerModel handles updates to add and remove markers, we only need to diff --git a/src/models/searchmodel.cpp b/src/models/searchmodel.cpp index faa15e7c4..f3853f872 100644 --- a/src/models/searchmodel.cpp +++ b/src/models/searchmodel.cpp @@ -108,11 +108,17 @@ QVariant SearchModel::data(const QModelIndex &index, int role) const case HighlightRole: return EventHandler::isHighlighted(m_room, &event); case EventIdRole: - return EventHandler::id(&event); + return event.displayId(); case IsThreadedRole: - return EventHandler::isThreaded(&event); + if (auto roomMessageEvent = eventCast(&event)) { + return roomMessageEvent->isThreaded(); + } + return {}; case ThreadRootRole: - return EventHandler::threadRoot(&event); + if (auto roomMessageEvent = eventCast(&event); roomMessageEvent->isThreaded()) { + return roomMessageEvent->threadRootEventId(); + } + return {}; case ContentModelRole: { if (!event.isStateEvent()) { return QVariant::fromValue(new MessageContentModel(m_room, event.id())); diff --git a/src/models/threadmodel.cpp b/src/models/threadmodel.cpp index a1a6e6046..a2fddaf36 100644 --- a/src/models/threadmodel.cpp +++ b/src/models/threadmodel.cpp @@ -25,7 +25,7 @@ ThreadModel::ThreadModel(const QString &threadRootId, NeoChatRoom *room) connect(room, &Quotient::Room::pendingEventAboutToAdd, this, [this](Quotient::RoomEvent *event) { if (auto roomEvent = eventCast(event)) { - if (EventHandler::isThreaded(roomEvent) && EventHandler::threadRoot(roomEvent) == m_threadRootId) { + if (roomEvent->isThreaded() && roomEvent->threadRootEventId() == m_threadRootId) { addNewEvent(event); addModels(); } @@ -34,7 +34,7 @@ ThreadModel::ThreadModel(const QString &threadRootId, NeoChatRoom *room) connect(room, &Quotient::Room::aboutToAddNewMessages, this, [this](Quotient::RoomEventsRange events) { for (const auto &event : events) { if (auto roomEvent = eventCast(event)) { - if (EventHandler::isThreaded(roomEvent) && EventHandler::threadRoot(roomEvent) == m_threadRootId) { + if (roomEvent->isThreaded() && roomEvent->threadRootEventId() == m_threadRootId) { addNewEvent(roomEvent); } } diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 315ca099d..ec5fa24c5 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -514,7 +514,7 @@ void NeoChatRoom::postHtmlMessage(const QString &text, QString replyEventId = isFallingBack ? fallbackId : QString(); if (isReply) { isFallingBack = false; - replyEventId = EventHandler::id(replyIt->get()); + replyEventId = replyIt->event()->displayId(); } // If we are not replying and there is no fallback ID it means a new thread