File indexing completed on 2025-01-12 04:33:54

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