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
8namespace Quotient {
9constexpr inline auto FavouriteTag = "m.favourite"_ls;
10constexpr inline auto LowPriorityTag = "m.lowpriority"_ls;
11constexpr inline auto ServerNoticeTag = "m.server_notice"_ls;
12
13struct TagRecord {
14 Omittable<float> order = none;
15};
16
17inline 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
24template <>
25struct 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
46using TagsMap = QHash<QString, TagRecord>;
47
48DEFINE_SIMPLE_EVENT(TagEvent, Event, "m.tag", TagsMap, tags, "tags")
49DEFINE_SIMPLE_EVENT(ReadMarkerEventImpl, Event, "m.fully_read", QString,
50 eventId, "event_id")
51class ReadMarkerEvent : public ReadMarkerEventImpl {
52public:
53 using ReadMarkerEventImpl::ReadMarkerEventImpl;
54 [[deprecated("Use ReadMarkerEvent::eventId() instead")]]
55 auto event_id() const { return eventId(); }
56};
57DEFINE_SIMPLE_EVENT(IgnoredUsersEventImpl, Event, "m.ignored_user_list",
58 QSet<QString>, ignoredUsers, "ignored_users")
59class IgnoredUsersEvent : public IgnoredUsersEventImpl {
60public:
61 using IgnoredUsersEventImpl::IgnoredUsersEventImpl;
62 [[deprecated("Use IgnoredUsersEvent::ignoredUsers() instead")]]
63 auto ignored_users() const { return ignoredUsers(); }
64};
65} // namespace Quotient
66