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

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "usersupdatejob.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 UsersUpdateJob::UsersUpdateJob(QObject *parent)
0017     : RestApiAbstractJob(parent)
0018 {
0019 }
0020 
0021 UsersUpdateJob::~UsersUpdateJob() = default;
0022 
0023 bool UsersUpdateJob::start()
0024 {
0025     if (!canStart()) {
0026         deleteLater();
0027         return false;
0028     }
0029     addStartRestApiInfo("UsersUpdateJob::start");
0030     submitPostRequest(json());
0031     return true;
0032 }
0033 
0034 void UsersUpdateJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("UsersUpdateJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT usersUpdateDone(replyObject);
0040     } else {
0041         emitFailedMessage(replyErrorString, replyObject);
0042         addLoggerWarning(QByteArrayLiteral("UsersUpdateJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0043     }
0044 }
0045 
0046 UpdateUserInfo UsersUpdateJob::updateInfo() const
0047 {
0048     return mUpdateInfo;
0049 }
0050 
0051 void UsersUpdateJob::setUpdateInfo(const UpdateUserInfo &updateInfo)
0052 {
0053     mUpdateInfo = updateInfo;
0054 }
0055 
0056 bool UsersUpdateJob::requireTwoFactorAuthentication() const
0057 {
0058     return true;
0059 }
0060 
0061 QString UsersUpdateJob::errorMessage(const QString &str, const QJsonObject &details)
0062 {
0063     if (str == QLatin1String("error-email-send-failed")) {
0064         return i18n("Error trying to send email %1", mUpdateInfo.mEmail);
0065     }
0066     return RestApiAbstractJob::errorMessage(str, details);
0067 }
0068 
0069 bool UsersUpdateJob::requireHttpAuthentication() const
0070 {
0071     return true;
0072 }
0073 
0074 bool UsersUpdateJob::canStart() const
0075 {
0076     if (!RestApiAbstractJob::canStart()) {
0077         return false;
0078     }
0079     if (!mUpdateInfo.isValid()) {
0080         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UsersUpdateJob: mUpdateInfo is not valid.";
0081         return false;
0082     }
0083     return true;
0084 }
0085 
0086 QNetworkRequest UsersUpdateJob::request() const
0087 {
0088     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersUpdate);
0089     QNetworkRequest request(url);
0090     addAuthRawHeader(request);
0091     addRequestAttribute(request);
0092     return request;
0093 }
0094 
0095 QJsonDocument UsersUpdateJob::json() const
0096 {
0097     auto t = mUpdateInfo.json();
0098     return t;
0099 }
0100 
0101 #include "moc_usersupdatejob.cpp"