2018-01-24 00:54:26 +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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-03-09 04:02:20 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2018-01-24 00:54:26 +01:00
|
|
|
{
|
2018-03-09 04:02:20 +01:00
|
|
|
"Federation (undocumented) :Get missing events."
|
2018-01-24 00:54:26 +01:00
|
|
|
};
|
|
|
|
|
2018-03-09 04:02:20 +01:00
|
|
|
resource
|
2018-01-24 00:54:26 +01:00
|
|
|
get_missing_events_resource
|
|
|
|
{
|
|
|
|
"/_matrix/federation/v1/get_missing_events/",
|
|
|
|
{
|
2018-03-09 04:02:20 +01:00
|
|
|
"Federation (undocumented) missing events handler",
|
2018-01-24 00:54:26 +01:00
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-09 04:02:20 +01:00
|
|
|
static resource::response get__missing_events(client &, const resource::request &);
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_get
|
|
|
|
{
|
|
|
|
get_missing_events_resource, "GET", get__missing_events,
|
|
|
|
{
|
|
|
|
method_get.VERIFY_ORIGIN
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_post
|
|
|
|
{
|
|
|
|
get_missing_events_resource, "POST", get__missing_events,
|
|
|
|
{
|
|
|
|
method_post.VERIFY_ORIGIN
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-23 06:01:22 +02:00
|
|
|
conf::item<ssize_t>
|
2018-03-09 04:02:20 +01:00
|
|
|
max_limit
|
|
|
|
{
|
|
|
|
{ "name", "ircd.federation.missing_events.max_limit" },
|
|
|
|
{ "default", 128L }
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<size_t>
|
2018-08-23 06:01:22 +02:00
|
|
|
flush_hiwat
|
2018-03-09 04:02:20 +01:00
|
|
|
{
|
2018-08-23 06:01:22 +02:00
|
|
|
{ "name", "ircd.federation.missing_events.flush.hiwat" },
|
|
|
|
{ "default", long(16_KiB) },
|
2018-03-09 04:02:20 +01:00
|
|
|
};
|
|
|
|
|
2018-01-24 00:54:26 +01:00
|
|
|
resource::response
|
2018-03-09 04:02:20 +01:00
|
|
|
get__missing_events(client &client,
|
|
|
|
const resource::request &request)
|
2018-01-24 00:54:26 +01:00
|
|
|
{
|
2018-06-03 04:47:11 +02:00
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"room_id path parameter required"
|
|
|
|
};
|
|
|
|
|
2018-01-24 00:54:26 +01:00
|
|
|
m::room::id::buf room_id
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(room_id, request.parv[0])
|
2018-01-24 00:54:26 +01:00
|
|
|
};
|
|
|
|
|
2018-08-23 06:01:22 +02:00
|
|
|
ssize_t limit
|
2018-01-24 00:54:26 +01:00
|
|
|
{
|
2018-03-09 04:02:20 +01:00
|
|
|
request["limit"]?
|
2018-08-23 06:01:22 +02:00
|
|
|
std::min(lex_cast<ssize_t>(request["limit"]), ssize_t(max_limit)):
|
|
|
|
ssize_t(10) // default limit (protocol spec)
|
2018-01-24 00:54:26 +01:00
|
|
|
};
|
|
|
|
|
2018-03-09 04:02:20 +01:00
|
|
|
const auto min_depth
|
2018-01-24 00:54:26 +01:00
|
|
|
{
|
2018-03-09 04:02:20 +01:00
|
|
|
request["min_depth"]?
|
|
|
|
lex_cast<uint64_t>(request["min_depth"]):
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &earliest
|
|
|
|
{
|
|
|
|
request["earliest_events"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &latest
|
|
|
|
{
|
2018-06-04 04:09:28 +02:00
|
|
|
request["latest_events"]
|
2018-03-09 04:02:20 +01:00
|
|
|
};
|
|
|
|
|
2018-08-23 06:01:22 +02:00
|
|
|
const auto in_earliest{[&earliest](const auto &event_id)
|
2018-03-09 04:02:20 +01:00
|
|
|
{
|
2018-08-23 06:01:22 +02:00
|
|
|
return end(earliest) != std::find_if(begin(earliest), end(earliest), [&event_id]
|
|
|
|
(const auto &event_id_)
|
2018-03-09 04:02:20 +01:00
|
|
|
{
|
2018-08-23 06:01:22 +02:00
|
|
|
return event_id == unquote(event_id_);
|
|
|
|
});
|
|
|
|
}};
|
|
|
|
|
|
|
|
resource::response::chunked response
|
2018-03-09 04:02:20 +01:00
|
|
|
{
|
2018-08-23 06:01:22 +02:00
|
|
|
client, http::OK
|
|
|
|
};
|
2018-03-09 04:02:20 +01:00
|
|
|
|
2018-08-23 06:01:22 +02:00
|
|
|
json::stack out
|
2018-06-03 04:47:11 +02:00
|
|
|
{
|
2018-09-05 07:48:23 +02:00
|
|
|
response.buf, response.flusher(), size_t(flush_hiwat)
|
2018-08-23 06:01:22 +02:00
|
|
|
};
|
2018-03-09 04:02:20 +01:00
|
|
|
|
2018-08-23 06:01:22 +02:00
|
|
|
json::stack::object top{out};
|
|
|
|
json::stack::array events
|
|
|
|
{
|
2019-03-06 08:54:36 +01:00
|
|
|
top, "events"
|
2018-08-23 06:01:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::deque<std::string> queue;
|
|
|
|
const auto add_queue{[&limit, &queue, &in_earliest]
|
|
|
|
(const m::event::id &event_id) -> bool
|
|
|
|
{
|
|
|
|
if(in_earliest(event_id))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(end(queue) != std::find(begin(queue), end(queue), event_id))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(--limit < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
queue.emplace_back(std::string{event_id});
|
|
|
|
return true;
|
|
|
|
}};
|
|
|
|
|
|
|
|
for(const auto &event_id : latest)
|
|
|
|
add_queue(unquote(event_id));
|
|
|
|
|
|
|
|
m::event::fetch event;
|
|
|
|
while(!queue.empty())
|
|
|
|
{
|
|
|
|
const auto &event_id{queue.front()};
|
|
|
|
const unwind pop{[&queue]
|
|
|
|
{
|
|
|
|
queue.pop_front();
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(!seek(event, event_id, std::nothrow))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!visible(event, request.node_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
events.append(event);
|
|
|
|
for(const json::array &prev : json::get<"prev_events"_>(event))
|
|
|
|
if(!add_queue(unquote(prev.at(0))))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2018-03-09 04:02:20 +01:00
|
|
|
}
|