diff --git a/matrix_client/client.py b/matrix_client/client.py index 703b825c..f2952add 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -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): + """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} diff --git a/samples/ListDirectChats.py b/samples/ListDirectChats.py new file mode 100644 index 00000000..c1bfdd02 --- /dev/null +++ b/samples/ListDirectChats.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# 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) +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) diff --git a/test/client_test.py b/test/client_test.py index c9b8464a..53b536c7 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -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) + + 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")