summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2021-04-15 13:49:21 +0100
committerErik Johnston <erik@matrix.org>2021-04-15 13:49:21 +0100
commit7575a686fd6a2cd5a77df830baf5269bfed65aad (patch)
tree9d2415c06ec35181bd968d1d119f0f14b249361e
parentNewsfile (diff)
downloadsynapse-7575a686fd6a2cd5a77df830baf5269bfed65aad.tar.xz
Fix module api
-rw-r--r--synapse/module_api/__init__.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py
index b7dbbfc27c..90a40cd16b 100644
--- a/synapse/module_api/__init__.py
+++ b/synapse/module_api/__init__.py
@@ -18,6 +18,7 @@ from typing import TYPE_CHECKING, Any, Generator, Iterable, List, Optional, Tupl
 from twisted.internet import defer
 
 from synapse.events import EventBase
+from synapse.handlers.presence import get_interested_remotes
 from synapse.http.client import SimpleHttpClient
 from synapse.http.site import SynapseRequest
 from synapse.logging.context import make_deferred_yieldable, run_in_background
@@ -50,6 +51,12 @@ class ModuleApi:
         self._auth_handler = auth_handler
         self._server_name = hs.hostname
         self._presence_stream = hs.get_event_sources().sources["presence"]
+        self._state = hs.get_state_handler()
+        self._presence_router = hs.get_presence_router()
+
+        self._federation = None
+        if hs.should_send_federation():
+            self._federation = self._hs.get_federation_sender()
 
         # We expose these as properties below in order to attach a helpful docstring.
         self._http_client = hs.get_simple_http_client()  # type: SimpleHttpClient
@@ -423,6 +430,9 @@ class ModuleApi:
                 # Force a presence initial_sync for this user next time
                 self._send_full_presence_to_local_users.add(user)
             else:
+                if not self._federation:
+                    continue
+
                 # Retrieve presence state for currently online users that this user
                 # is considered interested in
                 presence_events, _ = await self._presence_stream.get_new_events(
@@ -430,12 +440,16 @@ class ModuleApi:
                 )
 
                 # Send to remote destinations
-                await make_deferred_yieldable(
-                    # We pull the federation sender here as we can only do so on workers
-                    # that support sending presence
-                    self._hs.get_federation_sender().send_presence(presence_events)
+                hosts_and_states = await get_interested_remotes(
+                    self._store,
+                    self._presence_router,
+                    presence_events,
+                    self._state,
                 )
 
+                for destinations, states in hosts_and_states:
+                    self._federation.send_presence_to_destinations(states, destinations)
+
 
 class PublicRoomListManager:
     """Contains methods for adding to, removing from and querying whether a room