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

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "pinmessagejob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 PinMessageJob::PinMessageJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 PinMessageJob::~PinMessageJob() = default;
0021 
0022 bool PinMessageJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("PinMessageJob::start");
0029     submitPostRequest(json());
0030 
0031     return true;
0032 }
0033 
0034 void PinMessageJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037 
0038     if (replyObject[QLatin1String("success")].toBool()) {
0039         addLoggerInfo(QByteArrayLiteral("PinMessageJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0040         if (mPinMessage) {
0041             Q_EMIT pinMessageDone();
0042         } else {
0043             Q_EMIT unPinMessageDone();
0044         }
0045     } else {
0046         emitFailedMessage(replyErrorString, replyObject);
0047         addLoggerWarning(QByteArrayLiteral("PinMessageJob problem: ") + replyJson.toJson(QJsonDocument::Indented));
0048     }
0049 }
0050 
0051 bool PinMessageJob::requireHttpAuthentication() const
0052 {
0053     return true;
0054 }
0055 
0056 bool PinMessageJob::canStart() const
0057 {
0058     if (!RestApiAbstractJob::canStart()) {
0059         return false;
0060     }
0061     if (mMessageId.isEmpty()) {
0062         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "PinMessageJob: mMessageId is empty";
0063         return false;
0064     }
0065     return true;
0066 }
0067 
0068 QNetworkRequest PinMessageJob::request() const
0069 {
0070     const QUrl url = mRestApiMethod->generateUrl(mPinMessage ? RestApiUtil::RestApiUrlType::ChatPinMessage : RestApiUtil::RestApiUrlType::ChatUnPinMessage);
0071     QNetworkRequest request(url);
0072     addAuthRawHeader(request);
0073     addRequestAttribute(request);
0074     return request;
0075 }
0076 
0077 QString PinMessageJob::messageId() const
0078 {
0079     return mMessageId;
0080 }
0081 
0082 void PinMessageJob::setMessageId(const QString &messageId)
0083 {
0084     mMessageId = messageId;
0085 }
0086 
0087 bool PinMessageJob::pinMessage() const
0088 {
0089     return mPinMessage;
0090 }
0091 
0092 void PinMessageJob::setPinMessage(bool pinMessage)
0093 {
0094     mPinMessage = pinMessage;
0095 }
0096 
0097 QJsonDocument PinMessageJob::json() const
0098 {
0099     QJsonObject jsonObj;
0100     jsonObj[QLatin1String("messageId")] = mMessageId;
0101 
0102     const QJsonDocument postData = QJsonDocument(jsonObj);
0103     return postData;
0104 }
0105 
0106 #include "moc_pinmessagejob.cpp"