1 | #pragma once |
2 | |
3 | /// @file |
4 | /// @brief Response from the /sync API. |
5 | |
6 | #include <map> |
7 | #include <string> |
8 | #include <vector> |
9 | |
10 | #include "mtx/events/collections.hpp" |
11 | |
12 | #if __has_include(<nlohmann/json_fwd.hpp>) |
13 | #include <nlohmann/json_fwd.hpp> |
14 | #else |
15 | #include <nlohmann/json.hpp> |
16 | #endif |
17 | |
18 | namespace mtx { |
19 | namespace responses { |
20 | |
21 | //! Room specific Account Data events. |
22 | struct AccountData |
23 | { |
24 | //! List of events. |
25 | std::vector<events::collections::RoomAccountDataEvents> events; |
26 | |
27 | friend void from_json(const nlohmann::json &obj, AccountData &account_data); |
28 | }; |
29 | |
30 | //! State events. |
31 | struct State |
32 | { |
33 | //! List of events. |
34 | std::vector<events::collections::StateEvents> events; |
35 | |
36 | friend void from_json(const nlohmann::json &obj, State &state); |
37 | }; |
38 | |
39 | //! State and Room events. |
40 | struct Timeline |
41 | { |
42 | //! List of events. |
43 | std::vector<events::collections::TimelineEvents> events; |
44 | //! A token that can be supplied to to the from parameter of |
45 | //! the rooms/{roomId}/messages endpoint. |
46 | std::string prev_batch; |
47 | //! **true** if the number of events returned was limited by the |
48 | //! limit on the filter. |
49 | bool limited = false; |
50 | |
51 | friend void from_json(const nlohmann::json &obj, Timeline &timeline); |
52 | }; |
53 | |
54 | //! Counts of unread notifications for this room |
55 | struct UnreadNotifications |
56 | { |
57 | //! The number of unread notifications for this room with the |
58 | //! highlight flag set. |
59 | uint64_t highlight_count = 0; |
60 | //! The total number of unread notifications for this room. |
61 | uint64_t notification_count = 0; |
62 | |
63 | friend void from_json(const nlohmann::json &obj, UnreadNotifications ¬ifications); |
64 | }; |
65 | |
66 | //! The ephemeral events in the room that aren't recorded in |
67 | //! the timeline or state of the room. e.g. typing. |
68 | struct Ephemeral |
69 | { |
70 | //! A list of ephemeral events like typing and read notifications. |
71 | std::vector<events::collections::EphemeralEvents> events; |
72 | |
73 | friend void from_json(const nlohmann::json &obj, Ephemeral &ephemeral); |
74 | }; |
75 | |
76 | //! A room that the user has joined. |
77 | struct JoinedRoom |
78 | { |
79 | //! Updates to the state, between the time indicated by the since parameter, |
80 | //! and the start of the timeline (or all state up to the start of the timeline, |
81 | //! if since is not given, or full_state is true) |
82 | State state; |
83 | //! The timeline of messages and state changes in the room. |
84 | Timeline timeline; |
85 | //! Counts of unread notifications for this room. |
86 | UnreadNotifications unread_notifications; |
87 | //! The ephemeral events in the room that aren't recorded in the |
88 | //! timeline or state of the room. e.g. typing. |
89 | Ephemeral ephemeral; |
90 | //! The account_data events associated with this room. |
91 | AccountData account_data; |
92 | |
93 | friend void from_json(const nlohmann::json &obj, JoinedRoom &room); |
94 | }; |
95 | |
96 | //! A room that the user has left or been banned from. |
97 | struct LeftRoom |
98 | { |
99 | //! The state updates for the room up to the start of the timeline. |
100 | State state; |
101 | //! The timeline of messages and state changes in the room |
102 | //! up to the point when the user left. |
103 | Timeline timeline; |
104 | |
105 | friend void from_json(const nlohmann::json &obj, LeftRoom &room); |
106 | }; |
107 | |
108 | //! A room that the user has been invited to. |
109 | struct InvitedRoom |
110 | { |
111 | //! The state of a room that the user has been invited to. |
112 | //! These state events may only have the `sender`, `type`, |
113 | //! `state_key` and `content` keys present. |
114 | std::vector<events::collections::StrippedEvents> invite_state; |
115 | //! Returns the name of the room. |
116 | std::string name() const; |
117 | //! Returns the URL for the avatar of the room. |
118 | std::string avatar() const; |
119 | |
120 | friend void from_json(const nlohmann::json &obj, InvitedRoom &room); |
121 | }; |
122 | |
123 | //! A room that the user has knocked on. |
124 | struct KnockedRoom |
125 | { |
126 | //! The state of a room that the user has knocked on. |
127 | //! These state events may only have the `sender`, `type`, |
128 | //! `state_key` and `content` keys present. |
129 | std::vector<events::collections::StrippedEvents> knock_state; |
130 | //! Returns the name of the room. |
131 | std::string name() const; |
132 | //! Returns the URL for the avatar of the room. |
133 | std::string avatar() const; |
134 | |
135 | friend void from_json(const nlohmann::json &obj, KnockedRoom &room); |
136 | }; |
137 | |
138 | //! Room updates. |
139 | struct Rooms |
140 | { |
141 | //! The rooms that the user has joined. |
142 | std::map<std::string, JoinedRoom> join; |
143 | //! The rooms that the user has left or been banned from. |
144 | std::map<std::string, LeftRoom> leave; |
145 | //! The rooms that the user has been invited to. |
146 | std::map<std::string, InvitedRoom> invite; |
147 | |
148 | //! The rooms that the user has knocked on. |
149 | std::map<std::string, KnockedRoom> knock; |
150 | |
151 | friend void from_json(const nlohmann::json &obj, Rooms &rooms); |
152 | }; |
153 | |
154 | //! Information on e2e device updates. |
155 | struct DeviceLists |
156 | { |
157 | //! List of users who have updated their device identity keys |
158 | //! since the previous sync response. |
159 | std::vector<std::string> changed; |
160 | //! List of users who may have left all the end-to-end encrypted |
161 | //! rooms they previously shared with the user. |
162 | std::vector<std::string> left; |
163 | |
164 | friend void from_json(const nlohmann::json &obj, DeviceLists &device_lists); |
165 | }; |
166 | |
167 | //! Information on to_device events in sync. |
168 | struct ToDevice |
169 | { |
170 | //! Information on the send-to-device messages for the client device. |
171 | std::vector<events::collections::DeviceEvents> events; |
172 | |
173 | friend void from_json(const nlohmann::json &obj, ToDevice &to_device); |
174 | }; |
175 | |
176 | //! Response from the `GET /_matrix/client/r0/sync` endpoint. |
177 | struct Sync |
178 | { |
179 | //! The batch token to supply in the since param of the next /sync request. |
180 | std::string next_batch; |
181 | //! Updates to rooms. |
182 | Rooms rooms; |
183 | //! Information on the send-to-device messages for the client device. |
184 | ToDevice to_device; |
185 | //! Information about presence of other users |
186 | std::vector<mtx::events::Event<mtx::events::presence::Presence>> presence; |
187 | //! Information on end-to-end device updates, |
188 | DeviceLists device_lists; |
189 | //! A mapping from algorithm to the number of one time keys |
190 | //! the server has for the current device. |
191 | std::map<std::string, uint16_t> device_one_time_keys_count; |
192 | //! Required. The unused fallback key algorithms. Absence can be used to detect server support |
193 | std::optional<std::vector<std::string>> device_unused_fallback_key_types; |
194 | |
195 | //! global account data |
196 | AccountData account_data; |
197 | |
198 | friend void from_json(const nlohmann::json &obj, Sync &response); |
199 | }; |
200 | } |
201 | } |
202 | |