From c56054a05a0869538d77af5f585ba34a41786b2d Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 5 Apr 2025 10:08:44 +0000 Subject: [PATCH] Move pollhandler to ContentProvider Move `PollHandler` to `ContentProvider`, this also breaks the dependency on `PollHandler` for `NeoChatRoom` --- src/contentprovider.cpp | 22 ++++++++++++++++++++++ src/contentprovider.h | 12 ++++++++++++ src/models/messagecontentmodel.cpp | 2 +- src/models/messagemodel.h | 1 - src/neochatroom.cpp | 17 ----------------- src/neochatroom.h | 15 +-------------- 6 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/contentprovider.cpp b/src/contentprovider.cpp index 37b600ecd..62117a314 100644 --- a/src/contentprovider.cpp +++ b/src/contentprovider.cpp @@ -85,3 +85,25 @@ ThreadModel *ContentProvider::modelForThread(NeoChatRoom *room, const QString &t return m_threadModels.object(threadRootId); } + +static PollHandler *emptyPollHandler = new PollHandler; + +PollHandler *ContentProvider::handlerForPoll(NeoChatRoom *room, const QString &eventId) +{ + if (!room || eventId.isEmpty()) { + return nullptr; + } + + const auto event = room->getEvent(eventId); + if (event.first == nullptr || event.second) { + return emptyPollHandler; + } + + if (!m_pollHandlers.contains(eventId)) { + m_pollHandlers.insert(eventId, new PollHandler(room, eventId)); + } + + return m_pollHandlers.object(eventId); +} + +#include "moc_contentprovider.cpp" diff --git a/src/contentprovider.h b/src/contentprovider.h index 542e936d3..c5aee38b9 100644 --- a/src/contentprovider.h +++ b/src/contentprovider.h @@ -10,6 +10,7 @@ #include "models/messagecontentmodel.h" #include "models/threadmodel.h" #include "neochatroom.h" +#include "pollhandler.h" /** * @class ContentProvider @@ -70,9 +71,20 @@ public: */ ThreadModel *modelForThread(NeoChatRoom *room, const QString &threadRootId); + /** + * @brief Get a PollHandler object for the given event Id. + * + * Will return an existing PollHandler if one already exists for the event ID. + * A new PollHandler will be created if one doesn't exist. + * + * @sa PollHandler + */ + PollHandler *handlerForPoll(NeoChatRoom *room, const QString &eventId); + private: explicit ContentProvider(QObject *parent = nullptr); QCache m_eventContentModels; QCache m_threadModels; + QCache m_pollHandlers; }; diff --git a/src/models/messagecontentmodel.cpp b/src/models/messagecontentmodel.cpp index 439e053de..9e8b14482 100644 --- a/src/models/messagecontentmodel.cpp +++ b/src/models/messagecontentmodel.cpp @@ -334,7 +334,7 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const return EventHandler::locationAssetType(event.first); } if (role == PollHandlerRole) { - return QVariant::fromValue(m_room->poll(m_eventId)); + return QVariant::fromValue(ContentProvider::self().handlerForPoll(m_room, m_eventId)); } if (role == ReplyEventIdRole) { if (const auto roomMessageEvent = eventCast(event.first)) { diff --git a/src/models/messagemodel.h b/src/models/messagemodel.h index ca2743e55..835bba8bb 100644 --- a/src/models/messagemodel.h +++ b/src/models/messagemodel.h @@ -8,7 +8,6 @@ #include #include "neochatroom.h" -#include "pollhandler.h" #include "readmarkermodel.h" #include "threadmodel.h" diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 1f6778b99..8c431088b 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -1423,23 +1423,6 @@ bool NeoChatRoom::canEncryptRoom() const return !usesEncryption() && canSendState("m.room.encryption"_L1); } -static PollHandler *emptyPollHandler = new PollHandler; - -PollHandler *NeoChatRoom::poll(const QString &eventId) -{ - const auto event = getEvent(eventId); - if (event.first == nullptr || event.second) { - return emptyPollHandler; - } - - if (m_polls.contains(eventId)) { - return m_polls[eventId]; - } - auto handler = new PollHandler(this, eventId); - m_polls.insert(eventId, handler); - return handler; -} - void NeoChatRoom::postPoll(PollKind::Kind kind, const QString &question, const QList &answers) { QList answerStructs; diff --git a/src/neochatroom.h b/src/neochatroom.h index e292de7e0..ab20ead77 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -14,8 +14,8 @@ #include "enums/messagetype.h" #include "enums/pushrule.h" +#include "events/pollevent.h" #include "neochatroommember.h" -#include "pollhandler.h" namespace Quotient { @@ -488,18 +488,6 @@ public: */ Q_INVOKABLE void editLastMessage(); - /** - * @brief Get a PollHandler object for the given event Id. - * - * Will return an existing PollHandler if one already exists for the event ID. - * A new PollHandler will be created if one doesn't exist. - * - * @note Requires libQuotient 0.7. - * - * @sa PollHandler - */ - Q_INVOKABLE PollHandler *poll(const QString &eventId); - Q_INVOKABLE void postPoll(PollKind::Kind kind, const QString &question, const QList &answers); /** @@ -599,7 +587,6 @@ private: ChatBarCache *m_editCache; ChatBarCache *m_threadCache; - QCache m_polls; std::vector> m_extraEvents; void cleanupExtraEventRange(Quotient::RoomEventsRange events); void cleanupExtraEvent(const QString &eventId);