Create a content provider instance to get message content models from

This means that all content models will now come from the same source to remove duplication across multiple models and `chatbarcaches`.

It also handily breaks the dependency on needing `MessageContentModel` for `NeochatRoom`
This commit is contained in:
James Graham
2025-04-04 08:42:34 +00:00
parent 123d11945e
commit 88eb2223c5
20 changed files with 222 additions and 140 deletions

View File

@@ -6,7 +6,6 @@
#include <Quotient/events/roommessageevent.h>
#include <Quotient/room.h>
#include "messagecontentmodel.h"
#include "messagefiltermodel.h"
#include "timelinemessagemodel.h"

View File

@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include "messagecontentmodel.h"
#include "contentprovider.h"
#include "eventhandler.h"
#include "messagecomponenttype.h"
#include "neochatconfig.h"
@@ -26,6 +27,7 @@
#endif
#include "chatbarcache.h"
#include "contentprovider.h"
#include "filetype.h"
#include "linkpreviewer.h"
#include "models/reactionmodel.h"
@@ -767,4 +769,9 @@ void MessageContentModel::updateReactionModel()
resetContent();
}
ThreadModel *MessageContentModel::modelForThread(const QString &threadRootId)
{
return ContentProvider::self().modelForThread(m_room, threadRootId);
}
#include "moc_messagecontentmodel.cpp"

View File

@@ -14,6 +14,8 @@
#include "models/reactionmodel.h"
#include "neochatroommember.h"
class ThreadModel;
/**
* @class MessageContentModel
*
@@ -102,6 +104,14 @@ public:
*/
Q_INVOKABLE void closeLinkPreview(int row);
/**
* @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.
*/
Q_INVOKABLE ThreadModel *modelForThread(const QString &threadRootId);
Q_SIGNALS:
void showAuthorChanged();
void eventUpdated();

View File

@@ -7,7 +7,6 @@
#include <QVariant>
#include "enums/delegatetype.h"
#include "messagecontentmodel.h"
#include "neochatconfig.h"
#include "timelinemessagemodel.h"

View File

@@ -4,6 +4,7 @@
#include "messagemodel.h"
#include "neochatconfig.h"
#include "threadmodel.h"
#include <Quotient/events/encryptedevent.h>
#include <Quotient/events/roommessageevent.h>
@@ -14,6 +15,7 @@
#include <KFormat>
#include "contentprovider.h"
#include "enums/delegatetype.h"
#include "enums/messagecomponenttype.h"
#include "eventhandler.h"
@@ -122,14 +124,14 @@ QVariant MessageModel::data(const QModelIndex &idx, int role) const
if (role == ContentModelRole) {
if (event->get().is<EncryptedEvent>() || event->get().is<PollStartEvent>()) {
return QVariant::fromValue<MessageContentModel *>(m_room->contentModelForEvent(event->get().id()));
return QVariant::fromValue<MessageContentModel *>(ContentProvider::self().contentModelForEvent(m_room, event->get().id()));
}
auto roomMessageEvent = eventCast<const RoomMessageEvent>(&event.value().get());
if (NeoChatConfig::self()->threads() && roomMessageEvent && roomMessageEvent->isThreaded()) {
return QVariant::fromValue<MessageContentModel *>(m_room->contentModelForEvent(roomMessageEvent->threadRootEventId()));
return QVariant::fromValue<MessageContentModel *>(ContentProvider::self().contentModelForEvent(m_room, roomMessageEvent->threadRootEventId()));
}
return QVariant::fromValue<MessageContentModel *>(m_room->contentModelForEvent(&event->get()));
return QVariant::fromValue<MessageContentModel *>(ContentProvider::self().contentModelForEvent(m_room, &event->get()));
}
if (role == GenericDisplayRole) {
@@ -483,9 +485,4 @@ bool MessageModel::event(QEvent *event)
return QObject::event(event);
}
ThreadModel *MessageModel::threadModelForRootId(const QString &threadRootId) const
{
return m_room->modelForThread(threadRootId);
}
#include "moc_messagemodel.cpp"

View File

@@ -113,11 +113,6 @@ public:
*/
Q_INVOKABLE [[nodiscard]] int eventIdToRow(const QString &eventID) const;
/**
* @brief Get a ThreadModel for the give thread root Matrix ID.
*/
Q_INVOKABLE ThreadModel *threadModelForRootId(const QString &threadRootId) const;
Q_SIGNALS:
/**
* @brief Emitted when the room is changed.

View File

@@ -5,7 +5,6 @@
#include "enums/delegatetype.h"
#include "eventhandler.h"
#include "models/messagecontentmodel.h"
#include "neochatroom.h"
#include <QGuiApplication>

View File

@@ -9,6 +9,7 @@
#include <KLocalizedString>
#include "models/messagecontentmodel.h"
#include "neochatroom.h"
using namespace Qt::StringLiterals;

View File

@@ -9,6 +9,7 @@
#include <memory>
#include "chatbarcache.h"
#include "contentprovider.h"
#include "eventhandler.h"
#include "messagecomponenttype.h"
#include "messagecontentmodel.h"
@@ -136,9 +137,9 @@ void ThreadModel::addModels()
}
addSourceModel(m_threadFetchModel);
for (auto it = m_events.crbegin(); it != m_events.crend(); ++it) {
const auto contentModel = room->contentModelForEvent(*it);
const auto contentModel = ContentProvider::self().contentModelForEvent(room, *it);
if (contentModel != nullptr) {
addSourceModel(room->contentModelForEvent(*it));
addSourceModel(ContentProvider::self().contentModelForEvent(room, *it));
}
}
addSourceModel(m_threadChatBarModel);
@@ -155,7 +156,7 @@ void ThreadModel::clearModels()
}
removeSourceModel(m_threadFetchModel);
for (const auto &model : m_events) {
const auto contentModel = room->contentModelForEvent(model);
const auto contentModel = ContentProvider::self().contentModelForEvent(room, model);
if (sourceModels().contains(contentModel)) {
removeSourceModel(contentModel);
}