From cd78f802b22dd0feb1e841f5826b1f297c9980dc Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 2 Mar 2023 21:15:54 -0800 Subject: [PATCH] modules/admin/rooms: Implement GET /forward_extremities. --- matrix/matrix.cc | 1 + modules/Makefile.am | 2 + modules/admin/rooms.cc | 131 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 modules/admin/rooms.cc diff --git a/matrix/matrix.cc b/matrix/matrix.cc index 75328aaf4..67d259a49 100644 --- a/matrix/matrix.cc +++ b/matrix/matrix.cc @@ -183,6 +183,7 @@ ircd::m::module_names "admin_deactivate", "admin_server_version", "admin_federation", + "admin_rooms", }; /// This is a list of modules that are considered "optional" and any loading diff --git a/modules/Makefile.am b/modules/Makefile.am index b8e14cb70..bbd8ac92f 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -562,12 +562,14 @@ admin_admin_users_la_SOURCES = admin/users.cc admin_admin_deactivate_la_SOURCES = admin/deactivate.cc admin_admin_server_version_la_SOURCES = admin/server_version.cc admin_admin_federation_la_SOURCES = admin/federation.cc +admin_admin_rooms_la_SOURCES = admin/rooms.cc admin_module_LTLIBRARIES = \ admin/admin_users.la \ admin/admin_deactivate.la \ admin/admin_server_version.la \ admin/admin_federation.la \ + admin/admin_rooms.la \ ### ############################################################################### diff --git a/modules/admin/rooms.cc b/modules/admin/rooms.cc new file mode 100644 index 000000000..a1759a363 --- /dev/null +++ b/modules/admin/rooms.cc @@ -0,0 +1,131 @@ +// The Construct +// +// Copyright (C) The Construct Developers, Authors & Contributors +// Copyright (C) 2016-2020 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. + +namespace ircd::m::admin +{ + static resource::response handle_get_forward_extremis(client &, const resource::request &, const room::id &); + static resource::response handle_get(client &, const resource::request &); + + extern resource::method get_method; + extern resource rooms_resource; +}; + +ircd::mapi::header +IRCD_MODULE +{ + "Admin (undocumented) :Rooms" +}; + +decltype(ircd::m::admin::rooms_resource) +ircd::m::admin::rooms_resource +{ + "/_synapse/admin/v1/rooms/", + { + "(undocumented) Admin Rooms", + resource::DIRECTORY + } +}; + +decltype(ircd::m::admin::get_method) +ircd::m::admin::get_method +{ + rooms_resource, "GET", handle_get, + { + get_method.REQUIRES_OPER + } +}; + +ircd::m::resource::response +ircd::m::admin::handle_get(client &client, + const resource::request &request) +{ + char buf[768]; + const string_view &room_id_or_alias + { + request.parv[0]? + url::decode(buf, request.parv[0]): + string_view{} + }; + + const m::room::id::buf room_id + { + room_id_or_alias? + m::room_id(room_id_or_alias): + m::room::id::buf{} + }; + + const auto &cmd + { + request.parv[1] + }; + + if(cmd == "forward_extremities") + return handle_get_forward_extremis(client, request, room_id); + + throw m::NOT_FOUND + { + "/admin/rooms command not found" + }; +} + +ircd::m::resource::response +ircd::m::admin::handle_get_forward_extremis(client &client, + const resource::request &request, + const room::id &room_id) +{ + const m::room::head room_head + { + room_id + }; + + m::resource::response::chunked::json response + { + client, http::OK + }; + + json::stack::member + { + response, "count", json::value + { + long(room_head.count()) + } + }; + + json::stack::array results + { + response, "results" + }; + + room_head.for_each([&results] + (const auto &event_idx, const auto &event_id) + { + json::stack::object result + { + results + }; + + json::stack::member + { + result, "event_id", event_id + }; + + json::stack::member + { + result, "depth", json::value + { + m::get(std::nothrow, event_idx, "depth", 0L) + } + }; + + return true; + }); + + return response; +}