0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-10-02 13:48:53 +02:00

modules/client/rooms: Reactivate the /messages endpoint (with zero param respect).

This commit is contained in:
Jason Volk 2018-02-10 19:56:01 -08:00
parent a3a3239e6e
commit ca0e488ea5

View file

@ -34,40 +34,54 @@ get_messages(client &client,
const resource::request &request,
const m::room::id &room_id)
{
const m::vm::query<m::vm::where::equal> query
const string_view &from
{
{ "room_id", room_id },
{ "state_key", nullptr },
request.query.at("from")
};
const size_t count
const string_view &to
{
std::min(m::vm::count(query), 128UL)
request.query["to"]
};
if(!count && !exists(room_id))
throw m::NOT_FOUND
{
"No messages."
};
size_t j(0);
std::vector<json::value> ret(count);
m::vm::for_each(query, [&count, &j, &ret]
(const auto &event)
const char &dir
{
if(j < count)
{
if(!defined(json::get<"state_key"_>(event)))
ret[j++] = event;
}
request.query.at("dir").at(0)
};
const uint16_t limit
{
request.query["limit"]? lex_cast<uint16_t>(request.query.at("limit")) : ushort(10)
};
const string_view &filter
{
request.query["filter"]
};
const m::room room
{
room_id
};
const m::room::messages messages
{
room
};
std::vector<json::value> ret;
ret.reserve(limit);
messages.test([&limit, &ret](const m::event &event)
{
ret.emplace_back(event);
return ret.size() >= limit;
});
return resource::response
{
client, json::members
{
{ "chunk", json::value { ret.data(), j } }
{ "chunk", json::value { ret.data(), ret.size() } }
}
};
}