summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--synapse/handlers/room.py7
-rw-r--r--tests/handlers/test_room.py44
2 files changed, 48 insertions, 3 deletions
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index a000b44036..c802e8f69d 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -260,6 +260,7 @@ class RoomMemberHandler(BaseHandler):
 
         self.distributor = hs.get_distributor()
         self.distributor.declare("user_joined_room")
+        self.distributor.declare("user_left_room")
 
     @defer.inlineCallbacks
     def get_room_members(self, room_id, membership=Membership.JOIN):
@@ -387,6 +388,12 @@ class RoomMemberHandler(BaseHandler):
                 do_auth=do_auth,
             )
 
+            if prev_state and prev_state.membership == Membership.JOIN:
+                user = self.hs.parse_userid(event.user_id)
+                self.distributor.fire(
+                    "user_left_room", user=user, room_id=event.room_id
+                )
+
         defer.returnValue({"room_id": room_id})
 
     @defer.inlineCallbacks
diff --git a/tests/handlers/test_room.py b/tests/handlers/test_room.py
index 0279ab703a..3a71ed0aed 100644
--- a/tests/handlers/test_room.py
+++ b/tests/handlers/test_room.py
@@ -222,14 +222,52 @@ class RoomMemberHandlerTestCase(unittest.TestCase):
             user=user, room_id=room_id
         )
 
-    def _create_member(self, user_id, room_id):
+    @defer.inlineCallbacks
+    def test_simple_leave(self):
+        room_id = "!foo:red"
+        user_id = "@bob:red"
+        user = self.hs.parse_userid(user_id)
+
+        event = self._create_member(
+            user_id=user_id,
+            room_id=room_id,
+            membership=Membership.LEAVE,
+        )
+
+        prev_state = NonCallableMock()
+        prev_state.membership = Membership.JOIN
+        prev_state.sender = user_id
+        self.datastore.get_room_member.return_value = defer.succeed(prev_state)
+
+        event.state_events = {
+            (RoomMemberEvent.TYPE, user_id): event,
+        }
+
+        event.old_state_events = {
+            (RoomMemberEvent.TYPE, user_id): self._create_member(
+                user_id=user_id,
+                room_id=room_id,
+            ),
+        }
+
+        leave_signal_observer = Mock()
+        self.distributor.observe("user_left_room", leave_signal_observer)
+
+        # Actual invocation
+        yield self.room_member_handler.change_membership(event)
+
+        leave_signal_observer.assert_called_with(
+            user=user, room_id=room_id
+        )
+
+    def _create_member(self, user_id, room_id, membership=Membership.JOIN):
         return self.hs.get_event_factory().create_event(
             etype=RoomMemberEvent.TYPE,
             user_id=user_id,
             state_key=user_id,
             room_id=room_id,
-            membership=Membership.JOIN,
-            content={"membership": Membership.JOIN},
+            membership=membership,
+            content={"membership": membership},
         )