From 3196bce10b7842aa435036105d525e0f10153ad9 Mon Sep 17 00:00:00 2001 From: Rory& Date: Mon, 27 Oct 2025 19:23:42 +0100 Subject: [PATCH 18/25] admin api - send more data Signed-off-by: Rory& --- synapse/rest/admin/rooms.py | 14 ++++- synapse/rest/client/capabilities.py | 3 ++ synapse/storage/databases/main/room.py | 71 ++++++++++++++++++-------- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/synapse/rest/admin/rooms.py b/synapse/rest/admin/rooms.py index e97d076a44..09a8a01f77 100644 --- a/synapse/rest/admin/rooms.py +++ b/synapse/rest/admin/rooms.py @@ -302,7 +302,15 @@ class ListRoomRestServlet(RestServlet): reverse_order = True if direction == Direction.BACKWARDS else False emma_include_tombstone = parse_boolean( - request, "emma_include_tombstone", default=False + request, "gay.rory.synapse_admin_extensions.include_tombstone", default=parse_boolean( + request, "emma_include_tombstone", default=False + ) + ) + emma_include_topic = parse_boolean( + request, "gay.rory.synapse_admin_extensions.include_topic", default=False + ) + emma_include_create_evt = parse_boolean( + request, "gay.rory.synapse_admin_extensions.include_create_event", default=False ) # Return list of rooms according to parameters @@ -314,7 +322,9 @@ class ListRoomRestServlet(RestServlet): search_term, public_rooms, empty_rooms, - emma_include_tombstone = emma_include_tombstone + emma_include_tombstone, + emma_include_topic, + emma_include_create_evt ) response = { diff --git a/synapse/rest/client/capabilities.py b/synapse/rest/client/capabilities.py index 075c3de261..c0b0a9923f 100644 --- a/synapse/rest/client/capabilities.py +++ b/synapse/rest/client/capabilities.py @@ -76,6 +76,9 @@ class CapabilitiesRestServlet(RestServlet): }, "gay.rory.bulk_send_events": { "enabled": True + }, + "gay.rory.synapse_admin_extensions.room_list.query_events.v2": { + "enabled": True } } } diff --git a/synapse/storage/databases/main/room.py b/synapse/storage/databases/main/room.py index 7623208c28..08735e1fbb 100644 --- a/synapse/storage/databases/main/room.py +++ b/synapse/storage/databases/main/room.py @@ -606,6 +606,8 @@ class RoomWorkerStore(CacheInvalidationWorkerStore): public_rooms: bool | None, empty_rooms: bool | None, emma_include_tombstone: bool = False, + emma_include_topic: bool = False, + emma_include_create_evt: bool = False, ) -> tuple[list[dict[str, Any]], int]: """Function to retrieve a paginated list of rooms as json. @@ -626,10 +628,13 @@ class RoomWorkerStore(CacheInvalidationWorkerStore): if false, empty rooms are excluded from the query. When it is none (the default), both empty rooms and none-empty rooms are queried. emma_include_tombstone: If true, include tombstone events in the results. + emma_include_topic: If true, include topic events in the results. + emma_include_create_evt: If true, include create events in the results. Returns: A list of room dicts and an integer representing the total number of rooms that exist given this query """ + uses_emma_features = emma_include_tombstone or emma_include_topic or emma_include_create_evt # Filter room names by a string filter_ = [] where_args = [] @@ -799,35 +804,61 @@ class RoomWorkerStore(CacheInvalidationWorkerStore): _get_rooms_paginate_txn, ) - if emma_include_tombstone: + if uses_emma_features: room_id_sql, room_id_args = make_in_list_sql_clause( self.database_engine, "cse.room_id", [r["room_id"] for r in result[0]] ) - tombstone_sql = """ + current_state_evt_sql = """ SELECT cse.room_id, cse.event_id, ej.json - FROM current_state_events cse - JOIN event_json ej USING (event_id) - WHERE cse.type = 'm.room.tombstone' - AND {room_id_sql} + FROM current_state_events cse + JOIN event_json ej USING (event_id) + WHERE cse.type = '{type_sql}' + AND {room_id_sql} """.format( + type_sql="{type_sql}", room_id_sql=room_id_sql ) - def _get_tombstones_txn( - txn: LoggingTransaction, - ) -> Dict[str, JsonDict]: - txn.execute(tombstone_sql, room_id_args) - for room_id, event_id, json in txn: - for result_room in result[0]: - if result_room["room_id"] == room_id: - result_room["gay.rory.synapse_admin_extensions.tombstone"] = db_to_json(json) - break - return result[0], result[1] - - result = await self.db_pool.runInteraction( - "get_rooms_tombstones", _get_tombstones_txn, - ) + async def include_current_state_txn( + desc: str, event_type: str, result_key: str + ): + def _include_current_state_txn( + txn: LoggingTransaction, + ) -> dict[str, JsonDict]: + sql = current_state_evt_sql.format(type_sql=event_type) + logger.warning("emma %s: sql=%s args=%s", desc, sql, room_id_args) + txn.execute(sql, room_id_args) + current_state_matches = 0 + # logger.warning("emma %s: result=%s", desc, txn.fetchall()) + for room_id, event_id, json in txn: + for result_room in result[0]: + if result_room["room_id"] == room_id: + result_room[result_key] = db_to_json(json) + logger.warning("emma _include_current_state_txn[%s] %s/%s -> %s", current_state_matches, room_id, event_type, event_id) + current_state_matches += 1 + break + return result[0], result[1] + + return await self.db_pool.runInteraction( + desc, _include_current_state_txn, + ) + + if emma_include_tombstone: + result = await include_current_state_txn( + "get_rooms_tombstones", EventTypes.Tombstone, "gay.rory.synapse_admin_extensions.tombstone" + ) + logger.warning("emma_include_tombstone result: %s", result) + if emma_include_topic: + result = await include_current_state_txn( + "get_rooms_topics", EventTypes.Topic, "gay.rory.synapse_admin_extensions.room_topic" + ) + logger.warning("emma_include_topic result: %s", result) + if emma_include_create_evt: + result = await include_current_state_txn( + "get_rooms_create_evts", EventTypes.Create, "gay.rory.synapse_admin_extensions.create_event" + ) + logger.warning("emma_include_create_evt result: %s", result) return result -- 2.53.0