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

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 "removeinvitejob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 using namespace RocketChatRestApi;
0014 RemoveInviteJob::RemoveInviteJob(QObject *parent)
0015     : RestApiAbstractJob(parent)
0016 {
0017 }
0018 
0019 RemoveInviteJob::~RemoveInviteJob() = default;
0020 
0021 bool RemoveInviteJob::requireHttpAuthentication() const
0022 {
0023     return true;
0024 }
0025 
0026 bool RemoveInviteJob::canStart() const
0027 {
0028     if (!RestApiAbstractJob::canStart()) {
0029         return false;
0030     }
0031     if (mIdentifier.isEmpty()) {
0032         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "mIdentifier is Empty. Impossible to start RemoveInviteJob";
0033         return false;
0034     }
0035     return true;
0036 }
0037 
0038 bool RemoveInviteJob::start()
0039 {
0040     if (!canStart()) {
0041         deleteLater();
0042         return false;
0043     }
0044 
0045     mReply = networkAccessManager()->deleteResource(request());
0046     const QByteArray className = metaObject()->className();
0047     mReply->setProperty("jobClassName", className);
0048 
0049     connect(mReply, &QNetworkReply::finished, this, &RemoveInviteJob::slotRemoveInviteFinished);
0050     addStartRestApiInfo(QByteArrayLiteral("RemoveInviteJob: Ask for displaying all invite link url"));
0051     return true;
0052 }
0053 
0054 void RemoveInviteJob::slotRemoveInviteFinished()
0055 {
0056     auto reply = mReply;
0057     if (reply) {
0058         if (reply->readAll() == "true") {
0059             addLoggerInfo(QByteArrayLiteral("RemoveInviteJob: success: "));
0060             Q_EMIT removeInviteDone(mIdentifier);
0061         } else {
0062             // emitFailedMessage(replyObject, reply);
0063             addLoggerWarning(QByteArrayLiteral("RemoveInviteJob: Problem: "));
0064         }
0065 #if 0
0066         const QJsonDocument replyJson = convertToJsonDocument(reply);
0067         const QJsonObject replyObject = replyJson.object();
0068 
0069         if (replyObject[QLatin1String("success")].toBool()) {
0070             addLoggerInfo(QByteArrayLiteral("RemoveInviteJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0071             Q_EMIT removeInviteDone();
0072         } else {
0073             emitFailedMessage(replyObject, reply);
0074             addLoggerWarning(QByteArrayLiteral("RemoveInviteJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0075         }
0076 #endif
0077         reply->deleteLater();
0078     }
0079     deleteLater();
0080 }
0081 
0082 QString RemoveInviteJob::identifier() const
0083 {
0084     return mIdentifier;
0085 }
0086 
0087 void RemoveInviteJob::setIdentifier(const QString &identifier)
0088 {
0089     mIdentifier = identifier;
0090 }
0091 
0092 bool RemoveInviteJob::hasQueryParameterSupport() const
0093 {
0094     return true;
0095 }
0096 
0097 QNetworkRequest RemoveInviteJob::request() const
0098 {
0099     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::RemoveInvite, mIdentifier);
0100     QNetworkRequest request(url);
0101     addAuthRawHeader(request);
0102     addRequestAttribute(request, false);
0103     return request;
0104 }
0105 
0106 #include "moc_removeinvitejob.cpp"