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

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 "unfollowmessagejob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 using namespace RocketChatRestApi;
0014 UnFollowMessageJob::UnFollowMessageJob(QObject *parent)
0015     : RestApiAbstractJob(parent)
0016 {
0017 }
0018 
0019 UnFollowMessageJob::~UnFollowMessageJob() = default;
0020 
0021 bool UnFollowMessageJob::start()
0022 {
0023     if (!canStart()) {
0024         deleteLater();
0025         return false;
0026     }
0027     addStartRestApiInfo("UnFollowMessageJob::start");
0028     submitPostRequest(json());
0029 
0030     return true;
0031 }
0032 
0033 void UnFollowMessageJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0034 {
0035     const QJsonObject replyObject = replyJson.object();
0036 
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("UnFollowMessageJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT unFollowMessageDone();
0040     } else {
0041         emitFailedMessage(replyErrorString, replyObject);
0042         addLoggerWarning(QByteArrayLiteral("UnFollowMessageJob problem: ") + replyJson.toJson(QJsonDocument::Indented));
0043     }
0044 }
0045 
0046 bool UnFollowMessageJob::requireHttpAuthentication() const
0047 {
0048     return true;
0049 }
0050 
0051 bool UnFollowMessageJob::canStart() const
0052 {
0053     if (mMessageId.isEmpty()) {
0054         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UnFollowMessageJob: messageId is empty";
0055         return false;
0056     }
0057     if (!RestApiAbstractJob::canStart()) {
0058         return false;
0059     }
0060     return true;
0061 }
0062 
0063 QJsonDocument UnFollowMessageJob::json() const
0064 {
0065     QJsonObject jsonObj;
0066     jsonObj[QLatin1String("mid")] = mMessageId;
0067 
0068     const QJsonDocument postData = QJsonDocument(jsonObj);
0069     return postData;
0070 }
0071 
0072 QString UnFollowMessageJob::messageId() const
0073 {
0074     return mMessageId;
0075 }
0076 
0077 void UnFollowMessageJob::setMessageId(const QString &t)
0078 {
0079     mMessageId = t;
0080 }
0081 
0082 QNetworkRequest UnFollowMessageJob::request() const
0083 {
0084     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChatUnFollowMessage);
0085     QNetworkRequest request(url);
0086     addAuthRawHeader(request);
0087     addRequestAttribute(request);
0088     return request;
0089 }
0090 
0091 #include "moc_unfollowmessagejob.cpp"