19
19
from enum import Enum
20
20
from typing import Any , Dict , List , Optional , Tuple
21
21
22
- from synapse .api .constants import EventTypes , JoinRules
22
+ from synapse .api .constants import EventContentFields , EventTypes , JoinRules
23
23
from synapse .api .errors import StoreError
24
24
from synapse .api .room_versions import RoomVersion , RoomVersions
25
25
from synapse .events import EventBase
@@ -1012,9 +1012,9 @@ def get_rooms_for_retention_period_in_range_txn(txn):
1012
1012
class _BackgroundUpdates :
1013
1013
REMOVE_TOMESTONED_ROOMS_BG_UPDATE = "remove_tombstoned_rooms_from_directory"
1014
1014
ADD_ROOMS_ROOM_VERSION_COLUMN = "add_rooms_room_version_column"
1015
- POPULATE_ROOMS_CREATOR_COLUMN = "populate_rooms_creator_column"
1016
1015
POPULATE_ROOM_DEPTH_MIN_DEPTH2 = "populate_room_depth_min_depth2"
1017
1016
REPLACE_ROOM_DEPTH_MIN_DEPTH = "replace_room_depth_min_depth"
1017
+ POPULATE_ROOMS_CREATOR_COLUMN = "populate_rooms_creator_column"
1018
1018
1019
1019
1020
1020
_REPLACE_ROOM_DEPTH_SQL_COMMANDS = (
@@ -1046,11 +1046,6 @@ def __init__(self, database: DatabasePool, db_conn, hs):
1046
1046
self ._background_add_rooms_room_version_column ,
1047
1047
)
1048
1048
1049
- self .db_pool .updates .register_background_update_handler (
1050
- _BackgroundUpdates .POPULATE_ROOMS_CREATOR_COLUMN ,
1051
- self ._background_populate_rooms_creator_column ,
1052
- )
1053
-
1054
1049
# BG updates to change the type of room_depth.min_depth
1055
1050
self .db_pool .updates .register_background_update_handler (
1056
1051
_BackgroundUpdates .POPULATE_ROOM_DEPTH_MIN_DEPTH2 ,
@@ -1061,6 +1056,11 @@ def __init__(self, database: DatabasePool, db_conn, hs):
1061
1056
self ._background_replace_room_depth_min_depth ,
1062
1057
)
1063
1058
1059
+ self .db_pool .updates .register_background_update_handler (
1060
+ _BackgroundUpdates .POPULATE_ROOMS_CREATOR_COLUMN ,
1061
+ self ._background_populate_rooms_creator_column ,
1062
+ )
1063
+
1064
1064
async def _background_insert_retention (self , progress , batch_size ):
1065
1065
"""Retrieves a list of all rooms within a range and inserts an entry for each of
1066
1066
them into the room_retention table.
@@ -1198,63 +1198,6 @@ def _background_add_rooms_room_version_column_txn(txn: LoggingTransaction):
1198
1198
1199
1199
return batch_size
1200
1200
1201
- async def _background_populate_rooms_creator_column (
1202
- self , progress : dict , batch_size : int
1203
- ):
1204
- """Background update to go and add creator information to `rooms`
1205
- table from `current_state_events` table.
1206
- """
1207
-
1208
- last_room_id = progress .get ("room_id" , "" )
1209
-
1210
- def _background_populate_rooms_creator_column_txn (txn : LoggingTransaction ):
1211
- sql = """
1212
- SELECT room_id, json FROM current_state_events
1213
- INNER JOIN event_json USING (room_id, event_id)
1214
- WHERE room_id > ? AND type = 'm.room.create' AND state_key = ''
1215
- ORDER BY room_id
1216
- LIMIT ?
1217
- """
1218
-
1219
- txn .execute (sql , (last_room_id , batch_size ))
1220
-
1221
- new_last_room_id = ""
1222
- for room_id , event_json in txn :
1223
- event_dict = db_to_json (event_json )
1224
-
1225
- creator = event_dict .get ("content" ).get ("creator" )
1226
-
1227
- self .db_pool .simple_update_txn (
1228
- txn ,
1229
- table = "rooms" ,
1230
- keyvalues = {"room_id" : room_id },
1231
- updatevalues = {"creator" : creator },
1232
- )
1233
- new_last_room_id = room_id
1234
-
1235
- if new_last_room_id == "" :
1236
- return True
1237
-
1238
- self .db_pool .updates ._background_update_progress_txn (
1239
- txn ,
1240
- _BackgroundUpdates .POPULATE_ROOMS_CREATOR_COLUMN ,
1241
- {"room_id" : new_last_room_id },
1242
- )
1243
-
1244
- return False
1245
-
1246
- end = await self .db_pool .runInteraction (
1247
- "_background_populate_rooms_creator_column" ,
1248
- _background_populate_rooms_creator_column_txn ,
1249
- )
1250
-
1251
- if end :
1252
- await self .db_pool .updates ._end_background_update (
1253
- _BackgroundUpdates .POPULATE_ROOMS_CREATOR_COLUMN
1254
- )
1255
-
1256
- return batch_size
1257
-
1258
1201
async def _remove_tombstoned_rooms_from_directory (
1259
1202
self , progress , batch_size
1260
1203
) -> int :
@@ -1407,6 +1350,65 @@ def process(txn: Cursor) -> None:
1407
1350
1408
1351
return 0
1409
1352
1353
+ async def _background_populate_rooms_creator_column (
1354
+ self , progress : dict , batch_size : int
1355
+ ):
1356
+ """Background update to go and add creator information to `rooms`
1357
+ table from `current_state_events` table.
1358
+ """
1359
+
1360
+ last_room_id = progress .get ("room_id" , "" )
1361
+
1362
+ def _background_populate_rooms_creator_column_txn (txn : LoggingTransaction ):
1363
+ sql = """
1364
+ SELECT room_id, json FROM event_json
1365
+ INNER JOIN rooms AS room USING (room_id)
1366
+ INNER JOIN current_state_events AS state_event USING (room_id, event_id)
1367
+ WHERE room_id > ? AND (room.creator IS NULL OR room.creator = '') AND state_event.type = 'm.room.create' AND state_event.state_key = ''
1368
+ ORDER BY room_id
1369
+ LIMIT ?
1370
+ """
1371
+
1372
+ txn .execute (sql , (last_room_id , batch_size ))
1373
+ room_id_to_create_event_results = txn .fetchall ()
1374
+
1375
+ new_last_room_id = ""
1376
+ for room_id , event_json in room_id_to_create_event_results :
1377
+ event_dict = db_to_json (event_json )
1378
+
1379
+ creator = event_dict .get ("content" ).get (EventContentFields .ROOM_CREATOR )
1380
+
1381
+ self .db_pool .simple_update_txn (
1382
+ txn ,
1383
+ table = "rooms" ,
1384
+ keyvalues = {"room_id" : room_id },
1385
+ updatevalues = {"creator" : creator },
1386
+ )
1387
+ new_last_room_id = room_id
1388
+
1389
+ if new_last_room_id == "" :
1390
+ return True
1391
+
1392
+ self .db_pool .updates ._background_update_progress_txn (
1393
+ txn ,
1394
+ _BackgroundUpdates .POPULATE_ROOMS_CREATOR_COLUMN ,
1395
+ {"room_id" : new_last_room_id },
1396
+ )
1397
+
1398
+ return False
1399
+
1400
+ end = await self .db_pool .runInteraction (
1401
+ "_background_populate_rooms_creator_column" ,
1402
+ _background_populate_rooms_creator_column_txn ,
1403
+ )
1404
+
1405
+ if end :
1406
+ await self .db_pool .updates ._end_background_update (
1407
+ _BackgroundUpdates .POPULATE_ROOMS_CREATOR_COLUMN
1408
+ )
1409
+
1410
+ return batch_size
1411
+
1410
1412
1411
1413
class RoomStore (RoomBackgroundUpdateStore , RoomWorkerStore , SearchStore ):
1412
1414
def __init__ (self , database : DatabasePool , db_conn , hs ):
@@ -1438,7 +1440,12 @@ async def upsert_room_on_join(
1438
1440
# invalid, and it would fail auth checks anyway.
1439
1441
raise StoreError (400 , "No create event in state" )
1440
1442
1441
- room_creator = create_event .content .get ("creator" , None )
1443
+ room_creator = create_event .content .get (EventContentFields .ROOM_CREATOR )
1444
+
1445
+ if not isinstance (room_creator , str ):
1446
+ # If the create event does not have a creator then the room is
1447
+ # invalid, and it would fail auth checks anyway.
1448
+ raise StoreError (400 , "No creator defined on the create event" )
1442
1449
1443
1450
await self .db_pool .simple_upsert (
1444
1451
desc = "upsert_room_on_join" ,
@@ -1467,9 +1474,6 @@ async def maybe_store_room_on_outlier_membership(
1467
1474
# mark the room as having an auth chain cover index.
1468
1475
has_auth_chain_index = await self .has_auth_chain_index (room_id )
1469
1476
1470
- create_event = await self .get_create_event_for_room (room_id )
1471
- room_creator = create_event .content .get ("creator" , None )
1472
-
1473
1477
await self .db_pool .simple_upsert (
1474
1478
desc = "maybe_store_room_on_outlier_membership" ,
1475
1479
table = "rooms" ,
@@ -1478,7 +1482,10 @@ async def maybe_store_room_on_outlier_membership(
1478
1482
insertion_values = {
1479
1483
"room_version" : room_version .identifier ,
1480
1484
"is_public" : False ,
1481
- "creator" : room_creator ,
1485
+ # We don't worry about setting the `creator` here because
1486
+ # we don't process any messages in a room while a user is
1487
+ # invited (only after the join).
1488
+ "creator" : "" ,
1482
1489
"has_auth_chain_index" : has_auth_chain_index ,
1483
1490
},
1484
1491
# rooms has a unique constraint on room_id, so no need to lock when doing an
0 commit comments