Skip to content

Commit

Permalink
Handle case where user leaves room in _process_state_events
Browse files Browse the repository at this point in the history
Also fixed up one minor thing to modify list in place and added test for
user leaving room.
  • Loading branch information
non-Jedi committed Oct 11, 2017
1 parent 615ae52 commit 49284e4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ def _process_state_event(self, state_event, current_room):
state_event["state_key"],
state_event["content"].get("displayname", None))
)
elif state_event["content"]["membership"] in ("leave", "kick", "invite"):
current_room._rmmembers(state_event["state_key"])

for listener in current_room.state_listeners:
if (
Expand Down
9 changes: 6 additions & 3 deletions matrix_client/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def display_name(self):

members = self.get_joined_members()
# members without me
members = [u.get_display_name() for u in members if
self.client.user_id != u.user_id]
members[:] = [u.get_display_name() for u in members if
self.client.user_id != u.user_id]
first_two = members[:2]
if len(first_two) == 1:
return first_two[0]
Expand All @@ -81,7 +81,7 @@ def display_name(self):
elif len(members) > 2:
return "{0} and {1} others".format(
first_two[0],
first_two[1])
len(members) - 1)
elif len(first_two) == 0:
# TODO i18n
return "Empty room"
Expand Down Expand Up @@ -518,6 +518,9 @@ def _mkmembers(self, member):
if member.user_id not in [x.user_id for x in self._members]:
self._members.append(member)

def _rmmembers(self, user_id):
self._members[:] = [x for x in self._members if x.user_id != user_id]

def backfill_previous_messages(self, reverse=False, limit=10):
"""Backfill handling of previous messages.
Expand Down
8 changes: 6 additions & 2 deletions test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ def test_state_event():
client._process_state_event(ev, room)
assert room.aliases is aliases

# test member event
# test member join event
ev["type"] = "m.room.member"
ev["content"] = {'membership': 'join', 'displayname': 'stereo'}
ev["state_key"] = "@stereo:xxx.org"
client._process_state_event(ev, room)
assert len(room._members) == 1
assert room._members[0].user_id == "@stereo:xxx.org"
# test member leave event
ev["content"]['membership'] = 'leave'
client._process_state_event(ev, room)
assert len(room._members) == 0


def test_get_user():
Expand Down Expand Up @@ -196,4 +200,4 @@ def add_members(api, room, num):
assert room1.display_name == "Empty room"
assert room2.display_name == "ho1"
assert room3.display_name == "ho1 and ho2"
assert room4.display_name == "ho1 and ho2 others"
assert room4.display_name == "ho1 and 28 others"

0 comments on commit 49284e4

Please sign in to comment.