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