Debian Codegraph @

mtxclient/lib/structs/events/common.cpp

raw symbols SCIP
1 #include <nlohmann/json.hpp>
2
3 #include "mtx/events/common.hpp"
4
5 using json = nlohmann::json;
6
7 namespace {
8 template<class T>
9 T
10 safe_get(const json &obj, const std::string &name, T default_val = {})
11 try {
12 return obj.value(name, default_val);
13 } catch (const nlohmann::json::type_error &) {
14 return default_val;
15 }
16 }
17
18 namespace mtx {
19 namespace common {
20
21 void
22 from_json(const json &obj, ThumbnailInfo &info)
23 {
24 info.h = safe_get<uint64_t>(obj, "h");
25 info.w = safe_get<uint64_t>(obj, "w");
26 info.size = safe_get<uint64_t>(obj, "size");
27
28 if (obj.find("mimetype") != obj.end())
29 info.mimetype = obj.at("mimetype").get<std::string>();
30 }
31
32 void
33 to_json(json &obj, const ThumbnailInfo &info)
34 {
35 obj["h"] = info.h;
36 obj["w"] = info.w;
37 obj["size"] = info.size;
38 obj["mimetype"] = info.mimetype;
39 }
40
41 void
42 from_json(const json &obj, ImageInfo &info)
43 {
44 info.h = safe_get<uint64_t>(obj, "h");
45 info.w = safe_get<uint64_t>(obj, "w");
46 info.size = safe_get<uint64_t>(obj, "size");
47
48 if (obj.find("mimetype") != obj.end())
49 info.mimetype = obj.at("mimetype").get<std::string>();
50
51 if (obj.find("thumbnail_url") != obj.end())
52 info.thumbnail_url = obj.at("thumbnail_url").get<std::string>();
53
54 if (obj.find("thumbnail_info") != obj.end())
55 info.thumbnail_info = obj.at("thumbnail_info").get<ThumbnailInfo>();
56
57 if (obj.find("thumbnail_file") != obj.end())
58 info.thumbnail_file = obj.at("thumbnail_file").get<crypto::EncryptedFile>();
59
60 if (obj.find("xyz.amorgan.blurhash") != obj.end())
61 info.blurhash = obj.at("xyz.amorgan.blurhash").get<std::string>();
62 }
63
64 void
65 to_json(json &obj, const ImageInfo &info)
66 {
67 obj["h"] = info.h;
68 obj["w"] = info.w;
69 obj["size"] = info.size;
70 obj["mimetype"] = info.mimetype;
71 if (!info.thumbnail_url.empty()) {
72 obj["thumbnail_url"] = info.thumbnail_url;
73 obj["thumbnail_info"] = info.thumbnail_info;
74 }
75 if (info.thumbnail_file)
76 obj["thumbnail_file"] = info.thumbnail_file.value();
77 if (!info.blurhash.empty())
78 obj["xyz.amorgan.blurhash"] = info.blurhash;
79 }
80
81 void
82 from_json(const json &obj, FileInfo &info)
83 {
84 info.size = safe_get<uint64_t>(obj, "size");
85
86 if (obj.find("mimetype") != obj.end())
87 info.mimetype = obj.at("mimetype").get<std::string>();
88
89 if (obj.find("thumbnail_url") != obj.end())
90 info.thumbnail_url = obj.at("thumbnail_url").get<std::string>();
91
92 if (obj.find("thumbnail_info") != obj.end())
93 info.thumbnail_info = obj.at("thumbnail_info").get<ThumbnailInfo>();
94
95 if (obj.find("thumbnail_file") != obj.end())
96 info.thumbnail_file = obj.at("thumbnail_file").get<crypto::EncryptedFile>();
97 }
98
99 void
100 to_json(json &obj, const FileInfo &info)
101 {
102 obj["size"] = info.size;
103 obj["mimetype"] = info.mimetype;
104 if (!info.thumbnail_url.empty()) {
105 obj["thumbnail_url"] = info.thumbnail_url;
106 obj["thumbnail_info"] = info.thumbnail_info;
107 }
108 if (info.thumbnail_file)
109 obj["thumbnail_file"] = info.thumbnail_file.value();
110 }
111
112 void
113 from_json(const json &obj, AudioInfo &info)
114 {
115 info.duration = safe_get<uint64_t>(obj, "duration");
116 info.size = safe_get<uint64_t>(obj, "size");
117
118 if (obj.find("mimetype") != obj.end())
119 info.mimetype = obj.at("mimetype").get<std::string>();
120 }
121
122 void
123 to_json(json &obj, const AudioInfo &info)
124 {
125 obj["size"] = info.size;
126 obj["duration"] = info.duration;
127 obj["mimetype"] = info.mimetype;
128 }
129
130 void
131 from_json(const json &obj, VideoInfo &info)
132 {
133 info.h = safe_get<uint64_t>(obj, "h");
134 info.w = safe_get<uint64_t>(obj, "w");
135 info.size = safe_get<uint64_t>(obj, "size");
136 info.duration = safe_get<uint64_t>(obj, "duration");
137
138 if (obj.find("mimetype") != obj.end())
139 info.mimetype = obj.at("mimetype").get<std::string>();
140
141 if (obj.find("thumbnail_url") != obj.end())
142 info.thumbnail_url = obj.at("thumbnail_url").get<std::string>();
143
144 if (obj.find("thumbnail_info") != obj.end())
145 info.thumbnail_info = obj.at("thumbnail_info").get<ThumbnailInfo>();
146
147 if (obj.find("thumbnail_file") != obj.end())
148 info.thumbnail_file = obj.at("thumbnail_file").get<crypto::EncryptedFile>();
149
150 if (obj.find("xyz.amorgan.blurhash") != obj.end())
151 info.blurhash = obj.at("xyz.amorgan.blurhash").get<std::string>();
152 }
153
154 void
155 to_json(json &obj, const VideoInfo &info)
156 {
157 obj["size"] = info.size;
158 obj["h"] = info.h;
159 obj["w"] = info.w;
160 obj["duration"] = info.duration;
161 obj["mimetype"] = info.mimetype;
162 if (!info.thumbnail_url.empty()) {
163 obj["thumbnail_url"] = info.thumbnail_url;
164 obj["thumbnail_info"] = info.thumbnail_info;
165 }
166 if (info.thumbnail_file)
167 obj["thumbnail_file"] = info.thumbnail_file.value();
168 if (!info.blurhash.empty())
169 obj["xyz.amorgan.blurhash"] = info.blurhash;
170 }
171
172 void
173 from_json(const json &obj, LocationInfo &info)
174 {
175 if (obj.contains("thumbnail_url"))
176 info.thumbnail_url = obj.at("thumbnail_url").get<std::string>();
177
178 if (obj.contains("thumbnail_info"))
179 info.thumbnail_info = obj.at("thumbnail_info").get<ThumbnailInfo>();
180
181 if (obj.contains("thumbnail_file"))
182 info.thumbnail_file = obj.at("thumbnail_file").get<crypto::EncryptedFile>();
183 }
184
185 void
186 to_json(json &obj, const LocationInfo &info)
187 {
188 if (!info.thumbnail_url.empty()) {
189 obj["thumbnail_url"] = info.thumbnail_url;
190 obj["thumbnail_info"] = info.thumbnail_info;
191 }
192 if (info.thumbnail_file) {
193 obj["thumbnail_file"] = info.thumbnail_file.value();
194 obj["thumbnail_info"] = info.thumbnail_info;
195 }
196 }
197
198 void
199 from_json(const json &obj, Mentions &info)
200 {
201 info.room = obj.value("room", false);
202 info.user_ids = obj.value("user_ids", std::vector<std::string>{});
203 }
204
205 void
206 to_json(json &obj, const Mentions &info)
207 {
208 obj = json::object();
209
210 if (info.room)
211 obj["room"] = true;
212
213 if (!info.user_ids.empty())
214 obj["user_ids"] = info.user_ids;
215 }
216
217 void
218 to_json(json &obj, const RelationType &type)
219 {
220 switch (type) {
221 case RelationType::Annotation:
222 obj = "m.annotation";
223 break;
224 case RelationType::Reference:
225 obj = "m.reference";
226 break;
227 case RelationType::Replace:
228 obj = "m.replace";
229 break;
230 case RelationType::InReplyTo:
231 obj = "im.nheko.relations.v1.in_reply_to";
232 break;
233 case RelationType::Thread:
234 obj = "m.thread";
235 break;
236 case RelationType::Unsupported:
237 default:
238 obj = "unsupported";
239 break;
240 }
241 }
242
243 void
244 from_json(const json &obj, RelationType &type)
245 {
246 if (obj.get<std::string>() == "m.annotation")
247 type = RelationType::Annotation;
248 else if (obj.get<std::string>() == "m.reference")
249 type = RelationType::Reference;
250 else if (obj.get<std::string>() == "m.replace")
251 type = RelationType::Replace;
252 else if (obj.get<std::string>() == "im.nheko.relations.v1.in_reply_to" ||
253 obj.get<std::string>() == "m.in_reply_to")
254 type = RelationType::InReplyTo;
255 else if (obj.get<std::string>() == "m.thread")
256 type = RelationType::Thread;
257 else
258 type = RelationType::Unsupported;
259 }
260
261 std::optional<Mentions>
262 parse_mentions(const nlohmann::json &content)
263 {
264 if (auto mentions = content.find("m.mentions"); mentions != content.end())
265 return mentions->get<Mentions>();
266
267 return std::nullopt;
268 }
269
270 void
271 add_mentions(nlohmann::json &content, const std::optional<Mentions> &m)
272 {
273 if (m)
274 content["m.mentions"] = *m;
275 }
276
277 Relations
278 parse_relations(const nlohmann::json &content)
279 {
280 try {
281 if (content.contains("im.nheko.relations.v1.relations")) {
282 Relations rels;
283 rels.relations = content.at("im.nheko.relations.v1.relations")
284 .get<std::vector<mtx::common::Relation>>();
285 rels.synthesized = false;
286 return rels;
287 } else if (content.contains("m.relates_to")) {
288 const auto &relates_to = content.at("m.relates_to");
289 if (relates_to.contains("m.in_reply_to")) {
290 Relation r;
291 r.event_id = relates_to.at("m.in_reply_to").at("event_id").get<std::string>();
292 r.rel_type = RelationType::InReplyTo;
293
294 Relations rels;
295 if (auto thread_type = relates_to.find("rel_type");
296 thread_type != relates_to.end() && *thread_type == "m.thread") {
297 if (auto thread_id = relates_to.find("event_id");
298 thread_id != relates_to.end()) {
299 r.is_fallback = relates_to.value("is_falling_back", false);
300 rels.relations.push_back(relates_to.get<mtx::common::Relation>());
301 }
302 }
303
304 rels.relations.push_back(r);
305 rels.synthesized = true;
306 return rels;
307 } else {
308 Relation r = relates_to.get<mtx::common::Relation>();
309 Relations rels;
310 rels.relations.push_back(r);
311 rels.synthesized = true;
312
313 if (r.rel_type == RelationType::Replace && content.contains("m.new_content") &&
314 content.at("m.new_content").contains("m.relates_to")) {
315 const auto secondRel = content["m.new_content"]["m.relates_to"];
316 if (secondRel.contains("m.in_reply_to")) {
317 Relation r2{};
318 r.rel_type = RelationType::InReplyTo;
319 r.event_id =
320 secondRel.at("m.in_reply_to").at("event_id").get<std::string>();
321 rels.relations.push_back(r2);
322 } else {
323 rels.relations.push_back(secondRel.get<Relation>());
324 }
325 }
326
327 return rels;
328 }
329 }
330 } catch (nlohmann::json &e) {
331 }
332 return {};
333 }
334
335 void
336 add_relations(nlohmann::json &content, const Relations &relations)
337 {
338 if (relations.relations.empty())
339 return;
340
341 std::optional<Relation> edit, not_edit, reply;
342 for (const auto &r : relations.relations) {
343 if (r.rel_type == RelationType::Replace)
344 edit = r;
345 else if (r.rel_type == RelationType::InReplyTo)
346 reply = r;
347 else
348 not_edit = r;
349 }
350
351 if (not_edit) {
352 content["m.relates_to"] = *not_edit;
353 }
354
355 if (reply) {
356 content["m.relates_to"]["m.in_reply_to"]["event_id"] = reply->event_id;
357 if (reply->is_fallback && not_edit && not_edit->rel_type == RelationType::Thread)
358 content["m.relates_to"]["is_falling_back"] = true;
359 }
360
361 if (edit) {
362 if (not_edit)
363 content["m.new_content"]["m.relates_to"] = content["m.relates_to"];
364 content["m.relates_to"] = *edit;
365 }
366
367 if (!relations.synthesized) {
368 for (const auto &r : relations.relations) {
369 if (r.rel_type != RelationType::Unsupported)
370 content["im.nheko.relations.v1.relations"].push_back(r);
371 }
372 }
373 }
374 void
375 apply_relations(nlohmann::json &content, const Relations &relations)
376 {
377 add_relations(content, relations);
378
379 if (relations.replaces()) {
380 for (const auto &e : content.items()) {
381 if (e.key() != "m.relates_to" && e.key() != "im.nheko.relations.v1.relations" &&
382 e.key() != "m.new_content") {
383 content["m.new_content"][e.key()] = e.value();
384 }
385 }
386
387 if (content.contains("body")) {
388 content["body"] = "* " + content["body"].get<std::string>();
389 }
390 if (content.contains("formatted_body")) {
391 content["formatted_body"] = "* " + content["formatted_body"].get<std::string>();
392 }
393 }
394 }
395
396 void
397 from_json(const json &obj, Relation &relates_to)
398 {
399 if (auto it = obj.find("rel_type"); it != obj.end())
400 relates_to.rel_type = it->get<RelationType>();
401 if (auto it = obj.find("event_id"); it != obj.end())
402 relates_to.event_id = it->get<std::string>();
403 if (auto it = obj.find("key"); it != obj.end())
404 relates_to.key = it->get<std::string>();
405 if (auto it = obj.find("im.nheko.relations.v1.is_fallback"); it != obj.end())
406 relates_to.is_fallback = it->get<bool>();
407 }
408
409 void
410 to_json(json &obj, const Relation &relates_to)
411 {
412 obj["rel_type"] = relates_to.rel_type;
413 obj["event_id"] = relates_to.event_id;
414 if (relates_to.key.has_value())
415 obj["key"] = relates_to.key.value();
416 if (relates_to.is_fallback)
417 obj["im.nheko.relations.v1.is_fallback"] = true;
418 }
419
420 static inline std::optional<std::string>
421 return_first_relation_matching(RelationType t, const Relations &rels, bool include_fallback)
422 {
423 for (const auto &r : rels.relations)
424 if (r.rel_type == t && (include_fallback || r.is_fallback == false))
425 return r.event_id;
426 return std::nullopt;
427 }
428 std::optional<std::string>
429 Relations::reply_to(bool include_fallback) const
430 {
431 return return_first_relation_matching(RelationType::InReplyTo, *this, include_fallback);
432 }
433 std::optional<std::string>
434 Relations::replaces(bool include_fallback) const
435 {
436 return return_first_relation_matching(RelationType::Replace, *this, include_fallback);
437 }
438 std::optional<std::string>
439 Relations::references(bool include_fallback) const
440 {
441 return return_first_relation_matching(RelationType::Reference, *this, include_fallback);
442 }
443 std::optional<std::string>
444 Relations::thread(bool include_fallback) const
445 {
446 return return_first_relation_matching(RelationType::Thread, *this, include_fallback);
447 }
448 std::optional<Relation>
449 Relations::annotates(bool include_fallback) const
450 {
451 for (const auto &r : relations)
452 if (r.rel_type == RelationType::Annotation && (include_fallback || r.is_fallback == false))
453 return r;
454 return std::nullopt;
455 }
456 } // namespace common
457 } // namespace mtx