Debian Codegraph @

mtxclient/lib/structs/requests.cpp

raw symbols SCIP
1 #include "mtx/requests.hpp"
2
3 #include <nlohmann/json.hpp>
4
5 using json = nlohmann::json;
6 using namespace mtx::events::collections;
7
8 namespace mtx {
9 namespace requests {
10
11 std::string
12 presetToString(Preset preset)
13 {
14 switch (preset) {
15 case Preset::PrivateChat:
16 return "private_chat";
17 break;
18 case Preset::PublicChat:
19 return "public_chat";
20 break;
21 case Preset::TrustedPrivateChat:
22 return "trusted_private_chat";
23 break;
24 }
25
26 return "private_chat";
27 }
28
29 void
30 to_json(json &obj, const CreateRoom &request)
31 {
32 if (!request.name.empty())
33 obj["name"] = request.name;
34
35 if (!request.topic.empty())
36 obj["topic"] = request.topic;
37
38 if (!request.room_alias_name.empty())
39 obj["room_alias_name"] = request.room_alias_name;
40
41 if (request.invite.size() != 0)
42 obj["invite"] = request.invite;
43
44 obj["is_direct"] = request.is_direct;
45 obj["preset"] = presetToString(request.preset);
46 obj["visibility"] = visibilityToString(request.visibility);
47
48 if (!request.room_version.empty())
49 obj["room_version"] = request.room_version;
50
51 if (request.creation_content)
52 obj["creation_content"] = *request.creation_content;
53
54 if (!request.initial_state.empty()) {
55 auto arr = nlohmann::json::array();
56 for (const auto &ev : request.initial_state) {
57 auto event_json = std::visit([](auto e) { return json(e); }, ev);
58 event_json.erase("sender");
59 arr.push_back(std::move(event_json));
60 }
61 obj["initial_state"] = std::move(arr);
62 }
63 }
64
65 void
66 to_json(json &obj, const Login &request)
67 {
68 if (!request.token.empty())
69 obj["token"] = request.token;
70
71 if (!request.password.empty())
72 obj["password"] = request.password;
73
74 if (!request.device_id.empty())
75 obj["device_id"] = request.device_id;
76
77 if (!request.initial_device_display_name.empty())
78 obj["initial_device_display_name"] = request.initial_device_display_name;
79
80 std::visit(
81 [&obj](const auto &id) {
82 if constexpr (std::is_same_v<const login_identifier::User &, decltype(id)>) {
83 obj["identifier"]["type"] = "m.id.user";
84 obj["identifier"]["user"] = id.user;
85 } else if constexpr (std::is_same_v<const login_identifier::Thirdparty &, decltype(id)>) {
86 obj["identifier"]["type"] = "m.id.thirdparty";
87 obj["identifier"]["medium"] = id.medium;
88 obj["identifier"]["address"] = id.address;
89 } else if constexpr (std::is_same_v<const login_identifier::PhoneNumber &,
90 decltype(id)>) {
91 obj["identifier"]["type"] = "m.id.phone";
92 obj["identifier"]["country"] = id.country;
93 obj["identifier"]["phone"] = id.phone;
94 }
95 },
96 request.identifier);
97
98 obj["type"] = request.type;
99 }
100
101 void
102 to_json(json &obj, const RequestEmailToken &request)
103 {
104 obj["client_secret"] = request.client_secret;
105 obj["email"] = request.email;
106 obj["send_attempt"] = request.send_attempt;
107 }
108
109 void
110 to_json(json &obj, const RequestMSISDNToken &request)
111 {
112 obj["client_secret"] = request.client_secret;
113 obj["country"] = request.country;
114 obj["phone_number"] = request.phone_number;
115 obj["send_attempt"] = request.send_attempt;
116 }
117
118 void
119 to_json(json &obj, const IdentitySubmitToken &request)
120 {
121 obj["sid"] = request.sid;
122 obj["client_secret"] = request.client_secret;
123 obj["token"] = request.token;
124 }
125
126 void
127 to_json(json &obj, const AvatarUrl &request)
128 {
129 obj["avatar_url"] = request.avatar_url;
130 }
131
132 void
133 to_json(json &obj, const DisplayName &request)
134 {
135 obj["displayname"] = request.displayname;
136 }
137
138 void
139 to_json(json &obj, const RoomMembershipChange &request)
140 {
141 obj["user_id"] = request.user_id;
142
143 if (!request.reason.empty())
144 obj["reason"] = request.reason;
145 }
146
147 void
148 to_json(json &obj, const TypingNotification &request)
149 {
150 obj["typing"] = request.typing;
151 obj["timeout"] = request.timeout;
152 }
153
154 void
155 to_json(json &obj, const PublicRoomVisibility &request)
156 {
157 obj["visibility"] = mtx::common::visibilityToString(request.visibility);
158 }
159
160 void
161 to_json(json &obj, const PublicRoomsFilter &request)
162 {
163 obj["generic_search_term"] = request.generic_search_term;
164 }
165
166 void
167 to_json(json &obj, const PublicRooms &request)
168 {
169 if (request.limit > 0) {
170 obj["limit"] = request.limit;
171 }
172
173 if (!request.since.empty()) {
174 obj["since"] = request.since;
175 }
176
177 if (!request.filter.generic_search_term.empty()) {
178 obj["filter"] = request.filter;
179 }
180
181 // Based on the spec, third_party_instance_id can only be used if
182 // include_all_networks is false. A case where the latter is true and
183 // the former is set is invalid.
184 if (request.include_all_networks && !request.third_party_instance_id.empty()) {
185 throw std::invalid_argument(
186 "third_party_instance_id can only be set if include_all_networks is false");
187 } else if (!request.third_party_instance_id.empty()) {
188 obj["third_party_instance_id"] = request.third_party_instance_id;
189 obj["include_all_networks"] = false;
190 } else {
191 obj["include_all_networks"] = true;
192 }
193 }
194
195 void
196 to_json(json &obj, const DeviceUpdate &request)
197 {
198 obj["display_name"] = request.display_name;
199 }
200
201 void
202 to_json(json &obj, const SignedOneTimeKey &request)
203 {
204 if (request.fallback)
205 obj["fallback"] = true;
206 obj["key"] = request.key;
207 obj["signatures"] = request.signatures;
208 }
209
210 void
211 to_json(json &obj, const UploadKeys &request)
212 {
213 obj = json::object();
214
215 if (!request.device_keys.user_id.empty())
216 obj["device_keys"] = request.device_keys;
217
218 for (const auto &[key_id, key] : request.one_time_keys) {
219 obj["one_time_keys"][key_id] = std::visit([](const auto &e) { return json(e); }, key);
220 }
221
222 for (const auto &[key_id, key] : request.fallback_keys) {
223 obj["fallback_keys"][key_id] = std::visit([](const auto &e) { return json(e); }, key);
224 }
225 }
226
227 void
228 to_json(json &obj, const ClaimKeys &request)
229 {
230 obj["timeout"] = request.timeout;
231 obj["one_time_keys"] = request.one_time_keys;
232 }
233
234 void
235 to_json(json &obj, const QueryKeys &request)
236 {
237 obj["timeout"] = request.timeout;
238 obj["device_keys"] = request.device_keys;
239 obj["token"] = request.token;
240 }
241
242 void
243 to_json(json &obj, const KeySignaturesUpload &req)
244 {
245 for (const auto &[user_id, idToKey] : req.signatures)
246 for (const auto &[key_id, keyVar] : idToKey)
247 obj[user_id][key_id] = std::visit([](const auto &e) { return json(e); }, keyVar);
248 }
249
250 void
251 to_json(json &obj, const DeviceSigningUpload &req)
252 {
253 if (req.master_key)
254 obj["master_key"] = req.master_key.value();
255 if (req.self_signing_key)
256 obj["self_signing_key"] = req.self_signing_key.value();
257 if (req.user_signing_key)
258 obj["user_signing_key"] = req.user_signing_key.value();
259 }
260
261 void
262 to_json(json &obj, const PusherData &data)
263 {
264 if (!data.url.empty()) {
265 obj["url"] = data.url;
266 }
267 if (!data.format.empty()) {
268 obj["format"] = data.format;
269 }
270 if (data.default_payload) {
271 obj["default_payload"] = data.default_payload.value();
272 }
273 }
274
275 void
276 to_json(json &obj, const SetPusher &req)
277 {
278 obj["pushkey"] = req.pushkey;
279 obj["kind"] = req.kind;
280 obj["app_id"] = req.app_id;
281 obj["app_display_name"] = req.app_display_name;
282 obj["device_display_name"] = req.device_display_name;
283 if (!req.profile_tag.empty()) {
284 obj["profile_tag"] = req.profile_tag;
285 }
286 obj["lang"] = req.lang;
287 obj["data"] = req.data;
288 obj["append"] = req.append;
289 }
290
291 } // namespace requests
292 } // namespace mtx