3333import attr
3434from typing_extensions import TypedDict
3535
36+ import synapse .events .snapshot
3637from synapse .api .constants import (
3738 EventContentFields ,
3839 EventTypes ,
@@ -196,6 +197,31 @@ async def upgrade_room(
196197 400 , "An upgrade for this room is currently in progress"
197198 )
198199
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+
199225 # Upgrade the room
200226 #
201227 # If this user has sent multiple upgrade requests for the same room
@@ -207,18 +233,29 @@ async def upgrade_room(
207233 requester ,
208234 old_room_id ,
209235 new_version , # args for _upgrade_room
236+ tombstone_event ,
237+ tombstone_context ,
210238 )
211239
212240 return ret
213241
214242 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 ,
216249 ) -> str :
217250 """
218251 Args:
219252 requester: the user requesting the upgrade
220253 old_room_id: the id of the room to be replaced
221254 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
222259
223260 Raises:
224261 ShadowBanError if the requester is shadow-banned.
@@ -237,30 +274,7 @@ async def _upgrade_room(
237274 )
238275
239276 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
264278
265279 await self .clone_existing_room (
266280 requester ,
0 commit comments