File indexing completed on 2025-01-12 04:33:55

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "syncmessagesjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 #include <QUrlQuery>
0014 using namespace RocketChatRestApi;
0015 SyncMessagesJob::SyncMessagesJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 SyncMessagesJob::~SyncMessagesJob() = default;
0021 
0022 bool SyncMessagesJob::requireHttpAuthentication() const
0023 {
0024     return true;
0025 }
0026 
0027 bool SyncMessagesJob::canStart() const
0028 {
0029     if (!RestApiAbstractJob::canStart()) {
0030         return false;
0031     }
0032     if (mRoomId.isEmpty()) {
0033         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "SyncMessagesJob: mRoomId is empty";
0034         return false;
0035     }
0036     if (!mLastUpdate.isValid()) {
0037         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "SyncMessagesJob: mLastUpdate is invalid";
0038         return false;
0039     }
0040     return true;
0041 }
0042 
0043 bool SyncMessagesJob::start()
0044 {
0045     if (!canStart()) {
0046         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start SyncMessagesJob job";
0047         deleteLater();
0048         return false;
0049     }
0050     submitGetRequest();
0051 
0052     addStartRestApiInfo(QByteArrayLiteral("SyncMessagesJob: sync messages in room"));
0053     return true;
0054 }
0055 
0056 void SyncMessagesJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0057 {
0058     const QJsonObject replyObject = replyJson.object();
0059     if (replyObject[QLatin1String("success")].toBool()) {
0060         addLoggerInfo(QByteArrayLiteral("SyncMessagesJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0061         Q_EMIT syncMessagesDone(replyObject, mRoomId);
0062     } else {
0063         emitFailedMessage(replyErrorString, replyObject);
0064         addLoggerWarning(QByteArrayLiteral("SyncMessagesJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0065     }
0066 }
0067 
0068 QDateTime SyncMessagesJob::lastUpdate() const
0069 {
0070     return mLastUpdate;
0071 }
0072 
0073 void SyncMessagesJob::setLastUpdate(const QDateTime &newLastUpdate)
0074 {
0075     mLastUpdate = newLastUpdate;
0076 }
0077 
0078 QString SyncMessagesJob::roomId() const
0079 {
0080     return mRoomId;
0081 }
0082 
0083 void SyncMessagesJob::setRoomId(const QString &roomId)
0084 {
0085     mRoomId = roomId;
0086 }
0087 
0088 QNetworkRequest SyncMessagesJob::request() const
0089 {
0090     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChatSyncMessages);
0091     QUrlQuery queryUrl;
0092     queryUrl.addQueryItem(QStringLiteral("roomId"), mRoomId);
0093     queryUrl.addQueryItem(QStringLiteral("lastUpdate"), mLastUpdate.toUTC().toString(Qt::ISODateWithMs));
0094 
0095     // qDebug() << " queryUrl " << queryUrl.toString();
0096     addQueryParameter(queryUrl);
0097     url.setQuery(queryUrl);
0098 
0099     QNetworkRequest request(url);
0100     addRequestAttribute(request, false);
0101 
0102     addAuthRawHeader(request);
0103     return request;
0104 }
0105 
0106 bool SyncMessagesJob::hasQueryParameterSupport() const
0107 {
0108     return true;
0109 }
0110 
0111 #include "moc_syncmessagesjob.cpp"