diff --git a/matrix/matrix.cc b/matrix/matrix.cc index 40bff8e92..8b72c75bd 100644 --- a/matrix/matrix.cc +++ b/matrix/matrix.cc @@ -132,8 +132,7 @@ ircd::m::module_names "client_keys_query", "client_keys_signatures_upload", "client_keys_device_signing_upload", - "client_room_keys_version", - "client_room_keys_keys", + "client_room_keys", "client_presence", "client_groups", "client_joined_groups", diff --git a/modules/Makefile.am b/modules/Makefile.am index e43d33218..00d80857b 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -506,12 +506,14 @@ client_module_LTLIBRARIES += \ # client/room_keys/ # -client_client_room_keys_version_la_SOURCES = client/room_keys/version.cc -client_client_room_keys_keys_la_SOURCES = client/room_keys/keys.cc +client_client_room_keys_la_SOURCES = \ + client/room_keys/keys.cc \ + client/room_keys/version.cc \ + client/room_keys/room_keys.cc \ + ### client_module_LTLIBRARIES += \ - client/client_room_keys_version.la \ - client/client_room_keys_keys.la \ + client/client_room_keys.la \ ### # diff --git a/modules/client/room_keys/keys.cc b/modules/client/room_keys/keys.cc index bb5a9fe5e..e975c2086 100644 --- a/modules/client/room_keys/keys.cc +++ b/modules/client/room_keys/keys.cc @@ -8,11 +8,10 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. +#include "room_keys.h" + namespace ircd::m { - static string_view make_state_key(const mutable_buffer &, const string_view &, const string_view &, const event::idx &); - static std::tuple unmake_state_key(const string_view &); - static resource::response _get_room_keys_keys(client &, const resource::request &, const room::state &, const event::idx &, const string_view &, const string_view &); static void _get_room_keys_keys(client &, const resource::request &, const room::state &, const event::idx &, const string_view &, json::stack::object &); static resource::response get_room_keys_keys(client &, const resource::request &); @@ -30,12 +29,6 @@ namespace ircd::m extern resource room_keys_keys; } -ircd::mapi::header -IRCD_MODULE -{ - "Client (undocumented) :e2e Room Keys Keys" -}; - decltype(ircd::m::room_keys_keys) ircd::m::room_keys_keys { @@ -501,45 +494,36 @@ ircd::m::_get_room_keys_keys(client &client, return {}; // responded from closure or thrown } -std::tuple -ircd::m::unmake_state_key(const string_view &state_key) +std::tuple +ircd::m::count_etag(const room::state &state, + const event::idx &version) { - assert(state_key); - string_view part[3]; - const auto parts + char version_buf[64]; + const auto version_str { - tokens(state_key, ":::", part) + lex_cast(version) }; - assert(parts == 3); - if(unlikely(!m::valid(id::ROOM, part[0]))) - part[0] = {}; - - if(unlikely(!lex_castable(part[2]))) - part[2] = {}; - - return std::make_tuple - ( - part[0], part[1], part[2] - ); -} - -ircd::string_view -ircd::m::make_state_key(const mutable_buffer &buf, - const string_view &room_id, - const string_view &session_id, - const event::idx &version) -{ - assert(room_id); - assert(m::valid(id::ROOM, room_id)); - assert(session_id); - assert(session_id != "sessions"); - assert(version != 0); - return fmt::sprintf + uint64_t count(0), etag(0); + state.for_each("ircd.room_keys.key", [&] + (const string_view &type, const string_view &state_key, const event::idx &event_idx) { - buf, "%s:::%s:::%u", - room_id, - session_id, - version, + const auto &[room_id, session_id, _version_str] + { + unmake_state_key(state_key) + }; + + if(_version_str != version_str) + return true; + + etag += event_idx; + count += 1; + return true; + }); + + return + { + int64_t(count), + int64_t(etag), }; } diff --git a/modules/client/room_keys/room_keys.cc b/modules/client/room_keys/room_keys.cc new file mode 100644 index 000000000..a44f2e0fa --- /dev/null +++ b/modules/client/room_keys/room_keys.cc @@ -0,0 +1,60 @@ +// The Construct +// +// Copyright (C) The Construct Developers, Authors & Contributors +// Copyright (C) 2016-2023 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. + +#include "room_keys.h" + +ircd::mapi::header +IRCD_MODULE +{ + "Client :e2e Room Keys" +}; + +std::tuple +ircd::m::unmake_state_key(const string_view &state_key) +{ + assert(state_key); + string_view part[3]; + const auto parts + { + tokens(state_key, ":::", part) + }; + + assert(parts == 3); + if(unlikely(!m::valid(id::ROOM, part[0]))) + part[0] = {}; + + if(unlikely(!lex_castable(part[2]))) + part[2] = {}; + + return std::make_tuple + ( + part[0], part[1], part[2] + ); +} + +ircd::string_view +ircd::m::make_state_key(const mutable_buffer &buf, + const string_view &room_id, + const string_view &session_id, + const event::idx &version) +{ + assert(room_id); + assert(m::valid(id::ROOM, room_id)); + assert(session_id); + assert(session_id != "sessions"); + assert(version != 0); + return fmt::sprintf + { + buf, "%s:::%s:::%u", + room_id, + session_id, + version, + }; +} diff --git a/modules/client/room_keys/room_keys.h b/modules/client/room_keys/room_keys.h new file mode 100644 index 000000000..7b974ff60 --- /dev/null +++ b/modules/client/room_keys/room_keys.h @@ -0,0 +1,17 @@ +// The Construct +// +// Copyright (C) The Construct Developers, Authors & Contributors +// Copyright (C) 2016-2023 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 GCC visibility push(hidden) +namespace ircd::m +{ + string_view make_state_key(const mutable_buffer &, const string_view &, const string_view &, const event::idx &); + std::tuple unmake_state_key(const string_view &); +} +#pragma GCC visibility pop diff --git a/modules/client/room_keys/version.cc b/modules/client/room_keys/version.cc index 60c619405..d66fa279c 100644 --- a/modules/client/room_keys/version.cc +++ b/modules/client/room_keys/version.cc @@ -8,6 +8,8 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. +#include "room_keys.h" + namespace ircd::m { static resource::response get_room_keys_version(client &, const resource::request &); @@ -25,12 +27,6 @@ namespace ircd::m extern resource room_keys_version; } -ircd::mapi::header -IRCD_MODULE -{ - "Client (undocumented) :e2e Room Keys Version" -}; - decltype(ircd::m::room_keys_version) ircd::m::room_keys_version {