Skip to content

Commit

Permalink
Support spaces summary API
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-famedly committed Feb 2, 2022
1 parent 9781553 commit dc55f64
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
28 changes: 27 additions & 1 deletion include/mtx/responses/public_rooms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>

#include "mtx/common.hpp"
#include "mtx/events/collections.hpp"

#if __has_include(<nlohmann/json_fwd.hpp>)
#include <nlohmann/json_fwd.hpp>
Expand Down Expand Up @@ -48,6 +49,18 @@ struct PublicRoomsChunk
bool guest_can_join;
//! The URL for the room's avatar, if one is set.
std::string avatar_url;

//! The room’s join rule. When not present, the room is assumed to be public.
mtx::events::state::JoinRule join_rule = mtx::events::state::JoinRule::Public;

//! Required: The type of room (from m.room.create), if any.
std::string room_type;

//! The m.space.child events of the space-room, represented as Stripped State Events with an
//! added origin_server_ts key.
//!
//! If the room is not a space-room, this should be empty.
std::vector<mtx::events::collections::StrippedEvents> children_state;
};

void
Expand Down Expand Up @@ -75,5 +88,18 @@ struct PublicRooms
void
from_json(const nlohmann::json &obj, PublicRooms &publicRooms);

//! Response from the `GET /_matrix/client/v1/rooms/{roomId}/hierarchy`
struct HierarchyRooms
{
//! Required: The rooms for the current page, with the current filters.
std::vector<PublicRoomsChunk> rooms;
//! A token to supply to from to keep paginating the responses. Not present when there are no
//! further results.
std::string next_batch;
};

void
from_json(const nlohmann::json &obj, HierarchyRooms &publicRooms);

} // namespace responses
} // namespace mtx
} // namespace mtx
10 changes: 10 additions & 0 deletions include/mtxclient/http/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct Notifications;
struct Profile;
struct PublicRoomVisibility;
struct PublicRooms;
struct HierarchyRooms;
struct QueryDevices;
struct QueryKeys;
struct Register;
Expand Down Expand Up @@ -595,6 +596,15 @@ class Client : public std::enable_shared_from_this<Client>
Callback<mtx::responses::PublicRooms> cb,
const std::string &server = "");

//! Paginates over the space tree in a depth-first manner to locate child rooms of a given
//! space.
void get_hierarchy(const std::string &room_id,
Callback<mtx::responses::HierarchyRooms> cb,
const std::string &from = "",
size_t limit = 0,
size_t max_depth = 0,
bool suggested_only = false);

//
// Group related endpoints.
//
Expand Down
30 changes: 30 additions & 0 deletions lib/http/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,36 @@ Client::get_public_rooms(Callback<mtx::responses::PublicRooms> cb,
RequestErr err) { cb(res, err); });
}

void
Client::get_hierarchy(const std::string &room_id,
Callback<mtx::responses::HierarchyRooms> cb,
const std::string &from,
size_t limit,
size_t max_depth,
bool suggested_only)
{
std::string api_path =
"/client/v1/rooms/" + mtx::client::utils::url_encode(room_id) + "/hierarchy";

std::map<std::string, std::string> params;
if (limit > 0)
params["limit"] = std::to_string(limit);
if (max_depth > 0)
params["max_depth"] = std::to_string(max_depth);
if (suggested_only)
params["suggested_only"] = "true";
if (!from.empty())
params["from"] = from;

if (!params.empty())
api_path += "?" + mtx::client::utils::query_params(params);

get<mtx::responses::HierarchyRooms>(
api_path,
[cb = std::move(cb)](
const mtx::responses::HierarchyRooms &res, HeaderFields, RequestErr err) { cb(res, err); });
}

//
// Group related endpoints.
//
Expand Down
20 changes: 19 additions & 1 deletion lib/structs/responses/public_rooms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "mtx/common.hpp"
#include "mtx/identifiers.hpp"
#include "mtx/responses/common.hpp"
#include "mtx/responses/public_rooms.hpp"

namespace mtx {
Expand Down Expand Up @@ -33,6 +34,13 @@ from_json(const nlohmann::json &obj, PublicRoomsChunk &res)
res.guest_can_join = obj.at("guest_can_join").get<bool>();

res.avatar_url = obj.value("avatar_url", std::string{});

res.join_rule = mtx::events::state::stringToJoinRule(obj.value("join_rule", "public"));

res.room_type = obj.value("room_type", std::string{});

if (obj.contains("children_state"))
mtx::responses::utils::parse_stripped_events(obj.at("children_state"), res.children_state);
}

void
Expand All @@ -55,5 +63,15 @@ from_json(const nlohmann::json &obj, PublicRooms &publicRooms)
: std::nullopt;
}

void
from_json(const nlohmann::json &obj, HierarchyRooms &publicRooms)
{
publicRooms.rooms = obj.at("rooms").get<std::vector<PublicRoomsChunk>>();

if (obj.count("next_batch")) {
publicRooms.next_batch = obj.at("next_batch").get<std::string>();
}
}

} // namespace responses
} // namespace mtx
} // namespace mtx

0 comments on commit dc55f64

Please sign in to comment.