libQuotient
A Qt library for building matrix clients
accountdataevents.h
Go to the documentation of this file.
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  std::optional<float> order = std::nullopt;
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("order"_ls);
31  if (orderJv.isDouble())
32  rec.order = fromJson<float>(orderJv);
33  if (orderJv.isString()) {
34  bool ok = false;
35  rec.order = orderJv.toString().toFloat(&ok);
36  if (!ok)
37  rec.order = std::nullopt;
38  }
39  }
40  static void dumpTo(QJsonObject& jo, TagRecord rec)
41  {
42  addParam<IfNotEmpty>(jo, QStringLiteral("order"), 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(ReadMarkerEvent, Event, "m.fully_read", QString,
50  eventId, "event_id")
51 DEFINE_SIMPLE_EVENT(IgnoredUsersEvent, Event, "m.ignored_user_list",
52  QSet<QString>, ignoredUsers, "ignored_users")
53 } // namespace Quotient