From ae7bfa5bcb64d14b2f7c7ef17a8c076c53942cbd Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 11 Jan 2025 12:14:31 +0000 Subject: [PATCH] Move the storage of thread models to the room --- src/models/messagemodel.cpp | 7 +------ src/models/messagemodel.h | 1 - src/neochatroom.cpp | 14 ++++++++++++++ src/neochatroom.h | 10 ++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/models/messagemodel.cpp b/src/models/messagemodel.cpp index e6ac0af39..1a1db98bc 100644 --- a/src/models/messagemodel.cpp +++ b/src/models/messagemodel.cpp @@ -425,11 +425,6 @@ void MessageModel::createEventObjects(const Quotient::RoomEvent *event, bool isP senderId = m_room->localMember().id(); } - const auto roomMessageEvent = eventCast(event); - if (roomMessageEvent && roomMessageEvent->isThreaded() && !m_threadModels.contains(roomMessageEvent->threadRootEventId())) { - m_threadModels[roomMessageEvent->threadRootEventId()] = QSharedPointer(new ThreadModel(roomMessageEvent->threadRootEventId(), m_room)); - } - // ReadMarkerModel handles updates to add and remove markers, we only need to // handle adding and removing whole models here. if (m_readMarkerModels.contains(eventId)) { @@ -518,7 +513,7 @@ bool MessageModel::event(QEvent *event) ThreadModel *MessageModel::threadModelForRootId(const QString &threadRootId) const { - return m_threadModels[threadRootId].data(); + return m_room->modelForThread(threadRootId); } #include "moc_messagemodel.cpp" diff --git a/src/models/messagemodel.h b/src/models/messagemodel.h index 0583a7e3a..de41b715c 100644 --- a/src/models/messagemodel.h +++ b/src/models/messagemodel.h @@ -153,7 +153,6 @@ private: bool movingEvent = false; QMap> m_readMarkerModels; - QMap> m_threadModels; QMap> m_reactionModels; void createEventObjects(const Quotient::RoomEvent *event, bool isPending = false); diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 9f8772fe1..d6ed37af1 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -174,6 +174,7 @@ void NeoChatRoom::setVisible(bool visible) if (!visible) { m_memberObjects.clear(); m_eventContentModels.clear(); + m_threadModels.clear(); } } @@ -1797,4 +1798,17 @@ MessageContentModel *NeoChatRoom::contentModelForEvent(const QString &evtOrTxnId return nullptr; } +ThreadModel *NeoChatRoom::modelForThread(const QString &threadRootId) +{ + if (threadRootId.isEmpty()) { + return nullptr; + } + + if (!m_threadModels.contains(threadRootId)) { + return m_threadModels.emplace(threadRootId, std::make_unique(threadRootId, this)).first->second.get(); + } + + return m_threadModels[threadRootId].get(); +} + #include "moc_neochatroom.cpp" diff --git a/src/neochatroom.h b/src/neochatroom.h index d7ac384ca..746707d9d 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -15,6 +15,7 @@ #include "enums/messagetype.h" #include "enums/pushrule.h" #include "models/messagecontentmodel.h" +#include "models/threadmodel.h" #include "neochatroommember.h" #include "pollhandler.h" @@ -598,6 +599,14 @@ public: MessageContentModel *contentModelForEvent(const QString &evtOrTxnId); + /** + * @brief Returns the thread model for the given thread root event ID. + * + * A model is created is one doesn't exist. Will return nullptr if threadRootId + * is empty. + */ + ThreadModel *modelForThread(const QString &threadRootId); + private: bool m_visible = false; @@ -631,6 +640,7 @@ private: std::unordered_map> m_memberObjects; std::unordered_map> m_eventContentModels; + std::unordered_map> m_threadModels; private Q_SLOTS: void updatePushNotificationState(QString type);