diff options
Diffstat (limited to 'lib/csapi/notifications.cpp')
-rw-r--r-- | lib/csapi/notifications.cpp | 81 |
1 files changed, 16 insertions, 65 deletions
diff --git a/lib/csapi/notifications.cpp b/lib/csapi/notifications.cpp index c00b7cb0..a479d500 100644 --- a/lib/csapi/notifications.cpp +++ b/lib/csapi/notifications.cpp @@ -4,40 +4,12 @@ #include "notifications.h" -#include "converters.h" - #include <QtCore/QStringBuilder> -using namespace QMatrixClient; - -static const auto basePath = QStringLiteral("/_matrix/client/r0"); - -namespace QMatrixClient -{ - // Converters - - template <> struct JsonObjectConverter<GetNotificationsJob::Notification> - { - static void fillFrom(const QJsonObject& jo, GetNotificationsJob::Notification& result) - { - fromJson(jo.value("actions"_ls), result.actions); - fromJson(jo.value("event"_ls), result.event); - fromJson(jo.value("profile_tag"_ls), result.profileTag); - fromJson(jo.value("read"_ls), result.read); - fromJson(jo.value("room_id"_ls), result.roomId); - fromJson(jo.value("ts"_ls), result.ts); - } - }; -} // namespace QMatrixClient +using namespace Quotient; -class GetNotificationsJob::Private -{ - public: - QString nextToken; - std::vector<Notification> notifications; -}; - -BaseJob::Query queryToGetNotifications(const QString& from, Omittable<int> limit, const QString& only) +auto queryToGetNotifications(const QString& from, Omittable<int> limit, + const QString& only) { BaseJob::Query _q; addParam<IfNotEmpty>(_q, QStringLiteral("from"), from); @@ -46,43 +18,22 @@ BaseJob::Query queryToGetNotifications(const QString& from, Omittable<int> limit return _q; } -QUrl GetNotificationsJob::makeRequestUrl(QUrl baseUrl, const QString& from, Omittable<int> limit, const QString& only) +QUrl GetNotificationsJob::makeRequestUrl(QUrl baseUrl, const QString& from, + Omittable<int> limit, + const QString& only) { return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath % "/notifications", - queryToGetNotifications(from, limit, only)); + QStringLiteral("/_matrix/client/r0") + % "/notifications", + queryToGetNotifications(from, limit, only)); } -static const auto GetNotificationsJobName = QStringLiteral("GetNotificationsJob"); - -GetNotificationsJob::GetNotificationsJob(const QString& from, Omittable<int> limit, const QString& only) - : BaseJob(HttpVerb::Get, GetNotificationsJobName, - basePath % "/notifications", - queryToGetNotifications(from, limit, only)) - , d(new Private) +GetNotificationsJob::GetNotificationsJob(const QString& from, + Omittable<int> limit, + const QString& only) + : BaseJob(HttpVerb::Get, QStringLiteral("GetNotificationsJob"), + QStringLiteral("/_matrix/client/r0") % "/notifications", + queryToGetNotifications(from, limit, only)) { + addExpectedKey("notifications"); } - -GetNotificationsJob::~GetNotificationsJob() = default; - -const QString& GetNotificationsJob::nextToken() const -{ - return d->nextToken; -} - -std::vector<GetNotificationsJob::Notification>&& GetNotificationsJob::notifications() -{ - return std::move(d->notifications); -} - -BaseJob::Status GetNotificationsJob::parseJson(const QJsonDocument& data) -{ - auto json = data.object(); - fromJson(json.value("next_token"_ls), d->nextToken); - if (!json.contains("notifications"_ls)) - return { JsonParseError, - "The key 'notifications' not found in the response" }; - fromJson(json.value("notifications"_ls), d->notifications); - return Success; -} - |