summary refs log tree commit diff
path: root/synapse/handlers/user_directory.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2017-05-31 17:00:24 +0100
committerErik Johnston <erik@matrix.org>2017-05-31 17:00:24 +0100
commitb2d8d0710912ab25e327f03bfea69b3c8333b2c8 (patch)
treeee48c21e319844b0384a0399a6563cd6dfcb0d30 /synapse/handlers/user_directory.py
parentTypos (diff)
downloadsynapse-b2d8d0710912ab25e327f03bfea69b3c8333b2c8.tar.xz
Lifts things into separate function
Diffstat (limited to 'synapse/handlers/user_directory.py')
-rw-r--r--synapse/handlers/user_directory.py110
1 files changed, 54 insertions, 56 deletions
diff --git a/synapse/handlers/user_directory.py b/synapse/handlers/user_directory.py
index 7f8da1a876..48d3c48181 100644
--- a/synapse/handlers/user_directory.py
+++ b/synapse/handlers/user_directory.py
@@ -162,63 +162,10 @@ class UserDirectoyHandler(object):
 
             # For join rule and visibility changes we need to check if the room
             # may have become public or not and add/remove the users in said room
-            if typ == EventTypes.RoomHistoryVisibility:
-                change = yield self._get_key_change(
-                    prev_event_id, event_id,
-                    key_name="history_visibility",
-                    public_value="world_readable",
-                )
-
-                # If change is None, no change. True => become world readable,
-                # False => was world readable
-                if change is None:
-                    continue
-
-                # There's been a change to or from being world readable.
-
-                is_public = yield self.store.is_room_world_readable_or_publicly_joinable(
-                    room_id
-                )
-
-                if change and not is_public:
-                    # If we became world readable but room isn't currently public then
-                    # we ignore the change
-                    continue
-                elif not change and is_public:
-                    # If we stopped being world readable but are still public,
-                    # ignore the change
-                    continue
-
-                users_with_profile = yield self.state.get_current_user_in_room(room_id)
-                for user_id, profile in users_with_profile.iteritems():
-                    if change:
-                        yield self._handle_new_user(room_id, user_id, profile)
-                    else:
-                        yield self._handle_remove_user(room_id, user_id)
-            elif typ == EventTypes.JoinRules:
-                change = yield self._get_key_change(
-                    prev_event_id, event_id,
-                    key_name="join_rules",
-                    public_value=JoinRules.PUBLIC,
+            if typ in (EventTypes.RoomHistoryVisibility, EventTypes.JoinRules):
+                yield self._handle_room_publicity_change(
+                    room_id, prev_event_id, event_id, typ,
                 )
-                if change is None:
-                    continue
-
-                is_public = yield self.store.is_room_world_readable_or_publicly_joinable(
-                    room_id
-                )
-
-                if change and is_public:
-                    continue
-                elif not change and not is_public:
-                    continue
-
-                users_with_profile = yield self.state.get_current_user_in_room(room_id)
-                for user_id, profile in users_with_profile.iteritems():
-                    if change:
-                        yield self._handle_new_user(room_id, user_id, profile)
-                    else:
-                        yield self._handle_remove_user(room_id, user_id)
             elif typ == EventTypes.Member:
                 change = yield self._get_key_change(
                     prev_event_id, event_id,
@@ -255,6 +202,57 @@ class UserDirectoyHandler(object):
                 else:  # The user left
                     yield self._handle_remove_user(room_id, state_key)
 
+    def _handle_room_publicity_change(self, room_id, prev_event_id, event_id, typ):
+        """Handle a room having potentially changed from/to world_readable/publically
+        joinable.
+
+        Args:
+            room_id (str)
+            prev_event_id (str|None): The previous event before the state change
+            event_id (str|None): The new event after the state change
+            typ (str): Type of the event
+        """
+        if typ == EventTypes.RoomHistoryVisibility:
+            change = yield self._get_key_change(
+                prev_event_id, event_id,
+                key_name="history_visibility",
+                public_value="world_readable",
+            )
+        elif typ == EventTypes.JoinRules:
+            change = yield self._get_key_change(
+                prev_event_id, event_id,
+                key_name="join_rules",
+                public_value=JoinRules.PUBLIC,
+            )
+        else:
+            raise Exception("Invalid event type")
+        # If change is None, no change. True => become world_readable/public,
+        # False => was world_readable/public
+        if change is None:
+            return
+
+        # There's been a change to or from being world readable.
+
+        is_public = yield self.store.is_room_world_readable_or_publicly_joinable(
+            room_id
+        )
+
+        if change and not is_public:
+            # If we became world readable but room isn't currently public then
+            # we ignore the change
+            return
+        elif not change and is_public:
+            # If we stopped being world readable but are still public,
+            # ignore the change
+            return
+
+        users_with_profile = yield self.state.get_current_user_in_room(room_id)
+        for user_id, profile in users_with_profile.iteritems():
+            if change:
+                yield self._handle_new_user(room_id, user_id, profile)
+            else:
+                yield self._handle_remove_user(room_id, user_id)
+
     @defer.inlineCallbacks
     def _handle_new_user(self, room_id, user_id, profile):
         """Called when we might need to add user to directory