1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313# See the License for the specific language governing permissions and
1414# limitations under the License.
15+ from typing import Callable
1516
16- from twisted .internet import defer
17+ from synapse .events import EventBase
18+ from synapse .module_api import ModuleApi
19+ from synapse .types import StateMap
1720
1821
1922class ThirdPartyEventRules (object ):
@@ -36,11 +39,10 @@ def __init__(self, hs):
3639
3740 if module is not None :
3841 self .third_party_rules = module (
39- config = config , http_client = hs . get_simple_http_client ()
42+ config = config , module_api = ModuleApi ( hs , hs . get_auth_handler ()),
4043 )
4144
42- @defer .inlineCallbacks
43- def check_event_allowed (self , event , context ):
45+ async def check_event_allowed (self , event , context ):
4446 """Check if a provided event should be allowed in the given context.
4547
4648 Args:
@@ -53,18 +55,17 @@ def check_event_allowed(self, event, context):
5355 if self .third_party_rules is None :
5456 return True
5557
56- prev_state_ids = yield context .get_prev_state_ids ()
58+ prev_state_ids = await context .get_prev_state_ids ()
5759
5860 # Retrieve the state events from the database.
5961 state_events = {}
6062 for key , event_id in prev_state_ids .items ():
61- state_events [key ] = yield self .store .get_event (event_id , allow_none = True )
63+ state_events [key ] = await self .store .get_event (event_id , allow_none = True )
6264
63- ret = yield self .third_party_rules .check_event_allowed (event , state_events )
65+ ret = await self .third_party_rules .check_event_allowed (event , state_events )
6466 return ret
6567
66- @defer .inlineCallbacks
67- def on_create_room (self , requester , config , is_requester_admin ):
68+ async def on_create_room (self , requester , config , is_requester_admin ):
6869 """Intercept requests to create room to allow, deny or update the
6970 request config.
7071
@@ -80,13 +81,12 @@ def on_create_room(self, requester, config, is_requester_admin):
8081 if self .third_party_rules is None :
8182 return True
8283
83- ret = yield self .third_party_rules .on_create_room (
84+ ret = await self .third_party_rules .on_create_room (
8485 requester , config , is_requester_admin
8586 )
8687 return ret
8788
88- @defer .inlineCallbacks
89- def check_threepid_can_be_invited (self , medium , address , room_id ):
89+ async def check_threepid_can_be_invited (self , medium , address , room_id ):
9090 """Check if a provided 3PID can be invited in the given room.
9191
9292 Args:
@@ -101,14 +101,51 @@ def check_threepid_can_be_invited(self, medium, address, room_id):
101101 if self .third_party_rules is None :
102102 return True
103103
104- state_ids = yield self .store .get_filtered_current_state_ids (room_id )
105- room_state_events = yield self .store .get_events (state_ids .values ())
104+ state_events = await self ._get_state_map_for_room (room_id )
105+
106+ ret = await self .third_party_rules .check_threepid_can_be_invited (
107+ medium , address , state_events
108+ )
109+ return ret
110+
111+ async def check_visibility_can_be_modified (
112+ self , room_id : str , new_visibility : str
113+ ) -> bool :
114+ """Check if a room is allowed to be published to, or removed from, the public room
115+ list.
116+
117+ Args:
118+ room_id: The ID of the room.
119+ new_visibility: The new visibility state. Either "public" or "private".
120+
121+ Returns:
122+ True if the room's visibility can be modified, False if not.
123+ """
124+ if self .third_party_rules is None :
125+ return True
126+
127+ check_func = getattr (self .third_party_rules , "check_visibility_can_be_modified" )
128+ if not check_func or not isinstance (check_func , Callable ):
129+ return True
130+
131+ state_events = await self ._get_state_map_for_room (room_id )
132+
133+ return await check_func (room_id , state_events , new_visibility )
134+
135+ async def _get_state_map_for_room (self , room_id : str ) -> StateMap [EventBase ]:
136+ """Given a room ID, return the state events of that room.
137+
138+ Args:
139+ room_id: The ID of the room.
140+
141+ Returns:
142+ A dict mapping (event type, state key) to state event.
143+ """
144+ state_ids = await self .store .get_filtered_current_state_ids (room_id )
145+ room_state_events = await self .store .get_events (state_ids .values ())
106146
107147 state_events = {}
108148 for key , event_id in state_ids .items ():
109149 state_events [key ] = room_state_events [event_id ]
110150
111- ret = yield self .third_party_rules .check_threepid_can_be_invited (
112- medium , address , state_events
113- )
114- return ret
151+ return state_events
0 commit comments