From e4c56308a6e816c58bc7c5da30f7992bf107ebc9 Mon Sep 17 00:00:00 2001 From: Mathijs van Gorcum Date: Sun, 6 May 2018 23:20:20 +0200 Subject: [PATCH 1/3] add hooks for m.call.* --- matrix_client/api.py | 100 ++++++++++++++++++++++++++++++++++++++++++ matrix_client/room.py | 41 +++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/matrix_client/api.py b/matrix_client/api.py index 4ff54f69..8d03c1c9 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -253,6 +253,71 @@ def send_message_event(self, room_id, event_type, content, txn_id=None, if timestamp: params["ts"] = timestamp return self._send("PUT", path, content, query_params=params) + + def send_call_invite(self, room_id, call_id, sdp, version, lifetime, msgtype="m.call.invite", timestamp=None): + """Perform PUT /rooms/$room_id/send/m.room.call.invite + + Args: + room_id (str): The room ID to send the event in. + call_id (str): Call identifier string. + sdp (dict): Session Description Protocol dict. + version (int): The version of the VoIP specification this messages adheres to. + lifetime (int): The time in milliseconds that the invite is valid for. + timestamp (int): Set origin_server_ts (For application services only) + """ + return self.send_message_event( + room_id, "m.call.invite", + self.get_call_invite_content(call_id, sdp, version, lifetime), + timestamp=timestamp + ) + + def send_call_answer(self, room_id, call_id, sdp, version, lifetime, msgtype="m.call.answer", timestamp=None): + """Perform PUT /rooms/$room_id/send/m.room.call.answer + + Args: + room_id (str): The room ID to send the event in. + call_id (str): Call identifier string. + sdp (dict): Session Description Protocol dict. + version (int): The version of the VoIP specification this messages adheres to. + lifetime (int): The time in milliseconds that the answer is valid for. + timestamp (int): Set origin_server_ts (For application services only) + """ + return self.send_message_event( + room_id, "m.call.answer", + self.get_call_answer_content(call_id, sdp, lifetime, version), + timestamp=timestamp + ) + + def send_call_candidates(self, room_id, call_id, candidates, version, msgtype="m.call.candidates", timestamp=None): + """Perform PUT /rooms/$room_id/send/m.room.call.candidates + + Args: + room_id (str): The room ID to send the event in. + call_id (str): The call identifier string. + candidates (array of dicts): The candidates to send, each array element contains 'candidate', 'sdpMLineIndex', and 'sdpMid' + version (int): The version of the VoIP specification this messages adheres to. + timestamp (int): Set origin_server_ts (For application services only) + """ + return self.send_message_event( + room_id, "m.call.candidates", + self.get_call_candidates_content(call_id, candidates, version), + timestamp=timestamp + ) + + def send_call_hangup(self,room_id,call_id,version,msgtype="m.call.hangup",timestamp=None): + """Perform PUT /rooms/$room_id/send/m.room.call.hangup + + Args: + room_id (str): The room ID to send the event in. + call_id (str): The call identifier string. + version (int): The version of the VoIP specification this messages adheres to. + timestamp (int): Set origin_server_ts (For application services only) + """ + return self.send_message_event( + room_id, "m.call.hangup", + self.get_call_hangup_content(call_id, version), + timestamp=timestamp + ) def redact_event(self, room_id, event_id, reason=None, txn_id=None, timestamp=None): """Perform PUT /rooms/$room_id/redact/$event_id/$txn_id/ @@ -620,6 +685,41 @@ def get_text_body(self, text, msgtype="m.text"): "msgtype": msgtype, "body": text } + + def get_call_invite_content(self,call_id, sdp, version, lifetime,types="offer"): + return{ + "call_id": call_id, + "lifetime": lifetime, + "offer": { + "spd": sdp, + "type": types + }, + "version": version + } + + def get_call_answer_content(self,call_id, sdp, lifetime, version,types="answer"): + return{ + "call_id": call_id, + "lifetime": lifetime, + "answer": { + "sdp": sdp, + "type": types + }, + "version": version + } + + def get_call_candidates_content(self, call_id, candidates, version): + return{ + "call_id": call_id, + "candidates": candidates, + "version": version + } + + def get_call_hangup_content(self,call_id,version): + return{ + "call_id": call_id, + "version": version + } def get_emote_body(self, text): return { diff --git a/matrix_client/room.py b/matrix_client/room.py index 20b8ca01..0e74092d 100644 --- a/matrix_client/room.py +++ b/matrix_client/room.py @@ -222,6 +222,47 @@ def send_audio(self, url, name, **audioinfo): return self.client.api.send_content(self.room_id, url, name, "m.audio", extra_information=audioinfo) + def send_call_invite(self, call_id, sdp, version, lifetime): + """ Send a call invite event to the room. + + Args: + call_id (str): Random call identifier string. + sdp (dict): Invite Session Description Protocol dict. + version (int): The version of the VoIP specification this message adheres to. + lifetime (int): The time in milliseconds that the invite is valid for. + """ + return self.client.api.send_call_invite(self.room_id, call_id, sdp, version, lifetime) + + def send_call_answer(self, call_id, sdp, version, lifetime): + """ Send a call answer event to the room. + + Args: + call_id (str): The call identifier string. + sdp (dict): Session Description Protocol dict. + version (int): The version of the VoIP specification this messages adheres to. + lifetime (int): The time in milliseconds that the answer is valid for. + """ + return self.client.api.send_call_answer(self.room_id, call_id, sdp, version, lifetime) + + def send_call_candidates(self, call_id, candidates, version): + """ Send a call candidates event to the room. + + Args: + call_id (str): The call identifier string. + candidates (dict): The candidates to send + version (int): The version of the VoIP specification this messages adheres to. + """ + return self.client.api.send_call_candidates(self.room_id, call_id, candidates, version) + + def send_call_hangup(self, call_id, version): + """ Send a call hangup event to the room. + + Args: + call_id (str): The call identifier string. + version (int): The version of the VoIP specification this messages adheres to. + """ + return self.client.api.send_call_hangup(self.room_id, call_id, version) + def redact_message(self, event_id, reason=None): """ Redacts the message with specified event_id in the room. See https://matrix.org/docs/spec/r0.0.1/client_server.html#id112 From 7c2fab7440cd05f6efc762953bc452a1bc9d685d Mon Sep 17 00:00:00 2001 From: Mathijs van Gorcum Date: Wed, 9 May 2018 00:54:43 +0200 Subject: [PATCH 2/3] clean up my mess --- matrix_client/api.py | 89 +++++++++++++++++++++++-------------------- matrix_client/room.py | 19 +++++---- 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 8d03c1c9..f58daa46 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -253,8 +253,9 @@ def send_message_event(self, room_id, event_type, content, txn_id=None, if timestamp: params["ts"] = timestamp return self._send("PUT", path, content, query_params=params) - - def send_call_invite(self, room_id, call_id, sdp, version, lifetime, msgtype="m.call.invite", timestamp=None): + + def send_call_invite(self, room_id, call_id, sdp, version, lifetime, + msgtype="m.call.invite", timestamp=None): """Perform PUT /rooms/$room_id/send/m.room.call.invite Args: @@ -269,9 +270,10 @@ def send_call_invite(self, room_id, call_id, sdp, version, lifetime, msgtype="m. room_id, "m.call.invite", self.get_call_invite_content(call_id, sdp, version, lifetime), timestamp=timestamp - ) + ) - def send_call_answer(self, room_id, call_id, sdp, version, lifetime, msgtype="m.call.answer", timestamp=None): + def send_call_answer(self, room_id, call_id, sdp, version, lifetime, + msgtype="m.call.answer", timestamp=None): """Perform PUT /rooms/$room_id/send/m.room.call.answer Args: @@ -286,25 +288,28 @@ def send_call_answer(self, room_id, call_id, sdp, version, lifetime, msgtype="m. room_id, "m.call.answer", self.get_call_answer_content(call_id, sdp, lifetime, version), timestamp=timestamp - ) - - def send_call_candidates(self, room_id, call_id, candidates, version, msgtype="m.call.candidates", timestamp=None): + ) + + def send_call_candidates(self, room_id, call_id, candidates, version, + msgtype="m.call.candidates", timestamp=None): """Perform PUT /rooms/$room_id/send/m.room.call.candidates Args: room_id (str): The room ID to send the event in. call_id (str): The call identifier string. - candidates (array of dicts): The candidates to send, each array element contains 'candidate', 'sdpMLineIndex', and 'sdpMid' + candidates (array of dicts): The candidates to send, each array element + contains 'candidate', 'sdpMLineIndex', and 'sdpMid' version (int): The version of the VoIP specification this messages adheres to. timestamp (int): Set origin_server_ts (For application services only) """ - return self.send_message_event( + return self.send_message_event( room_id, "m.call.candidates", self.get_call_candidates_content(call_id, candidates, version), timestamp=timestamp - ) + ) - def send_call_hangup(self,room_id,call_id,version,msgtype="m.call.hangup",timestamp=None): + def send_call_hangup(self, room_id, call_id, version, + msgtype="m.call.hangup", timestamp=None): """Perform PUT /rooms/$room_id/send/m.room.call.hangup Args: @@ -313,11 +318,11 @@ def send_call_hangup(self,room_id,call_id,version,msgtype="m.call.hangup",timest version (int): The version of the VoIP specification this messages adheres to. timestamp (int): Set origin_server_ts (For application services only) """ - return self.send_message_event( + return self.send_message_event( room_id, "m.call.hangup", self.get_call_hangup_content(call_id, version), timestamp=timestamp - ) + ) def redact_event(self, room_id, event_id, reason=None, txn_id=None, timestamp=None): """Perform PUT /rooms/$room_id/redact/$event_id/$txn_id/ @@ -685,41 +690,41 @@ def get_text_body(self, text, msgtype="m.text"): "msgtype": msgtype, "body": text } - - def get_call_invite_content(self,call_id, sdp, version, lifetime,types="offer"): + + def get_call_invite_content(self, call_id, sdp, version, lifetime, types="offer"): return{ - "call_id": call_id, - "lifetime": lifetime, - "offer": { - "spd": sdp, - "type": types - }, - "version": version - } + "call_id": call_id, + "lifetime": lifetime, + "offer": { + "spd": sdp, + "type": types + }, + "version": version + } - def get_call_answer_content(self,call_id, sdp, lifetime, version,types="answer"): + def get_call_answer_content(self, call_id, sdp, lifetime, version, types="answer"): return{ - "call_id": call_id, - "lifetime": lifetime, - "answer": { - "sdp": sdp, - "type": types - }, - "version": version - } - + "call_id": call_id, + "lifetime": lifetime, + "answer": { + "sdp": sdp, + "type": types + }, + "version": version + } + def get_call_candidates_content(self, call_id, candidates, version): return{ - "call_id": call_id, - "candidates": candidates, - "version": version - } - - def get_call_hangup_content(self,call_id,version): + "call_id": call_id, + "candidates": candidates, + "version": version + } + + def get_call_hangup_content(self, call_id, version): return{ - "call_id": call_id, - "version": version - } + "call_id": call_id, + "version": version + } def get_emote_body(self, text): return { diff --git a/matrix_client/room.py b/matrix_client/room.py index 0e74092d..239d0bce 100644 --- a/matrix_client/room.py +++ b/matrix_client/room.py @@ -231,8 +231,9 @@ def send_call_invite(self, call_id, sdp, version, lifetime): version (int): The version of the VoIP specification this message adheres to. lifetime (int): The time in milliseconds that the invite is valid for. """ - return self.client.api.send_call_invite(self.room_id, call_id, sdp, version, lifetime) - + return self.client.api.send_call_invite(self.room_id, call_id, + sdp, version, lifetime) + def send_call_answer(self, call_id, sdp, version, lifetime): """ Send a call answer event to the room. @@ -240,10 +241,11 @@ def send_call_answer(self, call_id, sdp, version, lifetime): call_id (str): The call identifier string. sdp (dict): Session Description Protocol dict. version (int): The version of the VoIP specification this messages adheres to. - lifetime (int): The time in milliseconds that the answer is valid for. + lifetime (int): The time in milliseconds that the answer is valid for. """ - return self.client.api.send_call_answer(self.room_id, call_id, sdp, version, lifetime) - + return self.client.api.send_call_answer(self.room_id, call_id, + sdp, version, lifetime) + def send_call_candidates(self, call_id, candidates, version): """ Send a call candidates event to the room. @@ -252,8 +254,9 @@ def send_call_candidates(self, call_id, candidates, version): candidates (dict): The candidates to send version (int): The version of the VoIP specification this messages adheres to. """ - return self.client.api.send_call_candidates(self.room_id, call_id, candidates, version) - + return self.client.api.send_call_candidates(self.room_id, call_id, + candidates, version) + def send_call_hangup(self, call_id, version): """ Send a call hangup event to the room. @@ -262,7 +265,7 @@ def send_call_hangup(self, call_id, version): version (int): The version of the VoIP specification this messages adheres to. """ return self.client.api.send_call_hangup(self.room_id, call_id, version) - + def redact_message(self, event_id, reason=None): """ Redacts the message with specified event_id in the room. See https://matrix.org/docs/spec/r0.0.1/client_server.html#id112 From a7799057434af4b8f460e2ec33ff658b82c5ac06 Mon Sep 17 00:00:00 2001 From: Mathijs van Gorcum Date: Wed, 9 May 2018 00:59:36 +0200 Subject: [PATCH 3/3] that should be all --- matrix_client/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index f58daa46..77f14806 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -297,8 +297,8 @@ def send_call_candidates(self, room_id, call_id, candidates, version, Args: room_id (str): The room ID to send the event in. call_id (str): The call identifier string. - candidates (array of dicts): The candidates to send, each array element - contains 'candidate', 'sdpMLineIndex', and 'sdpMid' + candidates (array of dicts): The candidates to send: + each array element contains 'candidate', 'sdpMLineIndex', and 'sdpMid' version (int): The version of the VoIP specification this messages adheres to. timestamp (int): Set origin_server_ts (For application services only) """ @@ -722,7 +722,7 @@ def get_call_candidates_content(self, call_id, candidates, version): def get_call_hangup_content(self, call_id, version): return{ - "call_id": call_id, + "call_id": call_id, "version": version }