Skip to content
Snippets Groups Projects
Verified Commit 58eab4a7 authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Add support for the room summary API

parent 4c9a2515
Branches
Tags
No related merge requests found
Pipeline #3455 passed
...@@ -53,7 +53,6 @@ rc_joins: ...@@ -53,7 +53,6 @@ rc_joins:
burst_count: 100000 burst_count: 100000
experimental_features: experimental_features:
msc2716_enabled: true
msc3266_enabled: true msc3266_enabled: true
HEREDOC HEREDOC
......
...@@ -56,12 +56,22 @@ struct PublicRoomsChunk ...@@ -56,12 +56,22 @@ struct PublicRoomsChunk
//! Required: The type of room (from m.room.create), if any. //! Required: The type of room (from m.room.create), if any.
std::string room_type; std::string room_type;
//! Optional (not all APIs return this): The version of this room.
std::string room_version;
//! Optional: If you are a member of this room. Can usually be assumed to be "leave".
events::state::Membership membership = events::state::Membership::Leave;
//! Optional: If the room is encrypted, the encryption algorithm.
std::string encryption;
//! The m.space.child events of the space-room, represented as Stripped State Events with an //! The m.space.child events of the space-room, represented as Stripped State Events with an
//! added origin_server_ts key. //! added origin_server_ts key.
//! //!
//! If the room is not a space-room, this should be empty. //! If the room is not a space-room, this should be empty.
std::vector<mtx::events::collections::StrippedEvents> children_state; std::vector<mtx::events::collections::StrippedEvents> children_state;
}; };
using PublicRoom = PublicRoomsChunk;
void void
from_json(const nlohmann::json &obj, PublicRoomsChunk &res); from_json(const nlohmann::json &obj, PublicRoomsChunk &res);
......
...@@ -69,6 +69,7 @@ struct Messages; ...@@ -69,6 +69,7 @@ struct Messages;
struct Notifications; struct Notifications;
struct Profile; struct Profile;
struct PublicRoomVisibility; struct PublicRoomVisibility;
struct PublicRoomsChunk;
struct PublicRooms; struct PublicRooms;
struct HierarchyRooms; struct HierarchyRooms;
struct QueryDevices; struct QueryDevices;
...@@ -635,6 +636,11 @@ public: ...@@ -635,6 +636,11 @@ public:
size_t max_depth = 0, size_t max_depth = 0,
bool suggested_only = false); bool suggested_only = false);
//! summarize a room
void get_summary(const std::string &room_id,
Callback<mtx::responses::PublicRoomsChunk> cb,
std::vector<std::string> vias = {});
// //
// Device related endpoints. // Device related endpoints.
// //
......
...@@ -1245,6 +1245,58 @@ Client::get_hierarchy(const std::string &room_id, ...@@ -1245,6 +1245,58 @@ Client::get_hierarchy(const std::string &room_id,
const mtx::responses::HierarchyRooms &res, HeaderFields, RequestErr err) { cb(res, err); }); const mtx::responses::HierarchyRooms &res, HeaderFields, RequestErr err) { cb(res, err); });
} }
void
Client::get_summary(const std::string &room_id,
Callback<mtx::responses::PublicRoomsChunk> cb,
std::vector<std::string> via)
{
std::string query;
if (!via.empty()) {
query = "?via=" + mtx::client::utils::url_encode(via[0]);
for (size_t i = 1; i < via.size(); i++) {
query += "&via=" + mtx::client::utils::url_encode(via[i]);
}
}
std::string api_path = "/client/unstable/im.nheko.summary/rooms/" +
mtx::client::utils::url_encode(room_id) + "/summary" + query;
get<mtx::responses::PublicRoomsChunk>(
api_path,
[this, room_id, cb = std::move(cb)](
const mtx::responses::PublicRoomsChunk &res, HeaderFields, RequestErr err) {
if (!err && !(err->status_code == 404 || err->status_code == 400))
cb(res, err);
else if (!room_id.empty() && room_id[0] == '#')
resolve_room_alias(
room_id, [this, cb](const mtx::responses::RoomId &room, RequestErr err) {
if (room.room_id.empty())
cb({}, err);
else
get_hierarchy(
room.room_id,
[cb](const mtx::responses::HierarchyRooms &res, RequestErr err) {
if (res.rooms.empty())
cb({}, err);
else
cb(res.rooms.front(), err);
},
"",
1);
});
else
get_hierarchy(
room_id,
[cb](const mtx::responses::HierarchyRooms &res, RequestErr err) {
if (res.rooms.empty())
cb({}, err);
else
cb(res.rooms.front(), err);
},
"",
1);
});
}
// //
// Device related endpoints // Device related endpoints
// //
......
...@@ -39,6 +39,14 @@ from_json(const nlohmann::json &obj, PublicRoomsChunk &res) ...@@ -39,6 +39,14 @@ from_json(const nlohmann::json &obj, PublicRoomsChunk &res)
res.room_type = obj.value("room_type", std::string{}); res.room_type = obj.value("room_type", std::string{});
res.room_version = obj.value("im.nheko.summary.room_version",
obj.value("im.nheko.summary.version", std::string{}));
res.membership = mtx::events::state::stringToMembership(
obj.value("membership", obj.value("im.nheko.summary.membership", "leave")));
res.encryption = obj.value("im.nheko.summary.encryption", std::string{});
if (obj.contains("children_state")) if (obj.contains("children_state"))
mtx::responses::utils::parse_stripped_events(obj.at("children_state"), res.children_state); mtx::responses::utils::parse_stripped_events(obj.at("children_state"), res.children_state);
} }
......
...@@ -1117,7 +1117,7 @@ TEST(ClientAPI, PresenceOverSync) ...@@ -1117,7 +1117,7 @@ TEST(ClientAPI, PresenceOverSync)
bob->login( bob->login(
"bob", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); "bob", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); });
while (alice->access_token().empty() && bob->access_token().empty()) while (alice->access_token().empty() || bob->access_token().empty())
sleep(); sleep();
std::atomic<bool> can_exit = false; std::atomic<bool> can_exit = false;
...@@ -1844,3 +1844,47 @@ TEST(ClientAPI, PublicRooms) ...@@ -1844,3 +1844,47 @@ TEST(ClientAPI, PublicRooms)
alice->close(); alice->close();
bob->close(); bob->close();
} }
TEST(ClientAPI, Summary)
{
// Setup : Create a new (public) room with some settings
auto alice = make_test_client();
auto bob = make_test_client();
alice->login("alice", "secret", [alice](const mtx::responses::Login &, RequestErr err) {
check_error(err);
});
bob->login(
"bob", "secret", [bob](const mtx::responses::Login &, RequestErr err) { check_error(err); });
while (alice->access_token().empty() || bob->access_token().empty())
sleep();
std::string room_name = "Public Room" + alice->generate_txn_id();
mtx::requests::CreateRoom req;
req.name = room_name;
req.topic = "Test";
req.visibility = mtx::common::RoomVisibility::Public;
req.invite = {"@bob:" + server_name()};
req.room_alias_name = alice->generate_txn_id();
req.preset = Preset::PublicChat;
alice->create_room(
req, [alice, bob, room_name](const mtx::responses::CreateRoom &res, RequestErr err) {
check_error(err);
auto room_id = res.room_id;
bob->get_summary(room_id.to_string(),
[room_name](const mtx::responses::PublicRoom &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.name, room_name);
EXPECT_NE(res.room_version, "1");
EXPECT_NE(res.room_version, "");
EXPECT_EQ(res.membership, mtx::events::state::Membership::Invite);
});
});
alice->close();
bob->close();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment