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 &);
|
|
|
|
};
|
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
static bool
|
2019-01-11 01:56:32 +01:00
|
|
|
_append(json::stack::array &chunk,
|
|
|
|
const m::event &,
|
2019-03-10 04:13:42 +01:00
|
|
|
const m::event::idx &,
|
2019-04-15 21:16:48 +02:00
|
|
|
const m::user::room &user_room,
|
|
|
|
const int64_t &room_depth);
|
2019-01-11 01:56:32 +01:00
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2019-09-18 00:47:20 +02:00
|
|
|
conf::item<size_t>
|
|
|
|
prefetch_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.messages.prefetch.max" },
|
|
|
|
{ "default", 32L },
|
|
|
|
};
|
|
|
|
|
2019-04-18 23:20:47 +02:00
|
|
|
log::log
|
|
|
|
messages_log
|
|
|
|
{
|
2019-08-01 04:40:18 +02:00
|
|
|
"m.messages"
|
2019-04-18 23:20:47 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(filter_buf, filter_query)
|
2018-05-12 12:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!visible(room, request.user_id))
|
2018-05-31 14:45:29 +02:00
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted to view the room at this event"
|
|
|
|
};
|
|
|
|
|
2019-03-10 04:13:42 +01:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-04-15 21:16:48 +02:00
|
|
|
const auto room_depth
|
|
|
|
{
|
|
|
|
m::depth(std::nothrow, room)
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2019-03-05 00:46:44 +01:00
|
|
|
json::stack::object top
|
2018-04-30 19:40:52 +02:00
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
2018-04-05 03:34:52 +02:00
|
|
|
|
2019-03-04 21:21:11 +01:00
|
|
|
m::event::id::buf start
|
|
|
|
{
|
2019-04-19 00:11:48 +02:00
|
|
|
page.from
|
2019-03-05 00:46:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
m::event::id::buf end
|
|
|
|
{
|
2019-04-19 00:11:48 +02:00
|
|
|
page.to
|
2019-03-05 00:46:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array chunk
|
|
|
|
{
|
|
|
|
top, "chunk"
|
2019-03-04 21:21:11 +01:00
|
|
|
};
|
|
|
|
|
2019-09-18 00:47:20 +02:00
|
|
|
const auto &pfetch_max
|
|
|
|
{
|
|
|
|
std::min(size_t(page.limit), size_t(prefetch_max))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t pfetch{0}, pfetched{0};
|
|
|
|
m::room::events pf
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-03-05 00:46:44 +01:00
|
|
|
size_t hit{0}, miss{0};
|
2019-09-18 00:47:20 +02:00
|
|
|
m::room::events it
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-03-05 00:46:44 +01:00
|
|
|
for(; it; page.dir == 'b'? --it : ++it)
|
2018-04-30 19:40:52 +02:00
|
|
|
{
|
2019-09-18 00:47:20 +02:00
|
|
|
for(; pf && pfetch < pfetched + prefetch_max; page.dir == 'b'? --pf : ++pf)
|
|
|
|
pfetch += pf.prefetch();
|
|
|
|
|
|
|
|
if(hit + miss == 0 && pfetch > 0)
|
|
|
|
ctx::yield();
|
|
|
|
|
|
|
|
const m::event &event
|
|
|
|
{
|
|
|
|
*it
|
|
|
|
};
|
|
|
|
|
2019-07-10 09:26:25 +02:00
|
|
|
end = event.event_id;
|
2019-03-10 03:19:05 +01:00
|
|
|
if(hit > page.limit || miss >= size_t(max_filter_miss))
|
2019-03-05 00:46:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
if(!visible(event, request.user_id))
|
2018-04-05 03:34:52 +02:00
|
|
|
{
|
2019-03-05 00:46:44 +01:00
|
|
|
++miss;
|
|
|
|
continue;
|
2018-04-30 19:40:52 +02:00
|
|
|
}
|
2019-03-05 00:46:44 +01:00
|
|
|
|
|
|
|
if(!empty(filter_json) && !match(filter, event))
|
|
|
|
{
|
|
|
|
++miss;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
if(_append(chunk, event, it.event_idx(), user_room, room_depth))
|
|
|
|
++hit;
|
|
|
|
else
|
|
|
|
++miss;
|
2018-02-24 07:08:09 +01:00
|
|
|
}
|
2019-03-05 00:46:44 +01:00
|
|
|
chunk.~array();
|
2018-02-24 07:08:09 +01:00
|
|
|
|
2019-03-10 03:52:48 +01:00
|
|
|
if(it || page.dir == 'b')
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "start", json::value{start}
|
|
|
|
};
|
2018-04-30 19:40:52 +02:00
|
|
|
|
2019-03-10 03:52:48 +01:00
|
|
|
if(it || page.dir != 'b')
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "end", json::value{end}
|
|
|
|
};
|
2018-04-30 19:40:52 +02:00
|
|
|
|
2019-04-18 23:20:47 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
messages_log, "%s in %s from:%s to:%s dir:%c limit:%zu start:%s end:%s hit:%zu miss:%zu",
|
|
|
|
client.loghead(),
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{page.from},
|
|
|
|
string_view{page.to},
|
|
|
|
page.dir,
|
|
|
|
page.limit,
|
|
|
|
string_view{start},
|
|
|
|
string_view{end},
|
|
|
|
hit,
|
|
|
|
miss,
|
|
|
|
};
|
|
|
|
|
2018-04-30 19:40:52 +02:00
|
|
|
return {};
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|
2018-02-24 07:08:09 +01:00
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
bool
|
2019-01-11 01:56:32 +01:00
|
|
|
_append(json::stack::array &chunk,
|
|
|
|
const m::event &event,
|
2019-03-10 04:13:42 +01:00
|
|
|
const m::event::idx &event_idx,
|
2019-04-15 21:16:48 +02:00
|
|
|
const m::user::room &user_room,
|
|
|
|
const int64_t &room_depth)
|
2019-01-11 01:56:32 +01:00
|
|
|
{
|
2019-08-03 01:56:18 +02:00
|
|
|
m::event::append::opts opts;
|
2019-03-12 00:02:48 +01:00
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.user_id = &user_room.user.user_id;
|
|
|
|
opts.user_room = &user_room;
|
2019-04-15 21:16:48 +02:00
|
|
|
opts.room_depth = &room_depth;
|
2019-08-03 01:56:18 +02:00
|
|
|
return m::event::append(chunk, event, opts);
|
2019-01-11 01:56:32 +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)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// 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.
|
2019-02-17 01:28:34 +01:00
|
|
|
//
|
|
|
|
// NOTE: Synapse doesn't require this and many clients have forgotten to
|
|
|
|
// NOTE: use it so we have to relax the requirement here.
|
|
|
|
if(!empty(request.query["from"]))
|
|
|
|
from = url::decode(from, request.query.at("from"));
|
|
|
|
|
2018-02-24 07:08:09 +01:00
|
|
|
// 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"]))
|
2019-02-17 01:28:34 +01:00
|
|
|
to = url::decode(to, request.query.at("to"));
|
2018-02-24 07:08:09 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
};
|
|
|
|
}
|