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

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 "changechannelannouncementjob.h"
0008 
0009 #include "restapimethod.h"
0010 #include "rocketchatqtrestapi_debug.h"
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 ChangeChannelAnnouncementJob::ChangeChannelAnnouncementJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 ChangeChannelAnnouncementJob::~ChangeChannelAnnouncementJob() = default;
0021 
0022 bool ChangeChannelAnnouncementJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("ChangeChannelAnnouncementJob::start");
0029     submitPostRequest(json());
0030     return true;
0031 }
0032 
0033 void ChangeChannelAnnouncementJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0034 {
0035     const QJsonObject replyObject = replyJson.object();
0036 
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("Change announcement success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT changeAnnouncementDone();
0040     } else {
0041         emitFailedMessage(replyErrorString, replyObject);
0042         addLoggerWarning(QByteArrayLiteral("Problem when we tried to change announcement: ") + replyJson.toJson(QJsonDocument::Indented));
0043     }
0044 }
0045 
0046 bool ChangeChannelAnnouncementJob::requireHttpAuthentication() const
0047 {
0048     return true;
0049 }
0050 
0051 bool ChangeChannelAnnouncementJob::canStart() const
0052 {
0053     if (mRoomId.isEmpty()) {
0054         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChangeChannelAnnouncementJob: RoomId is empty";
0055         return false;
0056     }
0057     if (!RestApiAbstractJob::canStart()) {
0058         return false;
0059     }
0060     return true;
0061 }
0062 
0063 QJsonDocument ChangeChannelAnnouncementJob::json() const
0064 {
0065     QJsonObject jsonObj;
0066     jsonObj[QLatin1String("roomId")] = roomId();
0067     jsonObj[QLatin1String("announcement")] = announcement();
0068 
0069     const QJsonDocument postData = QJsonDocument(jsonObj);
0070     return postData;
0071 }
0072 
0073 QString ChangeChannelAnnouncementJob::roomId() const
0074 {
0075     return mRoomId;
0076 }
0077 
0078 void ChangeChannelAnnouncementJob::setRoomId(const QString &roomId)
0079 {
0080     mRoomId = roomId;
0081 }
0082 
0083 QString ChangeChannelAnnouncementJob::announcement() const
0084 {
0085     return mAnnouncement;
0086 }
0087 
0088 void ChangeChannelAnnouncementJob::setAnnouncement(const QString &t)
0089 {
0090     mAnnouncement = t;
0091 }
0092 
0093 QNetworkRequest ChangeChannelAnnouncementJob::request() const
0094 {
0095     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsSetAnnouncement);
0096     QNetworkRequest request(url);
0097     addAuthRawHeader(request);
0098     addRequestAttribute(request);
0099     return request;
0100 }
0101 
0102 #include "moc_changechannelannouncementjob.cpp"