| 1 | #include "mtx/events/mscs/image_packs.hpp" |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | namespace { |
| 6 | template<class T> |
| 7 | T |
| 8 | safe_get(const nlohmann::json &obj, const std::string &name, T default_val = {}) |
| 9 | try { |
| 10 | return obj.value(name, default_val); |
| 11 | } catch (const nlohmann::json::type_error &) { |
| 12 | return default_val; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | namespace mtx { |
| 17 | namespace events { |
| 18 | namespace msc2545 { |
| 19 | void |
| 20 | from_json(const nlohmann::json &obj, PackImage &content) |
| 21 | { |
| 22 | content.url = obj.at("url").get<std::string>(); |
| 23 | content.body = safe_get<std::string>(obj, "body", ""); |
| 24 | if (obj.contains("info")) |
| 25 | content.info = obj.at("info").get<mtx::common::ImageInfo>(); |
| 26 | |
| 27 | if (obj.contains("usage")) { |
| 28 | for (const auto &e : obj.at("usage")) { |
| 29 | if (e == "sticker") |
| 30 | content.usage.set(PackUsage::Sticker); |
| 31 | else if (e == "emoticon") |
| 32 | content.usage.set(PackUsage::Emoji); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | from_json(const nlohmann::json &obj, ImagePack::PackDescription &content) |
| 39 | { |
| 40 | content.avatar_url = safe_get<std::string>(obj, "avatar_url", ""); |
| 41 | content.display_name = safe_get<std::string>(obj, "display_name", ""); |
| 42 | content.attribution = safe_get<std::string>(obj, "attribution", ""); |
| 43 | |
| 44 | if (obj.contains("usage") && obj.at("usage").is_array()) { |
| 45 | for (const auto &e : obj.at("usage")) { |
| 46 | if (e == "sticker") |
| 47 | content.usage.set(PackUsage::Sticker); |
| 48 | else if (e == "emoticon") |
| 49 | content.usage.set(PackUsage::Emoji); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | from_json(const nlohmann::json &obj, ImagePack &content) |
| 56 | { |
| 57 | if (obj.contains("pack")) { |
| 58 | content.pack = ImagePack::PackDescription{}; |
| 59 | from_json(obj.at("pack"), *content.pack); |
| 60 | } |
| 61 | if (obj.contains("images")) |
| 62 | content.images = obj.at("images").get<std::map<std::string, PackImage>>(); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | to_json(nlohmann::json &obj, const PackImage &content) |
| 67 | { |
| 68 | obj["url"] = content.url; |
| 69 | |
| 70 | if (!content.body.empty()) |
| 71 | obj["body"] = content.body; |
| 72 | if (content.info) |
| 73 | obj["info"] = content.info.value(); |
| 74 | |
| 75 | if (content.usage.test(PackUsage::Sticker)) |
| 76 | obj["usage"].push_back("sticker"); |
| 77 | if (content.usage.test(PackUsage::Emoji)) |
| 78 | obj["usage"].push_back("emoticon"); |
| 79 | } |
| 80 | void |
| 81 | to_json(nlohmann::json &obj, const ImagePack::PackDescription &content) |
| 82 | { |
| 83 | if (!content.avatar_url.empty()) |
| 84 | obj["avatar_url"] = content.avatar_url; |
| 85 | if (!content.display_name.empty()) |
| 86 | obj["display_name"] = content.display_name; |
| 87 | if (!content.attribution.empty()) |
| 88 | obj["attribution"] = content.attribution; |
| 89 | |
| 90 | if (content.usage.test(PackUsage::Sticker)) |
| 91 | obj["usage"].push_back("sticker"); |
| 92 | if (content.usage.test(PackUsage::Emoji)) |
| 93 | obj["usage"].push_back("emoticon"); |
| 94 | } |
| 95 | void |
| 96 | to_json(nlohmann::json &obj, const ImagePack &content) |
| 97 | { |
| 98 | if (content.pack) |
| 99 | to_json(obj["pack"], *content.pack); |
| 100 | |
| 101 | obj["images"] = content.images; |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | from_json(const nlohmann::json &obj, ImagePackRooms &content) |
| 106 | { |
| 107 | if (obj.contains("rooms")) { |
| 108 | for (const auto &[roomid, packs] : obj["rooms"].items()) { |
| 109 | for (const auto &[packid, body] : packs.items()) { |
| 110 | content.rooms[roomid][packid] = body.dump(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | void |
| 116 | to_json(nlohmann::json &obj, const ImagePackRooms &content) |
| 117 | { |
| 118 | obj["rooms"] = nlohmann::json::object(); |
| 119 | for (const auto &[roomid, packs] : content.rooms) { |
| 120 | for (const auto &[packid, body] : packs) { |
| 121 | if (body.empty()) |
| 122 | obj["rooms"][roomid][packid] = nlohmann::json::object(); |
| 123 | else |
| 124 | obj["rooms"][roomid][packid] = nlohmann::json::parse(body); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |