File indexing completed on 2024-12-22 04:45:29
0001 /* 0002 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "userssetpreferencesjob.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 UsersSetPreferencesJob::UsersSetPreferencesJob(QObject *parent) 0019 : RestApiAbstractJob(parent) 0020 { 0021 } 0022 0023 UsersSetPreferencesJob::~UsersSetPreferencesJob() = default; 0024 0025 bool UsersSetPreferencesJob::start() 0026 { 0027 if (!canStart()) { 0028 deleteLater(); 0029 return false; 0030 } 0031 addStartRestApiInfo("UsersSetPreferencesJob::start"); 0032 submitPostRequest(json()); 0033 return true; 0034 } 0035 0036 void UsersSetPreferencesJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0037 { 0038 const QJsonObject replyObject = replyJson.object(); 0039 if (replyObject[QLatin1String("success")].toBool()) { 0040 addLoggerInfo(QByteArrayLiteral("UsersSetPreferencesJob: success: ") + replyJson.toJson(QJsonDocument::Indented)); 0041 Q_EMIT usersSetPreferencesDone(replyObject); 0042 } else { 0043 emitFailedMessage(replyErrorString, replyObject); 0044 addLoggerWarning(QByteArrayLiteral("UsersSetPreferencesJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0045 } 0046 } 0047 0048 UsersSetPreferencesJob::UsersSetPreferencesInfo UsersSetPreferencesJob::usersSetPreferencesInfo() const 0049 { 0050 return mUsersSetPreferencesInfo; 0051 } 0052 0053 void UsersSetPreferencesJob::setUsersSetPreferencesInfo(const UsersSetPreferencesInfo &usersSetPreferencesInfo) 0054 { 0055 mUsersSetPreferencesInfo = usersSetPreferencesInfo; 0056 } 0057 0058 bool UsersSetPreferencesJob::requireHttpAuthentication() const 0059 { 0060 return true; 0061 } 0062 0063 bool UsersSetPreferencesJob::canStart() const 0064 { 0065 if (!RestApiAbstractJob::canStart()) { 0066 return false; 0067 } 0068 if (!mUsersSetPreferencesInfo.isValid()) { 0069 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UsersSetPreferencesJob: mUsersSetPreferencesInfo is not valid."; 0070 return false; 0071 } 0072 return true; 0073 } 0074 0075 QNetworkRequest UsersSetPreferencesJob::request() const 0076 { 0077 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersSetPreferences); 0078 QNetworkRequest request(url); 0079 addAuthRawHeader(request); 0080 addRequestAttribute(request); 0081 return request; 0082 } 0083 0084 QJsonDocument UsersSetPreferencesJob::json() const 0085 { 0086 QJsonObject jsonObj; 0087 QJsonObject dataObj; 0088 jsonObj[QLatin1String("userId")] = mUsersSetPreferencesInfo.userId; 0089 if (!mUsersSetPreferencesInfo.newRoomNotification.isEmpty()) { 0090 dataObj[QLatin1String("newRoomNotification")] = mUsersSetPreferencesInfo.newRoomNotification; 0091 } 0092 if (!mUsersSetPreferencesInfo.newMessageNotification.isEmpty()) { 0093 dataObj[QLatin1String("newMessageNotification")] = mUsersSetPreferencesInfo.newMessageNotification; 0094 } 0095 if (!mUsersSetPreferencesInfo.desktopNotifications.isEmpty()) { 0096 dataObj[QLatin1String("desktopNotifications")] = mUsersSetPreferencesInfo.desktopNotifications; 0097 } 0098 if (!mUsersSetPreferencesInfo.pushNotifications.isEmpty()) { 0099 dataObj[QLatin1String("pushNotifications")] = mUsersSetPreferencesInfo.pushNotifications; 0100 } 0101 if (!mUsersSetPreferencesInfo.emailNotificationMode.isEmpty()) { 0102 dataObj[QLatin1String("emailNotificationMode")] = mUsersSetPreferencesInfo.emailNotificationMode; 0103 } 0104 if (!mUsersSetPreferencesInfo.highlights.isEmpty()) { 0105 dataObj[QLatin1String("highlights")] = QJsonArray::fromStringList(mUsersSetPreferencesInfo.highlights); 0106 } 0107 0108 if (mUsersSetPreferencesInfo.useEmoji != UsersSetPreferencesInfo::Unknown) { 0109 dataObj[QLatin1String("useEmojis")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.useEmoji); 0110 } 0111 if (mUsersSetPreferencesInfo.convertAsciiToEmoji != UsersSetPreferencesInfo::Unknown) { 0112 dataObj[QLatin1String("convertAsciiEmoji")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.convertAsciiToEmoji); 0113 } 0114 if (mUsersSetPreferencesInfo.hideRoles != UsersSetPreferencesInfo::Unknown) { 0115 dataObj[QLatin1String("hideRoles")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.hideRoles); 0116 } 0117 if (mUsersSetPreferencesInfo.displayAvatars != UsersSetPreferencesInfo::Unknown) { 0118 dataObj[QLatin1String("displayAvatars")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.displayAvatars); 0119 } 0120 if (mUsersSetPreferencesInfo.sidebarDisplayAvatar != UsersSetPreferencesInfo::Unknown) { 0121 dataObj[QLatin1String("sidebarDisplayAvatar")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.sidebarDisplayAvatar); 0122 } 0123 if (mUsersSetPreferencesInfo.sidebarShowUnread != UsersSetPreferencesInfo::Unknown) { 0124 dataObj[QLatin1String("sidebarShowUnread")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.sidebarShowUnread); 0125 } 0126 if (mUsersSetPreferencesInfo.sidebarShowFavorites != UsersSetPreferencesInfo::Unknown) { 0127 dataObj[QLatin1String("sidebarShowFavorites")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.sidebarShowFavorites); 0128 } 0129 if (!mUsersSetPreferencesInfo.sidebarSortby.isEmpty()) { 0130 dataObj[QLatin1String("sidebarSortby")] = mUsersSetPreferencesInfo.sidebarSortby; 0131 } 0132 if (mUsersSetPreferencesInfo.receiveLoginDetectionEmail != UsersSetPreferencesInfo::Unknown) { 0133 dataObj[QLatin1String("receiveLoginDetectionEmail")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.receiveLoginDetectionEmail); 0134 } 0135 if (mUsersSetPreferencesInfo.enableAutoAway != UsersSetPreferencesInfo::Unknown) { 0136 dataObj[QLatin1String("enableAutoAway")] = UsersSetPreferencesInfo::convertToBool(mUsersSetPreferencesInfo.enableAutoAway); 0137 } 0138 if (mUsersSetPreferencesInfo.idleTimeLimit != -1) { 0139 dataObj[QLatin1String("idleTimeLimit")] = mUsersSetPreferencesInfo.idleTimeLimit; 0140 } 0141 jsonObj[QLatin1String("data")] = dataObj; 0142 const QJsonDocument postData = QJsonDocument(jsonObj); 0143 return postData; 0144 } 0145 0146 QString UsersSetPreferencesJob::errorMessage(const QString &str, const QJsonObject &details) 0147 { 0148 if (str == QLatin1String("invalid-params")) { 0149 return i18n("Invalid parameters"); 0150 } 0151 return RestApiAbstractJob::errorMessage(str, details); 0152 } 0153 0154 QDebug operator<<(QDebug d, const RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo &t) 0155 { 0156 d << "userId : " << t.userId; 0157 d << "newRoomNotification : " << t.newRoomNotification; 0158 d << "newMessageNotification : " << t.newMessageNotification; 0159 d << "desktopNotifications : " << t.desktopNotifications; 0160 d << "pushNotifications : " << t.pushNotifications; 0161 d << "emailNotificationMode: " << t.emailNotificationMode; 0162 d << "userId : " << t.userId; 0163 d << " highlights : " << t.highlights; 0164 d << "useEmoji: " << t.useEmoji; 0165 d << "convertAsciiToEmoji: " << t.convertAsciiToEmoji; 0166 d << "hideRoles: " << t.hideRoles; 0167 d << "displayAvatars: " << t.displayAvatars; 0168 d << "sidebarShowUnread: " << t.sidebarShowUnread; 0169 d << "sidebarDisplayAvatar " << t.sidebarDisplayAvatar; 0170 d << "sidebarShowFavorites " << t.sidebarShowFavorites; 0171 d << "sidebarSortby: " << t.sidebarSortby; 0172 d << "receiveLoginDetectionEmail: " << t.receiveLoginDetectionEmail; 0173 return d; 0174 } 0175 0176 bool UsersSetPreferencesJob::UsersSetPreferencesInfo::isValid() const 0177 { 0178 return !userId.isEmpty(); 0179 } 0180 0181 bool UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToBool(State state) 0182 { 0183 switch (state) { 0184 case Unknown: 0185 return false; 0186 case Checked: 0187 return true; 0188 case Unchecked: 0189 return false; 0190 } 0191 return false; 0192 } 0193 0194 UsersSetPreferencesJob::UsersSetPreferencesInfo::State UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToState(bool checked) 0195 { 0196 return checked ? UsersSetPreferencesJob::UsersSetPreferencesInfo::Checked : UsersSetPreferencesJob::UsersSetPreferencesInfo::Unchecked; 0197 } 0198 0199 #include "moc_userssetpreferencesjob.cpp"