136136 ]
137137 ],
138138]
139+ USER_MAY_CREATE_ROOM_WITH_VISIBILITY_CALLBACK = Callable [
140+ [str , str ],
141+ Awaitable [
142+ Union [
143+ Literal ["NOT_SPAM" ],
144+ Codes ,
145+ ]
146+ ],
147+ ]
139148USER_MAY_CREATE_ROOM_ALIAS_CALLBACK = Callable [
140149 [str , RoomAlias ],
141150 Awaitable [
@@ -332,6 +341,9 @@ def __init__(self, hs: "synapse.server.HomeServer") -> None:
332341 USER_MAY_SEND_3PID_INVITE_CALLBACK
333342 ] = []
334343 self ._user_may_create_room_callbacks : List [USER_MAY_CREATE_ROOM_CALLBACK ] = []
344+ self ._user_may_create_room_with_visibility_callbacks : List [
345+ USER_MAY_CREATE_ROOM_WITH_VISIBILITY_CALLBACK
346+ ] = []
335347 self ._user_may_create_room_alias_callbacks : List [
336348 USER_MAY_CREATE_ROOM_ALIAS_CALLBACK
337349 ] = []
@@ -367,6 +379,9 @@ def register_callbacks(
367379 ] = None ,
368380 check_media_file_for_spam : Optional [CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK ] = None ,
369381 check_login_for_spam : Optional [CHECK_LOGIN_FOR_SPAM_CALLBACK ] = None ,
382+ user_may_create_room_with_visibility : Optional [
383+ USER_MAY_CREATE_ROOM_WITH_VISIBILITY_CALLBACK
384+ ] = None ,
370385 ) -> None :
371386 """Register callbacks from module for each hook."""
372387 if check_event_for_spam is not None :
@@ -391,6 +406,11 @@ def register_callbacks(
391406 if user_may_create_room is not None :
392407 self ._user_may_create_room_callbacks .append (user_may_create_room )
393408
409+ if user_may_create_room_with_visibility is not None :
410+ self ._user_may_create_room_with_visibility_callbacks .append (
411+ user_may_create_room_with_visibility ,
412+ )
413+
394414 if user_may_create_room_alias is not None :
395415 self ._user_may_create_room_alias_callbacks .append (
396416 user_may_create_room_alias ,
@@ -653,6 +673,29 @@ async def user_may_create_room(
653673
654674 return self .NOT_SPAM
655675
676+ async def user_may_create_room_with_visibility (
677+ self , userid : str , visibility : str
678+ ) -> Union [Tuple [Codes , dict ], Literal ["NOT_SPAM" ]]:
679+ """Checks if a given user may create a room with a given visibility
680+ Args:
681+ userid: The ID of the user attempting to create a room
682+ visibility: The visibility of the room to be created
683+ """
684+ for callback in self ._user_may_create_room_with_visibility_callbacks :
685+ with Measure (self .clock , f"{ callback .__module__ } .{ callback .__qualname__ } " ):
686+ res = await delay_cancellation (callback (userid , visibility ))
687+ if res is self .NOT_SPAM :
688+ continue
689+ elif isinstance (res , synapse .api .errors .Codes ):
690+ return res , {}
691+ else :
692+ logger .warning (
693+ "Module returned invalid value, rejecting room creation as spam"
694+ )
695+ return synapse .api .errors .Codes .FORBIDDEN , {}
696+
697+ return self .NOT_SPAM
698+
656699 async def user_may_create_room_alias (
657700 self , userid : str , room_alias : RoomAlias
658701 ) -> Union [Tuple [Codes , dict ], Literal ["NOT_SPAM" ]]:
0 commit comments