diff --git a/include/ircd/m/events.h b/include/ircd/m/events.h index 871fac411..60f6a4b17 100644 --- a/include/ircd/m/events.h +++ b/include/ircd/m/events.h @@ -14,42 +14,61 @@ namespace ircd::m::events { struct range; - using closure_bool = std::function; - using closure_type_bool = std::function; - using closure_sender_bool = std::function; - using closure_type_name_bool = std::function; - using closure_sender_name_bool = std::function; - using closure_origin_name_bool = std::function; - - // Iterate types starting with some prefix, or all types. - bool for_each_type(const closure_type_name_bool &); - bool for_each_type(const string_view &prefix, const closure_type_name_bool &); - - // Iterate servers starting at the hostname equal to or greater than provided - bool for_each_origin(const closure_origin_name_bool &); - bool for_each_origin(const string_view &hostlb, const closure_origin_name_bool &); - - // Iterate senders of events (users). - bool for_each_sender(const closure_sender_name_bool &); - bool for_each_sender(const string_view &key, const closure_sender_name_bool &); - - // Iterate event indexes matching the argument - bool for_each_in_type(const string_view &, const closure_type_bool &); - bool for_each_in_sender(const id::user &, const closure_sender_bool &); - bool for_each_in_origin(const string_view &, const closure_sender_bool &); + using closure = std::function; // Iterate viable event indexes in a range bool for_each(const range &, const event::closure_idx_bool &); bool for_each(const range &, const event_filter &, const event::closure_idx_bool &); // Iterate events in an index range - bool for_each(const range &, const closure_bool &); - bool for_each(const range &, const event_filter &, const closure_bool &); + bool for_each(const range &, const closure &); + bool for_each(const range &, const event_filter &, const closure &); // util void dump__file(const string_view &filename); } +/// Interface to the types of all events known to this server. +namespace ircd::m::events::type +{ + using closure = std::function; + using closure_name = std::function; + + // Iterate the names of all event types. + bool for_each(const closure_name &); + bool for_each(const string_view &prefix, const closure_name &); + + // Iterate the events for a specific type. + bool for_each_in(const string_view &, const closure &); +} + +/// Interface to the senders of all events known to the server. +namespace ircd::m::events::sender +{ + using closure = std::function; + using closure_name = std::function; + + // Iterate all of the sender mxids known to the server. + bool for_each(const closure_name &); + bool for_each(const string_view &key, const closure_name &); + + // Iterate all of the events for a specific sender mxid. + bool for_each_in(const id::user &, const closure &); +} + +/// Interface to the servers of the senders of all events known to this server. +namespace ircd::m::events::origin +{ + using closure_name = std::function; + + // Iterate all server names known to this server. + bool for_each(const closure_name &); + bool for_each(const string_view &hostlb, const closure_name &); + + // Iterate all senders mxids on a specific server. + bool for_each_in(const string_view &, const sender::closure &); +} + /// Range to start (inclusive) and stop (exclusive). If start is greater than /// stop a reverse iteration will occur. -1 (or unsigned max value) can be used /// to start or stop at the end. 0 can be used to start or stop at the beginning. @@ -67,3 +86,21 @@ struct ircd::m::events::range ,fopts{fopts} {} }; + +inline bool +ircd::m::events::origin::for_each(const closure_name &closure) +{ + return for_each(string_view{}, closure); +} + +inline bool +ircd::m::events::sender::for_each(const closure_name &closure) +{ + return for_each(string_view{}, closure); +} + +inline bool +ircd::m::events::type::for_each(const closure_name &closure) +{ + return for_each(string_view{}, closure); +} diff --git a/modules/console.cc b/modules/console.cc index d3e0327fe..b7fb3ccb8 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -6335,7 +6335,7 @@ console_cmd__events__in__sender(opt &out, const string_view &line) param.at("user_id") }; - m::events::for_each_in_sender(user_id, [&out] + m::events::sender::for_each_in(user_id, [&out] (const m::user::id &user_id, const m::event::idx &event_idx) { const m::event::fetch event @@ -6369,7 +6369,7 @@ console_cmd__events__in__origin(opt &out, const string_view &line) lstrip(param.at("origin"), ':') }; - m::events::for_each_in_origin(origin, [&out] + m::events::origin::for_each_in(origin, [&out] (const m::user::id &user_id, const m::event::idx &event_idx) { const m::event::fetch event @@ -6403,7 +6403,7 @@ console_cmd__events__in__type(opt &out, const string_view &line) param.at("type") }; - m::events::for_each_in_type(type, [&out] + m::events::type::for_each_in(type, [&out] (const string_view &type, const m::event::idx &event_idx) { const m::event::fetch event @@ -6459,7 +6459,7 @@ console_cmd__events__type(opt &out, const string_view &line) param["prefix"] }; - m::events::for_each_type(prefix, [&out] + m::events::type::for_each(prefix, [&out] (const string_view &type) { out << type << std::endl; @@ -6482,7 +6482,7 @@ console_cmd__events__sender(opt &out, const string_view &line) param["prefix"] }; - m::events::for_each_sender(prefix, [&out] + m::events::sender::for_each(prefix, [&out] (const m::user::id &user_id) { out << user_id << std::endl; @@ -6505,7 +6505,7 @@ console_cmd__events__origin(opt &out, const string_view &line) param["prefix"] }; - m::events::for_each_origin(prefix, [&out] + m::events::origin::for_each(prefix, [&out] (const string_view &origin) { out << origin << std::endl; diff --git a/modules/m_events.cc b/modules/m_events.cc index a65c6736c..fa33ee150 100644 --- a/modules/m_events.cc +++ b/modules/m_events.cc @@ -106,7 +106,7 @@ bool IRCD_MODULE_EXPORT ircd::m::events::for_each(const range &range, const event_filter &filter, - const closure_bool &closure) + const closure &closure) { auto limit { @@ -130,7 +130,7 @@ ircd::m::events::for_each(const range &range, bool IRCD_MODULE_EXPORT ircd::m::events::for_each(const range &range, - const closure_bool &closure) + const closure &closure) { event::fetch event { @@ -255,85 +255,14 @@ ircd::m::events::for_each(const range &range, return true; } -bool -IRCD_MODULE_EXPORT -ircd::m::events::for_each_in_origin(const string_view &origin, - const closure_sender_bool &closure) -{ - auto &column - { - dbs::event_sender - }; - - char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE]; - const string_view &key - { - dbs::event_sender_origin_key(buf, origin) - }; - - auto it - { - column.begin(key) - }; - - for(; bool(it); ++it) - { - const auto &keyp - { - dbs::event_sender_origin_key(it->first) - }; - - const user::id::buf user_id - { - std::get<0>(keyp), origin - }; - - if(!closure(user_id, std::get<1>(keyp))) - return false; - } - - return true; -} +// +// events::type +// bool IRCD_MODULE_EXPORT -ircd::m::events::for_each_in_sender(const id::user &user, - const closure_sender_bool &closure) -{ - auto &column - { - dbs::event_sender - }; - - char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE]; - const string_view &key - { - dbs::event_sender_key(buf, user) - }; - - auto it - { - column.begin(key) - }; - - for(; bool(it); ++it) - { - const auto &keyp - { - dbs::event_sender_key(it->first) - }; - - if(!closure(user, std::get(keyp))) - return false; - } - - return true; -} - -bool -IRCD_MODULE_EXPORT -ircd::m::events::for_each_in_type(const string_view &type, - const closure_type_bool &closure) +ircd::m::events::type::for_each_in(const string_view &type, + const closure &closure) { auto &column { @@ -367,15 +296,8 @@ ircd::m::events::for_each_in_type(const string_view &type, bool IRCD_MODULE_EXPORT -ircd::m::events::for_each_type(const closure_type_name_bool &closure) -{ - return for_each_type(string_view{}, closure); -} - -bool -IRCD_MODULE_EXPORT -ircd::m::events::for_each_type(const string_view &prefix, - const closure_type_name_bool &closure) +ircd::m::events::type::for_each(const string_view &prefix, + const closure_name &closure) { db::column &column { @@ -414,17 +336,140 @@ ircd::m::events::for_each_type(const string_view &prefix, return true; } +// +// events::origin +// + bool IRCD_MODULE_EXPORT -ircd::m::events::for_each_sender(const closure_sender_name_bool &closure) +ircd::m::events::origin::for_each_in(const string_view &origin, + const sender::closure &closure) { - return for_each_sender(string_view{}, closure); + auto &column + { + dbs::event_sender + }; + + char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE]; + const string_view &key + { + dbs::event_sender_origin_key(buf, origin) + }; + + auto it + { + column.begin(key) + }; + + for(; bool(it); ++it) + { + const auto &keyp + { + dbs::event_sender_origin_key(it->first) + }; + + const user::id::buf user_id + { + std::get<0>(keyp), origin + }; + + if(!closure(user_id, std::get<1>(keyp))) + return false; + } + + return true; } bool IRCD_MODULE_EXPORT -ircd::m::events::for_each_sender(const string_view &prefix_, - const closure_sender_name_bool &closure) +ircd::m::events::origin::for_each(const string_view &prefix, + const closure_name &closure) +{ + db::column &column + { + dbs::event_sender + }; + + const auto &prefixer + { + dbs::desc::events__event_sender__pfx + }; + + if(unlikely(startswith(prefix, '@'))) + throw panic + { + "Prefix argument should be a hostname. It must not start with '@'" + }; + + string_view last; + char buf[rfc3986::DOMAIN_BUFSIZE]; + for(auto it(column.lower_bound(prefix)); bool(it); ++it) + { + if(!m::dbs::is_event_sender_origin_key(it->first)) + break; + + const string_view &host + { + prefixer.get(it->first) + }; + + if(host == last) + continue; + + if(!startswith(host, prefix)) + break; + + last = { buf, copy(buf, host) }; + if(!closure(host)) + return false; + } + + return true; +} + +// +// events::sender +// + +bool +IRCD_MODULE_EXPORT +ircd::m::events::sender::for_each_in(const id::user &user, + const closure &closure) +{ + auto &column + { + dbs::event_sender + }; + + char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE]; + const string_view &key + { + dbs::event_sender_key(buf, user) + }; + + auto it + { + column.begin(key) + }; + + for(; bool(it); ++it) + { + const auto &keyp + { + dbs::event_sender_key(it->first) + }; + + if(!closure(user, std::get(keyp))) + return false; + } + + return true; +} + +bool +IRCD_MODULE_EXPORT +ircd::m::events::sender::for_each(const string_view &prefix_, + const closure_name &closure) { db::column &column { @@ -475,57 +520,3 @@ ircd::m::events::for_each_sender(const string_view &prefix_, return true; } - -bool -IRCD_MODULE_EXPORT -ircd::m::events::for_each_origin(const closure_origin_name_bool &closure) -{ - return for_each_origin(string_view{}, closure); -} - -bool -IRCD_MODULE_EXPORT -ircd::m::events::for_each_origin(const string_view &prefix, - const closure_origin_name_bool &closure) -{ - db::column &column - { - dbs::event_sender - }; - - const auto &prefixer - { - dbs::desc::events__event_sender__pfx - }; - - if(unlikely(startswith(prefix, '@'))) - throw panic - { - "Prefix argument should be a hostname. It must not start with '@'" - }; - - string_view last; - char buf[rfc3986::DOMAIN_BUFSIZE]; - for(auto it(column.lower_bound(prefix)); bool(it); ++it) - { - if(!m::dbs::is_event_sender_origin_key(it->first)) - break; - - const string_view &host - { - prefixer.get(it->first) - }; - - if(host == last) - continue; - - if(!startswith(host, prefix)) - break; - - last = { buf, copy(buf, host) }; - if(!closure(host)) - return false; - } - - return true; -} diff --git a/modules/m_rooms.cc b/modules/m_rooms.cc index a570463b9..747576650 100644 --- a/modules/m_rooms.cc +++ b/modules/m_rooms.cc @@ -115,7 +115,7 @@ ircd::m::rooms::for_each(const opts &opts, }); } - return events::for_each_in_type("m.room.create", [&proffer, &ret] + return events::type::for_each_in("m.room.create", [&proffer, &ret] (const string_view &type, const event::idx &event_idx) { assert(type == "m.room.create"); diff --git a/modules/m_users.cc b/modules/m_users.cc index 40ab06cd5..34e6e9acf 100644 --- a/modules/m_users.cc +++ b/modules/m_users.cc @@ -66,7 +66,7 @@ ircd::m::users::for_each(const opts &opts, return for_each_host(opts, closure); bool ret{true}; - events::for_each_sender(opts.localpart, [&opts, &ret, &closure] + events::sender::for_each(opts.localpart, [&opts, &ret, &closure] (const id::user &sender) { if(opts.localpart && !opts.localpart_prefix) @@ -98,7 +98,7 @@ ircd::m::users::for_each_host(const opts &opts, const user::closure_bool &closure) { bool ret{true}; - events::for_each_origin(opts.hostpart, [&ret, &opts, &closure] + events::origin::for_each(opts.hostpart, [&ret, &opts, &closure] (const string_view &origin) { if(opts.hostpart && !opts.hostpart_prefix) @@ -125,7 +125,7 @@ ircd::m::users::for_each_in_host(const opts &opts, { bool ret{true}; m::user::id::buf last; - events::for_each_in_origin(opts.hostpart, [&opts, &ret, &closure, &last] + events::origin::for_each_in(opts.hostpart, [&opts, &ret, &closure, &last] (const id::user &sender, const auto &event_idx) { if(sender == last)