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

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "groupremoveownerjob.h"
0008 
0009 #include "restapimethod.h"
0010 #include "rocketchatqtrestapi_debug.h"
0011 #include <KLocalizedString>
0012 #include <QJsonDocument>
0013 #include <QJsonObject>
0014 #include <QNetworkReply>
0015 using namespace RocketChatRestApi;
0016 GroupRemoveOwnerJob::GroupRemoveOwnerJob(QObject *parent)
0017     : ChannelGroupBaseJob(parent)
0018 {
0019 }
0020 
0021 GroupRemoveOwnerJob::~GroupRemoveOwnerJob() = default;
0022 
0023 bool GroupRemoveOwnerJob::start()
0024 {
0025     if (!canStart()) {
0026         deleteLater();
0027         return false;
0028     }
0029     addStartRestApiInfo("GroupRemoveOwnerJob::start");
0030     submitPostRequest(json());
0031 
0032     return true;
0033 }
0034 
0035 void GroupRemoveOwnerJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0036 {
0037     const QJsonObject replyObject = replyJson.object();
0038 
0039     if (replyObject[QLatin1String("success")].toBool()) {
0040         addLoggerInfo(QByteArrayLiteral("GroupRemoveOwnerJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0041         Q_EMIT groupRemoveOwnerDone();
0042     } else {
0043         emitFailedMessage(replyErrorString, replyObject);
0044         addLoggerWarning(QByteArrayLiteral("GroupRemoveOwnerJob problem: ") + replyJson.toJson(QJsonDocument::Indented));
0045         if (replyObject[QLatin1String("errorType")].toString() == QLatin1String("error-remove-last-owner")) {
0046             Q_EMIT failed(i18n("This is the last owner. Please set a new owner before removing this one."));
0047         }
0048     }
0049 }
0050 
0051 QString GroupRemoveOwnerJob::removeUserId() const
0052 {
0053     return mRemoveUserId;
0054 }
0055 
0056 void GroupRemoveOwnerJob::setRemoveUserId(const QString &removeUserId)
0057 {
0058     mRemoveUserId = removeUserId;
0059 }
0060 
0061 bool GroupRemoveOwnerJob::requireHttpAuthentication() const
0062 {
0063     return true;
0064 }
0065 
0066 bool GroupRemoveOwnerJob::canStart() const
0067 {
0068     if (mRemoveUserId.isEmpty()) {
0069         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "GroupRemoveOwnerJob: remove userid is empty";
0070         return false;
0071     }
0072     if (!hasIdentifier()) {
0073         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "GroupRemoveOwnerJob: RoomId is empty";
0074         return false;
0075     }
0076     if (!RestApiAbstractJob::canStart()) {
0077         return false;
0078     }
0079     return true;
0080 }
0081 
0082 QJsonDocument GroupRemoveOwnerJob::json() const
0083 {
0084     QJsonObject jsonObj;
0085     generateJson(jsonObj);
0086     jsonObj[QLatin1String("userId")] = removeUserId();
0087 
0088     const QJsonDocument postData = QJsonDocument(jsonObj);
0089     return postData;
0090 }
0091 
0092 QNetworkRequest GroupRemoveOwnerJob::request() const
0093 {
0094     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::GroupRemoveOwner);
0095     QNetworkRequest request(url);
0096     addAuthRawHeader(request);
0097     addRequestAttribute(request);
0098     return request;
0099 }
0100 
0101 #include "moc_groupremoveownerjob.cpp"