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 #include "messagecache.h"
0008 #include "connection.h"
0009 #include "rocketchataccount.h"
0010 #include "ruqola_debug.h"
0011 
0012 #include "chat/getmessagejob.h"
0013 #include "chat/getthreadmessagesjob.h"
0014 
0015 #include "model/threadmessagemodel.h"
0016 
0017 MessageCache::MessageCache(RocketChatAccount *account, QObject *parent)
0018     : QObject(parent)
0019     , mRocketChatAccount(account)
0020 {
0021 }
0022 
0023 MessageCache::~MessageCache() = default;
0024 
0025 ThreadMessageModel *MessageCache::threadMessageModel(const QString &threadMessageId)
0026 {
0027     ThreadMessageModel *cachedModel = mThreadMessageModels.object(threadMessageId);
0028     if (cachedModel) {
0029         return cachedModel;
0030     } else if (!mThreadMessageJobs.contains(threadMessageId)) {
0031         // Load the base msg of the thread
0032         auto job = new RocketChatRestApi::GetThreadMessagesJob(this);
0033         mThreadMessageJobs.insert(threadMessageId, job);
0034         job->setThreadMessageId(threadMessageId);
0035         connect(job, &RocketChatRestApi::GetThreadMessagesJob::getThreadMessagesDone, this, &MessageCache::slotGetThreadMessagesDone);
0036         if (!startJob(job)) {
0037             qCDebug(RUQOLA_LOG) << "Impossible to start GetThreadMessagesJob";
0038         }
0039     }
0040     return nullptr;
0041 }
0042 
0043 Message *MessageCache::messageForId(const QString &messageId)
0044 {
0045     Message *cachedMessage = mMessages.object(messageId);
0046     if (cachedMessage) {
0047         return cachedMessage;
0048     } else if (!mMessageJobs.contains(messageId)) {
0049         auto job = new RocketChatRestApi::GetMessageJob(this);
0050         mMessageJobs.insert(messageId, job);
0051         job->setMessageId(messageId);
0052         connect(job, &RocketChatRestApi::GetMessageJob::getMessageDone, this, &MessageCache::slotGetMessageDone);
0053         if (!startJob(job)) {
0054             qCDebug(RUQOLA_LOG) << "Impossible to start GetMessageJob";
0055         }
0056     }
0057     return nullptr;
0058 }
0059 
0060 void MessageCache::slotGetThreadMessagesDone(const QJsonObject &obj, const QString &threadMessageId)
0061 {
0062     ThreadMessageModel *model = mThreadMessageModels.object(threadMessageId);
0063     if (!model) {
0064         model = new ThreadMessageModel;
0065         model->parseThreadMessages(obj);
0066         mThreadMessageModels.insert(threadMessageId, model);
0067     } else {
0068         model->loadMoreThreadMessages(obj);
0069     }
0070     mThreadMessageJobs.remove(threadMessageId);
0071     Q_EMIT modelLoaded();
0072 }
0073 
0074 void MessageCache::slotGetMessageDone(const QJsonObject &obj, const QString &messageId)
0075 {
0076     const QJsonObject msgObject = obj[QLatin1String("message")].toObject();
0077     Q_ASSERT(!msgObject.isEmpty());
0078     auto message = new Message;
0079     message->parseMessage(msgObject, true);
0080     Q_ASSERT(messageId == message->messageId());
0081     mMessages.insert(message->messageId(), message);
0082     mMessageJobs.remove(messageId);
0083     Q_EMIT messageLoaded(message->messageId());
0084 }
0085 
0086 bool MessageCache::startJob(RocketChatRestApi::RestApiAbstractJob *job)
0087 {
0088     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0089     return job->start();
0090 }
0091 
0092 #include "moc_messagecache.cpp"