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

Add support for sending messages

parent 4258b3ec
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 42c2279914fd6806d74e8490e692a3b4c4a975c0
GIT_TAG baf455a6fee01be8235815c60e1231a8183ef1a3
BUILD_IN_SOURCE 1
SOURCE_DIR ${MATRIX_STRUCTS_ROOT}
......
......@@ -42,6 +42,8 @@ public:
void set_next_batch_token(const std::string &token) { next_batch_token_ = token; }
//! Retrieve the current next batch token.
std::string next_batch_token() const { return next_batch_token_; }
//! Generate a new transaction id.
std::string generate_txn_id() { return utils::random_token(32, false); }
using HeaderFields = std::experimental::optional<boost::beast::http::fields>;
using RequestErr = std::experimental::optional<mtx::client::errors::ClientError>;
......@@ -81,7 +83,6 @@ public:
//! Paginate through room messages.
/* void get_messages(); */
//! Send a message into a room.
/* void send_room_message(); */
//! Get the supported versions from the server.
void versions(std::function<void(const mtx::responses::Versions &res, RequestErr err)>);
......@@ -104,6 +105,19 @@ public:
//! Remove typing notifications from the room.
void stop_typing(const mtx::identifiers::Room &room_id,
std::function<void(RequestErr err)> cb);
//! Send a room message with auto-generated transaction id.
template<class Payload, mtx::events::EventType Event>
void send_room_message(
const mtx::identifiers::Room &room_id,
Payload payload,
std::function<void(const mtx::responses::EventId &, RequestErr)> callback);
//! Send a room message by providing transaction id.
template<class Payload, mtx::events::EventType Event>
void send_room_message(
const mtx::identifiers::Room &room_id,
const std::string &txn_id,
Payload payload,
std::function<void(const mtx::responses::EventId &, RequestErr)> callback);
/* void download_room_avatar(); */
/* void download_user_avatar(); */
/* void download_media(); */
......@@ -377,3 +391,27 @@ mtx::client::Client::create_session(
return session;
}
template<class Payload, mtx::events::EventType Event>
void
mtx::client::Client::send_room_message(
const mtx::identifiers::Room &room_id,
Payload payload,
std::function<void(const mtx::responses::EventId &, RequestErr)> callback)
{
send_room_message<Payload, Event>(room_id, generate_txn_id(), payload, callback);
}
template<class Payload, mtx::events::EventType Event>
void
mtx::client::Client::send_room_message(
const mtx::identifiers::Room &room_id,
const std::string &txn_id,
Payload payload,
std::function<void(const mtx::responses::EventId &, RequestErr)> callback)
{
const auto api_path = "/client/r0/rooms/" + room_id.toString() + "/send/" +
mtx::events::to_string(Event) + "/" + txn_id;
put<nlohmann::json>(api_path, payload, callback);
}
......@@ -8,6 +8,7 @@
#include "client.hpp"
#include "mtx/requests.hpp"
#include "mtx/responses.hpp"
#include "variant.hpp"
using namespace mtx::client;
using namespace mtx::identifiers;
......@@ -25,6 +26,17 @@ validate_login(const std::string &user, const mtx::responses::Login &res)
ASSERT_TRUE(res.device_id.size() > 5);
}
vector<string>
get_event_ids(const std::vector<mtx::events::collections::TimelineEvents> &events)
{
vector<string> ids;
for (const auto &e : events)
ids.push_back(mpark::visit([](auto msg) { return msg.event_id; }, e));
return ids;
}
TEST(ClientAPI, LoginSuccess)
{
std::shared_ptr<Client> mtx_client = std::make_shared<Client>("localhost");
......@@ -577,3 +589,86 @@ TEST(ClientAPI, Typing)
alice->close();
}
TEST(ClientAPI, SendMessages)
{
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);
});
while (alice->access_token().empty() && bob->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
mtx::requests::CreateRoom req;
req.invite = {"@bob:localhost"};
alice->create_room(req, [alice, bob](const mtx::responses::CreateRoom &res, ErrType err) {
ASSERT_FALSE(err);
auto room_id = res.room_id;
bob->join_room(
res.room_id, [alice, bob, room_id](const nlohmann::json &, ErrType err) {
ASSERT_FALSE(err);
// Flag to indicate when those messages would be ready to be read by
// alice.
vector<string> event_ids;
mtx::events::msg::Text text;
text.body = "hello alice!";
bob->send_room_message<mtx::events::msg::Text,
mtx::events::EventType::RoomMessage>(
room_id,
text,
[&event_ids](const mtx::responses::EventId &res, ErrType err) {
event_ids.push_back(res.event_id.toString());
ASSERT_FALSE(err);
});
mtx::events::msg::Emote emote;
emote.body = "*bob tests";
bob->send_room_message<mtx::events::msg::Emote,
mtx::events::EventType::RoomMessage>(
room_id,
emote,
[&event_ids](const mtx::responses::EventId &res, ErrType err) {
event_ids.push_back(res.event_id.toString());
ASSERT_FALSE(err);
});
while (event_ids.size() != 2)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
alice->sync(
"",
"",
false,
0,
[room_id, event_ids](const mtx::responses::Sync &res, ErrType err) {
ASSERT_FALSE(err);
auto ids = get_event_ids(
res.rooms.join.at(room_id.toString()).timeline.events);
// The sent event ids should be visible in the timeline.
for (const auto &event_id : event_ids)
ASSERT_TRUE(std::find(ids.begin(),
ids.end(),
event_id) != std::end(ids));
});
});
});
alice->close();
bob->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