File indexing completed on 2025-01-12 04:33:55
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 "sendmessagejob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 #include <QJsonDocument> 0011 #include <QJsonObject> 0012 #include <QNetworkReply> 0013 using namespace RocketChatRestApi; 0014 SendMessageJob::SendMessageJob(QObject *parent) 0015 : RestApiAbstractJob(parent) 0016 { 0017 } 0018 0019 SendMessageJob::~SendMessageJob() = default; 0020 0021 bool SendMessageJob::start() 0022 { 0023 if (!canStart()) { 0024 deleteLater(); 0025 return false; 0026 } 0027 addStartRestApiInfo("SendMessageJob::start"); 0028 submitPostRequest(json()); 0029 0030 return true; 0031 } 0032 0033 bool SendMessageJob::requireHttpAuthentication() const 0034 { 0035 return true; 0036 } 0037 0038 void SendMessageJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0039 { 0040 const QJsonObject replyObject = replyJson.object(); 0041 0042 if (replyObject[QLatin1String("success")].toBool()) { 0043 addLoggerInfo(QByteArrayLiteral("SendMessageJob success: ") + replyJson.toJson(QJsonDocument::Indented)); 0044 Q_EMIT sendMessageDone(); 0045 } else { 0046 emitFailedMessage(replyErrorString, replyObject); 0047 addLoggerWarning(QByteArrayLiteral("SendMessageJob problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0048 } 0049 } 0050 0051 SendMessageJob::SendMessageArguments SendMessageJob::sendMessageArguments() const 0052 { 0053 return mSendMessageArguments; 0054 } 0055 0056 void SendMessageJob::setSendMessageArguments(const SendMessageArguments &sendMessageArguments) 0057 { 0058 mSendMessageArguments = sendMessageArguments; 0059 } 0060 0061 QNetworkRequest SendMessageJob::request() const 0062 { 0063 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChatSendMessage); 0064 QNetworkRequest request(url); 0065 addAuthRawHeader(request); 0066 addRequestAttribute(request); 0067 return request; 0068 } 0069 0070 bool SendMessageJob::canStart() const 0071 { 0072 if (!RestApiAbstractJob::canStart()) { 0073 return false; 0074 } 0075 if (mSendMessageArguments.message.isEmpty()) { 0076 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Text is empty"; 0077 return false; 0078 } 0079 if (mSendMessageArguments.roomId.isEmpty()) { 0080 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "roomId is not defined"; 0081 return false; 0082 } 0083 return true; 0084 } 0085 0086 QJsonDocument SendMessageJob::json() const 0087 { 0088 QJsonObject message; 0089 QJsonObject jsonObj; 0090 if (!mSendMessageArguments.messageId.isEmpty()) { 0091 jsonObj[QLatin1String("_id")] = mSendMessageArguments.messageId; 0092 } 0093 if (!mSendMessageArguments.threadMessageId.isEmpty()) { 0094 jsonObj[QLatin1String("tmid")] = mSendMessageArguments.threadMessageId; 0095 } 0096 jsonObj[QLatin1String("rid")] = mSendMessageArguments.roomId; 0097 jsonObj[QLatin1String("msg")] = mSendMessageArguments.message; 0098 message[QLatin1String("message")] = jsonObj; 0099 const QJsonDocument postData = QJsonDocument(message); 0100 return postData; 0101 } 0102 0103 #include "moc_sendmessagejob.cpp"