File indexing completed on 2024-12-22 04:45:13

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "bannersdismissjob.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 BannersDismissJob::BannersDismissJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 BannersDismissJob::~BannersDismissJob() = default;
0021 
0022 bool BannersDismissJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("BannersDismissJob::start");
0029     submitPostRequest(json());
0030 
0031     return true;
0032 }
0033 
0034 void BannersDismissJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037 
0038     if (replyObject[QLatin1String("success")].toBool()) {
0039         addLoggerInfo(QByteArrayLiteral("BannersDismissJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0040         Q_EMIT dimissBannerDone();
0041     } else {
0042         emitFailedMessage(replyErrorString, replyObject);
0043         addLoggerWarning(QByteArrayLiteral("BannersDismissJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0044     }
0045 }
0046 
0047 const QString &BannersDismissJob::bannerId() const
0048 {
0049     return mBannerId;
0050 }
0051 
0052 void BannersDismissJob::setBannerId(const QString &newBannerId)
0053 {
0054     mBannerId = newBannerId;
0055 }
0056 
0057 bool BannersDismissJob::requireHttpAuthentication() const
0058 {
0059     return true;
0060 }
0061 
0062 bool BannersDismissJob::canStart() const
0063 {
0064     if (!RestApiAbstractJob::canStart()) {
0065         return false;
0066     }
0067     if (mBannerId.isEmpty()) {
0068         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "BannersDismissJob: mBannerId is empty";
0069         return false;
0070     }
0071     return true;
0072 }
0073 
0074 QNetworkRequest BannersDismissJob::request() const
0075 {
0076     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::BannersDismiss);
0077     QNetworkRequest request(url);
0078     addAuthRawHeader(request);
0079     addRequestAttribute(request);
0080     return request;
0081 }
0082 
0083 QJsonDocument BannersDismissJob::json() const
0084 {
0085     QJsonObject jsonObj;
0086     jsonObj[QLatin1String("bannerId")] = mBannerId;
0087     const QJsonDocument postData = QJsonDocument(jsonObj);
0088     return postData;
0089 }
0090 
0091 #include "moc_bannersdismissjob.cpp"