0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-12 13:01:07 +01:00

ircd:Ⓜ️:event: Add auth::chain utility.

This commit is contained in:
Jason Volk 2019-02-14 16:48:45 -08:00
parent eca9bd7da9
commit 2660b30061
4 changed files with 101 additions and 19 deletions

View file

@ -46,25 +46,23 @@ struct ircd::m::event::auth::refs
refs(const event::idx &idx) refs(const event::idx &idx)
:idx{idx} :idx{idx}
{ {}
assert(idx);
}
}; };
struct ircd::m::event::auth::chain struct ircd::m::event::auth::chain
{ {
using closure_bool = std::function<bool (const event::idx &, const event &)>;
event::idx idx; event::idx idx;
public: static bool for_each(const auth::chain &, const closure_bool &);
using closure_bool = std::function<bool (const vector_view<const event::id> &)>;
public:
bool for_each(const closure_bool &) const; bool for_each(const closure_bool &) const;
bool has(const string_view &type) const noexcept; bool has(const string_view &type) const noexcept;
size_t depth() const noexcept; size_t depth() const noexcept;
chain(const event::idx &idx) chain(const event::idx &idx)
:idx{idx} :idx{idx}
{ {}
assert(idx);
}
}; };

View file

@ -1433,7 +1433,7 @@ ircd::m::event::auth::chain::depth()
const noexcept const noexcept
{ {
size_t ret(0); size_t ret(0);
for_each([&ret](const auto &) for_each([&ret](const auto &, const auto &)
{ {
++ret; ++ret;
return true; return true;
@ -1448,15 +1448,9 @@ const noexcept
{ {
bool ret(false); bool ret(false);
for_each([&type, &ret] for_each([&type, &ret]
(const vector_view<const event::id> &v) (const auto &, const auto &event)
{ {
for(auto it(begin(v)); !ret && it != end(v); ++it) ret = type == json::get<"type"_>(event);
m::get(std::nothrow, *it, "type", [&type, &ret]
(const string_view &value)
{
ret = type == value;
});
return !ret; return !ret;
}); });
@ -1467,8 +1461,21 @@ bool
ircd::m::event::auth::chain::for_each(const closure_bool &closure) ircd::m::event::auth::chain::for_each(const closure_bool &closure)
const const
{ {
assert(idx); return chain::for_each(*this, closure);
return true; }
bool
ircd::m::event::auth::chain::for_each(const auth::chain &c,
const closure_bool &closure)
{
using prototype = bool (const auth::chain &, const closure_bool &);
static mods::import<prototype> call
{
"m_event", "ircd::m::event::auth::chain::for_each"
};
return call(c, closure);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View file

@ -5921,6 +5921,36 @@ console_cmd__event__visible(opt &out, const string_view &line)
return true; return true;
} }
bool
console_cmd__event__auth(opt &out, const string_view &line)
{
const params param{line, " ",
{
"event_id"
}};
const m::event::id &event_id
{
param.at("event_id")
};
const m::event::auth::chain ac
{
m::index(event_id)
};
ac.for_each([&out](const auto &idx, const auto &event)
{
out << idx
<< " " << pretty_oneline(event)
<< std::endl;
return true;
});
return true;
}
bool bool
console_cmd__event__refs__rebuild(opt &out, const string_view &line) console_cmd__event__refs__rebuild(opt &out, const string_view &line)
{ {

View file

@ -562,6 +562,53 @@ ircd::m::pretty_oneline(std::ostream &s,
return s; return s;
} }
bool
IRCD_MODULE_EXPORT
ircd::m::event::auth::chain::for_each(const auth::chain &c,
const closure_bool &closure)
{
m::event::fetch e, a;
std::set<event::idx> ae;
std::deque<event::idx> aq {c.idx}; do
{
const auto idx(aq.front());
aq.pop_front();
if(!seek(e, idx, std::nothrow))
continue;
const m::event::prev prev{e};
const auto count(prev.auth_events_count());
for(size_t i(0); i < count && i < 4; ++i)
{
const auto &auth_event_id(prev.auth_event(i));
const auto &auth_event_idx(index(auth_event_id, std::nothrow));
if(!auth_event_idx)
continue;
auto it(ae.lower_bound(auth_event_idx));
if(it == end(ae) || *it != auth_event_idx)
{
seek(a, auth_event_idx, std::nothrow);
ae.emplace_hint(it, auth_event_idx);
if(a.valid)
aq.emplace_back(auth_event_idx);
}
}
}
while(!aq.empty());
for(const auto &idx : ae)
{
if(!seek(e, idx, std::nothrow))
continue;
if(!closure(idx, e))
return false;
}
return true;
}
void void
IRCD_MODULE_EXPORT IRCD_MODULE_EXPORT
ircd::m::event::refs::rebuild() ircd::m::event::refs::rebuild()