Skip to content

Commit

Permalink
Add event callbacks for presence updates
Browse files Browse the repository at this point in the history
I'm working on a chatbot that needs to know about users' presence -
these callbacks are modeled off the others nearby.

Signed-off-by: Jonathan Frederickson <jonathan@terracrypt.net>
  • Loading branch information
jfrederickson committed Oct 14, 2017
1 parent 49284e4 commit 53add77
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(self, base_url, token=None, user_id=None,
self.api = MatrixHttpApi(base_url, token)
self.api.validate_certificate(valid_cert_check)
self.listeners = []
self.presence_listeners = []
self.invite_listeners = []
self.left_listeners = []
self.ephemeral_listeners = []
Expand Down Expand Up @@ -281,6 +282,34 @@ def remove_listener(self, uid):
self.listeners[:] = (listener for listener in self.listeners
if listener['uid'] != uid)

def add_presence_listener(self, callback):
""" Add a presence listener that will send a callback when the client receives
a presence update.
Args:
callback (func(roomchunk)): Callback called when a presence update arrives.
Returns:
uuid.UUID: Unique id of the listener, can be used to identify the listener.
"""
listener_uid = uuid4()
self.presence_listeners.append(
{
'uid': listener_uid,
'callback': callback
}
)
return listener_uid

def remove_presence_listener(self, uid):
""" Remove presence listener with given uid
Args:
uuid.UUID: Unique id of the listener to remove
"""
self.presence_listeners[:] = (listener for listener in self.presence_listeners
if listener['uid'] != uid)

def add_ephemeral_listener(self, callback, event_type=None):
""" Add an ephemeral listener that will send a callback when the client recieves
an ephemeral event.
Expand Down Expand Up @@ -461,11 +490,14 @@ def _process_state_event(self, state_event, current_room):
listener['callback'](state_event)

def _sync(self, timeout_ms=30000):
# TODO: Deal with presence
# TODO: Deal with left rooms
response = self.api.sync(self.sync_token, timeout_ms, filter=self.sync_filter)
self.sync_token = response["next_batch"]

for presence_update in response['presence']['events']:
for listener in self.presence_listeners:
listener['callback'](presence_update)

for room_id, invite_room in response['rooms']['invite'].items():
for listener in self.invite_listeners:
listener(room_id, invite_room['invite_state'])
Expand Down

0 comments on commit 53add77

Please sign in to comment.