summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2016-05-17 15:58:46 +0100
committerMark Haines <mark.haines@matrix.org>2016-05-17 15:58:46 +0100
commit0cb441fedd77b42f307745a441b804fee6386cb5 (patch)
tree5b240661fd780a583871812dd606491c1b38683f /synapse
parentMerge pull request #791 from matrix-org/markjh/app_service_config (diff)
downloadsynapse-0cb441fedd77b42f307745a441b804fee6386cb5.tar.xz
Move typing handler out of the Handlers object
Diffstat (limited to 'synapse')
-rw-r--r--synapse/handlers/__init__.py2
-rw-r--r--synapse/handlers/typing.py33
-rw-r--r--synapse/replication/resource.py2
-rw-r--r--synapse/rest/client/v1/room.py7
-rw-r--r--synapse/server.py5
5 files changed, 24 insertions, 25 deletions
diff --git a/synapse/handlers/__init__.py b/synapse/handlers/__init__.py
index e1fc9a58ad..9442ae6f1d 100644
--- a/synapse/handlers/__init__.py
+++ b/synapse/handlers/__init__.py
@@ -25,7 +25,6 @@ from .events import EventStreamHandler, EventHandler
 from .federation import FederationHandler
 from .profile import ProfileHandler
 from .directory import DirectoryHandler
-from .typing import TypingNotificationHandler
 from .admin import AdminHandler
 from .appservice import ApplicationServicesHandler
 from .auth import AuthHandler
@@ -53,7 +52,6 @@ class Handlers(object):
         self.profile_handler = ProfileHandler(hs)
         self.room_list_handler = RoomListHandler(hs)
         self.directory_handler = DirectoryHandler(hs)
-        self.typing_notification_handler = TypingNotificationHandler(hs)
         self.admin_handler = AdminHandler(hs)
         self.receipts_handler = ReceiptsHandler(hs)
         asapi = ApplicationServiceApi(hs)
diff --git a/synapse/handlers/typing.py b/synapse/handlers/typing.py
index fca8d25c3f..d46f05f426 100644
--- a/synapse/handlers/typing.py
+++ b/synapse/handlers/typing.py
@@ -15,8 +15,6 @@
 
 from twisted.internet import defer
 
-from ._base import BaseHandler
-
 from synapse.api.errors import SynapseError, AuthError
 from synapse.util.logcontext import PreserveLoggingContext
 from synapse.util.metrics import Measure
@@ -35,12 +33,13 @@ logger = logging.getLogger(__name__)
 RoomMember = namedtuple("RoomMember", ("room_id", "user"))
 
 
-class TypingNotificationHandler(BaseHandler):
+class TypingHandler(object):
     def __init__(self, hs):
-        super(TypingNotificationHandler, self).__init__(hs)
-
         self.store = hs.get_datastore()
         self.server_name = hs.config.server_name
+        self.auth = hs.get_auth()
+        self.is_mine = hs.is_mine
+        self.notifier = hs.get_notifier()
 
         self.clock = hs.get_clock()
 
@@ -68,7 +67,7 @@ class TypingNotificationHandler(BaseHandler):
 
     @defer.inlineCallbacks
     def started_typing(self, target_user, auth_user, room_id, timeout):
-        if not self.hs.is_mine(target_user):
+        if not self.is_mine(target_user):
             raise SynapseError(400, "User is not hosted on this Home Server")
 
         if target_user != auth_user:
@@ -111,7 +110,7 @@ class TypingNotificationHandler(BaseHandler):
 
     @defer.inlineCallbacks
     def stopped_typing(self, target_user, auth_user, room_id):
-        if not self.hs.is_mine(target_user):
+        if not self.is_mine(target_user):
             raise SynapseError(400, "User is not hosted on this Home Server")
 
         if target_user != auth_user:
@@ -133,7 +132,7 @@ class TypingNotificationHandler(BaseHandler):
 
     @defer.inlineCallbacks
     def user_left_room(self, user, room_id):
-        if self.hs.is_mine(user):
+        if self.is_mine(user):
             member = RoomMember(room_id=room_id, user=user)
             yield self._stopped_typing(member)
 
@@ -228,16 +227,14 @@ class TypingNotificationEventSource(object):
     def __init__(self, hs):
         self.hs = hs
         self.clock = hs.get_clock()
