libQuotient
A Qt library for building matrix clients
syncdata.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 "quotient_common.h"
7 
8 #include "events/stateevent.h"
9 
10 namespace Quotient {
11 
12 constexpr inline auto UnreadNotificationsKey = "unread_notifications"_ls;
13 constexpr inline auto PartiallyReadCountKey = "x-quotient.since_fully_read_count"_ls;
14 constexpr inline auto NewUnreadCountKey = "org.matrix.msc2654.unread_count"_ls;
15 constexpr inline auto HighlightCountKey = "highlight_count"_ls;
16 
17 //! \brief Room summary, as defined in MSC688
18 //!
19 //! Every member of this structure is an optional; as per the MSC, only changed values are sent
20 //! from the server so if nothing is in the payload the respective member will be omitted.
21 //! In particular, `!heroes.has_value()` means that nothing has come from the server;
22 //! `heroes.value().isEmpty()` means a peculiar but valid case of a room with the only member -
23 //! the current user.
24 struct QUOTIENT_API RoomSummary {
25  std::optional<int> joinedMemberCount;
26  std::optional<int> invitedMemberCount;
27  std::optional<QStringList> heroes; //!< mxids used to form the room name
28 
29  bool isEmpty() const;
30 };
31 QUOTIENT_API QDebug operator<<(QDebug dbg, const RoomSummary& rs);
32 
33 template <>
34 struct JsonObjectConverter<RoomSummary> {
35  static void dumpTo(QJsonObject& jo, const RoomSummary& rs);
36  static void fillFrom(const QJsonObject& jo, RoomSummary& rs);
37 };
38 
39 /// Information on e2e device updates. Note: only present on an
40 /// incremental sync.
41 struct DevicesList {
42  /// List of users who have updated their device identity keys, or who
43  /// now share an encrypted room with the client since the previous
44  /// sync response.
45  QStringList changed;
46 
47  /// List of users with whom we do not share any encrypted rooms
48  /// any more since the previous sync response.
49  QStringList left;
50 };
51 
52 QDebug operator<<(QDebug dhg, const DevicesList& devicesList);
53 
54 template <>
55 struct JsonObjectConverter<DevicesList> {
56  static void dumpTo(QJsonObject &jo, const DevicesList &dev);
57  static void fillFrom(const QJsonObject& jo, DevicesList& rs);
58 };
59 
60 class QUOTIENT_API SyncRoomData {
61 public:
62  QString roomId;
63  JoinState joinState;
64  RoomSummary summary;
65  StateEvents state;
66  RoomEvents timeline;
67  Events ephemeral;
68  Events accountData;
69 
70  bool timelineLimited;
71  QString timelinePrevBatch;
72  std::optional<int> partiallyReadCount;
73  std::optional<int> unreadCount;
74  std::optional<int> highlightCount;
75 
76  SyncRoomData(QString roomId, JoinState joinState,
77  const QJsonObject& roomJson);
78 };
79 
80 // QVector cannot work with non-copyable objects, std::vector can.
81 using SyncDataList = std::vector<SyncRoomData>;
82 
83 class QUOTIENT_API SyncData {
84 public:
85  SyncData() = default;
86  explicit SyncData(const QString& cacheFileName);
87  //! Parse sync response into room events
88  //! \param json response from /sync or a room state cache
89  void parseJson(const QJsonObject& json, const QString& baseDir = {});
90 
91  Events takePresenceData();
92  Events takeAccountData();
93  Events takeToDeviceEvents();
94  const QHash<QString, int>& deviceOneTimeKeysCount() const
95  {
96  return deviceOneTimeKeysCount_;
97  }
98  SyncDataList takeRoomData();
99  DevicesList takeDevicesList();
100 
101  QString nextBatch() const { return nextBatch_; }
102 
103  QStringList unresolvedRooms() const { return unresolvedRoomIds; }
104 
105  static constexpr int MajorCacheVersion = 11;
106  static std::pair<int, int> cacheVersion();
107  static QString fileNameForRoom(QString roomId);
108 
109 private:
110  QString nextBatch_;
111  Events presenceData;
112  Events accountData;
113  Events toDeviceEvents;
114  SyncDataList roomData;
115  QStringList unresolvedRoomIds;
116  QHash<QString, int> deviceOneTimeKeysCount_;
117  DevicesList devicesList;
118 };
119 } // namespace Quotient