File indexing completed on 2025-02-02 04:51:46

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "saveroomsettingsjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QJsonArray>
0014 #include <QJsonDocument>
0015 #include <QJsonObject>
0016 #include <QNetworkReply>
0017 using namespace RocketChatRestApi;
0018 SaveRoomSettingsJob::SaveRoomSettingsJob(QObject *parent)
0019     : RestApiAbstractJob(parent)
0020 {
0021 }
0022 
0023 SaveRoomSettingsJob::~SaveRoomSettingsJob() = default;
0024 
0025 bool SaveRoomSettingsJob::start()
0026 {
0027     if (!canStart()) {
0028         deleteLater();
0029         return false;
0030     }
0031     addStartRestApiInfo("SaveRoomSettingsJob::start");
0032     submitPostRequest(json());
0033 
0034     return true;
0035 }
0036 
0037 void SaveRoomSettingsJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0038 {
0039     const QJsonObject replyObject = replyJson.object();
0040 
0041     if (replyObject[QLatin1String("success")].toBool()) {
0042         addLoggerInfo(QByteArrayLiteral("SaveRoomSettingsJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0043         Q_EMIT saveRoomSettingsDone(replyObject.value(QStringLiteral("rid")).toString());
0044     } else {
0045         emitFailedMessage(replyErrorString, replyObject);
0046         addLoggerWarning(QByteArrayLiteral("SaveRoomSettingsJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0047     }
0048 }
0049 
0050 SaveRoomSettingsJob::SaveRoomSettingsInfo SaveRoomSettingsJob::saveRoomSettingsInfo() const
0051 {
0052     return mSaveRoomSettingsInfo;
0053 }
0054 
0055 void SaveRoomSettingsJob::setSaveRoomSettingsInfo(const SaveRoomSettingsInfo &saveRoomSettingsInfo)
0056 {
0057     mSaveRoomSettingsInfo = saveRoomSettingsInfo;
0058 }
0059 
0060 QString SaveRoomSettingsJob::errorMessage(const QString &str, const QJsonObject &detail)
0061 {
0062     if (str == QLatin1String("error-invalid-room-name")) {
0063         return i18n("\'%1\' is not a valid room name", detail.value(QStringLiteral("channel_name")).toString());
0064     } else if (str == QLatin1String("error-action-not-allowed")) {
0065         const QString detailActionStr = detail[QStringLiteral("action")].toString();
0066         // qDebug() << " detailActionStr " << detailActionStr;
0067         if (detailActionStr == QLatin1String("Change_Room_Encrypted")) {
0068             return i18n("Only groups or direct channels can enable encryption");
0069         } else if (detailActionStr == QLatin1String("Editing_room")) {
0070             return i18n("Room does not have retention policy");
0071         }
0072     }
0073     return RestApiAbstractJob::errorMessage(str, detail);
0074 }
0075 
0076 bool SaveRoomSettingsJob::requireHttpAuthentication() const
0077 {
0078     return true;
0079 }
0080 
0081 bool SaveRoomSettingsJob::canStart() const
0082 {
0083     if (!RestApiAbstractJob::canStart()) {
0084         return false;
0085     }
0086     if (!mSaveRoomSettingsInfo.isValid()) {
0087         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "SaveRoomSettingsJob: mRoomId is empty";
0088         return false;
0089     }
0090     return true;
0091 }
0092 
0093 QNetworkRequest SaveRoomSettingsJob::request() const
0094 {
0095     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::RoomsSaveSettings);
0096     QNetworkRequest request(url);
0097     addAuthRawHeader(request);
0098     addRequestAttribute(request);
0099     return request;
0100 }
0101 
0102 QJsonDocument SaveRoomSettingsJob::json() const
0103 {
0104     QJsonObject jsonObj;
0105     jsonObj[QLatin1String("rid")] = mSaveRoomSettingsInfo.roomId;
0106     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::SystemMessages) {
0107         const QJsonArray systemSettingsArray = QJsonArray::fromStringList(mSaveRoomSettingsInfo.systemMessages);
0108         jsonObj[QLatin1String("systemMessages")] = systemSettingsArray;
0109     }
0110     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RetentionEnabled) {
0111         jsonObj[QLatin1String("retentionEnabled")] = mSaveRoomSettingsInfo.retentionEnabled;
0112     }
0113     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RetentionExcludePinned) {
0114         jsonObj[QLatin1String("retentionExcludePinned")] = mSaveRoomSettingsInfo.retentionExcludePinned;
0115     }
0116     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RetentionFilesOnly) {
0117         jsonObj[QLatin1String("retentionFilesOnly")] = mSaveRoomSettingsInfo.retentionFilesOnly;
0118     }
0119     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RetentionIgnoreThreads) {
0120         jsonObj[QLatin1String("retentionIgnoreThreads")] = mSaveRoomSettingsInfo.retentionIgnoreThreads;
0121     }
0122     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RetentionOverrideGlobal) {
0123         jsonObj[QLatin1String("retentionOverrideGlobal")] = mSaveRoomSettingsInfo.retentionOverrideGlobal;
0124     }
0125     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RetentionMaxAge) {
0126         jsonObj[QLatin1String("retentionMaxAge")] = mSaveRoomSettingsInfo.retentionMaxAge;
0127     }
0128     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RoomName) {
0129         jsonObj[QLatin1String("roomName")] = mSaveRoomSettingsInfo.roomName;
0130     }
0131     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RoomTopic) {
0132         jsonObj[QLatin1String("roomTopic")] = mSaveRoomSettingsInfo.roomTopic;
0133     }
0134     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RoomAnnouncement) {
0135         jsonObj[QLatin1String("roomAnnouncement")] = mSaveRoomSettingsInfo.roomAnnouncement;
0136     }
0137     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RoomDescription) {
0138         jsonObj[QLatin1String("roomDescription")] = mSaveRoomSettingsInfo.roomDescription;
0139     }
0140     QJsonObject jsonObjFavorite;
0141     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::Favorite) {
0142         jsonObjFavorite[QLatin1String("favorite")] = mSaveRoomSettingsInfo.favorite;
0143     }
0144     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::DefaultValue) {
0145         jsonObjFavorite[QLatin1String("defaultValue")] = mSaveRoomSettingsInfo.defaultValue;
0146     }
0147     jsonObj[QLatin1String("favorite")] = jsonObjFavorite;
0148 
0149     jsonObj[QLatin1String("roomType")] = mSaveRoomSettingsInfo.roomType;
0150     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::JoinCode) {
0151         jsonObj[QLatin1String("joinCode")] = mSaveRoomSettingsInfo.joinCode;
0152     }
0153     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::ReadOnly) {
0154         jsonObj[QLatin1String("readOnly")] = mSaveRoomSettingsInfo.readOnly;
0155     }
0156     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::Encrypted) {
0157         jsonObj[QLatin1String("encrypted")] = mSaveRoomSettingsInfo.encrypted;
0158     }
0159     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::ReactWhenReadOnly) {
0160         jsonObj[QLatin1String("reactWhenReadOnly")] = mSaveRoomSettingsInfo.reactWhenReadOnly;
0161     }
0162     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::Featured) {
0163         jsonObj[QLatin1String("featured")] = mSaveRoomSettingsInfo.featured;
0164     }
0165     if (mSaveRoomSettingsInfo.mSettingsWillBeChanged & SaveRoomSettingsInfo::RoomAvatar) {
0166         // "roomAvatar":null if we revert it.
0167         jsonObj[QLatin1String("roomAvatar")] = mSaveRoomSettingsInfo.roomAvatar;
0168     }
0169 
0170     const QJsonDocument postData = QJsonDocument(jsonObj);
0171     return postData;
0172 }
0173 
0174 bool SaveRoomSettingsJob::SaveRoomSettingsInfo::isValid() const
0175 {
0176     return !roomId.isEmpty() && (mSettingsWillBeChanged != SettingChanged::Unknown);
0177 }
0178 
0179 QDebug operator<<(QDebug d, const RocketChatRestApi::SaveRoomSettingsJob::SaveRoomSettingsInfo &t)
0180 {
0181     d << "systemMessages : " << t.systemMessages;
0182     d << "roomId : " << t.roomId;
0183     d << "roomName : " << t.roomName;
0184     d << "roomTopic : " << t.roomTopic;
0185     d << "roomAnnouncement : " << t.roomAnnouncement;
0186     d << "roomDescription : " << t.roomDescription;
0187     d << "retentionEnabled : " << t.retentionEnabled;
0188     d << "retentionExcludePinned : " << t.retentionExcludePinned;
0189     d << "retentionFilesOnly : " << t.retentionFilesOnly;
0190     d << "retentionIgnoreThreads : " << t.retentionIgnoreThreads;
0191     d << "retentionOverrideGlobal : " << t.retentionOverrideGlobal;
0192     d << "retentionMaxAge : " << t.retentionMaxAge;
0193     d << "favorite : " << t.favorite;
0194     d << "roomType : " << t.roomType;
0195     d << "readOnly : " << t.readOnly;
0196     d << "encrypted : " << t.encrypted;
0197     d << "roomAvatar : " << t.roomAvatar;
0198     d << "featured : " << t.featured;
0199     // hide password d << "joinCode : " << t.joinCode;
0200     d << " mSettingsWillBeChanged " << t.mSettingsWillBeChanged;
0201     return d;
0202 }
0203 
0204 #include "moc_saveroomsettingsjob.cpp"