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

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