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

Implement /messages endpoint

fixes #7
parent 209c8ac6
No related branches found
No related tags found
No related merge requests found
......@@ -455,3 +455,33 @@ Client::stop_typing(const mtx::identifiers::Room &room_id, std::function<void(Re
put<mtx::requests::TypingNotification>(api_path, req, callback);
}
void
Client::messages(const mtx::identifiers::Room &room_id,
const std::string &from,
const std::string &to,
PaginationDirection dir,
uint16_t limit,
const std::string &filter,
std::function<void(const mtx::responses::Messages &res, RequestErr err)> callback)
{
std::map<std::string, std::string> params;
params.emplace("from", from);
params.emplace("dir", to_string(dir));
if (!to.empty())
params.emplace("to", to);
if (limit > 0)
params.emplace("limit", std::to_string(limit));
if (!filter.empty())
params.emplace("filter", filter);
const auto api_path =
"/client/r0/rooms/" + room_id.toString() + "/messages?" + utils::query_params(params);
get<mtx::responses::Messages>(
api_path, [callback](const mtx::responses::Messages &res, HeaderFields, RequestErr err) {
callback(res, err);
});
}
......@@ -20,6 +20,21 @@
namespace mtx {
namespace client {
enum class PaginationDirection
{
Backwards,
Forwards,
};
inline std::string
to_string(PaginationDirection dir)
{
if (dir == PaginationDirection::Backwards)
return "b";
return "f";
}
//! The main object that the user will interact.
class Client : public std::enable_shared_from_this<Client>
{
......@@ -89,9 +104,16 @@ public:
bool full_state,
uint16_t timeout,
std::function<void(const mtx::responses::Sync &res, RequestErr err)>);
//! Paginate through room messages.
/* void get_messages(); */
//! Send a message into a room.
void messages(const mtx::identifiers::Room &room_id,
const std::string &from,
const std::string &to,
PaginationDirection dir,
uint16_t limit,
const std::string &filter,
std::function<void(const mtx::responses::Messages &res, RequestErr err)>);
//! Get the supported versions from the server.
void versions(std::function<void(const mtx::responses::Versions &res, RequestErr err)>);
......@@ -140,13 +162,8 @@ public:
const mtx::identifiers::Room &room_id,
Payload payload,
std::function<void(const mtx::responses::EventId &, RequestErr)> callback);
/* void download_room_avatar(); */
/* void download_media(); */
/* void upload_filter(); */
/* void send_typing_notification(); */
/* void remove_typing_notification(); */
/* void read_event(); */
private:
......
......@@ -809,3 +809,52 @@ TEST(ClientAPI, SendStateEvents)
alice->close();
bob->close();
}
TEST(ClientAPI, Pagination)
{
auto alice = std::make_shared<Client>("localhost");
alice->login("alice", "secret", [alice](const mtx::responses::Login &, ErrType err) {
check_error(err);
});
while (alice->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
mtx::requests::CreateRoom req;
alice->create_room(req, [alice](const mtx::responses::CreateRoom &res, ErrType err) {
check_error(err);
auto room_id = res.room_id;
alice->messages(res.room_id,
"", // from
"", // to
PaginationDirection::Backwards,
30, // limit. Just enough messages so can fetch the whole history on
// this newly created room.
"", // filter
[room_id, alice](const mtx::responses::Messages &res, ErrType err) {
check_error(err);
ASSERT_TRUE(res.chunk.size() > 5);
ASSERT_NE(res.start, res.end);
alice->messages(
room_id,
res.end,
"",
PaginationDirection::Backwards,
30,
"",
[](const mtx::responses::Messages &res, ErrType err) {
check_error(err);
// We reached the start of the timeline.
EXPECT_EQ(res.start, res.end);
EXPECT_EQ(res.chunk.size(), 0);
});
});
});
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