Skip to content
Snippets Groups Projects
Commit 1eef8082 authored by ajberchek's avatar ajberchek Committed by mujx
Browse files

Adds functionality to invite a user to a room (#20)

fixes #1 
parent 4feb4d89
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ ExternalProject_Add(
MatrixStructs
GIT_REPOSITORY https://github.com/mujx/matrix-structs
GIT_TAG a84b37215ead77620dcddc0789c4b9b443757a17
GIT_TAG 0659554443a98bd018691337c8cfc11bfe909007
BUILD_IN_SOURCE 1
SOURCE_DIR ${MATRIX_STRUCTS_ROOT}
......
......@@ -306,6 +306,19 @@ Client::leave_room(const mtx::identifiers::Room &room_id,
post<std::string, nlohmann::json>(api_path, "", callback);
}
void
Client::invite_user(const mtx::identifiers::Room &room_id,
const std::string &user_id,
std::function<void(const mtx::responses::RoomInvite &, RequestErr)> callback)
{
mtx::requests::RoomInvite req;
req.user_id = user_id;
auto api_path = "/client/r0/rooms/" + room_id.toString() + "/invite";
post<mtx::requests::RoomInvite, mtx::responses::RoomInvite>(api_path, req, callback);
}
void
Client::sync(const std::string &filter,
const std::string &since,
......
......@@ -67,7 +67,10 @@ public:
void leave_room(const mtx::identifiers::Room &room_id,
std::function<void(const nlohmann::json &res, RequestErr err)>);
//! Invite a user to a room.
/* void invite_user(); */
void invite_user(
const mtx::identifiers::Room &room_id,
const std::string &user_id,
std::function<void(const mtx::responses::RoomInvite &res, RequestErr err)>);
//! Perform sync.
void sync(const std::string &filter,
const std::string &since,
......
......@@ -385,6 +385,92 @@ TEST(ClientAPI, LeaveRoom)
bob->close();
}
TEST(ClientAPI, InviteRoom)
{
auto alice = std::make_shared<Client>("localhost");
auto bob = std::make_shared<Client>("localhost");
alice->login("alice", "secret", [alice](const mtx::responses::Login &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
});
bob->login("bob", "secret", [bob](const mtx::responses::Login &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
});
// Waiting for the previous requests to complete.
std::this_thread::sleep_for(std::chrono::seconds(3));
mtx::requests::CreateRoom req;
req.name = "Name";
req.topic = "Topic";
req.invite = {};
alice->create_room(req, [alice, bob](const mtx::responses::CreateRoom &res, ErrType err) {
ASSERT_FALSE(err);
auto room_id = res.room_id;
alice->invite_user(
room_id,
"@bob:localhost",
[room_id, bob](const mtx::responses::Empty &, ErrType err) {
ASSERT_FALSE(err);
if (err) {
std::cout << "Received error when inviting user" << std::endl;
}
bob->join_room(room_id, [](const nlohmann::json &, ErrType err) {
ASSERT_FALSE(err);
});
});
});
alice->close();
bob->close();
}
TEST(ClientAPI, InvalidInvite)
{
auto alice = std::make_shared<Client>("localhost");
auto bob = std::make_shared<Client>("localhost");
alice->login("alice", "secret", [alice](const mtx::responses::Login &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
});
bob->login("bob", "secret", [bob](const mtx::responses::Login &res, ErrType err) {
boost::ignore_unused(res);
ASSERT_FALSE(err);
});
// Waiting for the previous requests to complete.
std::this_thread::sleep_for(std::chrono::seconds(3));
mtx::requests::CreateRoom req;
req.name = "Name";
req.topic = "Topic";
req.invite = {};
alice->create_room(req, [alice, bob](const mtx::responses::CreateRoom &res, ErrType err) {
ASSERT_FALSE(err);
auto room_id = res.room_id;
bob->invite_user(room_id,
"@carl:localhost",
[room_id, bob](const mtx::responses::Empty &, ErrType err) {
ASSERT_TRUE(err);
EXPECT_EQ(
mtx::errors::to_string(err->matrix_error.errcode),
"M_FORBIDDEN");
});
});
alice->close();
bob->close();
}
TEST(ClientAPI, Sync)
{
std::shared_ptr<Client> mtx_client = std::make_shared<Client>("localhost");
......
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