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

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