-
Notifications
You must be signed in to change notification settings - Fork 119
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"] | ||
|
@@ -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: | ||
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. | ||
|
||
|
@@ -667,3 +680,14 @@ def remove_room_alias(self, room_alias): | |
return True | ||
except MatrixRequestError: | ||
return False | ||
|
||
def get_account_data(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
"""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} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is deprecated, simply use |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to use |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
|
There was a problem hiding this comment.
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!