diff --git a/modules/client/room_keys/keys.cc b/modules/client/room_keys/keys.cc index e975c2086..03f2a59af 100644 --- a/modules/client/room_keys/keys.cc +++ b/modules/client/room_keys/keys.cc @@ -127,9 +127,23 @@ ircd::m::delete_room_keys_keys(client &client, } else delete_room_keys_key(client, request, user_room, room_id, session_id, version); + const auto &[count, etag] + { + count_etag(state, version) + }; + + const json::value _etag + { + lex_cast(etag), json::STRING + }; + return resource::response { - client, http::OK + client, json::members + { + { "count", count }, + { "etag", _etag }, + } }; } @@ -228,6 +242,16 @@ ircd::m::put_room_keys_keys(client &client, request.query.at("version") }; + const m::user::room user_room + { + request.user_id + }; + + const m::room::state state + { + user_room + }; + if(!room_id && !session_id) { const json::object &rooms @@ -258,9 +282,23 @@ ircd::m::put_room_keys_keys(client &client, } else put_room_keys_keys_key(client, request, room_id, session_id, version, request); + const auto &[count, etag] + { + count_etag(state, version) + }; + + const json::value _etag + { + lex_cast(etag), json::STRING + }; + return resource::response { - client, http::OK + client, json::members + { + { "count", count }, + { "etag", _etag }, + } }; } @@ -493,37 +531,3 @@ ircd::m::_get_room_keys_keys(client &client, return {}; // responded from closure or thrown } - -std::tuple -ircd::m::count_etag(const room::state &state, - const event::idx &version) -{ - char version_buf[64]; - const auto version_str - { - lex_cast(version) - }; - - 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) - { - 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 index a44f2e0fa..c811a9906 100644 --- a/modules/client/room_keys/room_keys.cc +++ b/modules/client/room_keys/room_keys.cc @@ -16,6 +16,40 @@ IRCD_MODULE "Client :e2e Room Keys" }; +std::tuple +ircd::m::count_etag(const room::state &state, + const event::idx &version) +{ + char version_buf[64]; + const auto version_str + { + lex_cast(version, version_buf) + }; + + 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) + { + 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), + }; +} + std::tuple ircd::m::unmake_state_key(const string_view &state_key) { diff --git a/modules/client/room_keys/room_keys.h b/modules/client/room_keys/room_keys.h index 7b974ff60..8e8949e03 100644 --- a/modules/client/room_keys/room_keys.h +++ b/modules/client/room_keys/room_keys.h @@ -13,5 +13,6 @@ 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 &); + std::tuple count_etag(const room::state &, const event::idx &version); } #pragma GCC visibility pop diff --git a/modules/client/room_keys/version.cc b/modules/client/room_keys/version.cc index d66fa279c..9d5517917 100644 --- a/modules/client/room_keys/version.cc +++ b/modules/client/room_keys/version.cc @@ -327,9 +327,24 @@ ircd::m::get_room_keys_version(client &client, event_idx }; - m::get(version_idx, "content", [&client, &event_idx] + const m::room::state state + { + user_room + }; + + m::get(version_idx, "content", [&client, &event_idx, &state] (const json::object &content) { + const auto &[count, etag] + { + count_etag(state, event_idx) + }; + + const json::value _etag + { + lex_cast(etag), json::STRING + }; + const json::value version { lex_cast(event_idx), json::STRING @@ -341,6 +356,8 @@ ircd::m::get_room_keys_version(client &client, { { "algorithm", content["algorithm"] }, { "auth_data", content["auth_data"] }, + { "count", count }, + { "etag", _etag }, { "version", version }, } };