| 1 | #include <nlohmann/json.hpp> |
| 2 | |
| 3 | #include "mtx/common.hpp" |
| 4 | #include "mtx/responses/common.hpp" |
| 5 | #include "mtx/responses/public_rooms.hpp" |
| 6 | |
| 7 | namespace mtx { |
| 8 | namespace responses { |
| 9 | |
| 10 | void |
| 11 | from_json(const nlohmann::json &obj, PublicRoomVisibility &res) |
| 12 | { |
| 13 | res.visibility = mtx::common::stringToVisibility(obj.at("visibility").get<std::string>()); |
| 14 | } |
| 15 | |
| 16 | void |
| 17 | from_json(const nlohmann::json &obj, PublicRoomsChunk &res) |
| 18 | { |
| 19 | res.aliases = obj.value("aliases", std::vector<std::string>{}); |
| 20 | |
| 21 | res.canonical_alias = obj.value("canonical_alias", std::string{}); |
| 22 | |
| 23 | res.name = obj.value("name", std::string{}); |
| 24 | |
| 25 | res.num_joined_members = obj.at("num_joined_members").get<size_t>(); |
| 26 | |
| 27 | res.room_id = obj.at("room_id").get<std::string>(); |
| 28 | |
| 29 | res.topic = obj.value("topic", std::string{}); |
| 30 | |
| 31 | res.world_readable = obj.at("world_readable").get<bool>(); |
| 32 | |
| 33 | res.guest_can_join = obj.at("guest_can_join").get<bool>(); |
| 34 | |
| 35 | res.avatar_url = obj.value("avatar_url", std::string{}); |
| 36 | |
| 37 | res.join_rule = mtx::events::state::stringToJoinRule(obj.value("join_rule", "public")); |
| 38 | |
| 39 | res.room_type = obj.value("room_type", std::string{}); |
| 40 | |
| 41 | res.room_version = obj.value("im.nheko.summary.room_version", |
| 42 | obj.value("im.nheko.summary.version", std::string{})); |
| 43 | |
| 44 | res.membership = mtx::events::state::stringToMembership( |
| 45 | obj.value("membership", obj.value("im.nheko.summary.membership", "leave"))); |
| 46 | |
| 47 | res.encryption = obj.value("im.nheko.summary.encryption", std::string{}); |
| 48 | |
| 49 | if (obj.contains("children_state")) |
| 50 | mtx::responses::utils::parse_stripped_events(obj.at("children_state"), res.children_state); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | from_json(const nlohmann::json &obj, PublicRooms &publicRooms) |
| 55 | { |
| 56 | // PublicRoomsChunk is CopyConstructible & DefaultConstructible |
| 57 | publicRooms.chunk = obj.at("chunk").get<std::vector<PublicRoomsChunk>>(); |
| 58 | |
| 59 | if (obj.count("next_batch")) { |
| 60 | publicRooms.next_batch = obj.at("next_batch").get<std::string>(); |
| 61 | } |
| 62 | |
| 63 | if (obj.count("prev_batch")) { |
| 64 | publicRooms.prev_batch = obj.at("prev_batch").get<std::string>(); |
| 65 | } |
| 66 | |
| 67 | publicRooms.total_room_count_estimate = |
| 68 | obj.count("total_room_count_estimate") |
| 69 | ? std::optional<size_t>{obj.at("total_room_count_estimate").get<size_t>()} |
| 70 | : std::nullopt; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | from_json(const nlohmann::json &obj, HierarchyRooms &publicRooms) |
| 75 | { |
| 76 | publicRooms.rooms = obj.at("rooms").get<std::vector<PublicRoomsChunk>>(); |
| 77 | |
| 78 | if (obj.count("next_batch")) { |
| 79 | publicRooms.next_batch = obj.at("next_batch").get<std::string>(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } // namespace responses |
| 84 | } // namespace mtx |