diff --git a/include/ircd/m/relates.h b/include/ircd/m/relates.h index 5ed0149dd..032f4a695 100644 --- a/include/ircd/m/relates.h +++ b/include/ircd/m/relates.h @@ -38,21 +38,27 @@ struct ircd::m::relates_to /// struct ircd::m::relates { - event::refs refs; - bool match_sender {false}; - bool prefetch_depth {false}; - bool prefetch_sender {false}; - - public: using closure = util::closure_bool < std::function, const event::idx &, const json::object &, const m::relates_to & >; + event::refs refs; + bool match_sender {false}; + bool prefetch_depth {false}; + bool prefetch_sender {false}; + + private: + bool _each(const string_view &, const closure &, const event::idx &) const; + + public: bool for_each(const string_view &type, const closure &) const; bool for_each(const closure &) const; + bool rfor_each(const string_view &type, const closure &) const; + bool rfor_each(const closure &) const; + event::idx get(const string_view &type, const uint at = 0) const; event::idx latest(const string_view &type, uint *const at = nullptr) const; @@ -63,3 +69,41 @@ struct ircd::m::relates size_t count(const string_view &type = {}) const; bool prefetch(const string_view &type = {}) const; }; + +inline bool +ircd::m::relates::rfor_each(const closure &closure) +const +{ + return rfor_each(string_view{}, closure); +} + +inline bool +ircd::m::relates::rfor_each(const string_view &rel_type, + const closure &closure) +const +{ + return refs.rfor_each(dbs::ref::M_RELATES, [this, &rel_type, &closure] + (const auto &event_idx, const auto &type) + { + return _each(rel_type, closure, event_idx); + }); +} + +inline bool +ircd::m::relates::for_each(const closure &closure) +const +{ + return for_each(string_view{}, closure); +} + +inline bool +ircd::m::relates::for_each(const string_view &rel_type, + const closure &closure) +const +{ + return refs.for_each(dbs::ref::M_RELATES, [this, &rel_type, &closure] + (const auto &event_idx, const auto &type) + { + return _each(rel_type, closure, event_idx); + }); +} diff --git a/matrix/relates.cc b/matrix/relates.cc index 2fd1fdb14..38552a2bc 100644 --- a/matrix/relates.cc +++ b/matrix/relates.cc @@ -82,13 +82,6 @@ const }); } -bool -ircd::m::relates::for_each(const closure &closure) -const -{ - return for_each(string_view{}, closure); -} - ircd::m::event::idx ircd::m::relates::latest(const string_view &type, uint *const at) @@ -143,47 +136,44 @@ const } bool -ircd::m::relates::for_each(const string_view &rel_type, - const closure &closure) +ircd::m::relates::_each(const string_view &rel_type, + const closure &closure, + const event::idx &event_idx) const { - return refs.for_each(dbs::ref::M_RELATES, [this, &rel_type, &closure] - (const auto &event_idx, const auto &type) + thread_local char buf[event::MAX_SIZE]; + const json::object content { - thread_local char buf[event::MAX_SIZE]; - const json::object content - { - m::get(std::nothrow, event_idx, "content", buf) - }; + m::get(std::nothrow, event_idx, "content", buf) + }; - if(!content) + if(!content) + return true; + + m::relates_to relates + { + content["m.relates_to"] + }; + + if(!json::get<"rel_type"_>(relates)) + if(json::get<"m.in_reply_to"_>(relates)) + json::get<"rel_type"_>(relates) = "m.in_reply_to"; + + if(rel_type) + if(json::get<"rel_type"_>(relates) != rel_type) return true; - m::relates_to relates + if(match_sender) + { + const pair idx { - content["m.relates_to"] + refs.idx, event_idx }; - if(!json::get<"rel_type"_>(relates)) - if(json::get<"m.in_reply_to"_>(relates)) - json::get<"rel_type"_>(relates) = "m.in_reply_to"; + const std::equal_to equal_to; + if(!m::query(std::nothrow, idx, "sender", equal_to)) + return true; + } - if(rel_type) - if(json::get<"rel_type"_>(relates) != rel_type) - return true; - - if(this->match_sender) - { - const pair idx - { - refs.idx, event_idx - }; - - const std::equal_to equal_to; - if(!m::query(std::nothrow, idx, "sender", equal_to)) - return true; - } - - return closure(event_idx, content, relates); - }); + return closure(event_idx, content, relates); }