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

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 "changechannelreadonlyjob.h"
0008 
0009 #include "restapimethod.h"
0010 #include "rocketchatqtrestapi_debug.h"
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 ChangeChannelReadonlyJob::ChangeChannelReadonlyJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 ChangeChannelReadonlyJob::~ChangeChannelReadonlyJob() = default;
0021 
0022 bool ChangeChannelReadonlyJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("ChangeChannelReadonlyJob::start");
0029     submitPostRequest(json());
0030     return true;
0031 }
0032 
0033 void ChangeChannelReadonlyJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0034 {
0035     const QJsonObject replyObject = replyJson.object();
0036 
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("Change read only success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT changeReadonlyDone();
0040     } else {
0041         addLoggerInfo(QByteArrayLiteral("Problem when we tried to change read only status: ") + replyJson.toJson(QJsonDocument::Indented));
0042     }
0043 }
0044 
0045 bool ChangeChannelReadonlyJob::readOnly() const
0046 {
0047     return mReadOnly;
0048 }
0049 
0050 void ChangeChannelReadonlyJob::setReadOnly(bool readOnly)
0051 {
0052     mReadOnly = readOnly;
0053 }
0054 
0055 bool ChangeChannelReadonlyJob::requireHttpAuthentication() const
0056 {
0057     return true;
0058 }
0059 
0060 bool ChangeChannelReadonlyJob::canStart() const
0061 {
0062     if (mRoomId.isEmpty()) {
0063         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChangeChannelReadonlyJob: RoomId is empty";
0064         return false;
0065     }
0066     if (!RestApiAbstractJob::canStart()) {
0067         return false;
0068     }
0069     return true;
0070 }
0071 
0072 QJsonDocument ChangeChannelReadonlyJob::json() const
0073 {
0074     QJsonObject jsonObj;
0075     jsonObj[QLatin1String("roomId")] = roomId();
0076     jsonObj[QLatin1String("readOnly")] = readOnly();
0077 
0078     const QJsonDocument postData = QJsonDocument(jsonObj);
0079     return postData;
0080 }
0081 
0082 QString ChangeChannelReadonlyJob::roomId() const
0083 {
0084     return mRoomId;
0085 }
0086 
0087 void ChangeChannelReadonlyJob::setRoomId(const QString &roomId)
0088 {
0089     mRoomId = roomId;
0090 }
0091 
0092 QNetworkRequest ChangeChannelReadonlyJob::request() const
0093 {
0094     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsSetReadOnly);
0095     QNetworkRequest request(url);
0096     addAuthRawHeader(request);
0097     addRequestAttribute(request);
0098     return request;
0099 }
0100 
0101 #include "moc_changechannelreadonlyjob.cpp"