Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 20196c8

Browse files
committed
Merge branch 'madlittlemods/populate-rooms-creator-field' into madlittlemods/room-creator-allowed-to-msc2716-events
2 parents a3581d3 + 462ab25 commit 20196c8

File tree

3 files changed

+81
-71
lines changed

3 files changed

+81
-71
lines changed

synapse/api/constants.py

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ class EventContentFields:
198198
# cf https://github.com/matrix-org/matrix-doc/pull/1772
199199
ROOM_TYPE = "type"
200200

201+
# The creator of the room, as used in `m.room.create` events.
202+
ROOM_CREATOR = "creator"
203+
201204
# Used on normal messages to indicate they were historically imported after the fact
202205
MSC2716_HISTORICAL = "org.matrix.msc2716.historical"
203206
# For "insertion" events to indicate what the next chunk ID should be in

synapse/storage/databases/main/room.py

+76-69
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from enum import Enum
2020
from typing import Any, Dict, List, Optional, Tuple
2121

22-
from synapse.api.constants import EventTypes, JoinRules
22+
from synapse.api.constants import EventContentFields, EventTypes, JoinRules
2323
from synapse.api.errors import StoreError
2424
from synapse.api.room_versions import RoomVersion, RoomVersions
2525
from synapse.events import EventBase
@@ -1012,9 +1012,9 @@ def get_rooms_for_retention_period_in_range_txn(txn):
10121012
class _BackgroundUpdates:
10131013
REMOVE_TOMESTONED_ROOMS_BG_UPDATE = "remove_tombstoned_rooms_from_directory"
10141014
ADD_ROOMS_ROOM_VERSION_COLUMN = "add_rooms_room_version_column"
1015-
POPULATE_ROOMS_CREATOR_COLUMN = "populate_rooms_creator_column"
10161015
POPULATE_ROOM_DEPTH_MIN_DEPTH2 = "populate_room_depth_min_depth2"
10171016
REPLACE_ROOM_DEPTH_MIN_DEPTH = "replace_room_depth_min_depth"
1017+
POPULATE_ROOMS_CREATOR_COLUMN = "populate_rooms_creator_column"
10181018

10191019

10201020
_REPLACE_ROOM_DEPTH_SQL_COMMANDS = (
@@ -1046,11 +1046,6 @@ def __init__(self, database: DatabasePool, db_conn, hs):
10461046
self._background_add_rooms_room_version_column,
10471047
)
10481048

1049-
self.db_pool.updates.register_background_update_handler(
1050-
_BackgroundUpdates.POPULATE_ROOMS_CREATOR_COLUMN,
1051-
self._background_populate_rooms_creator_column,
1052-
)
1053-
10541049
# BG updates to change the type of room_depth.min_depth
10551050
self.db_pool.updates.register_background_update_handler(
10561051
_BackgroundUpdates.POPULATE_ROOM_DEPTH_MIN_DEPTH2,
@@ -1061,6 +1056,11 @@ def __init__(self, database: DatabasePool, db_conn, hs):
10611056
self._background_replace_room_depth_min_depth,
10621057
)
10631058

1059+
self.db_pool.updates.register_background_update_handler(
1060+
_BackgroundUpdates.POPULATE_ROOMS_CREATOR_COLUMN,
1061+
self._background_populate_rooms_creator_column,
1062+
)
1063+
10641064
async def _background_insert_retention(self, progress, batch_size):
10651065
"""Retrieves a list of all rooms within a range and inserts an entry for each of
10661066
them into the room_retention table.
@@ -1198,63 +1198,6 @@ def _background_add_rooms_room_version_column_txn(txn: LoggingTransaction):
11981198

11991199
return batch_size
12001200

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-
12581201
async def _remove_tombstoned_rooms_from_directory(
12591202
self, progress, batch_size
12601203
) -> int:
@@ -1407,6 +1350,65 @@ def process(txn: Cursor) -> None:
14071350

14081351
return 0
14091352

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+
14101412

14111413
class RoomStore(RoomBackgroundUpdateStore, RoomWorkerStore, SearchStore):
14121414
def __init__(self, database: DatabasePool, db_conn, hs):
@@ -1438,7 +1440,12 @@ async def upsert_room_on_join(
14381440
# invalid, and it would fail auth checks anyway.
14391441
raise StoreError(400, "No create event in state")
14401442

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")
14421449

14431450
await self.db_pool.simple_upsert(
14441451
desc="upsert_room_on_join",
@@ -1467,9 +1474,6 @@ async def maybe_store_room_on_outlier_membership(
14671474
# mark the room as having an auth chain cover index.
14681475
has_auth_chain_index = await self.has_auth_chain_index(room_id)
14691476

1470-
create_event = await self.get_create_event_for_room(room_id)
1471-
room_creator = create_event.content.get("creator", None)
1472-
14731477
await self.db_pool.simple_upsert(
14741478
desc="maybe_store_room_on_outlier_membership",
14751479
table="rooms",
@@ -1478,7 +1482,10 @@ async def maybe_store_room_on_outlier_membership(
14781482
insertion_values={
14791483
"room_version": room_version.identifier,
14801484
"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": "",
14821489
"has_auth_chain_index": has_auth_chain_index,
14831490
},
14841491
# rooms has a unique constraint on room_id, so no need to lock when doing an

synapse/storage/schema/main/delta/63/02populate-rooms-creator.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
* limitations under the License.
1414
*/
1515

16-
INSERT into background_updates (update_name, progress_json)
17-
VALUES ('populate_rooms_creator_column', '{}');
16+
INSERT INTO background_updates (ordering, update_name, progress_json)
17+
VALUES (6302, 'populate_rooms_creator_column', '{}');

0 commit comments

Comments
 (0)