Debian Codegraph @

mtxclient/include/mtx/requests.hpp

raw symbols SCIP
1 #pragma once
2
3 /// @file
4 /// @brief Structs for for requests to the Matrix API.
5
6 #include <string>
7 #include <variant>
8
9 #include <mtx/common.hpp>
10 #include <mtx/events/collections.hpp>
11 #include <mtx/identifiers.hpp>
12
13 #include <nlohmann/json.hpp>
14
15 namespace mtx {
16 //! Namespace for request structs
17 namespace requests {
18 //! Convenience parameter for setting various default state events based on a preset.
19 enum class Preset
20 {
21 //! `join_rules` is set to `invite`. `history_visibility` is set to `shared`.
22 PrivateChat,
23 //! `join_rules` is set to `public`. `history_visibility` is set to `shared`.
24 PublicChat,
25 //! `join_rules` is set to `invite`. `history_visibility` is set to `shared`.
26 //! All invitees are given the same power level as the room creator.
27 TrustedPrivateChat,
28 };
29
30 //! Request payload for the `POST /_matrix/client/r0/createRoom` endpoint.
31 struct CreateRoom
32 {
33 //! If this is included, an `m.room.name` event will
34 //! be sent into the room to indicate the name of the room.
35 std::string name;
36 //! If this is included, an `m.room.topic` event will be sent
37 //! into the room to indicate the topic for the room.
38 std::string topic;
39 //! The desired room alias local part. e.g `#foo:example.com`.
40 std::string room_alias_name;
41 //! A list of user IDs to invite to the room.
42 std::vector<std::string> invite;
43 //! This flag makes the server set the is_direct flag on the
44 //! `m.room.member` events sent to the users in @p invite and @p invite_3pid.
45 bool is_direct = false;
46 //! Convenience parameter for setting various default state events.
47 Preset preset = Preset::PrivateChat;
48 //! Whether or not the room will be visible by non members.
49 common::RoomVisibility visibility = common::RoomVisibility::Private;
50
51 //! A list of state events to set in the new room. This allows the user to override the default
52 //! state events set in the new room. The expected format of the state events are an object with
53 //! type, state_key and content keys set.
54 std::vector<events::collections::StrippedEvents> initial_state;
55
56 //! The room version to set for the room. If not provided, the homeserver is to use its
57 //! configured default. If provided, the homeserver will return a 400 error with the errcode
58 //! M_UNSUPPORTED_ROOM_VERSION if it does not support the room version.
59 std::string room_version;
60
61 //! Extra keys, such as m.federate, to be added to the content of the m.room.create event. The
62 //! server will overwrite the following keys: creator, room_version. Future versions of the
63 //! specification may allow the server to overwrite other keys.
64 std::optional<events::state::Create> creation_content;
65
66 friend void to_json(nlohmann::json &obj, const CreateRoom &request);
67 };
68
69 namespace login_identifier {
70 //! The user is identified by their Matrix ID.
71 struct User
72 {
73 //! A client can identify a user using their Matrix ID. This can either be the fully
74 //! qualified Matrix user ID, or just the localpart of the user ID.
75 std::string user;
76 };
77 //! The user is identified by a third-party identifier in canonicalised form.
78 struct Thirdparty
79 {
80 //! The medium of the third party identifier
81 std::string medium;
82 //! The canonicalised third party address of the user
83 std::string address;
84 };
85 //! The user is identified by a phone number.
86 struct PhoneNumber
87 {
88 //! The country is the two-letter uppercase ISO-3166-1 alpha-2 country code that the number
89 //! in phone should be parsed as if it were dialled from.
90 std::string country;
91 //! The phone number. If the client wishes to canonicalise the phone number, then it can use
92 //! the m.id.thirdparty identifier type with a medium of msisdn instead.
93 std::string phone;
94 };
95 }
96
97 //! Request payload for the `POST /_matrix/client/r0/login` endpoint.
98 struct Login
99 {
100 //! The login type being used. One of ["m.login.password", "m.login.token"]
101 std::string type = "m.login.password";
102 //! Identification information for the user. Usually you use `login_identifier::User` with
103 //! an mxid. Required.
104 std::
105 variant<login_identifier::User, login_identifier::Thirdparty, login_identifier::PhoneNumber>
106 identifier;
107 //! Required when type is m.login.token. The login token.
108 std::string token;
109 //! Required when type is m.login.password. The user's password.
110 std::string password;
111 //! ID of the client device. If this does not correspond to a known client device,
112 //! a new device will be created.
113 //! The server will auto-generate a device_id if this is not specified.
114 std::string device_id;
115 //! A display name to assign to the newly-created device.
116 //! Ignored if device_id corresponds to a known device.
117 std::string initial_device_display_name;
118
119 friend void to_json(nlohmann::json &obj, const Login &request);
120 };
121
122 /// @brief Request payload for
123 /// `POST /_matrix/client/r0/{register,account/password}/email/requestToken`
124 ///
125 /// The homeserver should validate the email itself, either by sending a validation email itself or
126 /// by using a service it has control over.
127 struct RequestEmailToken
128 {
129 //! Required. A unique string generated by the client, and used to identify the validation
130 //! attempt. It must be a string consisting of the characters [0-9a-zA-Z.=_-]. Its length must
131 //! not exceed 255 characters and it must not be empty.
132 std::string client_secret;
133 //! Required. The email address to validate.
134 std::string email;
135
136 //! Required. The server will only send an email if the send_attempt is a number greater than
137 //! the most recent one which it has seen, scoped to that email + client_secret pair. This is to
138 //! avoid repeatedly sending the same email in the case of request retries between the POSTing
139 //! user and the identity server. The client should increment this value if they desire a new
140 //! email (e.g. a reminder) to be sent. If they do not, the server should respond with success
141 //! but not resend the email.
142 int send_attempt = 0;
143
144 friend void to_json(nlohmann::json &obj, const RequestEmailToken &request);
145 };
146
147 /// @brief Request payload for
148 /// `POST /_matrix/client/r0/{register,account/password}/msisdn/requestToken`
149 ///
150 /// The homeserver should validate the email itself, either by sending a validation email itself or
151 /// by using a service it has control over.
152 struct RequestMSISDNToken
153 {
154 //! Required. A unique string generated by the client, and used to identify the validation
155 //! attempt. It must be a string consisting of the characters [0-9a-zA-Z.=_-]. Its length must
156 //! not exceed 255 characters and it must not be empty.
157 std::string client_secret;
158 //! Required. The two-letter uppercase ISO-3166-1 alpha-2 country code that the number in
159 //! phone_number should be parsed as if it were dialled from.
160 std::string country;
161 //! Required. The phone number to validate.
162 std::string phone_number;
163
164 //! Required. The server will only send an SMS if the send_attempt is a number greater than the
165 //! most recent one which it has seen, scoped to that country + phone_number + client_secret
166 //! triple. This is to avoid repeatedly sending the same SMS in the case of request retries
167 //! between the POSTing user and the identity server. The client should increment this value if
168 //! they desire a new SMS (e.g. a reminder) to be sent.
169 int send_attempt = 0;
170 };
171
172 void
173 to_json(nlohmann::json &obj, const RequestMSISDNToken &request);
174
175 //! Validate ownership of an email address/phone number.
176 struct IdentitySubmitToken
177 {
178 //! Required. The session ID, generated by the requestToken call.
179 std::string sid;
180 //! Required. The client secret that was supplied to the requestToken call.
181 std::string client_secret;
182 //! Required. The token generated by the requestToken call and emailed to the user.
183 std::string token;
184
185 friend void to_json(nlohmann::json &obj, const IdentitySubmitToken &request);
186 };
187
188 //! Request payload for the `PUT /_matrix/client/r0/sendToDevice/{eventType}/{transcationID}`
189 template<typename EventContent>
190 using ToDeviceMessages = std::map<mtx::identifiers::User, std::map<std::string, EventContent>>;
191
192 //! Request payload for the `POST /_matrix/client/r0/profile/{userId}/avatar_url` endpoint.
193 struct AvatarUrl
194 {
195 std::string avatar_url;
196
197 friend void to_json(nlohmann::json &obj, const AvatarUrl &request);
198 };
199
200 //! Request payload for the `PUT /_matrix/client/r0/profile/{userId}/displayname` endpoint.
201 struct DisplayName
202 {
203 //! The new display name for this device. If not given, the display name is unchanged.
204 std::string displayname;
205
206 friend void to_json(nlohmann::json &obj, const DisplayName &request);
207 };
208
209 //! Request payload for the `POST /_matrix/client/r0/rooms/{roomId}/invite` endpoint as well as ban,
210 //! unban and kick.
211 struct RoomMembershipChange
212 {
213 std::string user_id;
214
215 //! optional kick, invite or ban reason
216 std::string reason;
217
218 friend void to_json(nlohmann::json &obj, const RoomMembershipChange &request);
219 };
220
221 //! Request payload for the `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}` endpoint.
222 struct TypingNotification
223 {
224 //! Whether the user is typing or not.
225 bool typing = false;
226 //! The length of time in milliseconds to mark this user as typing.
227 uint64_t timeout = 0;
228
229 friend void to_json(nlohmann::json &obj, const TypingNotification &request);
230 };
231
232 //! Request payload for the `PUT /_matrix/client/r0/devices/{deviceId}` endpoint.
233 struct DeviceUpdate
234 {
235 std::string display_name;
236
237 friend void to_json(nlohmann::json &obj, const DeviceUpdate &request);
238 };
239
240 //! Request payload for the `PUT /_matrix/client/r0/directory/list/room/{roomId}` endpoint
241 struct PublicRoomVisibility
242 {
243 //! The new visibility setting for the room. Defaults to 'public'. One of: ["private",
244 //! "public"]
245 common::RoomVisibility visibility;
246
247 friend void to_json(nlohmann::json &obj, const PublicRoomVisibility &request);
248 };
249
250 struct PublicRoomsFilter
251 {
252 //! A string to search for in the room metadata,
253 //! e.g. name, topic, canonical alias etc. (Optional).
254 std::string generic_search_term;
255
256 friend void to_json(nlohmann::json &obj, const PublicRoomsFilter &req);
257 };
258
259 //! Request payload for the `POST /_matrix/client/r0/publicRooms` endpoint.
260 struct PublicRooms
261 {
262 //! Limit the number of results returned.
263 int limit;
264 //! A pagination token from a previous request, allowing clients
265 //! to get the next (or previous) batch of rooms. The direction of
266 //! pagination is specified solely by which token is supplied,
267 //! rather than via an explicit flag.
268 std::string since;
269 //! Filter to apply to the results.
270 PublicRoomsFilter filter;
271 //! Whether or not to include all known networks/protocols from
272 //! application services on the homeserver. Defaults to false.
273 bool include_all_networks = false;
274 //! The specific third party network/protocol to request from
275 //! the homeserver. Can only be used if include_all_networks is false.
276 std::string third_party_instance_id;
277
278 friend void to_json(nlohmann::json &obj, const PublicRooms &request);
279 };
280
281 //! An empty response.
282 ///
283 /// Some endpoints return this on success.
284 struct Empty
285 {
286 friend inline void to_json(nlohmann::json &, const Empty &) {}
287 };
288
289 //! A successfull logout.
290 using Logout = Empty;
291
292 //! A signed onetime key. Usually curve25519.
293 struct SignedOneTimeKey
294 {
295 //! Required. The unpadded Base64-encoded 32-byte Curve25519 public key.
296 std::string key;
297 //! When uploading a signed key, an additional fallback: true key should be included to denote
298 //! that the key is a fallback key.
299 bool fallback = false;
300 //! Required. Signatures of the key object.
301 //! The signature is calculated using the process described at Signing JSON.
302 std::map<std::string, std::map<std::string, std::string>> signatures;
303
304 friend void to_json(nlohmann::json &obj, const SignedOneTimeKey &);
305 };
306
307 //! Upload a mix of device, one time and fallback keys.
308 struct UploadKeys
309 {
310 //! Identity keys for the device.
311 //! May be absent if no new identity keys are required.
312 mtx::crypto::DeviceKeys device_keys;
313 //! One-time public keys for "pre-key" messages.
314 //! The names of the properties should be in the format `<algorithm>:<key_id>`.
315 //! The format of the key is determined by the key algorithm.
316 std::map<std::string, std::variant<std::string, SignedOneTimeKey>> one_time_keys;
317
318 //! The public key which should be used if the device’s one-time keys are exhausted. The
319 //! fallback key is not deleted once used, but should be replaced when additional one-time keys
320 //! are being uploaded. The server will notify the client of the fallback key being used through
321 //! /sync.
322 //!
323 //! There can only be at most one key per algorithm uploaded, and the server will only persist
324 //! one key per algorithm.
325 //!
326 //! When uploading a signed key, an additional fallback: true key should be included to denote
327 //! that the key is a fallback key.
328 //!
329 //! May be absent if a new fallback key is not required.
330 std::map<std::string, std::variant<std::string, SignedOneTimeKey>> fallback_keys;
331
332 friend void to_json(nlohmann::json &obj, const UploadKeys &);
333 };
334
335 //! The default timeout for a download.
336 inline constexpr uint64_t DEFAULT_DOWNLOAD_TIMEOUT = 10UL * 1000; // 10 seconds
337
338 //! Query the keys of devices of specific users.
339 struct QueryKeys
340 {
341 //! The time (in milliseconds) to wait when downloading keys from remote servers.
342 uint64_t timeout = DEFAULT_DOWNLOAD_TIMEOUT;
343 //! The keys to be downloaded.
344 //! A map from user ID, to a list of device IDs, or to an empty
345 //! list to indicate all devices for the corresponding user.
346 std::map<std::string, std::vector<std::string>> device_keys;
347 //! If the client is fetching keys as a result of a device update
348 //! received in a sync request, this should be the 'since' token of
349 //! that sync request, or any later sync token.
350 //! This allows the server to ensure its response contains the keys
351 //! advertised by the notification in that sync.
352 std::string token;
353
354 friend void to_json(nlohmann::json &obj, const QueryKeys &);
355 };
356
357 //! Claim onetime keys of devices of specific users.
358 struct ClaimKeys
359 {
360 //! The time (in milliseconds) to wait when downloading keys from remote servers.
361 uint64_t timeout = DEFAULT_DOWNLOAD_TIMEOUT;
362 //! The keys to be claimed. A map from user ID, to a map from device ID to algorithm name.
363 std::map<std::string, std::map<std::string, std::string>> one_time_keys;
364
365 friend void to_json(nlohmann::json &obj, const ClaimKeys &request);
366 };
367
368 //! Upload new signatures for a device or cross-signing key.
369 struct KeySignaturesUpload
370 {
371 //! map from user_id to either a map of device id to DeviceKey with new signatures or the
372 //! key id to CrossSigningKeys with new signatures
373 std::map<
374 std::string,
375 std::map<std::string, std::variant<mtx::crypto::DeviceKeys, mtx::crypto::CrossSigningKeys>>>
376 signatures;
377
378 friend void to_json(nlohmann::json &obj, const KeySignaturesUpload &req);
379 };
380
381 //! Upload cross signing keys
382 struct DeviceSigningUpload
383 {
384 //! Optional. The user's master key.
385 std::optional<mtx::crypto::CrossSigningKeys> master_key;
386 //! Optional. The user's self-signing key. Must be signed by the accompanying master key, or by
387 //! the user's most recently uploaded master key if no master key is included in the request.
388 std::optional<mtx::crypto::CrossSigningKeys> self_signing_key;
389 //! Optional. The user's user-signing key. Must be signed by the accompanying master key, or by
390 //! the user's most recently uploaded master key if no master key is included in the request.
391 std::optional<mtx::crypto::CrossSigningKeys> user_signing_key;
392
393 friend void to_json(nlohmann::json &obj, const DeviceSigningUpload &req);
394 };
395
396 //! Information about a pusher.
397 struct PusherData
398 {
399 //! Required if `kind` is http. The URL to use to send notifications to.
400 //! MUST be an HTTPS URL with a path of /_matrix/push/v1/notify.
401 std::string url;
402 //! The format to send notifications in to Push Gateways if the kind is http.
403 //! The details about what fields the homeserver should send to the push gateway are
404 //! defined in the Push Gateway Specification. Currently the only format available is
405 //! 'event_id_only'.
406 std::string format;
407 //! Push gateway specific data.
408 std::optional<nlohmann::json> default_payload;
409
410 friend void to_json(nlohmann::json &obj, const PusherData &data);
411 };
412
413 //! Request payload for the `POST /_matrix/client/r0/pushers/set` endpoint.
414 struct SetPusher
415 {
416 //! Required. Unique identifier for this pusher.
417 std::string pushkey;
418 //! Required. The kind of pusher to configure. "http" makes a pusher that sends HTTP pokes.
419 //! "email" makes a pusher that emails the user with unread notifications.
420 //! null deletes the pusher.
421 std::string kind;
422 //! Required. This is a reverse-DNS style identifier for the application.
423 //! If the `kind` is "email", this is "m.email".
424 std::string app_id;
425 //! Required. A string that will allow the user to identify what application owns this
426 //! pusher.
427 std::string app_display_name;
428 //! Required. A string that will allow the user to identify what device owns this pusher.
429 std::string device_display_name;
430 //! Determines which set of device specific rules this pusher executes.
431 std::string profile_tag;
432 //! Required. The preferred language for receiving notifications.
433 std::string lang;
434 //! Required. Data for the pusher implementation (for example, if `kind` is `http`, includes
435 //! the URL to push to).
436 PusherData data;
437 //! If true, add another pusher instead of updating an existing one.
438 bool append = false;
439
440 friend void to_json(nlohmann::json &obj, const SetPusher &req);
441 };
442 } // namespace requests
443 } // namespace mtx