33
33
import attr
34
34
from typing_extensions import TypedDict
35
35
36
+ import synapse .events .snapshot
36
37
from synapse .api .constants import (
37
38
EventContentFields ,
38
39
EventTypes ,
@@ -196,6 +197,31 @@ async def upgrade_room(
196
197
400 , "An upgrade for this room is currently in progress"
197
198
)
198
199
200
+ # Check whether the user has the power level to carry out the upgrade.
201
+ # `check_auth_rules_from_context` will check that they are in the room and have
202
+ # the required power level to send the tombstone event.
203
+ (
204
+ tombstone_event ,
205
+ tombstone_context ,
206
+ ) = await self .event_creation_handler .create_event (
207
+ requester ,
208
+ {
209
+ "type" : EventTypes .Tombstone ,
210
+ "state_key" : "" ,
211
+ "room_id" : old_room_id ,
212
+ "sender" : user_id ,
213
+ "content" : {
214
+ "body" : "This room has been replaced" ,
215
+ "replacement_room" : None , # Use a placeholder value for now
216
+ },
217
+ },
218
+ )
219
+ old_room_version = await self .store .get_room_version (old_room_id )
220
+ validate_event_for_room_version (old_room_version , tombstone_event )
221
+ await self ._event_auth_handler .check_auth_rules_from_context (
222
+ old_room_version , tombstone_event , tombstone_context
223
+ )
224
+
199
225
# Upgrade the room
200
226
#
201
227
# If this user has sent multiple upgrade requests for the same room
@@ -207,18 +233,29 @@ async def upgrade_room(
207
233
requester ,
208
234
old_room_id ,
209
235
new_version , # args for _upgrade_room
236
+ tombstone_event ,
237
+ tombstone_context ,
210
238
)
211
239
212
240
return ret
213
241
214
242
async def _upgrade_room (
215
- self , requester : Requester , old_room_id : str , new_version : RoomVersion
243
+ self ,
244
+ requester : Requester ,
245
+ old_room_id : str ,
246
+ new_version : RoomVersion ,
247
+ tombstone_event : EventBase ,
248
+ tombstone_context : synapse .events .snapshot .EventContext ,
216
249
) -> str :
217
250
"""
218
251
Args:
219
252
requester: the user requesting the upgrade
220
253
old_room_id: the id of the room to be replaced
221
254
new_versions: the version to upgrade the room to
255
+ tombstone_event: a template for the tombstone event to send to the old room.
256
+ `content["replacement_room"]` will be filled in the the id of the new
257
+ room.
258
+ tombstone_context: the context for the tombstone event
222
259
223
260
Raises:
224
261
ShadowBanError if the requester is shadow-banned.
@@ -237,30 +274,7 @@ async def _upgrade_room(
237
274
)
238
275
239
276
logger .info ("Creating new room %s to replace %s" , new_room_id , old_room_id )
240
-
241
- # we create and auth the tombstone event before properly creating the new
242
- # room, to check our user has perms in the old room.
243
- (
244
- tombstone_event ,
245
- tombstone_context ,
246
- ) = await self .event_creation_handler .create_event (
247
- requester ,
248
- {
249
- "type" : EventTypes .Tombstone ,
250
- "state_key" : "" ,
251
- "room_id" : old_room_id ,
252
- "sender" : user_id ,
253
- "content" : {
254
- "body" : "This room has been replaced" ,
255
- "replacement_room" : new_room_id ,
256
- },
257
- },
258
- )
259
- old_room_version = await self .store .get_room_version (old_room_id )
260
- validate_event_for_room_version (old_room_version , tombstone_event )
261
- await self ._event_auth_handler .check_auth_rules_from_context (
262
- old_room_version , tombstone_event , tombstone_context
263
- )
277
+ tombstone_event .content ["replacement_room" ] = new_room_id
264
278
265
279
await self .clone_existing_room (
266
280
requester ,
0 commit comments