| 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("body", ""); |
| 18 | content.msgtype = obj.at("msgtype").get<std::string>(); |
| 19 | |
| 20 | content.url = obj.value("url", ""); |
| 21 | |
| 22 | if (obj.find("info") != obj.end()) |
| 23 | content.info = obj.at("info").get<common::ImageInfo>(); |
| 24 | |
| 25 | if (obj.find("file") != obj.end()) |
| 26 | content.file = obj.at("file").get<crypto::EncryptedFile>(); |
| 27 | |
| 28 | content.relations = common::parse_relations(obj); |
| 29 | content.mentions = common::parse_mentions(obj); |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | to_json(json &obj, const Image &content) |
| 34 | { |
| 35 | obj["msgtype"] = "m.image"; |
| 36 | obj["body"] = content.body; |
| 37 | obj["info"] = content.info; |
| 38 | |
| 39 | if (content.file) |
| 40 | obj["file"] = content.file.value(); |
| 41 | else |
| 42 | obj["url"] = content.url; |
| 43 | |
| 44 | common::add_mentions(obj, content.mentions); |
| 45 | common::apply_relations(obj, content.relations); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | from_json(const json &obj, StickerImage &content) |
| 50 | { |
| 51 | content.body = obj.value("body", ""); |
| 52 | |
| 53 | content.url = obj.value("url", ""); |
| 54 | |
| 55 | if (obj.find("info") != obj.end()) |
| 56 | content.info = obj.at("info").get<common::ImageInfo>(); |
| 57 | |
| 58 | if (obj.find("file") != obj.end()) |
| 59 | content.file = obj.at("file").get<crypto::EncryptedFile>(); |
| 60 | |
| 61 | content.relations = common::parse_relations(obj); |
| 62 | content.mentions = common::parse_mentions(obj); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | to_json(json &obj, const StickerImage &content) |
| 67 | { |
| 68 | obj["body"] = content.body; |
| 69 | obj["info"] = content.info; |
| 70 | |
| 71 | if (content.file) |
| 72 | obj["file"] = content.file.value(); |
| 73 | else |
| 74 | obj["url"] = content.url; |
| 75 | |
| 76 | common::add_mentions(obj, content.mentions); |
| 77 | common::apply_relations(obj, content.relations); |
| 78 | } |
| 79 | |
| 80 | } // namespace msg |
| 81 | } // namespace events |
| 82 | } // namespace mtx |