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

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