diff --git a/autotests/savenotificationjobtest.cpp b/autotests/savenotificationjobtest.cpp index 83434423..3c5f4b39 100644 --- a/autotests/savenotificationjobtest.cpp +++ b/autotests/savenotificationjobtest.cpp @@ -1,85 +1,99 @@ /* Copyright (c) 2018 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "savenotificationjobtest.h" #include "restapi/rooms/savenotificationjob.h" #include "ruqola_restapi_helper.h" #include #include QTEST_GUILESS_MAIN(SaveNotificationJobTest) SaveNotificationJobTest::SaveNotificationJobTest(QObject *parent) : QObject(parent) { } void SaveNotificationJobTest::shouldHaveDefaultValue() { SaveNotificationJob job; verifyDefaultValue(&job); QVERIFY(job.requireHttpAuthentication()); QVERIFY(job.roomId().isEmpty()); } void SaveNotificationJobTest::shouldGenerateRequest() { SaveNotificationJob job; QNetworkRequest request = QNetworkRequest(QUrl()); verifyAuthentication(&job, request); QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/rooms.saveNotification"))); QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json")); } void SaveNotificationJobTest::shouldGenerateJson() { SaveNotificationJob job; const QString roomId = QStringLiteral("foo1"); job.setRoomId(roomId); QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"notifications\":{},\"roomId\":\"%1\"}").arg(roomId).toLatin1()); + + //Add settings + const QString audioNotification = QStringLiteral("all"); + job.setAudioNotifications(audioNotification); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), + QStringLiteral("{\"notifications\":{\"audioNotifications\":\"%2\"},\"roomId\":\"%1\"}") + .arg(roomId).arg(audioNotification).toLatin1()); + + const bool hideUnread = true; + job.setHideUnreadStatus(hideUnread); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), + QStringLiteral("{\"notifications\":{\"audioNotifications\":\"%2\",\"hideUnreadStatus\":\"1\"},\"roomId\":\"%1\"}") + .arg(roomId).arg(audioNotification).toLatin1()); + //TODO add more settings } void SaveNotificationJobTest::shouldNotStarting() { SaveNotificationJob job; RestApiMethod *method = new RestApiMethod; method->setServerUrl(QStringLiteral("http://www.kde.org")); job.setRestApiMethod(method); QNetworkAccessManager *mNetworkAccessManager = new QNetworkAccessManager; job.setNetworkAccessManager(mNetworkAccessManager); QVERIFY(!job.canStart()); const QString auth = QStringLiteral("foo"); const QString userId = QStringLiteral("foo"); job.setAuthToken(auth); QVERIFY(!job.canStart()); job.setUserId(userId); QVERIFY(!job.canStart()); const QString roomId = QStringLiteral("foo1"); job.setRoomId(roomId); QVERIFY(!job.canStart()); //We need to change a settings job.setAudioNotificationValue(QStringLiteral("foo")); QVERIFY(job.canStart()); delete method; delete mNetworkAccessManager; } diff --git a/src/restapi/rooms/savenotificationjob.cpp b/src/restapi/rooms/savenotificationjob.cpp index 7c95cdd0..bb0101a1 100644 --- a/src/restapi/rooms/savenotificationjob.cpp +++ b/src/restapi/rooms/savenotificationjob.cpp @@ -1,230 +1,230 @@ /* Copyright (c) 2018 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "savenotificationjob.h" #include "restapimethod.h" #include "ruqola_restapi_debug.h" #include #include #include SaveNotificationJob::SaveNotificationJob(QObject *parent) : RestApiAbstractJob(parent) { } SaveNotificationJob::~SaveNotificationJob() { } bool SaveNotificationJob::start() { if (!canStart()) { deleteLater(); return false; } const QByteArray baPostData = json().toJson(QJsonDocument::Compact); addLoggerInfo("SaveNotificationJob::start: " + baPostData); QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); connect(reply, &QNetworkReply::finished, this, &SaveNotificationJob::slotChangeNotificationFinished); addLoggerInfo(QByteArrayLiteral("SaveNotificationJob: start")); return true; } void SaveNotificationJob::slotChangeNotificationFinished() { QNetworkReply *reply = qobject_cast(sender()); if (reply) { const QByteArray data = reply->readAll(); addLoggerInfo(QByteArrayLiteral("SaveNotificationJob: finished: ") + data); Q_EMIT changeNotificationDone(); } deleteLater(); } QString SaveNotificationJob::unreadAlert() const { return mUnreadAlert; } void SaveNotificationJob::setUnreadAlert(const QString &unreadAlert) { mSettingsWillBeChanged = mSettingsWillBeChanged & UnreadAlert; mUnreadAlert = unreadAlert; } int SaveNotificationJob::desktopNotificationDuration() const { return mDesktopNotificationDuration; } void SaveNotificationJob::setDesktopNotificationDuration(int desktopNotificationDuration) { mSettingsWillBeChanged |= DesktopNotificationDuration; mDesktopNotificationDuration = desktopNotificationDuration; } QString SaveNotificationJob::audioNotificationValue() const { return mAudioNotificationValue; } void SaveNotificationJob::setAudioNotificationValue(const QString &audioNotificationValue) { mSettingsWillBeChanged |= AudioNotificationValue; mAudioNotificationValue = audioNotificationValue; } QString SaveNotificationJob::mobilePushNotifications() const { return mMobilePushNotifications; } void SaveNotificationJob::setMobilePushNotifications(const QString &mobilePushNotifications) { mSettingsWillBeChanged |= MobilePushNotifications; mMobilePushNotifications = mobilePushNotifications; } QString SaveNotificationJob::audioNotifications() const { return mAudioNotifications; } void SaveNotificationJob::setAudioNotifications(const QString &audioNotifications) { mSettingsWillBeChanged |= AudioNotifications; mAudioNotifications = audioNotifications; } QString SaveNotificationJob::emailNotifications() const { return mEmailNotifications; } void SaveNotificationJob::setEmailNotifications(const QString &emailNotifications) { mSettingsWillBeChanged |= EmailNotifications; mEmailNotifications = emailNotifications; } bool SaveNotificationJob::hideUnreadStatus() const { return mHideUnreadStatus; } void SaveNotificationJob::setHideUnreadStatus(bool hideUnreadStatus) { mSettingsWillBeChanged |= HideUnreadStatus; mHideUnreadStatus = hideUnreadStatus; } bool SaveNotificationJob::disableNotifications() const { return mDisableNotifications; } void SaveNotificationJob::setDisableNotifications(bool disableNotifications) { mSettingsWillBeChanged |= DisableNotifications; mDisableNotifications = disableNotifications; } QString SaveNotificationJob::roomId() const { return mRoomId; } void SaveNotificationJob::setRoomId(const QString &roomId) { mRoomId = roomId; } bool SaveNotificationJob::requireHttpAuthentication() const { return true; } bool SaveNotificationJob::canStart() const { if (!RestApiAbstractJob::canStart()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start SaveNotificationJob"; return false; } if (mRoomId.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "SaveNotificationJob: mRoomId is empty"; return false; } if (mSettingsWillBeChanged == Unknown) { qCWarning(RUQOLA_RESTAPI_LOG) << "SaveNotificationJob: any settings will be changed! it's a bug"; return false; } return true; } QNetworkRequest SaveNotificationJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::RoomsSaveNotification); QNetworkRequest request(url); addAuthRawHeader(request); request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json")); return request; } QJsonDocument SaveNotificationJob::json() const { QJsonObject jsonObj; jsonObj[QLatin1String("roomId")] = mRoomId; QJsonObject notificationsJson; if (mSettingsWillBeChanged & EmailNotifications) { notificationsJson[QLatin1String("emailNotifications")] = emailNotifications(); } if (mSettingsWillBeChanged & AudioNotifications) { notificationsJson[QLatin1String("audioNotifications")] = audioNotifications(); } if (mSettingsWillBeChanged & MobilePushNotifications) { notificationsJson[QLatin1String("mobilePushNotifications")] = mobilePushNotifications(); } if (mSettingsWillBeChanged & AudioNotificationValue) { notificationsJson[QLatin1String("audioNotificationValue")] = audioNotificationValue(); } if (mSettingsWillBeChanged & UnreadAlert) { notificationsJson[QLatin1String("unreadAlert")] = unreadAlert(); } if (mSettingsWillBeChanged & DesktopNotificationDuration) { notificationsJson[QLatin1String("desktopNotificationDuration")] = desktopNotificationDuration(); } if (mSettingsWillBeChanged & DisableNotifications) { notificationsJson[QLatin1String("disableNotifications")] = disableNotifications(); } if (mSettingsWillBeChanged & HideUnreadStatus) { - notificationsJson[QLatin1String("hideUnreadStatus")] = hideUnreadStatus(); + notificationsJson[QLatin1String("hideUnreadStatus")] = hideUnreadStatus() ? QStringLiteral("1") : QStringLiteral("0"); } //TODO don't assign all. => todo add enum jsonObj[QLatin1String("notifications")] = notificationsJson; const QJsonDocument postData = QJsonDocument(jsonObj); return postData; }