File indexing completed on 2024-12-08 10:25:50

0001 /*
0002    SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "libruqolacore_export.h"
0010 #include "model/threadmessagemodel.h"
0011 #include <QCache>
0012 #include <QMap>
0013 #include <QObject>
0014 
0015 class Message;
0016 namespace RocketChatRestApi
0017 {
0018 class RestApiAbstractJob;
0019 class GetThreadMessagesJob;
0020 class GetMessageJob;
0021 }
0022 
0023 class LIBRUQOLACORE_EXPORT MessageCache : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     explicit MessageCache(RocketChatAccount *account, QObject *parent = nullptr);
0028     ~MessageCache() override;
0029 
0030     [[nodiscard]] ThreadMessageModel *threadMessageModel(const QString &threadMessageId);
0031     [[nodiscard]] Message *messageForId(const QString &messageId);
0032 
0033 Q_SIGNALS:
0034     void modelLoaded();
0035     void messageLoaded(const QString &threadMessageId);
0036 
0037 protected:
0038     virtual bool startJob(RocketChatRestApi::RestApiAbstractJob *job); // virtual for unittest
0039 
0040 private:
0041     LIBRUQOLACORE_NO_EXPORT void slotGetThreadMessagesDone(const QJsonObject &obj, const QString &threadMessageId);
0042     LIBRUQOLACORE_NO_EXPORT void slotGetMessageDone(const QJsonObject &obj, const QString &messageId);
0043 
0044     mutable QMap<QString, RocketChatRestApi::GetThreadMessagesJob *> mThreadMessageJobs;
0045     QCache<QString, ThreadMessageModel> mThreadMessageModels;
0046 
0047     mutable QMap<QString, RocketChatRestApi::GetMessageJob *> mMessageJobs;
0048     QCache<QString, Message> mMessages;
0049     RocketChatAccount *const mRocketChatAccount;
0050 };