diff --git a/include/ircd/m/filter.h b/include/ircd/m/filter.h index 8fad0814b..485b11008 100644 --- a/include/ircd/m/filter.h +++ b/include/ircd/m/filter.h @@ -77,14 +77,16 @@ struct ircd::m::filter json::property > { - static room filters; - using super_type::tuple; using super_type::operator=; - static size_t size(const string_view &filter_id); + using closure = std::function; - filter(const string_view &filter_id, const mutable_buffer &); + static bool get(std::nothrow_t, const user &, const string_view &filter_id, const closure &); + static void get(const user &, const string_view &filter_id, const closure &); + static string_view set(const mutable_buffer &id, const user &, const json::object &filter); + + filter(const user &, const string_view &filter_id, const mutable_buffer &); }; #pragma GCC diagnostic pop diff --git a/ircd/m/filter.cc b/ircd/m/filter.cc index 39dabebb0..8d5c9eb52 100644 --- a/ircd/m/filter.cc +++ b/ircd/m/filter.cc @@ -10,46 +10,93 @@ #include -const ircd::m::room::id::buf -filters_room_id -{ - "filters", ircd::my_host() -}; - -ircd::m::room -ircd::m::filter::filters -{ - filters_room_id -}; - -ircd::m::filter::filter(const string_view &filter_id, +ircd::m::filter::filter(const user &user, + const string_view &filter_id, const mutable_buffer &buf) { - size_t len{0}; - //TODO: really has to become event in user's room - filters.get("ircd.filter"_sv, filter_id, [&buf, &len] - (const m::event &event) + get(user, filter_id, [this, &buf] + (const json::object &filter) { - len = copy(buf, json::get<"content"_>(event)); - }); - - new (this) filter{json::object{buf}}; -} - -size_t -ircd::m::filter::size(const string_view &filter_id) -{ - size_t len{0}; - filters.get("ircd.filter"_sv, filter_id, [&len] - (const m::event &event) - { - const string_view filter + const size_t len { - json::get<"content"_>(event) + copy(buf, string_view{filter}) }; - len = size(filter); + new (this) m::filter + { + json::object + { + data(buf), len + } + }; + }); +} + +ircd::string_view +ircd::m::filter::set(const mutable_buffer &idbuf, + const user &user, + const json::object &filter) +{ + const auto user_room_id + { + user.room_id() + }; + + const m::room room + { + user_room_id + }; + + const sha256::buf hash + { + sha256{filter} + }; + + const string_view filter_id + { + b64encode_unpadded(idbuf, hash) + }; + + send(room, user.user_id, "ircd.filter", filter_id, filter); + return filter_id; +} + +void +ircd::m::filter::get(const user &user, + const string_view &filter_id, + const closure &closure) +{ + if(!get(std::nothrow, user, filter_id, closure)) + throw m::NOT_FOUND + { + "Filter not found" + }; +} + +bool +ircd::m::filter::get(std::nothrow_t, + const user &user, + const string_view &filter_id, + const closure &closure) +{ + const auto user_room_id + { + user.room_id() + }; + + const m::room room + { + user_room_id + }; + + return room.get(std::nothrow, "ircd.filter", filter_id, [&closure] + (const m::event &event) + { + const json::object &content + { + at<"content"_>(event) + }; + + closure(content); }); - - return len; } diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 0bf70b6fd..b8d19f1c5 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -231,12 +231,6 @@ ircd::m::init::bootstrap() { "name", "User Tokens" } }); - create(filter::filters, me.user_id); - send(filter::filters, me.user_id, "m.room.name", "", - { - { "name", "User Filters Database" } - }); - _keys.bootstrap(); message(control, me.user_id, "Welcome to the control room."); diff --git a/modules/client/user.cc b/modules/client/user.cc index d9a43286d..e0ae4c402 100644 --- a/modules/client/user.cc +++ b/modules/client/user.cc @@ -24,27 +24,23 @@ get_filter(client &client, const resource::request &request, const m::user::id &user_id) { - m::event::id::buf filter_id + char filter_id_buf[64]; + const auto filter_id { - url::decode(request.parv[2], filter_id) + url::decode(request.parv[2], filter_id_buf) }; - //TODO: ?? - const unique_buffer buffer + m::filter::get(user_id, filter_id, [&client] + (const json::object &filter) { - m::filter::size(filter_id) - }; + resource::response + { + client, filter + }; + }); - //TODO: get direct - const m::filter filter - { - filter_id, buffer - }; - - return resource::response - { - client, json::object{buffer} - }; + // Responded from closure. + return {}; } // (5.2) Uploads a new filter definition to the homeserver. Returns a filter ID that @@ -105,9 +101,10 @@ post_filter(client &client, json::get<"presence"_>(request) }; + char filter_id_buf[64]; const auto filter_id { - send(m::filter::filters, user_id, "ircd.filter"_sv, request.body) + m::filter::set(filter_id_buf, user_id, request.body) }; return resource::response