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

Make account_data accessible #295

Open
wants to merge 1 commit 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
24 changes: 24 additions & 0 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def __init__(self, base_url, token=None, user_id=None,
self.users = {
# user_id: User
}
self.account_data = {
# type: contents
}
if token:
response = self.api.whoami()
self.user_id = response["user_id"]
Expand Down Expand Up @@ -638,6 +641,16 @@ def _sync(self, timeout_ms=30000):
):
listener['callback'](event)

for event in response['account_data']['events']:
self.account_data[event['type']] = event['content']

for listener in self.listeners:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there's a problem with the way this is done.

A silly example is that if I want to listen for messages, I will add a listener with the event type m.room.message. But nothing stops me (at least not the HS) from having an event type in account_data which is also m.room.message. Then the listener will pick up both, which is not the expected behavior and could cause strange bugs. This may actually happen if you substitute m.room.message for an event type that is less well known.

Hence you should probably handle this with separate listeners, in the same way invite and presence listeners are done. Not 100% sure this is the right way though, so if you have another idea I'd gladly consider it!

if (
listener['event_type'] is None or
listener['event_type'] == event['type']
):
listener['callback'](event)

def get_user(self, user_id):
"""Deprecated. Return a User by their id.

Expand Down Expand Up @@ -667,3 +680,14 @@ def remove_room_alias(self, room_alias):
return True
except MatrixRequestError:
return False

def get_account_data(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain more precisely how this particular formatting is useful? Maybe give an example, as I don't get in what way it eases a call to api.set_account_data.

Copy link
Author

Choose a reason for hiding this comment

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

Hey, sorry for the late response, @Zil0. I chose to have another look.

I chose this particular formatting because it matches the JSON that the API returns. But looking at the api.set_account_data call, you're right, I can probably just return the account_data dict, do yo agree?

"""Return `account_data` structure

Returns:
dict: Formatted for use in e.g. `api.set_account_data`
"""
events = []
for k,v in self.account_data.items():
events.append({'type': k, 'content': v})
return {'events': events}
40 changes: 40 additions & 0 deletions samples/ListDirectChats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should drop this sample, as adding a 40 lines file with one line worth of information is not useful.

The comments below are FYI :)


# Dump the list of direct chats
# Args: host:port username password
# Error Codes:
# 2 - Could not find the server.
# 3 - Bad URL Format.
# 4 - Bad username/password.

import sys
import samples_common

from matrix_client.client import MatrixClient
from matrix_client.api import MatrixRequestError
from requests.exceptions import MissingSchema


host, username, password = samples_common.get_user_details(sys.argv)

client = MatrixClient(host)

try:
client.login_with_password_no_sync(username, password)
Copy link
Contributor

Choose a reason for hiding this comment

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

This method is deprecated, simply use client.login with sync=False.

except MatrixRequestError as e:
print(e)
if e.code == 403:
print("Bad username or password.")
sys.exit(4)
else:
print("Check your server details are correct.")
sys.exit(2)
except MissingSchema as e:
print("Bad URL format.")
print(e)
sys.exit(3)

room_ids = []

import yaml
print(yaml.dumps(client.account_data['m.direct']), default_flow_style=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to use yaml.dump? Also you didn't sync so this will always raise KeyError.

10 changes: 10 additions & 0 deletions test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ def test_get_rooms():
assert len(rooms) == 3


def test_get_account_data():
client = MatrixClient("http://example.com")
assert isinstance(client.account_data, dict)
assert (len(client.account_data) == 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Outside parentheses look unnecessary, applies also to the lines below.


ab = client.get_account_data()
assert('events' in ab)
assert (len(ab['events']) == 0)


def test_bad_state_events():
client = MatrixClient("http://example.com")
room = client._mkroom("!abc:matrix.org")
Expand Down