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

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "teamdeletejob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonArray>
0012 #include <QJsonDocument>
0013 #include <QJsonObject>
0014 
0015 #include <QNetworkReply>
0016 using namespace RocketChatRestApi;
0017 TeamDeleteJob::TeamDeleteJob(QObject *parent)
0018     : RestApiAbstractJob(parent)
0019 {
0020 }
0021 
0022 TeamDeleteJob::~TeamDeleteJob() = default;
0023 
0024 bool TeamDeleteJob::start()
0025 {
0026     if (!canStart()) {
0027         deleteLater();
0028         return false;
0029     }
0030     addStartRestApiInfo("TeamDeleteJob::start");
0031     submitPostRequest(json());
0032 
0033     return true;
0034 }
0035 
0036 void TeamDeleteJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0037 {
0038     const QJsonObject replyObject = replyJson.object();
0039 
0040     if (replyObject[QLatin1String("success")].toBool()) {
0041         addLoggerInfo(QByteArrayLiteral("TeamDeleteJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0042         Q_EMIT deleteTeamDone();
0043     } else {
0044         emitFailedMessage(replyErrorString, replyObject);
0045         addLoggerWarning(QByteArrayLiteral("TeamDeleteJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0046     }
0047 }
0048 
0049 QStringList TeamDeleteJob::roomsId() const
0050 {
0051     return mRoomToDeleteId;
0052 }
0053 
0054 void TeamDeleteJob::setRoomsId(const QStringList &roomsId)
0055 {
0056     mRoomToDeleteId = roomsId;
0057 }
0058 
0059 QString TeamDeleteJob::teamId() const
0060 {
0061     return mTeamId;
0062 }
0063 
0064 void TeamDeleteJob::setTeamId(const QString &teamId)
0065 {
0066     mTeamId = teamId;
0067 }
0068 
0069 bool TeamDeleteJob::requireHttpAuthentication() const
0070 {
0071     return true;
0072 }
0073 
0074 bool TeamDeleteJob::canStart() const
0075 {
0076     if (!RestApiAbstractJob::canStart()) {
0077         return false;
0078     }
0079     if (mTeamId.isEmpty()) {
0080         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "TeamDeleteJob: mTeamId is empty";
0081         return false;
0082     }
0083     return true;
0084 }
0085 
0086 QNetworkRequest TeamDeleteJob::request() const
0087 {
0088     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::TeamsDelete);
0089     QNetworkRequest request(url);
0090     addAuthRawHeader(request);
0091     addRequestAttribute(request);
0092     return request;
0093 }
0094 
0095 QJsonDocument TeamDeleteJob::json() const
0096 {
0097     QJsonObject jsonObj;
0098     if (!mRoomToDeleteId.isEmpty()) {
0099         jsonObj[QLatin1String("roomsToRemove")] = QJsonArray::fromStringList(mRoomToDeleteId);
0100     }
0101     jsonObj[QLatin1String("teamId")] = mTeamId;
0102     const QJsonDocument postData = QJsonDocument(jsonObj);
0103     return postData;
0104 }
0105 
0106 #include "moc_teamdeletejob.cpp"