| 1 | #include <nlohmann/json.hpp> |
| 2 | #include <string> |
| 3 | |
| 4 | #include "mtx/events/common.hpp" |
| 5 | #include "mtx/events/messages/text.hpp" |
| 6 | |
| 7 | using json = nlohmann::json; |
| 8 | |
| 9 | namespace mtx { |
| 10 | namespace events { |
| 11 | namespace msg { |
| 12 | |
| 13 | void |
| 14 | from_json(const json &obj, Text &content) |
| 15 | { |
| 16 | content.body = obj.at("body").get<std::string>(); |
| 17 | content.msgtype = obj.at("msgtype").get<std::string>(); |
| 18 | |
| 19 | if (obj.count("format") != 0) |
| 20 | content.format = obj.at("format").get<std::string>(); |
| 21 | |
| 22 | if (obj.count("formatted_body") != 0) |
| 23 | content.formatted_body = obj.at("formatted_body").get<std::string>(); |
| 24 | |
| 25 | content.relations = common::parse_relations(obj); |
| 26 | content.mentions = common::parse_mentions(obj); |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | to_json(json &obj, const Text &content) |
| 31 | { |
| 32 | obj["msgtype"] = "m.text"; |
| 33 | obj["body"] = content.body; |
| 34 | |
| 35 | if (!content.formatted_body.empty()) { |
| 36 | obj["format"] = mtx::common::FORMAT_MSG_TYPE; |
| 37 | obj["formatted_body"] = content.formatted_body; |
| 38 | } |
| 39 | |
| 40 | common::add_mentions(obj, content.mentions); |
| 41 | common::apply_relations(obj, content.relations); |
| 42 | } |
| 43 | |
| 44 | } // namespace msg |
| 45 | } // namespace events |
| 46 | } // namespace mtx |