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

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