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 "channellistjob.h"
8 #include "restapimethod.h"
9 #include "rocketchatqtrestapi_debug.h"
10 #include <QJsonDocument>
11 #include <QJsonObject>
12 #include <QNetworkReply>
13 using namespace RocketChatRestApi;
ChannelListJob(QObject * parent)14 ChannelListJob::ChannelListJob(QObject *parent)
15     : RestApiAbstractJob(parent)
16 {
17 }
18 
19 ChannelListJob::~ChannelListJob() = default;
20 
start()21 bool ChannelListJob::start()
22 {
23     if (!canStart()) {
24         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start channel list job";
25         deleteLater();
26         return false;
27     }
28 
29     submitGetRequest();
30     addStartRestApiInfo(QByteArrayLiteral("ChannelListJob: ask channel list"));
31     return false;
32 }
33 
onGetRequestResponse(const QJsonDocument & replyJson)34 void ChannelListJob::onGetRequestResponse(const QJsonDocument &replyJson)
35 {
36     const QJsonObject replyObject = replyJson.object();
37     if (replyObject[QStringLiteral("success")].toBool()) {
38         addLoggerInfo(QByteArrayLiteral("ChannelListJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
39         Q_EMIT channelListDone(replyObject);
40     } else {
41         emitFailedMessage(replyObject);
42         addLoggerWarning(QByteArrayLiteral("ChannelListJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
43     }
44 }
45 
request() const46 QNetworkRequest ChannelListJob::request() const
47 {
48     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsList);
49     QNetworkRequest request(url);
50     addAuthRawHeader(request);
51     return request;
52 }
53 
requireHttpAuthentication() const54 bool ChannelListJob::requireHttpAuthentication() const
55 {
56     return true;
57 }
58