File indexing completed on 2025-01-12 04:33:55
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 "reactonmessagejob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 #include <KLocalizedString> 0011 0012 #include <QJsonDocument> 0013 #include <QJsonObject> 0014 #include <QNetworkReply> 0015 0016 using namespace RocketChatRestApi; 0017 ReactOnMessageJob::ReactOnMessageJob(QObject *parent) 0018 : RestApiAbstractJob(parent) 0019 { 0020 } 0021 0022 ReactOnMessageJob::~ReactOnMessageJob() = default; 0023 0024 bool ReactOnMessageJob::start() 0025 { 0026 if (!canStart()) { 0027 deleteLater(); 0028 return false; 0029 } 0030 addStartRestApiInfo("ReactOnMessageJob::start"); 0031 submitPostRequest(json()); 0032 0033 return true; 0034 } 0035 0036 void ReactOnMessageJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0037 { 0038 const QJsonObject replyObject = replyJson.object(); 0039 0040 if (replyObject[QLatin1String("success")].toBool()) { 0041 addLoggerInfo(QByteArrayLiteral("ReactOnMessageJob success: ") + replyJson.toJson(QJsonDocument::Indented)); 0042 Q_EMIT reactOnMessageDone(); 0043 } else { 0044 emitFailedMessage(replyErrorString, replyObject); 0045 addLoggerWarning(QByteArrayLiteral("ReactOnMessageJob problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0046 } 0047 } 0048 0049 QString ReactOnMessageJob::emoji() const 0050 { 0051 return mEmoji; 0052 } 0053 0054 void ReactOnMessageJob::setEmoji(const QString &emoji) 0055 { 0056 mEmoji = emoji; 0057 } 0058 0059 void ReactOnMessageJob::setShouldReact(bool added) 0060 { 0061 mAddReact = added; 0062 } 0063 0064 bool ReactOnMessageJob::shouldReact() const 0065 { 0066 return mAddReact; 0067 } 0068 0069 bool ReactOnMessageJob::requireHttpAuthentication() const 0070 { 0071 return true; 0072 } 0073 0074 bool ReactOnMessageJob::canStart() const 0075 { 0076 if (mEmoji.isEmpty()) { 0077 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ReactOnMessageJob: emoji is empty"; 0078 return false; 0079 } 0080 if (mMessageId.isEmpty()) { 0081 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ReactOnMessageJob: messageId is empty"; 0082 return false; 0083 } 0084 if (!RestApiAbstractJob::canStart()) { 0085 return false; 0086 } 0087 return true; 0088 } 0089 0090 QJsonDocument ReactOnMessageJob::json() const 0091 { 0092 QJsonObject jsonObj; 0093 jsonObj[QLatin1String("emoji")] = mEmoji; 0094 jsonObj[QLatin1String("messageId")] = mMessageId; 0095 jsonObj[QLatin1String("shouldReact")] = mAddReact; 0096 0097 const QJsonDocument postData = QJsonDocument(jsonObj); 0098 return postData; 0099 } 0100 0101 QString ReactOnMessageJob::messageId() const 0102 { 0103 return mMessageId; 0104 } 0105 0106 void ReactOnMessageJob::setMessageId(const QString &t) 0107 { 0108 mMessageId = t; 0109 } 0110 0111 QNetworkRequest ReactOnMessageJob::request() const 0112 { 0113 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChatReact); 0114 QNetworkRequest request(url); 0115 addAuthRawHeader(request); 0116 addRequestAttribute(request); 0117 return request; 0118 } 0119 0120 QString RocketChatRestApi::ReactOnMessageJob::errorMessage(const QString &str, const QJsonObject &details) 0121 { 0122 if (str == QLatin1String("error-not-allowed")) { 0123 return i18n("Invalid emoji provided."); 0124 } 0125 return RestApiAbstractJob::errorMessage(str, details); 0126 } 0127 0128 #include "moc_reactonmessagejob.cpp"