-        self._handler = None
-
-    def handler(self):
-        # Avoid cyclic dependency in handler setup
-        if not self._handler:
-            self._handler = self.hs.get_handlers().typing_notification_handler
-        return self._handler
+        # We can't call get_typing_handler here because there's a cycle:
+        #
+        #   Typing -> Notifier -> TypingNotificationEventSource -> Typing
+        #
+        self.get_typing_handler = hs.get_typing_handler
 
     def _make_event_for(self, room_id):
-        typing = self.handler()._room_typing[room_id]
+        typing = self.get_typing_handler()._room_typing[room_id]
         return {
             "type": "m.typing",
             "room_id": room_id,
@@ -249,7 +246,7 @@ class TypingNotificationEventSource(object):
     def get_new_events(self, from_key, room_ids, **kwargs):
         with Measure(self.clock, "typing.get_new_events"):
             from_key = int(from_key)
-            handler = self.handler()
+            handler = self.get_typing_handler()
 
             events = []
             for room_id in room_ids:
@@ -263,7 +260,7 @@ class TypingNotificationEventSource(object):
             return events, handler._latest_room_serial
 
     def get_current_key(self):
-        return self.handler()._latest_room_serial
+        return self.get_typing_handler()._latest_room_serial
 
     def get_pagination_rows(self, user, pagination_config, key):
         return ([], pagination_config.from_key)
diff --git a/synapse/replication/resource.py b/synapse/replication/resource.py
index b0e7a17670..847f212a3d 100644
--- a/synapse/replication/resource.py
+++ b/synapse/replication/resource.py
@@ -110,7 +110,7 @@ class ReplicationResource(Resource):
         self.store = hs.get_datastore()
         self.sources = hs.get_event_sources()
         self.presence_handler = hs.get_presence_handler()
-        self.typing_handler = hs.get_handlers().typing_notification_handler
+        self.typing_handler = hs.get_typing_handler()
         self.notifier = hs.notifier
         self.clock = hs.get_clock()
 
diff --git a/synapse/rest/client/v1/room.py b/synapse/rest/client/v1/room.py
index 9c89442ce6..cf478c6f79 100644
--- a/synapse/rest/client/v1/room.py
+++ b/synapse/rest/client/v1/room.py
@@ -571,6 +571,7 @@ class RoomTypingRestServlet(ClientV1RestServlet):
     def __init__(self, hs):
         super(RoomTypingRestServlet, self).__init__(hs)
         self.presence_handler = hs.get_presence_handler()
+        self.typing_handler = hs.get_typing_handler()
 
     @defer.inlineCallbacks
     def on_PUT(self, request, room_id, user_id):
@@ -581,19 +582,17 @@ class RoomTypingRestServlet(ClientV1RestServlet):
 
         content = parse_json_object_from_request(request)
 
-        typing_handler = self.handlers.typing_notification_handler
-
         yield self.presence_handler.bump_presence_active_time(requester.user)
 
         if content["typing"]:
-            yield typing_handler.started_typing(
+            yield self.typing_handler.started_typing(
                 target_user=target_user,
                 auth_user=requester.user,
                 room_id=room_id,
                 timeout=content.get("timeout", 30000),
             )
         else:
-            yield typing_handler.stopped_typing(
+            yield self.typing_handler.stopped_typing(
                 target_user=target_user,
                 auth_user=requester.user,
                 room_id=room_id,
diff --git a/synapse/server.py b/synapse/server.py
index 785a087452..01f828819f 100644
--- a/synapse/server.py
+++ b/synapse/server.py
@@ -29,6 +29,7 @@ from synapse.api.auth import Auth
 from synapse.handlers import Handlers
 from synapse.handlers.presence import PresenceHandler
 from synapse.handlers.sync import SyncHandler
+from synapse.handlers.typing import TypingHandler
 from synapse.state import StateHandler
 from synapse.storage import DataStore
 from synapse.util import Clock
@@ -82,6 +83,7 @@ class HomeServer(object):
         'state_handler',
         'presence_handler',
         'sync_handler',
+        'typing_handler',
         'notifier',
         'distributor',
         'client_resource',
@@ -171,6 +173,9 @@ class HomeServer(object):
     def build_presence_handler(self):
         return PresenceHandler(self)
 
+    def build_typing_handler(self):
+        return TypingHandler(self)
+
     def build_sync_handler(self):
         return SyncHandler(self)