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

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Feb 18, 2022
1 parent 357542c commit 15e7510
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/12028.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add third party event rules functions `check_can_shutdown_room` and `check_can_deactivate_user`.
44 changes: 44 additions & 0 deletions docs/modules/third_party_rules_callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,50 @@ deny an incoming event, see [`check_event_for_spam`](spam_checker_callbacks.md#c

If multiple modules implement this callback, Synapse runs them all in order.

### `check_can_shutdown_room`

_First introduced in Synapse v1.5X.0_

```python
async def check_can_shutdown_room(
user_id: str
room_id: str,
) -> bool:
```

Called when a user requests the shutdown of a room. The module must return a
boolean indicating whether the shutdown can go through. If the callback returns `False`,
the shutdown will not proceed and the caller will see a `M_FOBIDDEN` error.

If multiple modules implement this callback, they will be considered in order. If a
callback returns `True`, Synapse falls through to the next one. The value of the first
callback that does not return `True` will be used. If this happens, Synapse will not call
any of the subsequent implementations of this callback.

### `check_can_deactivate_user`

_First introduced in Synapse v1.5X.0_

```python
async def check_can_deactivate_user(
requester: "synapse.types.Requester",
user_id: str,
) -> bool:
```

Called when a user requests the deactivation of a user. User deactivation may be
performed by an admin or the user themselves, so developers are encouraged to check the
requester when implementing this callback. The module must return a
boolean indicating whether the deactivation can go through. If the callback returns `False`,
the deactivation will not proceed and the caller will see a `M_FOBIDDEN` error.

If multiple modules implement this callback, they will be considered in order. If a
callback returns `True`, Synapse falls through to the next one. The value of the first
callback that does not return `True` will be used. If this happens, Synapse will not call
any of the subsequent implementations of this callback.



## Example

The example below is a module that implements the third-party rules callback
Expand Down
12 changes: 6 additions & 6 deletions synapse/events/third_party_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def __init__(self, hs: "HomeServer"):
CHECK_VISIBILITY_CAN_BE_MODIFIED_CALLBACK
] = []
self._on_new_event_callbacks: List[ON_NEW_EVENT_CALLBACK] = []
self._check_can_delete_room: List[CHECK_CAN_SHUTDOWN_ROOM_CALLBACK] = []
self._check_can_shutdown_room: List[CHECK_CAN_SHUTDOWN_ROOM_CALLBACK] = []
self._check_can_deactivate_user: List[CHECK_CAN_DEACTIVATE_USER_CALLBACK] = []

def register_third_party_rules_callbacks(
Expand Down Expand Up @@ -194,7 +194,7 @@ def register_third_party_rules_callbacks(
self._on_new_event_callbacks.append(on_new_event)

if check_can_shutdown_room is not None:
self._check_can_delete_room.append(check_can_shutdown_room)
self._check_can_shutdown_room.append(check_can_shutdown_room)

if check_can_deactivate_user is not None:
self._check_can_deactivate_user.append(check_can_deactivate_user)
Expand Down Expand Up @@ -365,19 +365,19 @@ async def on_new_event(self, event_id: str) -> None:
"Failed to run module API callback %s: %s", callback, e
)

async def check_can_shutdown_room(self, requester: Requester, room_id: str) -> None:
async def check_can_shutdown_room(self, user_id: str, room_id: str) -> None:
"""Intercept requests to delete room to maybe deny it by returning False.
Args:
requester
requester: The ID of the user requesting the shutdown.
room_id: The ID of the room.
Raises:
ModuleFailureError if a callback raised any exception.
"""
for callback in self._check_can_delete_room:
for callback in self._check_can_shutdown_room:
try:
if await callback(requester, room_id) is False:
if await callback(user_id, room_id) is False:
return False
except Exception as e:
logger.exception(
Expand Down

0 comments on commit 15e7510

Please sign in to comment.