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

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 "archivechanneljob.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 ArchiveChannelJob::ArchiveChannelJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 ArchiveChannelJob::~ArchiveChannelJob() = default;
0021 
0022 bool ArchiveChannelJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("ArchiveChannelJob::start");
0029     submitPostRequest(json());
0030     return true;
0031 }
0032 
0033 void ArchiveChannelJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0034 {
0035     const QJsonObject replyObject = replyJson.object();
0036     if (replyObject[QLatin1String("success")].toBool()) {
0037         addLoggerInfo(QByteArrayLiteral("archive or unarchive channel success: ") + replyJson.toJson(QJsonDocument::Indented));
0038         Q_EMIT archiveChannelDone();
0039     } else {
0040         emitFailedMessage(replyErrorString, replyObject);
0041         addLoggerWarning(QByteArrayLiteral("Problem when we tried to archive or unarchive a channel: ") + replyJson.toJson(QJsonDocument::Indented));
0042     }
0043 }
0044 
0045 bool ArchiveChannelJob::archive() const
0046 {
0047     return mArchive;
0048 }
0049 
0050 void ArchiveChannelJob::setArchive(bool archive)
0051 {
0052     mArchive = archive;
0053 }
0054 
0055 bool ArchiveChannelJob::requireHttpAuthentication() const
0056 {
0057     return true;
0058 }
0059 
0060 bool ArchiveChannelJob::canStart() const
0061 {
0062     if (mRoomId.isEmpty()) {
0063         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ArchiveChannelJob: RoomId is empty";
0064         return false;
0065     }
0066     if (!RestApiAbstractJob::canStart()) {
0067         return false;
0068     }
0069     return true;
0070 }
0071 
0072 QJsonDocument ArchiveChannelJob::json() const
0073 {
0074     QJsonObject jsonObj;
0075     jsonObj[QLatin1String("roomId")] = roomId();
0076 
0077     const QJsonDocument postData = QJsonDocument(jsonObj);
0078     return postData;
0079 }
0080 
0081 QString ArchiveChannelJob::roomId() const
0082 {
0083     return mRoomId;
0084 }
0085 
0086 void ArchiveChannelJob::setRoomId(const QString &roomId)
0087 {
0088     mRoomId = roomId;
0089 }
0090 
0091 QNetworkRequest ArchiveChannelJob::request() const
0092 {
0093     const QUrl url = mRestApiMethod->generateUrl(mArchive ? RestApiUtil::RestApiUrlType::ChannelsArchive : RestApiUtil::RestApiUrlType::ChannelsUnarchive);
0094     QNetworkRequest request(url);
0095     addAuthRawHeader(request);
0096     addRequestAttribute(request);
0097     return request;
0098 }
0099 
0100 #include "moc_archivechanneljob.cpp"