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

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 "resetavatarjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 ResetAvatarJob::ResetAvatarJob(QObject *parent)
0016     : UserBaseJob(parent)
0017 {
0018 }
0019 
0020 ResetAvatarJob::~ResetAvatarJob() = default;
0021 
0022 bool ResetAvatarJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("ResetAvatarJob::start");
0029     submitPostRequest(json());
0030     return true;
0031 }
0032 
0033 void ResetAvatarJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0034 {
0035     const QJsonObject replyObject = replyJson.object();
0036     if (replyObject[QLatin1String("success")].toBool()) {
0037         addLoggerInfo(QByteArrayLiteral("ResetAvatarJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0038         Q_EMIT resetAvatarDone();
0039     } else {
0040         emitFailedMessage(replyErrorString, replyObject);
0041         addLoggerWarning(QByteArrayLiteral("ResetAvatarJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0042     }
0043 }
0044 
0045 bool ResetAvatarJob::requireHttpAuthentication() const
0046 {
0047     return true;
0048 }
0049 
0050 bool ResetAvatarJob::canStart() const
0051 {
0052     if (!RestApiAbstractJob::canStart()) {
0053         return false;
0054     }
0055     if (!hasUserIdentifier()) {
0056         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ResetAvatarJob: identifier is empty";
0057         return false;
0058     }
0059     return true;
0060 }
0061 
0062 QNetworkRequest ResetAvatarJob::request() const
0063 {
0064     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersResetAvatar);
0065     QNetworkRequest request(url);
0066     addAuthRawHeader(request);
0067     addRequestAttribute(request);
0068     return request;
0069 }
0070 
0071 QJsonDocument ResetAvatarJob::json() const
0072 {
0073     QJsonObject jsonObj;
0074     generateJson(jsonObj);
0075     const QJsonDocument postData = QJsonDocument(jsonObj);
0076     return postData;
0077 }
0078 
0079 #include "moc_resetavatarjob.cpp"