1 /*
2    SPDX-FileCopyrightText: 2018-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "userinfojob.h"
8 #include "restapimethod.h"
9 #include "rocketchatqtrestapi_debug.h"
10 #include <QJsonDocument>
11 #include <QJsonObject>
12 #include <QNetworkReply>
13 using namespace RocketChatRestApi;
UserInfoJob(QObject * parent)14 UserInfoJob::UserInfoJob(QObject *parent)
15     : UserBaseJob(parent)
16 {
17 }
18 
19 UserInfoJob::~UserInfoJob() = default;
20 
requireHttpAuthentication() const21 bool UserInfoJob::requireHttpAuthentication() const
22 {
23     return true;
24 }
25 
start()26 bool UserInfoJob::start()
27 {
28     if (!canStart()) {
29         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start usersinfo job";
30         deleteLater();
31         return false;
32     }
33     submitGetRequest();
34     addStartRestApiInfo(QByteArrayLiteral("UsersInfoJob: Ask info about me"));
35     return true;
36 }
37 
onGetRequestResponse(const QJsonDocument & replyJson)38 void UserInfoJob::onGetRequestResponse(const QJsonDocument &replyJson)
39 {
40     const QJsonObject replyObject = replyJson.object();
41     if (replyObject[QStringLiteral("success")].toBool()) {
42         addLoggerInfo(QByteArrayLiteral("UsersInfoJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
43         Q_EMIT userInfoDone(replyObject);
44     } else {
45         emitFailedMessage(replyObject);
46         addLoggerWarning(QByteArrayLiteral("UsersInfoJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
47     }
48 }
49 
request() const50 QNetworkRequest UserInfoJob::request() const
51 {
52     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersInfo);
53     addQueryUrl(url);
54 
55     QNetworkRequest request(url);
56     addAuthRawHeader(request);
57     addRequestAttribute(request, false);
58 
59     return request;
60 }
61 
canStart() const62 bool UserInfoJob::canStart() const
63 {
64     if (!hasUserIdentifier()) {
65         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UsersInfoJob: identifier is empty";
66         return false;
67     }
68     if (!RestApiAbstractJob::canStart()) {
69         return false;
70     }
71     return true;
72 }
73