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

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "runcommandjob.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 RunCommandJob::RunCommandJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 RunCommandJob::~RunCommandJob() = default;
0021 
0022 bool RunCommandJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("RunCommandJob::start");
0029     submitPostRequest(json());
0030 
0031     return true;
0032 }
0033 
0034 void RunCommandJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037 
0038     if (replyObject[QLatin1String("success")].toBool()) {
0039         addLoggerInfo(QByteArrayLiteral("RunCommandJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0040         Q_EMIT runCommandDone();
0041     } else {
0042         emitFailedMessage(replyErrorString, replyObject);
0043         addLoggerWarning(QByteArrayLiteral("RunCommandJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0044     }
0045 }
0046 
0047 RunCommandJob::RunCommandInfo RunCommandJob::runCommandInfo() const
0048 {
0049     return mRunCommandInfo;
0050 }
0051 
0052 void RunCommandJob::setRunCommandInfo(const RunCommandInfo &runCommandInfo)
0053 {
0054     mRunCommandInfo = runCommandInfo;
0055 }
0056 
0057 RunCommandJob::RunCommandInfo RunCommandJob::parseString(const QString &str, const QString &roomId, const QString &tmid)
0058 {
0059     RunCommandJob::RunCommandInfo info;
0060     if (str.length() > 1) {
0061         QString newStr = str.mid(1);
0062         QStringList lst = newStr.split(QLatin1Char(' '), Qt::SkipEmptyParts);
0063         const int numberElement = lst.count();
0064         info.commandName = lst.takeAt(0);
0065         info.roomId = roomId;
0066         info.threadMessageId = tmid;
0067         if (numberElement > 1) {
0068             info.params = lst.join(QLatin1Char(' '));
0069         }
0070     }
0071     return info;
0072 }
0073 
0074 bool RunCommandJob::requireHttpAuthentication() const
0075 {
0076     return true;
0077 }
0078 
0079 bool RunCommandJob::canStart() const
0080 {
0081     if (!RestApiAbstractJob::canStart()) {
0082         return false;
0083     }
0084     if (!mRunCommandInfo.isValid()) {
0085         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "RunCommandJob: RoomId and CommandName are empty";
0086         return false;
0087     }
0088     return true;
0089 }
0090 
0091 QNetworkRequest RunCommandJob::request() const
0092 {
0093     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::CommandsRun);
0094     QNetworkRequest request(url);
0095     addAuthRawHeader(request);
0096     addRequestAttribute(request);
0097     return request;
0098 }
0099 
0100 QJsonDocument RunCommandJob::json() const
0101 {
0102     QJsonObject jsonObj;
0103     jsonObj[QLatin1String("command")] = mRunCommandInfo.commandName;
0104     jsonObj[QLatin1String("roomId")] = mRunCommandInfo.roomId;
0105     if (!mRunCommandInfo.threadMessageId.isEmpty()) {
0106         jsonObj[QLatin1String("tmid")] = mRunCommandInfo.threadMessageId;
0107     }
0108     if (!mRunCommandInfo.triggerId.isEmpty()) {
0109         jsonObj[QLatin1String("triggerId")] = mRunCommandInfo.triggerId;
0110     }
0111 
0112     if (!mRunCommandInfo.params.isEmpty()) {
0113         jsonObj[QLatin1String("params")] = mRunCommandInfo.params;
0114     }
0115     const QJsonDocument postData = QJsonDocument(jsonObj);
0116     return postData;
0117 }
0118 
0119 bool RunCommandJob::RunCommandInfo::isValid() const
0120 {
0121     return !commandName.isEmpty() && !roomId.isEmpty();
0122 }
0123 
0124 QDebug operator<<(QDebug d, const RunCommandJob::RunCommandInfo &t)
0125 {
0126     d << " commandName " << t.commandName;
0127     d << " roomId " << t.roomId;
0128     d << " threadMessageId " << t.threadMessageId;
0129     d << " triggerId " << t.triggerId;
0130     d << " params " << t.params;
0131     return d;
0132 }
0133 
0134 #include "moc_runcommandjob.cpp"