From e9df8b699c921d2fabe5b592aaa4d6a468108e01 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 11 Apr 2018 14:04:05 -0700 Subject: [PATCH] ircd::m: Add user room membership convenience interface; apply. --- include/ircd/m/user.h | 7 ++++ ircd/m/m.cc | 75 +++++++++++++++++++++++++++++++++++ modules/client/initialsync.cc | 22 +++------- 3 files changed, 87 insertions(+), 17 deletions(-) diff --git a/include/ircd/m/user.h b/include/ircd/m/user.h index ba9395929..53772426b 100644 --- a/include/ircd/m/user.h +++ b/include/ircd/m/user.h @@ -53,6 +53,13 @@ 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(); diff --git a/ircd/m/m.cc b/ircd/m/m.cc index bf46b5a4e..25147bcca 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -1058,6 +1058,81 @@ 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, diff --git a/modules/client/initialsync.cc b/modules/client/initialsync.cc index 72589de99..cb5fee635 100644 --- a/modules/client/initialsync.cc +++ b/modules/client/initialsync.cc @@ -341,24 +341,12 @@ initialsync_rooms__membership(client &client, const m::user::room &user_room, const string_view &membership) { - const m::room::state user_state{user_room}; - user_state.for_each("ircd.member", [&] - (const m::event &event) + const m::user &user{user_room.user}; + user.for_each(membership, [&client, &request, &out, &user_room] + (const m::room &room, const string_view &membership) { - const auto &membership_ - { - unquote(at<"content"_>(event).at("membership")) - }; - - if(membership_ != membership) - return; - - const m::room::id &room_id - { - unquote(at<"state_key"_>(event)) - }; - - json::stack::member member{out, string_view{room_id}}; + const m::room::id &room_id{room.room_id}; + json::stack::member member{out, room_id}; json::stack::object object{member}; initialsync_room(client, request, object, user_room, room_id); });