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

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 "savenotificationjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 SaveNotificationJob::SaveNotificationJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 SaveNotificationJob::~SaveNotificationJob() = default;
0021 
0022 bool SaveNotificationJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("SaveNotificationJob::start");
0029     submitPostRequest(json());
0030 
0031     return true;
0032 }
0033 
0034 void SaveNotificationJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037 
0038     if (replyObject[QLatin1String("success")].toBool()) {
0039         addLoggerInfo(QByteArrayLiteral("SaveNotificationJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0040         Q_EMIT changeNotificationDone();
0041     } else {
0042         emitFailedMessage(replyErrorString, replyObject);
0043         addLoggerWarning(QByteArrayLiteral("SaveNotificationJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0044     }
0045 }
0046 
0047 bool SaveNotificationJob::hideMentionStatus() const
0048 {
0049     return mHideMentionStatus;
0050 }
0051 
0052 void SaveNotificationJob::setHideMentionStatus(bool newHideMentionStatus)
0053 {
0054     mSettingsWillBeChanged |= HideMentionStatus;
0055     mHideMentionStatus = newHideMentionStatus;
0056 }
0057 
0058 QString SaveNotificationJob::desktopNotifications() const
0059 {
0060     return mDesktopNotifications;
0061 }
0062 
0063 void SaveNotificationJob::setDesktopNotifications(const QString &desktopNotifications)
0064 {
0065     mSettingsWillBeChanged |= DesktopNotification;
0066     mDesktopNotifications = desktopNotifications;
0067 }
0068 
0069 QString SaveNotificationJob::unreadAlert() const
0070 {
0071     return mUnreadAlert;
0072 }
0073 
0074 void SaveNotificationJob::setUnreadAlert(const QString &unreadAlert)
0075 {
0076     mSettingsWillBeChanged = mSettingsWillBeChanged & UnreadAlert;
0077     mUnreadAlert = unreadAlert;
0078 }
0079 
0080 int SaveNotificationJob::desktopNotificationDuration() const
0081 {
0082     return mDesktopNotificationDuration;
0083 }
0084 
0085 void SaveNotificationJob::setDesktopNotificationDuration(int desktopNotificationDuration)
0086 {
0087     mSettingsWillBeChanged |= DesktopNotificationDuration;
0088     mDesktopNotificationDuration = desktopNotificationDuration;
0089 }
0090 
0091 QString SaveNotificationJob::audioNotificationValue() const
0092 {
0093     return mAudioNotificationValue;
0094 }
0095 
0096 void SaveNotificationJob::setAudioNotificationValue(const QString &audioNotificationValue)
0097 {
0098     mSettingsWillBeChanged |= AudioNotificationValue;
0099     mAudioNotificationValue = audioNotificationValue;
0100 }
0101 
0102 QString SaveNotificationJob::mobilePushNotifications() const
0103 {
0104     return mMobilePushNotifications;
0105 }
0106 
0107 void SaveNotificationJob::setMobilePushNotifications(const QString &mobilePushNotifications)
0108 {
0109     mSettingsWillBeChanged |= MobilePushNotifications;
0110     mMobilePushNotifications = mobilePushNotifications;
0111 }
0112 
0113 QString SaveNotificationJob::emailNotifications() const
0114 {
0115     return mEmailNotifications;
0116 }
0117 
0118 void SaveNotificationJob::setEmailNotifications(const QString &emailNotifications)
0119 {
0120     mSettingsWillBeChanged |= EmailNotifications;
0121     mEmailNotifications = emailNotifications;
0122 }
0123 
0124 bool SaveNotificationJob::hideUnreadStatus() const
0125 {
0126     return mHideUnreadStatus;
0127 }
0128 
0129 void SaveNotificationJob::setHideUnreadStatus(bool hideUnreadStatus)
0130 {
0131     mSettingsWillBeChanged |= HideUnreadStatus;
0132     mHideUnreadStatus = hideUnreadStatus;
0133 }
0134 
0135 bool SaveNotificationJob::disableNotifications() const
0136 {
0137     return mDisableNotifications;
0138 }
0139 
0140 void SaveNotificationJob::setDisableNotifications(bool disableNotifications)
0141 {
0142     mSettingsWillBeChanged |= DisableNotifications;
0143     mDisableNotifications = disableNotifications;
0144 }
0145 
0146 bool SaveNotificationJob::muteGroupMentions() const
0147 {
0148     return mMuteGroupMentions;
0149 }
0150 
0151 void SaveNotificationJob::setMuteGroupMentions(bool muteGroupMentions)
0152 {
0153     mSettingsWillBeChanged |= MuteGroupMentions;
0154     mMuteGroupMentions = muteGroupMentions;
0155 }
0156 
0157 QString SaveNotificationJob::roomId() const
0158 {
0159     return mRoomId;
0160 }
0161 
0162 void SaveNotificationJob::setRoomId(const QString &roomId)
0163 {
0164     mRoomId = roomId;
0165 }
0166 
0167 bool SaveNotificationJob::requireHttpAuthentication() const
0168 {
0169     return true;
0170 }
0171 
0172 bool SaveNotificationJob::canStart() const
0173 {
0174     if (!RestApiAbstractJob::canStart()) {
0175         return false;
0176     }
0177     if (mRoomId.isEmpty()) {
0178         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "SaveNotificationJob: mRoomId is empty";
0179         return false;
0180     }
0181     if (mSettingsWillBeChanged == Unknown) {
0182         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "SaveNotificationJob: any settings will be changed! it's a bug";
0183         return false;
0184     }
0185     return true;
0186 }
0187 
0188 QNetworkRequest SaveNotificationJob::request() const
0189 {
0190     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::RoomsSaveNotification);
0191     QNetworkRequest request(url);
0192     addAuthRawHeader(request);
0193     addRequestAttribute(request);
0194     return request;
0195 }
0196 
0197 QJsonDocument SaveNotificationJob::json() const
0198 {
0199     QJsonObject jsonObj;
0200     jsonObj[QLatin1String("roomId")] = mRoomId;
0201     QJsonObject notificationsJson;
0202 
0203     if (mSettingsWillBeChanged & EmailNotifications) {
0204         notificationsJson[QLatin1String("emailNotifications")] = emailNotifications();
0205     }
0206     if (mSettingsWillBeChanged & MobilePushNotifications) {
0207         notificationsJson[QLatin1String("mobilePushNotifications")] = mobilePushNotifications();
0208     }
0209     if (mSettingsWillBeChanged & AudioNotificationValue) {
0210         notificationsJson[QLatin1String("audioNotificationValue")] = audioNotificationValue();
0211     }
0212     if (mSettingsWillBeChanged & UnreadAlert) {
0213         notificationsJson[QLatin1String("unreadAlert")] = unreadAlert();
0214     }
0215     if (mSettingsWillBeChanged & DesktopNotificationDuration) {
0216         notificationsJson[QLatin1String("desktopNotificationDuration")] = desktopNotificationDuration();
0217     }
0218     if (mSettingsWillBeChanged & DisableNotifications) {
0219         notificationsJson[QLatin1String("disableNotifications")] = disableNotifications() ? QStringLiteral("1") : QStringLiteral("0");
0220     }
0221     if (mSettingsWillBeChanged & HideUnreadStatus) {
0222         notificationsJson[QLatin1String("hideUnreadStatus")] = hideUnreadStatus() ? QStringLiteral("1") : QStringLiteral("0");
0223     }
0224     if (mSettingsWillBeChanged & MuteGroupMentions) {
0225         notificationsJson[QLatin1String("muteGroupMentions")] = muteGroupMentions() ? QStringLiteral("1") : QStringLiteral("0");
0226     }
0227     if (mSettingsWillBeChanged & DesktopNotification) {
0228         notificationsJson[QLatin1String("desktopNotifications")] = desktopNotifications();
0229     }
0230     if (mSettingsWillBeChanged & HideMentionStatus) {
0231         notificationsJson[QLatin1String("hideMentionStatus")] = hideMentionStatus() ? QStringLiteral("1") : QStringLiteral("0");
0232     }
0233     jsonObj[QLatin1String("notifications")] = notificationsJson;
0234 
0235     const QJsonDocument postData = QJsonDocument(jsonObj);
0236     return postData;
0237 }
0238 
0239 #include "moc_savenotificationjob.cpp"