From 55e4a8fcaf758f3ac713aee73dd411be3ec10496 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 11 Apr 2018 15:28:00 -0700 Subject: [PATCH] ircd::m: Split user's rooms iteration into separate interface. --- include/ircd/m/rooms.h | 8 +- include/ircd/m/user.h | 28 ++++-- ircd/m/m.cc | 177 ++++++++++++++++++---------------- modules/client/initialsync.cc | 8 +- 4 files changed, 125 insertions(+), 96 deletions(-) diff --git a/include/ircd/m/rooms.h b/include/ircd/m/rooms.h index df2fc7caf..51706d77d 100644 --- a/include/ircd/m/rooms.h +++ b/include/ircd/m/rooms.h @@ -22,8 +22,8 @@ namespace ircd::m::rooms void for_each(const room::closure &); // All rooms for a user (alias to interface in user::) - void for_each(const user &, const string_view &membership, const user::member_closure_bool &); - void for_each(const user &, const string_view &membership, const user::member_closure &); - void for_each(const user &, const user::member_closure_bool &); - void for_each(const user &, const user::member_closure &); + void for_each(const user &, const string_view &membership, const user::rooms::closure_bool &); + void for_each(const user &, const string_view &membership, const user::rooms::closure &); + void for_each(const user &, const user::rooms::closure_bool &); + void for_each(const user &, const user::rooms::closure &); } diff --git a/include/ircd/m/user.h b/include/ircd/m/user.h index e99c9ecbc..958554365 100644 --- a/include/ircd/m/user.h +++ b/include/ircd/m/user.h @@ -24,6 +24,7 @@ namespace ircd::m struct ircd::m::user { struct room; + struct rooms; using id = m::id::user; using closure = std::function; using closure_bool = std::function; @@ -55,13 +56,6 @@ struct ircd::m::user json::object account_data(const mutable_buffer &out, const string_view &type) const; //nothrow event::id::buf account_data(const m::user &sender, const string_view &type, const json::object &value); - using member_closure = std::function; - using member_closure_bool = std::function; - void for_each(const string_view &membership, const member_closure_bool &) const; - void for_each(const string_view &membership, const member_closure &) const; - void for_each(const member_closure_bool &) const; - void for_each(const member_closure &) const; - bool is_active() const; event::id::buf deactivate(); event::id::buf activate(); @@ -86,6 +80,26 @@ struct ircd::m::user::room room &operator=(const room &) = delete; }; +/// Interface to the rooms for a user. +struct ircd::m::user::rooms +{ + using closure = std::function; + using closure_bool = std::function; + + m::user::room user_room; + + public: + // All rooms with specific membership + void for_each(const string_view &membership, const closure_bool &) const; + void for_each(const string_view &membership, const closure &) const; + + // All rooms with any membership + void for_each(const closure_bool &) const; + void for_each(const closure &) const; + + rooms(const m::user &user); +}; + inline ircd::m::user::operator const ircd::m::user::id &() const diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 783830b07..5b4bd765c 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -902,32 +902,36 @@ ircd::m::node::room::room(const m::node &node) void ircd::m::rooms::for_each(const user &user, - const user::member_closure &closure) + const user::rooms::closure &closure) { - user.for_each(closure); + const m::user::rooms rooms{user}; + rooms.for_each(closure); } void ircd::m::rooms::for_each(const user &user, - const user::member_closure_bool &closure) + const user::rooms::closure_bool &closure) { - user.for_each(closure); + const m::user::rooms rooms{user}; + rooms.for_each(closure); } void ircd::m::rooms::for_each(const user &user, const string_view &membership, - const user::member_closure &closure) + const user::rooms::closure &closure) { - user.for_each(membership, closure); + const m::user::rooms rooms{user}; + rooms.for_each(membership, closure); } void ircd::m::rooms::for_each(const user &user, const string_view &membership, - const user::member_closure_bool &closure) + const user::rooms::closure_bool &closure) { - user.for_each(membership, closure); + const m::user::rooms rooms{user}; + rooms.for_each(membership, closure); } void @@ -1140,81 +1144,6 @@ const return function(*this); } -void -ircd::m::user::for_each(const member_closure &closure) -const -{ - for_each(member_closure_bool{[&closure] - (const m::room &room, const string_view &membership) - { - closure(room, membership); - return true; - }}); -} - -void -ircd::m::user::for_each(const member_closure_bool &closure) -const -{ - const m::user::room user_room{*this}; - const m::room::state state{user_room}; - state.test("ircd.member", [&closure] - (const m::event &event) - { - const m::room::id &room_id - { - at<"state_key"_>(event) - }; - - const string_view &membership - { - unquote(at<"content"_>(event).at("membership")) - }; - - return !closure(room_id, membership); - }); -} - -void -ircd::m::user::for_each(const string_view &membership, - const member_closure &closure) -const -{ - for_each(membership, member_closure_bool{[&closure] - (const m::room &room, const string_view &membership) - { - closure(room, membership); - return true; - }}); -} - -void -ircd::m::user::for_each(const string_view &membership, - const member_closure_bool &closure) -const -{ - const m::user::room user_room{*this}; - const m::room::state state{user_room}; - state.test("ircd.member", [&membership, &closure] - (const m::event &event) - { - const string_view &membership_ - { - unquote(at<"content"_>(event).at("membership")) - }; - - if(membership_ != membership) - return false; - - const m::room::id &room_id - { - at<"state_key"_>(event) - }; - - return !closure(room_id, membership); - }); -} - ircd::m::event::id::buf ircd::m::user::account_data(const m::user &sender, const string_view &type, @@ -1386,6 +1315,88 @@ ircd::m::user::room::room(const m::user &user) static_cast(*this) = room_id; } +// +// user::rooms +// + +ircd::m::user::rooms::rooms(const m::user &user) +:user_room{user} +{ +} + +void +ircd::m::user::rooms::for_each(const closure &closure) +const +{ + for_each(closure_bool{[&closure] + (const m::room &room, const string_view &membership) + { + closure(room, membership); + return true; + }}); +} + +void +ircd::m::user::rooms::for_each(const closure_bool &closure) +const +{ + const m::room::state state{user_room}; + state.test("ircd.member", [&closure] + (const m::event &event) + { + const m::room::id &room_id + { + at<"state_key"_>(event) + }; + + const string_view &membership + { + unquote(at<"content"_>(event).at("membership")) + }; + + return !closure(room_id, membership); + }); +} + +void +ircd::m::user::rooms::for_each(const string_view &membership, + const closure &closure) +const +{ + for_each(membership, closure_bool{[&closure] + (const m::room &room, const string_view &membership) + { + closure(room, membership); + return true; + }}); +} + +void +ircd::m::user::rooms::for_each(const string_view &membership, + const closure_bool &closure) +const +{ + const m::room::state state{user_room}; + state.test("ircd.member", [&membership, &closure] + (const m::event &event) + { + const string_view &membership_ + { + unquote(at<"content"_>(event).at("membership")) + }; + + if(membership_ != membership) + return false; + + const m::room::id &room_id + { + at<"state_key"_>(event) + }; + + return !closure(room_id, membership); + }); +} + /////////////////////////////////////////////////////////////////////////////// // // m/room.h diff --git a/modules/client/initialsync.cc b/modules/client/initialsync.cc index cb5fee635..0d030eb67 100644 --- a/modules/client/initialsync.cc +++ b/modules/client/initialsync.cc @@ -341,8 +341,12 @@ initialsync_rooms__membership(client &client, const m::user::room &user_room, const string_view &membership) { - const m::user &user{user_room.user}; - user.for_each(membership, [&client, &request, &out, &user_room] + const m::user::rooms rooms + { + user_room.user + }; + + rooms.for_each(membership, [&client, &request, &out, &user_room] (const m::room &room, const string_view &membership) { const m::room::id &room_id{room.room_id};