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

Notifier: accept callbacks to fire on room joins #13254

Merged
merged 2 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13254.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preparatory work for a per-room rate limiter on joins.
18 changes: 18 additions & 0 deletions synapse/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def __init__(self, hs: "HomeServer"):

# Called when there are new things to stream over replication
self.replication_callbacks: List[Callable[[], None]] = []
self._new_join_in_room_callbacks: List[Callable[[str, str], None]] = []

self._federation_client = hs.get_federation_http_client()

Expand Down Expand Up @@ -280,6 +281,19 @@ def add_replication_callback(self, cb: Callable[[], None]) -> None:
"""
self.replication_callbacks.append(cb)

def add_new_join_in_room_callback(self, cb: Callable[[str, str], None]) -> None:
"""Add a callback that will be called when a user joins a room.

This only fires on genuine membership changes, e.g. "invite" -> "join".
Membership transitions like "join" -> "join" (for e.g. displayname changes) do
not trigger the callback.

When called, the callback receives two arguments: the event ID and the room ID.
It should *not* return a Deferred - if it needs to do any asynchronous work, a
background thread should be started and wrapped with run_as_background_process.
"""
self._new_join_in_room_callbacks.append(cb)

async def on_new_room_event(
self,
event: EventBase,
Expand Down Expand Up @@ -723,6 +737,10 @@ def notify_replication(self) -> None:
for cb in self.replication_callbacks:
cb()

def notify_user_joined_room(self, event_id: str, room_id: str) -> None:
for cb in self._new_join_in_room_callbacks:
cb(event_id, room_id)

def notify_remote_server_up(self, server: str) -> None:
"""Notify any replication that a remote server has come back up"""
# We call federation_sender directly rather than registering as a
Expand Down