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 "updatemessagejob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 using namespace RocketChatRestApi;
0014 UpdateMessageJob::UpdateMessageJob(QObject *parent)
0015     : RestApiAbstractJob(parent)
0016 {
0017 }
0018 
0019 UpdateMessageJob::~UpdateMessageJob() = default;
0020 
0021 bool UpdateMessageJob::start()
0022 {
0023     if (!canStart()) {
0024         deleteLater();
0025         return false;
0026     }
0027     addStartRestApiInfo("UpdateMessageJob::start");
0028     submitPostRequest(json());
0029 
0030     return true;
0031 }
0032 
0033 void UpdateMessageJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0034 {
0035     const QJsonObject replyObject = replyJson.object();
0036 
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("UpdateMessageJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT updateMessageDone();
0040     } else {
0041         emitFailedMessage(replyErrorString, replyObject);
0042         Q_EMIT updateMessageFailed(mUpdatedText);
0043         addLoggerWarning(QByteArrayLiteral("UpdateMessageJob: problem: ") + replyJson.toJson(QJsonDocument::Indented));
0044     }
0045 }
0046 
0047 QString UpdateMessageJob::updatedText() const
0048 {
0049     return mUpdatedText;
0050 }
0051 
0052 void UpdateMessageJob::setUpdatedText(const QString &updatedText)
0053 {
0054     mUpdatedText = updatedText;
0055 }
0056 
0057 bool UpdateMessageJob::requireHttpAuthentication() const
0058 {
0059     return true;
0060 }
0061 
0062 bool UpdateMessageJob::canStart() const
0063 {
0064     if (mRoomId.isEmpty()) {
0065         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UpdateMessageJob: roomId is empty";
0066         return false;
0067     }
0068     if (mMessageId.isEmpty()) {
0069         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UpdateMessageJob: messageId is empty";
0070         return false;
0071     }
0072     // Updated text I think can be empty.
0073 
0074     if (!RestApiAbstractJob::canStart()) {
0075         return false;
0076     }
0077     return true;
0078 }
0079 
0080 QJsonDocument UpdateMessageJob::json() const
0081 {
0082     QJsonObject jsonObj;
0083     jsonObj[QLatin1String("roomId")] = mRoomId;
0084     jsonObj[QLatin1String("msgId")] = mMessageId;
0085     jsonObj[QLatin1String("text")] = mUpdatedText;
0086 
0087     const QJsonDocument postData = QJsonDocument(jsonObj);
0088     return postData;
0089 }
0090 
0091 QString UpdateMessageJob::roomId() const
0092 {
0093     return mRoomId;
0094 }
0095 
0096 void UpdateMessageJob::setRoomId(const QString &roomId)
0097 {
0098     mRoomId = roomId;
0099 }
0100 
0101 QString UpdateMessageJob::messageId() const
0102 {
0103     return mMessageId;
0104 }
0105 
0106 void UpdateMessageJob::setMessageId(const QString &t)
0107 {
0108     mMessageId = t;
0109 }
0110 
0111 QNetworkRequest UpdateMessageJob::request() const
0112 {
0113     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChatUpdate);
0114     QNetworkRequest request(url);
0115     addAuthRawHeader(request);
0116     addRequestAttribute(request);
0117     return request;
0118 }
0119 
0120 #include "moc_updatemessagejob.cpp"