| 1 | // SPDX-FileCopyrightText: 2018 Kitsune Ral <kitsune-ral@users.sf.net> |
| 2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "event.h" |
| 7 | |
| 8 | namespace Quotient { |
| 9 | constexpr inline auto FavouriteTag = "m.favourite"_ls ; |
| 10 | constexpr inline auto LowPriorityTag = "m.lowpriority"_ls ; |
| 11 | constexpr inline auto ServerNoticeTag = "m.server_notice"_ls ; |
| 12 | |
| 13 | struct TagRecord { |
| 14 | Omittable<float> order = none; |
| 15 | }; |
| 16 | |
| 17 | inline bool operator<(TagRecord lhs, TagRecord rhs) |
| 18 | { |
| 19 | // Per The Spec, rooms with no order should be after those with order, |
| 20 | // against std::optional<>::operator<() convention. |
| 21 | return lhs.order && (!rhs.order || *lhs.order < *rhs.order); |
| 22 | } |
| 23 | |
| 24 | template <> |
| 25 | struct JsonObjectConverter<TagRecord> { |
| 26 | static void fillFrom(const QJsonObject& jo, TagRecord& rec) |
| 27 | { |
| 28 | // Parse a float both from JSON double and JSON string because |
| 29 | // the library previously used to use strings to store order. |
| 30 | const auto orderJv = jo.value(key: "order"_ls ); |
| 31 | if (orderJv.isDouble()) |
| 32 | rec.order = fromJson<float>(jv: orderJv); |
| 33 | if (orderJv.isString()) { |
| 34 | bool ok = false; |
| 35 | rec.order = orderJv.toString().toFloat(ok: &ok); |
| 36 | if (!ok) |
| 37 | rec.order = none; |
| 38 | } |
| 39 | } |
| 40 | static void dumpTo(QJsonObject& jo, TagRecord rec) |
| 41 | { |
| 42 | addParam<IfNotEmpty>(container&: jo, QStringLiteral("order" ), value&: rec.order); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | using TagsMap = QHash<QString, TagRecord>; |
| 47 | |
| 48 | DEFINE_SIMPLE_EVENT(TagEvent, Event, "m.tag" , TagsMap, tags, "tags" ) |
| 49 | DEFINE_SIMPLE_EVENT(ReadMarkerEventImpl, Event, "m.fully_read" , QString, |
| 50 | eventId, "event_id" ) |
| 51 | class ReadMarkerEvent : public ReadMarkerEventImpl { |
| 52 | public: |
| 53 | using ReadMarkerEventImpl::ReadMarkerEventImpl; |
| 54 | [[deprecated("Use ReadMarkerEvent::eventId() instead" )]] |
| 55 | auto event_id() const { return eventId(); } |
| 56 | }; |
| 57 | DEFINE_SIMPLE_EVENT(IgnoredUsersEventImpl, Event, "m.ignored_user_list" , |
| 58 | QSet<QString>, ignoredUsers, "ignored_users" ) |
| 59 | class IgnoredUsersEvent : public IgnoredUsersEventImpl { |
| 60 | public: |
| 61 | using IgnoredUsersEventImpl::IgnoredUsersEventImpl; |
| 62 | [[deprecated("Use IgnoredUsersEvent::ignoredUsers() instead" )]] |
| 63 | auto ignored_users() const { return ignoredUsers(); } |
| 64 | }; |
| 65 | } // namespace Quotient |
| 66 | |