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