2018-02-14 21:23:20 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#include "rooms.h"
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-24 07:08:09 +01:00
|
|
|
struct pagination_tokens
|
|
|
|
{
|
|
|
|
uint8_t limit;
|
|
|
|
char dir;
|
|
|
|
m::event::id::buf from;
|
|
|
|
m::event::id::buf to;
|
|
|
|
|
|
|
|
pagination_tokens(const resource::request &);
|
|
|
|
};
|
|
|
|
|
2018-05-13 10:24:23 +02:00
|
|
|
conf::item<size_t>
|
|
|
|
max_filter_miss
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.messages.max_filter_miss" },
|
|
|
|
{ "default", 2048L },
|
|
|
|
};
|
|
|
|
|
2018-08-19 07:05:32 +02:00
|
|
|
static const m::event::fetch::opts
|
|
|
|
default_fetch_opts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content",
|
|
|
|
"depth",
|
|
|
|
"event_id",
|
|
|
|
"membership",
|
|
|
|
"origin_server_ts",
|
|
|
|
"prev_events",
|
|
|
|
"redacts",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"state_key",
|
|
|
|
"type",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
resource::response
|
|
|
|
get__messages(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2018-02-24 07:08:09 +01:00
|
|
|
const pagination_tokens page
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-02-24 07:08:09 +01:00
|
|
|
request
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2018-05-12 12:19:50 +02:00
|
|
|
const auto &filter_query
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
|
|
|
request.query["filter"]
|
|
|
|
};
|
|
|
|
|
2018-05-12 12:19:50 +02:00
|
|
|
const unique_buffer<mutable_buffer> filter_buf
|
|
|
|
{
|
2018-05-13 10:24:23 +02:00
|
|
|
size(filter_query)
|
2018-05-12 12:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &filter_json
|
|
|
|
{
|
|
|
|
url::decode(filter_query, filter_buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room_event_filter filter
|
|
|
|
{
|
|
|
|
filter_json.has("filter_json")?
|
|
|
|
json::object{filter_json.get("filter_json")}:
|
|
|
|
filter_json
|
|
|
|
};
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
const m::room room
|
|
|
|
{
|
2018-05-31 14:45:29 +02:00
|
|
|
room_id, page.from
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2018-05-31 14:45:29 +02:00
|
|
|
if(!room.visible(request.user_id))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted to view the room at this event"
|
|
|
|
};
|
|
|
|
|
2018-02-24 07:08:09 +01:00
|
|
|
m::room::messages it
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-08-19 07:05:32 +02:00
|
|
|
room, page.from, &default_fetch_opts
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2018-04-30 19:40:52 +02:00
|
|
|
resource::response::chunked response
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-04-30 19:40:52 +02:00
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
|
2018-09-05 07:48:23 +02:00
|
|
|
json::stack out
|
2018-04-30 19:40:52 +02:00
|
|
|
{
|
2018-09-05 07:48:23 +02:00
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
2018-04-30 19:40:52 +02:00
|
|
|
|
|
|
|
json::stack::object ret
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
2018-04-05 03:34:52 +02:00
|
|
|
|
2018-04-30 19:40:52 +02:00
|
|
|
// Spec sez the 'from' token is exclusive
|
|
|
|
if(it && page.dir == 'b')
|
|
|
|
--it;
|
|
|
|
else if(it)
|
|
|
|
++it;
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2018-05-13 10:24:23 +02:00
|
|
|
size_t hit{0}, miss{0};
|
2018-04-30 19:40:52 +02:00
|
|
|
m::event::id::buf start, end;
|
|
|
|
{
|
|
|
|
json::stack::member chunk{ret, "chunk"};
|
|
|
|
json::stack::array messages{chunk};
|
|
|
|
for(; it; page.dir == 'b'? --it : ++it)
|
2018-04-05 03:34:52 +02:00
|
|
|
{
|
2018-04-30 19:40:52 +02:00
|
|
|
const m::event &event{*it};
|
2018-05-31 14:45:29 +02:00
|
|
|
if(!visible(event, request.user_id))
|
|
|
|
break;
|
|
|
|
|
2018-04-30 19:40:52 +02:00
|
|
|
if(page.to && at<"event_id"_>(event) == page.to)
|
2018-04-05 03:34:52 +02:00
|
|
|
{
|
2018-04-30 19:40:52 +02:00
|
|
|
if(page.dir != 'b')
|
|
|
|
start = at<"event_id"_>(event);
|
|
|
|
|
|
|
|
break;
|
2018-04-05 03:34:52 +02:00
|
|
|
}
|
2018-02-24 07:08:09 +01:00
|
|
|
|
2018-05-12 12:19:50 +02:00
|
|
|
if(empty(filter_json) || match(filter, event))
|
|
|
|
{
|
|
|
|
messages.append(event);
|
2018-05-13 10:24:23 +02:00
|
|
|
++hit;
|
2018-05-12 12:19:50 +02:00
|
|
|
}
|
2018-05-13 10:24:23 +02:00
|
|
|
else ++miss;
|
2018-05-12 12:19:50 +02:00
|
|
|
|
2018-05-13 10:24:23 +02:00
|
|
|
if(hit >= page.limit || miss >= size_t(max_filter_miss))
|
2018-04-30 19:40:52 +02:00
|
|
|
{
|
|
|
|
if(page.dir == 'b')
|
|
|
|
end = at<"event_id"_>(event);
|
|
|
|
else
|
|
|
|
start = at<"event_id"_>(event);
|
2018-02-24 07:08:09 +01:00
|
|
|
|
2018-04-30 19:40:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-24 07:08:09 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 19:40:52 +02:00
|
|
|
json::stack::member
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-04-30 19:40:52 +02:00
|
|
|
ret, "start", json::value{start}
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
2018-04-30 19:40:52 +02:00
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
ret, "end", json::value{end}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {};
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|
2018-02-24 07:08:09 +01:00
|
|
|
|
|
|
|
// Client-Server 6.3.6 query parameters
|
|
|
|
pagination_tokens::pagination_tokens(const resource::request &request)
|
|
|
|
try
|
|
|
|
:limit
|
|
|
|
{
|
|
|
|
// The maximum number of events to return. Default: 10.
|
|
|
|
// > we limit this to 255 via narrowing cast
|
|
|
|
request.query["limit"]?
|
|
|
|
uint8_t(lex_cast<ushort>(request.query.at("limit"))):
|
|
|
|
uint8_t(10)
|
|
|
|
}
|
|
|
|
,dir
|
|
|
|
{
|
|
|
|
// Required. The direction to return events from. One of: ["b", "f"]
|
|
|
|
request.query.at("dir").at(0)
|
|
|
|
}
|
|
|
|
,from
|
|
|
|
{
|
|
|
|
// Required. The token to start returning events from. This token can be
|
|
|
|
// obtained from a prev_batch token returned for each room by the sync
|
|
|
|
// API, or from a start or end token returned by a previous request to
|
|
|
|
// this endpoint.
|
|
|
|
url::decode(request.query.at("from"), from)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// The token to stop returning events at. This token can be obtained from
|
|
|
|
// a prev_batch token returned for each room by the sync endpoint, or from
|
|
|
|
// a start or end token returned by a previous request to this endpoint.
|
|
|
|
if(!empty(request.query["to"]))
|
|
|
|
url::decode(request.query.at("to"), to);
|
|
|
|
|
|
|
|
if(dir != 'b' && dir != 'f')
|
|
|
|
throw m::BAD_PAGINATION
|
|
|
|
{
|
|
|
|
"query parameter 'dir' must be 'b' or 'f'"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &)
|
|
|
|
{
|
|
|
|
throw m::BAD_PAGINATION
|
|
|
|
{
|
|
|
|
"query parameter 'limit' is invalid"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const m::INVALID_MXID &)
|
|
|
|
{
|
|
|
|
throw m::BAD_PAGINATION
|
|
|
|
{
|
|
|
|
"query parameter 'from' or 'to' is not a valid token"
|
|
|
|
};
|
|
|
|
}
|