Skip to content
Snippets Groups Projects
Commit 7157d957 authored by Konstantinos Sideris's avatar Konstantinos Sideris
Browse files

Implement /join with an alias or room id

fixes #2
parent 2b47a737
No related branches found
No related tags found
No related merge requests found
......@@ -254,6 +254,15 @@ Client::join_room(const mtx::identifiers::Room &room_id,
post<std::string, nlohmann::json>(api_path, "", callback);
}
void
Client::join_room(const std::string &room,
std::function<void(const nlohmann::json &, RequestErr)> callback)
{
auto api_path = "/join/" + room;
post<std::string, nlohmann::json>(api_path, "", callback);
}
void
Client::leave_room(const mtx::identifiers::Room &room_id,
std::function<void(const nlohmann::json &, RequestErr)> callback)
......
......@@ -54,6 +54,9 @@ public:
//! Join a room by its room_id.
void join_room(const mtx::identifiers::Room &room_id,
std::function<void(const nlohmann::json &res, RequestErr err)>);
//! Join a room by an alias or a room_id.
void join_room(const std::string &room,
std::function<void(const nlohmann::json &res, RequestErr err)>);
//! Leave a room by its room_id.
void leave_room(const mtx::identifiers::Room &room_id,
std::function<void(const nlohmann::json &res, RequestErr err)>);
......
......@@ -4,12 +4,15 @@
#include <boost/random/uniform_int_distribution.hpp>
std::string
mtx::client::utils::random_token(uint8_t len)
mtx::client::utils::random_token(uint8_t len, bool with_symbols)
{
std::string chars("abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
"!@#$%^&*()");
std::string symbols = "!@#$%^&*()";
std::string alphanumberic("abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890");
const auto chars = with_symbols ? alphanumberic + symbols : alphanumberic;
boost::random::random_device rng;
boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
......
......@@ -9,7 +9,7 @@ namespace utils {
//! Generates a random string of the given size.
std::string
random_token(uint8_t len = 12);
random_token(uint8_t len = 12, bool with_symbols = true);
//! Construct query string from the given parameter pairs.
std::string
query_params(const std::map<std::string, std::string> &params);
......
......@@ -169,11 +169,16 @@ TEST(ClientAPI, JoinRoom)
// Waiting for the previous requests to complete.
std::this_thread::sleep_for(std::chrono::seconds(3));
// Creating a random room alias.
// TODO: add a type for room aliases.
const auto alias = utils::random_token(20, false);
mtx::requests::CreateRoom req;
req.name = "Name";
req.topic = "Topic";
req.invite = {"@bob:localhost"};
alice->create_room(req, [bob](const mtx::responses::CreateRoom &res, ErrType err) {
req.name = "Name";
req.topic = "Topic";
req.invite = {"@bob:localhost"};
req.room_alias_name = alias;
alice->create_room(req, [bob, alias](const mtx::responses::CreateRoom &res, ErrType err) {
ASSERT_FALSE(err);
auto room_id = res.room_id;
......@@ -188,6 +193,9 @@ TEST(ClientAPI, JoinRoom)
"M_UNRECOGNIZED");
});
// Join the room using an alias.
bob->join_room("#" + alias + ":localhost",
[](const nlohmann::json &, ErrType err) { ASSERT_FALSE(err); });
});
alice->close();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment