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
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::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,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response get__missing_events(client &, const m::resource::request &);
|
2018-03-09 04:02:20 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2018-03-09 04:02:20 +01:00
|
|
|
method_get
|
|
|
|
{
|
|
|
|
get_missing_events_resource, "GET", get__missing_events,
|
|
|
|
{
|
|
|
|
method_get.VERIFY_ORIGIN
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2018-03-09 04:02:20 +01:00
|
|
|
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
|
|
|
|
{
|
2020-10-24 04:43:16 +02:00
|
|
|
{ "name", "ircd.federation.missing_events.limit.max" },
|
|
|
|
{ "default", 256L },
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<ssize_t>
|
|
|
|
min_limit
|
|
|
|
{
|
|
|
|
{ "name", "ircd.federation.missing_events.limit.min" },
|
|
|
|
{ "default", 1L },
|
2018-03-09 04:02:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-03-09 04:02:20 +01:00
|
|
|
get__missing_events(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::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
|
|
|
};
|
|
|
|
|
2019-05-27 05:36:26 +02:00
|
|
|
if(m::room::server_acl::enable_read && !m::room::server_acl::check(room_id, request.node_id))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted by the room's server access control list."
|
|
|
|
};
|
|
|
|
|
2018-08-23 06:01:22 +02:00
|
|
|
ssize_t limit
|
2018-01-24 00:54:26 +01:00
|
|
|
{
|
2020-10-24 04:43:16 +02:00
|
|
|
std::clamp
|
|
|
|
(
|
|
|
|
request.get("limit", 10L), // protocol spec default
|
|
|
|
ssize_t(min_limit),
|
|
|
|
ssize_t(max_limit)
|
|
|
|
)
|
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
|
|
|
};
|
|
|
|
|
2020-10-24 05:01:50 +02:00
|
|
|
static const auto in_array{[]
|
|
|
|
(const json::array &array, const auto &event_id)
|
2018-03-09 04:02:20 +01:00
|
|
|
{
|
2020-10-24 05:01:50 +02:00
|
|
|
return end(array) != std::find_if(begin(array), end(array), [&event_id]
|
|
|
|
(const json::string &_event_id)
|
2018-03-09 04:02:20 +01:00
|
|
|
{
|
2020-10-24 05:01:50 +02:00
|
|
|
return event_id == _event_id;
|
2018-08-23 06:01:22 +02:00
|
|
|
});
|
|
|
|
}};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::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;
|
2020-10-24 05:01:50 +02:00
|
|
|
const auto add_queue{[&limit, &queue, &earliest]
|
2018-08-23 06:01:22 +02:00
|
|
|
(const m::event::id &event_id) -> bool
|
|
|
|
{
|
2020-10-24 05:01:50 +02:00
|
|
|
if(in_array(earliest, event_id))
|
2018-08-23 06:01:22 +02:00
|
|
|
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;
|
|
|
|
}};
|
|
|
|
|
2021-02-25 23:33:52 +01:00
|
|
|
for(const json::string event_id : latest)
|
2020-10-24 05:01:50 +02:00
|
|
|
add_queue(event_id);
|
2018-08-23 06:01:22 +02:00
|
|
|
|
|
|
|
m::event::fetch event;
|
|
|
|
while(!queue.empty())
|
|
|
|
{
|
2020-10-24 05:01:50 +02:00
|
|
|
const auto &event_id
|
|
|
|
{
|
|
|
|
queue.front()
|
|
|
|
};
|
|
|
|
|
2018-08-23 06:01:22 +02:00
|
|
|
const unwind pop{[&queue]
|
|
|
|
{
|
|
|
|
queue.pop_front();
|
|
|
|
}};
|
|
|
|
|
2020-12-09 06:57:47 +01:00
|
|
|
bool ok(true);
|
2020-04-03 04:50:00 +02:00
|
|
|
if(!seek(std::nothrow, event, event_id))
|
2020-12-09 06:57:47 +01:00
|
|
|
ok = false;
|
|
|
|
|
|
|
|
if(ok && !visible(event, request.node_id))
|
|
|
|
ok = false;
|
|
|
|
|
|
|
|
if(!ok)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
m::log, "Failed to divulge missing %s in %s to '%s' queue:%zu limit:%ld",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
request.node_id,
|
|
|
|
queue.size(),
|
|
|
|
limit,
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ok)
|
2018-08-23 06:01:22 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
events.append(event);
|
2019-07-12 01:24:43 +02:00
|
|
|
const m::event::prev prev(event);
|
|
|
|
for(size_t i(0); i < prev.prev_events_count(); ++i)
|
|
|
|
if(!add_queue(prev.prev_event(i)))
|
2018-08-23 06:01:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-24 07:09:41 +02:00
|
|
|
return std::move(response);
|
2018-03-09 04:02:20 +01:00
|
|
|
}
|