summary refs log tree commit diff
path: root/packages/overlays/matrix-synapse/patches/0018-admin-api-send-more-data.patch
blob: 08f135762c29f6665e69e2409d07adb25a9b27bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
From 3196bce10b7842aa435036105d525e0f10153ad9 Mon Sep 17 00:00:00 2001
From: Rory& <root@rory.gay>
Date: Mon, 27 Oct 2025 19:23:42 +0100
Subject: [PATCH 18/25] admin api - send more data

Signed-off-by: Rory& <root@rory.gay>
---
 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