mirror of
https://github.com/matrix-construct/construct
synced 2024-11-26 00:32:35 +01:00
ircd:Ⓜ️:room::auth: Add central interface for chain fetch/eval.
This commit is contained in:
parent
b73bb789b2
commit
1c5b7d2d71
3 changed files with 67 additions and 49 deletions
|
@ -22,6 +22,10 @@ struct ircd::m::room::auth
|
|||
static bool for_each(const auth &, const closure_bool &);
|
||||
static void make_refs(const auth &, json::stack::array &, const types &, const m::id::user & = {});
|
||||
|
||||
using fetch_closure = std::function<bool (const json::object &)>;
|
||||
static bool chain_fetch(const auth &, const net::hostport &, const fetch_closure &);
|
||||
static void chain_eval(const auth &, const net::hostport &);
|
||||
|
||||
m::room room;
|
||||
|
||||
public:
|
||||
|
|
|
@ -2259,6 +2259,35 @@ ircd::m::room::head::reset(const head &h)
|
|||
// room::auth
|
||||
//
|
||||
|
||||
void
|
||||
ircd::m::room::auth::chain_eval(const auth &a,
|
||||
const net::hostport &h)
|
||||
{
|
||||
using prototype = bool (const auth &, const net::hostport &);
|
||||
|
||||
static mods::import<prototype> call
|
||||
{
|
||||
"s_fetch", "ircd::m::room::auth::chain_eval"
|
||||
};
|
||||
|
||||
call(a, h);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::room::auth::chain_fetch(const auth &a,
|
||||
const net::hostport &h,
|
||||
const fetch_closure &c)
|
||||
{
|
||||
using prototype = bool (const auth &, const net::hostport &, const fetch_closure &);
|
||||
|
||||
static mods::import<prototype> call
|
||||
{
|
||||
"s_fetch", "ircd::m::room::auth::chain_fetch"
|
||||
};
|
||||
|
||||
return call(a, h, c);
|
||||
}
|
||||
|
||||
ircd::json::array
|
||||
ircd::m::room::auth::make_refs(const mutable_buffer &buf,
|
||||
const types &types,
|
||||
|
|
|
@ -106,30 +106,38 @@ ircd::m::fetch::hook_handler(const event &event,
|
|||
}
|
||||
|
||||
//
|
||||
// util
|
||||
// auth chain fetch
|
||||
//
|
||||
|
||||
namespace ircd::m::fetch
|
||||
void
|
||||
IRCD_MODULE_EXPORT
|
||||
ircd::m::room::auth::chain_eval(const auth &auth,
|
||||
const net::hostport &remote)
|
||||
{
|
||||
extern "C" void
|
||||
auth_chain_fetch(const m::room::id &room_id,
|
||||
const m::event::id &event_id,
|
||||
const net::hostport &remote,
|
||||
const milliseconds &timeout,
|
||||
const std::function<bool (const m::event &)> &);
|
||||
m::vm::opts opts;
|
||||
opts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
||||
opts.infolog_accept = true;
|
||||
opts.warnlog |= m::vm::fault::STATE;
|
||||
opts.warnlog &= ~m::vm::fault::EXISTS;
|
||||
opts.errorlog &= ~m::vm::fault::STATE;
|
||||
|
||||
extern "C" void
|
||||
auth_chain_eval(const m::room::id &room_id,
|
||||
const m::event::id &event_id,
|
||||
const net::hostport &remote);
|
||||
chain_fetch(auth, remote, [&opts]
|
||||
(const json::object &event)
|
||||
{
|
||||
m::vm::eval
|
||||
{
|
||||
m::event{event}, opts
|
||||
};
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
extern "C" void
|
||||
ircd::m::fetch::auth_chain_fetch(const m::room::id &room_id,
|
||||
const m::event::id &event_id,
|
||||
bool
|
||||
IRCD_MODULE_EXPORT
|
||||
ircd::m::room::auth::chain_fetch(const auth &auth,
|
||||
const net::hostport &remote,
|
||||
const milliseconds &timeout,
|
||||
const std::function<bool (const m::event &)> &closure)
|
||||
const fetch_closure &closure)
|
||||
{
|
||||
m::v1::event_auth::opts opts;
|
||||
opts.remote = remote;
|
||||
|
@ -141,52 +149,29 @@ ircd::m::fetch::auth_chain_fetch(const m::room::id &room_id,
|
|||
|
||||
m::v1::event_auth request
|
||||
{
|
||||
room_id, event_id, buf, std::move(opts)
|
||||
auth.room.room_id, auth.room.event_id, buf, std::move(opts)
|
||||
};
|
||||
|
||||
request.wait(timeout);
|
||||
request.wait(seconds(20)); //TODO: conf
|
||||
request.get();
|
||||
|
||||
const json::array &auth_chain
|
||||
{
|
||||
request
|
||||
};
|
||||
|
||||
std::vector<m::event> events{auth_chain.count()};
|
||||
std::transform(begin(auth_chain), end(auth_chain), begin(events), []
|
||||
(const json::object &pdu)
|
||||
std::vector<json::object> events(auth_chain.count());
|
||||
std::copy(begin(auth_chain), end(auth_chain), begin(events));
|
||||
std::sort(begin(events), end(events), []
|
||||
(const json::object &a, const json::object &b)
|
||||
{
|
||||
return m::event{pdu};
|
||||
return a.at<uint64_t>("depth") < b.at<uint64_t>("depth");
|
||||
});
|
||||
|
||||
std::sort(begin(events), end(events));
|
||||
for(const auto &event : events)
|
||||
if(!closure(event))
|
||||
return;
|
||||
}
|
||||
return false;
|
||||
|
||||
extern "C" void
|
||||
ircd::m::fetch::auth_chain_eval(const m::room::id &room_id,
|
||||
const m::event::id &event_id,
|
||||
const net::hostport &remote)
|
||||
{
|
||||
m::vm::opts opts;
|
||||
opts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
||||
opts.infolog_accept = true;
|
||||
opts.warnlog |= m::vm::fault::STATE;
|
||||
opts.warnlog &= ~m::vm::fault::EXISTS;
|
||||
opts.errorlog &= ~m::vm::fault::STATE;
|
||||
|
||||
auth_chain_fetch(room_id, event_id, remote, seconds(30), [&opts]
|
||||
(const auto &event)
|
||||
{
|
||||
m::vm::eval
|
||||
{
|
||||
event, opts
|
||||
};
|
||||
|
||||
return true;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue