From b878f577d5ebfde1b5eaf671105ce5ba34183196 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 4 Jan 2019 14:47:46 -0800 Subject: [PATCH] ircd::m::user: Add a query for user to user visibility. --- include/ircd/m/user.h | 4 ++++ ircd/m/m.cc | 14 ++++++++++++++ modules/console.cc | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/include/ircd/m/user.h b/include/ircd/m/user.h index 5bf9a755a..c360fafd1 100644 --- a/include/ircd/m/user.h +++ b/include/ircd/m/user.h @@ -160,9 +160,13 @@ struct ircd::m::user::mitsein bool for_each(const closure_bool &) const; void for_each(const closure &) const; + // Counting convenience size_t count(const m::user &, const string_view &membership = {}) const; size_t count(const string_view &membership = {}) const; + // Existential convenience (does `user` and `other` share any common room). + bool has(const m::user &other, const string_view &membership = {}) const; + mitsein(const m::user &user); }; diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 1aa20b372..aa2e99f6d 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -2711,6 +2711,20 @@ ircd::m::user::mitsein::mitsein(const m::user &user) { } +bool +ircd::m::user::mitsein::has(const m::user &other, + const string_view &membership) +const +{ + // Return true if broken out of loop. + return !for_each(other, membership, rooms::closure_bool{[] + (const m::room &, const string_view &) + { + // Break out of loop at first shared room + return false; + }}); +} + size_t ircd::m::user::mitsein::count(const string_view &membership) const diff --git a/modules/console.cc b/modules/console.cc index d20b641e1..c6085c86b 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -8312,6 +8312,45 @@ console_cmd__user__events__count(opt &out, const string_view &line) return true; } +bool +console_cmd__user__sees(opt &out, const string_view &line) +{ + const params param{line, " ", + { + "user_id_a", "user_id_b", "membership" + }}; + + const m::user user_a + { + m::user(param.at("user_id_a")) + }; + + const m::user user_b + { + m::user(param.at("user_id_b")) + }; + + const string_view membership + { + param.at("membership", "join"_sv) + }; + + const m::user::mitsein mitsein + { + user_a + }; + + const bool result + { + mitsein.has(user_b, membership) + }; + + out << std::boolalpha << result + << std::endl; + + return true; +} + // // users //