From 11699baff0cc9c8054fb435dbb6fc91e3542ed6e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 18 Sep 2019 10:30:23 -0700 Subject: [PATCH] ircd::m: Consolidate m::membership() suite to header; minor cleanup. --- include/ircd/m/event/event.h | 5 +- include/ircd/m/m.h | 1 + include/ircd/m/membership.h | 32 ++++++++++ include/ircd/m/room/members.h | 3 - ircd/m.cc | 100 ++++++++++++++++++++++++++++++++ ircd/m_event.cc | 62 ++++++-------------- ircd/m_room.cc | 73 +---------------------- modules/client/rooms/members.cc | 4 +- modules/console.cc | 2 +- modules/m_room_bootstrap.cc | 3 +- 10 files changed, 158 insertions(+), 127 deletions(-) create mode 100644 include/ircd/m/membership.h diff --git a/include/ircd/m/event/event.h b/include/ircd/m/event/event.h index 2a407a00e..3c1bd0253 100644 --- a/include/ircd/m/event/event.h +++ b/include/ircd/m/event/event.h @@ -18,10 +18,6 @@ namespace ircd::m // General util bool my(const id::event &); bool my(const event &); - size_t degree(const event &); - string_view membership(const event &); - - // [GET] bool exists(const id::event &); bool exists(const id::event &, const bool &good); bool cached(const id::event &); @@ -38,6 +34,7 @@ namespace ircd::m bool operator>=(const event &, const event &); // Topological comparison + size_t degree(const event &); bool before(const event &a, const event &b); bool check_id(const event &) noexcept; diff --git a/include/ircd/m/m.h b/include/ircd/m/m.h index e92a479d6..c663003a7 100644 --- a/include/ircd/m/m.h +++ b/include/ircd/m/m.h @@ -57,6 +57,7 @@ namespace ircd::m #include "users.h" #include "rooms.h" #include "rooms_summary.h" +#include "membership.h" #include "filter.h" #include "events.h" #include "node.h" diff --git a/include/ircd/m/membership.h b/include/ircd/m/membership.h new file mode 100644 index 000000000..01bbcbdfa --- /dev/null +++ b/include/ircd/m/membership.h @@ -0,0 +1,32 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2019 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_M_MEMBERSHIP_H + +namespace ircd::m +{ + // Extract membership string from event data. + string_view membership(const event &); + + // Query and copy membership string to buffer. Note that the event type + // is not checked here, only content.membership is sought. + string_view membership(const mutable_buffer &out, const event::idx &); + + // Query and compare membership string to argument string. Returns + // true on equal; false on not equal; false on not found. + bool membership(const event::idx &, const string_view &); + + // Query and copy membership string to buffer; queries room state. (also room.h) + string_view membership(const mutable_buffer &out, const room &, const id::user &); + + // Query and compare membership string; queries room state. (also room.h) + bool membership(const room &, const id::user &, const string_view &); +} diff --git a/include/ircd/m/room/members.h b/include/ircd/m/room/members.h index 1ab2e2c31..f60ae18ec 100644 --- a/include/ircd/m/room/members.h +++ b/include/ircd/m/room/members.h @@ -21,9 +21,6 @@ struct ircd::m::room::members using closure_idx = std::function; using closure = std::function; - static string_view membership(const mutable_buffer &out, const event::idx &); - static bool membership(const event::idx &, const string_view &); - m::room room; bool for_each_join_present(const string_view &host, const closure &) const; diff --git a/ircd/m.cc b/ircd/m.cc index 4997b8bb7..b97c92dba 100644 --- a/ircd/m.cc +++ b/ircd/m.cc @@ -2318,6 +2318,106 @@ ircd::m::event_filter::event_filter(const mutable_buffer &buf, { } +/////////////////////////////////////////////////////////////////////////////// +// +// m/membership.h +// + +bool +ircd::m::membership(const room &room, + const user::id &user_id, + const string_view &membership) +{ + const room::state state + { + room + }; + + const auto event_idx + { + state.get(std::nothrow, "m.room.member", user_id) + }; + + return m::membership(event_idx, membership); +} + +ircd::string_view +ircd::m::membership(const mutable_buffer &out, + const room &room, + const user::id &user_id) +{ + const room::state state + { + room + }; + + const auto event_idx + { + state.get(std::nothrow, "m.room.member", user_id) + }; + + return m::membership(out, event_idx); +} + +bool +ircd::m::membership(const event::idx &event_idx, + const string_view &membership) +{ + return m::query(std::nothrow, event_idx, "content", [&membership] + (const json::object &content) + { + const json::string &content_membership + { + content["membership"] + }; + + return content_membership == membership; + }); +} + +ircd::string_view +ircd::m::membership(const mutable_buffer &out, + const event::idx &event_idx) +{ + return m::query(std::nothrow, event_idx, "content", [&out] + (const json::object &content) -> string_view + { + const json::string &content_membership + { + content["membership"] + }; + + return strlcpy + { + out, content_membership + }; + }); +} + +ircd::string_view +ircd::m::membership(const event &event) +{ + const json::object &content + { + json::get<"content"_>(event) + }; + + const string_view &membership + { + json::get<"membership"_>(event) + }; + + if(membership) + return membership; + + const json::string &content_membership + { + content.get("membership") + }; + + return content_membership; +} + /////////////////////////////////////////////////////////////////////////////// // // m/user.h diff --git a/ircd/m_event.cc b/ircd/m_event.cc index c0b45a1d7..7db5dff52 100644 --- a/ircd/m_event.cc +++ b/ircd/m_event.cc @@ -3099,6 +3099,25 @@ ircd::m::before(const event &a, return prev.prev_events_has(a.event_id); } +size_t +ircd::m::degree(const event &event) +{ + return degree(event::prev{event}); +} + +size_t +ircd::m::degree(const event::prev &prev) +{ + size_t ret{0}; + json::for_each(prev, [&ret] + (const auto &, const json::array &prevs) + { + ret += prevs.count(); + }); + + return ret; +} + bool ircd::m::operator>=(const event &a, const event &b) { @@ -3186,49 +3205,6 @@ ircd::m::exists(const id::event &event_id) return bool(event_id) && has(column, event_id); } -ircd::string_view -ircd::m::membership(const event &event) -{ - const json::object &content - { - json::get<"content"_>(event) - }; - - const string_view &membership - { - json::get<"membership"_>(event) - }; - - if(membership) - return membership; - - const json::string &content_membership - { - content.get("membership") - }; - - return content_membership; -} - -size_t -ircd::m::degree(const event &event) -{ - return degree(event::prev{event}); -} - -size_t -ircd::m::degree(const event::prev &prev) -{ - size_t ret{0}; - json::for_each(prev, [&ret] - (const auto &, const json::array &prevs) - { - ret += prevs.count(); - }); - - return ret; -} - bool ircd::m::my(const event &event) { diff --git a/ircd/m_room.cc b/ircd/m_room.cc index 1094929e6..619dc9a76 100644 --- a/ircd/m_room.cc +++ b/ircd/m_room.cc @@ -891,24 +891,6 @@ ircd::m::any_user(const room &room, return ret; } -ircd::string_view -ircd::m::membership(const mutable_buffer &out, - const room &room, - const user::id &user_id) -{ - const room::state state - { - room - }; - - const auto event_idx - { - state.get(std::nothrow, "m.room.member", user_id) - }; - - return room::members::membership(out, event_idx); -} - /// Receive the join_rule of the room into buffer of sufficient size. /// The protocol does not specify a join_rule string longer than 7 /// characters but do be considerate of the future. This function @@ -1161,24 +1143,6 @@ ircd::m::join_rule(const room &room, return join_rule(buf, room) == rule; } -bool -ircd::m::membership(const room &room, - const user::id &user_id, - const string_view &membership) -{ - const room::state state - { - room - }; - - const auto event_idx - { - state.get(std::nothrow, "m.room.member", user_id) - }; - - return room::members::membership(event_idx, membership); -} - bool ircd::m::creator(const room::id &room_id, const user::id &user_id) @@ -3188,7 +3152,7 @@ const if(host && user_id.host() != host) return true; - return !membership || this->membership(event_idx, membership)? + return !membership || m::membership(event_idx, membership)? closure(user_id, event_idx): true; }); @@ -3232,41 +3196,6 @@ const return true; } -bool -ircd::m::room::members::membership(const event::idx &event_idx, - const string_view &membership) -{ - return m::query(std::nothrow, event_idx, "content", [&membership] - (const json::object &content) - { - const json::string &content_membership - { - content["membership"] - }; - - return content_membership && content_membership == membership; - }); -} - -ircd::string_view -ircd::m::room::members::membership(const mutable_buffer &out, - const event::idx &event_idx) -{ - return m::query(std::nothrow, event_idx, "content", [&out] - (const json::object &content) -> string_view - { - const json::string &content_membership - { - content["membership"] - }; - - return strlcpy - { - out, content_membership - }; - }); -} - // // room::origins // diff --git a/modules/client/rooms/members.cc b/modules/client/rooms/members.cc index 5d70f35f6..127a719ea 100644 --- a/modules/client/rooms/members.cc +++ b/modules/client/rooms/members.cc @@ -102,7 +102,7 @@ get__members(client &client, members.for_each(membership, [¬_membership] (const m::user::id &member, const m::event::idx &event_idx) { - if(m::room::members::membership(event_idx, not_membership)) + if(m::membership(event_idx, not_membership)) return true; m::prefetch(event_idx); @@ -112,7 +112,7 @@ get__members(client &client, members.for_each(membership, [&request, &chunk, ¬_membership] (const m::user::id &member, const m::event::idx &event_idx) { - if(m::room::members::membership(event_idx, not_membership)) + if(m::membership(event_idx, not_membership)) return true; const m::event::fetch event diff --git a/modules/console.cc b/modules/console.cc index 18bf348f0..7fc5fb030 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -8701,7 +8701,7 @@ console_cmd__room__members(opt &out, const string_view &line) (const m::user::id &user_id, const m::event::idx &event_idx) { char buf[32]; - out << std::setw(8) << std::left << m::room::members::membership(buf, event_idx) + out << std::setw(8) << std::left << m::membership(buf, event_idx) << " " << user_id << std::endl; return true; diff --git a/modules/m_room_bootstrap.cc b/modules/m_room_bootstrap.cc index 7b6b6de3c..9008fe266 100644 --- a/modules/m_room_bootstrap.cc +++ b/modules/m_room_bootstrap.cc @@ -108,8 +108,7 @@ ircd::m::room::bootstrap::bootstrap(m::event::id::buf &event_id_buf, const bool existing_join { - member_event_idx && - m::room::members::membership(member_event_idx, "join") + m::membership(member_event_idx, "join") }; if(existing_join)