1 | #include <nlohmann/json.hpp> |
2 | #include <string> |
3 | |
4 | #include "mtx/events/messages/image.hpp" |
5 | |
6 | using json = nlohmann::json; |
7 | |
8 | namespace common = mtx::common; |
9 | |
10 | namespace mtx { |
11 | namespace events { |
12 | namespace msg { |
13 | |
14 | void |
15 | from_json(const json &obj, Image &content) |
16 | { |
17 | content.body = obj.value(key: "body" , default_value: "" ); |
18 | content.msgtype = obj.at(key: "msgtype" ).get<std::string>(); |
19 | |
20 | content.url = obj.value(key: "url" , default_value: "" ); |
21 | |
22 | if (obj.find(key: "info" ) != obj.end()) |
23 | content.info = obj.at(key: "info" ).get<common::ImageInfo>(); |
24 | |
25 | if (obj.find(key: "file" ) != obj.end()) |
26 | content.file = obj.at(key: "file" ).get<crypto::EncryptedFile>(); |
27 | |
28 | content.relations = common::parse_relations(obj); |
29 | } |
30 | |
31 | void |
32 | to_json(json &obj, const Image &content) |
33 | { |
34 | obj["msgtype" ] = "m.image" ; |
35 | obj["body" ] = content.body; |
36 | obj["info" ] = content.info; |
37 | |
38 | if (content.file) |
39 | obj["file" ] = content.file.value(); |
40 | else |
41 | obj["url" ] = content.url; |
42 | |
43 | common::apply_relations(obj, relations: content.relations); |
44 | } |
45 | |
46 | void |
47 | from_json(const json &obj, StickerImage &content) |
48 | { |
49 | content.body = obj.value(key: "body" , default_value: "" ); |
50 | |
51 | content.url = obj.value(key: "url" , default_value: "" ); |
52 | |
53 | if (obj.find(key: "info" ) != obj.end()) |
54 | content.info = obj.at(key: "info" ).get<common::ImageInfo>(); |
55 | |
56 | if (obj.find(key: "file" ) != obj.end()) |
57 | content.file = obj.at(key: "file" ).get<crypto::EncryptedFile>(); |
58 | |
59 | content.relations = common::parse_relations(obj); |
60 | } |
61 | |
62 | void |
63 | to_json(json &obj, const StickerImage &content) |
64 | { |
65 | obj["body" ] = content.body; |
66 | obj["info" ] = content.info; |
67 | |
68 | if (content.file) |
69 | obj["file" ] = content.file.value(); |
70 | else |
71 | obj["url" ] = content.url; |
72 | |
73 | common::apply_relations(obj, relations: content.relations); |
74 | } |
75 | |
76 | } // namespace msg |
77 | } // namespace events |
78 | } // namespace mtx |
79 | |