Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle all m.room.aliases chunk, not only first #270

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions matrix_client/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,17 @@ def update_aliases(self):
"""
try:
response = self.client.api.get_room_state(self.room_id)
for chunk in response:
if "content" in chunk and "aliases" in chunk["content"]:
if chunk["content"]["aliases"] != self.aliases:
self.aliases = chunk["content"]["aliases"]
return True
else:
return False
except MatrixRequestError:
return False
self.aliases = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have self.aliases.clear() here.

changed = False
for chunk in response:
if "content" in chunk and "aliases" in chunk["content"]:
for alias in chunk["content"]["aliases"]:
if alias not in self.aliases:
self.aliases.append(alias)
changed = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works as expected. Since self.aliases will always be empty because of self.aliases = [] above, changed will always be True as long as there is at least one alias, even if it's the same as before.

return changed

def add_room_alias(self, room_alias):
"""Add an alias to the room and return True if successful."""
Expand Down Expand Up @@ -649,7 +651,9 @@ def _process_state_event(self, state_event):
elif etype == "m.room.topic":
self.topic = econtent.get("topic")
elif etype == "m.room.aliases":
self.aliases = econtent.get("aliases")
for alias in econtent.get("aliases", []):
if alias not in self.aliases:
self.aliases.append(alias)
elif etype == "m.room.join_rules":
self.invite_only = econtent["join_rule"] == "invite"
elif etype == "m.room.guest_access":
Expand Down
5 changes: 2 additions & 3 deletions test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def test_state_event():

room.name = False
room.topic = False
room.aliases = False
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that when initializing aliases in rooms.py used empty list

self.aliases = []
, so using False here doesn't look correct.


ev = {
"type": "m.room.name",
Expand All @@ -97,12 +96,12 @@ def test_state_event():

ev["type"] = "m.room.aliases"
room._process_state_event(ev)
assert room.aliases is None
assert room.aliases == []
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since aliases is initialized as an empty list, it can no longer be None.


aliases = ["#foo:matrix.org", "#bar:matrix.org"]
ev["content"]["aliases"] = aliases
room._process_state_event(ev)
assert room.aliases is aliases
assert room.aliases == aliases
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since now we are processing list element by element, is will no longer work here.


# test member join event
ev["type"] = "m.room.member"
Expand Down