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

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "groupsinfojob.h"
0008 
0009 #include "restapimethod.h"
0010 #include "rocketchatqtrestapi_debug.h"
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 #include <QUrlQuery>
0015 using namespace RocketChatRestApi;
0016 GroupsInfoJob::GroupsInfoJob(QObject *parent)
0017     : RestApiAbstractJob(parent)
0018 {
0019 }
0020 
0021 GroupsInfoJob::~GroupsInfoJob() = default;
0022 
0023 bool GroupsInfoJob::start()
0024 {
0025     if (!canStart()) {
0026         deleteLater();
0027         return false;
0028     }
0029     addStartRestApiInfo("GroupsInfoJob::start: ");
0030     submitGetRequest();
0031 
0032     return true;
0033 }
0034 
0035 void GroupsInfoJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0036 {
0037     const QJsonObject replyObject = replyJson.object();
0038 
0039     if (replyObject[QLatin1String("success")].toBool()) {
0040         addLoggerInfo(QByteArrayLiteral("groupInfoDone success: ") + replyJson.toJson(QJsonDocument::Indented));
0041         Q_EMIT channelInfoDone(replyObject, mRoomId);
0042     } else {
0043         emitFailedMessage(replyErrorString, replyObject);
0044         addLoggerWarning(QByteArrayLiteral("groupInfoDone problem: ") + replyJson.toJson(QJsonDocument::Indented));
0045     }
0046 }
0047 
0048 bool GroupsInfoJob::requireHttpAuthentication() const
0049 {
0050     return true;
0051 }
0052 
0053 bool GroupsInfoJob::canStart() const
0054 {
0055     if (mRoomId.isEmpty()) {
0056         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "GroupsInfoJob: RoomId is empty";
0057         return false;
0058     }
0059     if (!RestApiAbstractJob::canStart()) {
0060         return false;
0061     }
0062     return true;
0063 }
0064 
0065 QString GroupsInfoJob::roomId() const
0066 {
0067     return mRoomId;
0068 }
0069 
0070 void GroupsInfoJob::setRoomId(const QString &roomId)
0071 {
0072     mRoomId = roomId;
0073 }
0074 
0075 QNetworkRequest GroupsInfoJob::request() const
0076 {
0077     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::GroupsInfo);
0078     QUrlQuery queryUrl;
0079     queryUrl.addQueryItem(QStringLiteral("roomId"), mRoomId);
0080     url.setQuery(queryUrl);
0081     QNetworkRequest request(url);
0082     addAuthRawHeader(request);
0083     addRequestAttribute(request, false);
0084 
0085     return request;
0086 }
0087 
0088 #include "moc_groupsinfojob.cpp"