From f471093d3e6b5db0e4b240fd95f1ece8378559cd Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 1 Mar 2020 17:13:15 -0800 Subject: [PATCH] ircd::m::user::rooms: Use _event_state to eliminate the ircd.member type. --- include/ircd/m/user/rooms.h | 4 +-- matrix/user_rooms.cc | 38 ++++++++++++-------- modules/client/rooms/forget.cc | 31 ++-------------- modules/client/rooms/leave.cc | 35 ------------------ modules/m_room_member.cc | 65 ---------------------------------- 5 files changed, 29 insertions(+), 144 deletions(-) diff --git a/include/ircd/m/user/rooms.h b/include/ircd/m/user/rooms.h index 90c47e138..5c5eb2d43 100644 --- a/include/ircd/m/user/rooms.h +++ b/include/ircd/m/user/rooms.h @@ -19,7 +19,7 @@ struct ircd::m::user::rooms using closure = std::function; using closure_bool = std::function; - m::user::room user_room; + m::user user; public: // All rooms with specific membership @@ -34,6 +34,6 @@ struct ircd::m::user::rooms size_t count() const; rooms(const m::user &user) - :user_room{user} + :user{user} {} }; diff --git a/matrix/user_rooms.cc b/matrix/user_rooms.cc index 387700438..9543221d8 100644 --- a/matrix/user_rooms.cc +++ b/matrix/user_rooms.cc @@ -79,28 +79,38 @@ ircd::m::user::rooms::for_each(const string_view &membership, const closure_bool &closure) const { - const m::room::state state - { - user_room - }; + m::events::state::tuple query; + std::get<0>(query) = user; + std::get<1>(query) = "m.room.member"; - return state.for_each("ircd.member", [&membership, &closure] - (const string_view &, const string_view &state_key, const m::event::idx &event_idx) + m::room::id::buf room_id_; + return m::events::state::for_each(query, [&] + (const auto &tuple) { bool ret{true}; - m::get(std::nothrow, event_idx, "content", [&state_key, &membership, &closure, &ret] + const auto &[state_key, type, room_id, depth, event_idx] + { + tuple + }; + + assert(type == "m.room.member"); + assert(state_key == user); + if(room_id != room_id_) + room_id_ = room_id; + else + return ret; + + const auto &_room_id(room_id); + m::get(std::nothrow, event_idx, "content", [&] (const json::object &content) { - const json::string &membership_ + const json::string &_membership { - content.get("membership") + content["membership"] }; - if(!membership || membership_ == membership) - { - const m::room::id &room_id{state_key}; - ret = closure(room_id, membership_); - } + if(!membership || _membership == membership) + ret = closure(_room_id, _membership); }); return ret; diff --git a/modules/client/rooms/forget.cc b/modules/client/rooms/forget.cc index f95b0842c..fdeabc333 100644 --- a/modules/client/rooms/forget.cc +++ b/modules/client/rooms/forget.cc @@ -27,34 +27,9 @@ post__forget(client &client, request.user_id }; - const auto event_idx - { - user_room.get(std::nothrow, "ircd.member", room_id) - }; - - if(!event_idx) - throw m::NOT_FOUND - { - "No user membership found for room %s.", - string_view{room_id} - }; - - if(m::membership(event_idx, m::membership_positive)) - throw m::error - { - http::UNPROCESSABLE_ENTITY, "M_MEMBERSHIP_POSITIVE", - "You must leave or be banned from the room to forget it." - }; - - const auto event_id - { - m::event_id(event_idx) - }; - - const auto redact_id - { - redact(user_room, request.user_id, event_id, "forget") - }; + // + //TODO: XXX + // return m::resource::response { diff --git a/modules/client/rooms/leave.cc b/modules/client/rooms/leave.cc index 750170ff3..f945e3889 100644 --- a/modules/client/rooms/leave.cc +++ b/modules/client/rooms/leave.cc @@ -22,41 +22,6 @@ post__leave(client &client, room_id }; - if(!room.has("m.room.member", request.user_id)) - { - const m::user::room user_room - { - request.user_id - }; - - // In case the user's user room falls out of sync with the real room - // state; we directly set the user's user room state. - if(!user_room.has("ircd.member", room_id)) - throw m::error - { - http::NOT_MODIFIED, "M_TARGET_NOT_IN_ROOM", - "The user %s has no membership state in %s", - string_view{request.user_id}, - string_view{room_id}, - }; - - const auto event_id - { - send(user_room, request.user_id, "ircd.member", room_id, json::members - { - { "membership", "leave" } - }) - }; - - return m::resource::response - { - client, http::OK, json::members - { - { "event_id", event_id } - } - }; - } - const auto event_id { m::leave(room, request.user_id) diff --git a/modules/m_room_member.cc b/modules/m_room_member.cc index 91c3deb42..0ed2b138f 100644 --- a/modules/m_room_member.cc +++ b/modules/m_room_member.cc @@ -35,71 +35,6 @@ IRCD_MODULE "Matrix m.room.member" }; -decltype(ircd::m::affect_user_room_hookfn) -ircd::m::affect_user_room_hookfn -{ - affect_user_room, - { - { "_site", "vm.effect" }, - { "type", "m.room.member" }, - } -}; - -void -ircd::m::affect_user_room(const m::event &event, - m::vm::eval &eval) -try -{ - const auto &room_id - { - at<"room_id"_>(event) - }; - - const auto &sender - { - at<"sender"_>(event) - }; - - const m::user::id &subject - { - at<"state_key"_>(event) - }; - - //TODO: ABA / TXN - if(!exists(subject)) - create(subject); - - m::user::room user_room - { - subject - }; - - //TODO: ABA / TXN - assert(exists(user_room)); - const auto &prev_idx - { - user_room.get(std::nothrow, "ircd.member", room_id) - }; - - int64_t prev_depth; - if(m::get(prev_idx, "depth", prev_depth)) - if(at<"depth"_>(event) < prev_depth) - return; - - send(user_room, sender, "ircd.member", room_id, at<"content"_>(event)); -} -catch(const std::exception &e) -{ - log::error - { - log, "Failed to update user %s room for membership change in %s by %s :%s", - json::get<"state_key"_>(event), - json::get<"room_id"_>(event), - json::get<"sender"_>(event), - e.what() - }; -} - decltype(ircd::m::auth_room_member_hookfn) ircd::m::auth_room_member_hookfn {