Refactor of join/leave/invite API.
This commit is contained in:
parent
9912415453
commit
ad208cbb3a
3 changed files with 40 additions and 45 deletions
|
@ -165,11 +165,46 @@ class MatrixClient:
|
||||||
|
|
||||||
path = ("{api}/rooms/{room}/messages?{query_parameters}").format(
|
path = ("{api}/rooms/{room}/messages?{query_parameters}").format(
|
||||||
api=MATRIX_API_PATH,
|
api=MATRIX_API_PATH,
|
||||||
room=room_id,
|
room=quote(room_id),
|
||||||
query_parameters=urlencode(query_parameters))
|
query_parameters=urlencode(query_parameters))
|
||||||
|
|
||||||
return HttpRequest(RequestType.GET, self.host, path)
|
return HttpRequest(RequestType.GET, self.host, path)
|
||||||
|
|
||||||
|
def room_join(self, room_id):
|
||||||
|
query_parameters = {"access_token": self.access_token}
|
||||||
|
|
||||||
|
path = ("{api}/rooms/{room_id}/join?"
|
||||||
|
"{query_parameters}").format(
|
||||||
|
api=MATRIX_API_PATH,
|
||||||
|
room_id=quote(room_id),
|
||||||
|
query_parameters=urlencode(query_parameters))
|
||||||
|
|
||||||
|
return HttpRequest(RequestType.POST, self.host, path)
|
||||||
|
|
||||||
|
def room_leave(self, room_id):
|
||||||
|
query_parameters = {"access_token": self.access_token}
|
||||||
|
|
||||||
|
path = ("{api}/rooms/{room_id}/leave?"
|
||||||
|
"{query_parameters}").format(
|
||||||
|
api=MATRIX_API_PATH,
|
||||||
|
room_id=quote(room_id),
|
||||||
|
query_parameters=urlencode(query_parameters))
|
||||||
|
|
||||||
|
return HttpRequest(RequestType.POST, self.host, path)
|
||||||
|
|
||||||
|
def room_invite(self, room_id, user_id):
|
||||||
|
query_parameters = {"access_token": self.access_token}
|
||||||
|
|
||||||
|
content = {"user_id": user_id}
|
||||||
|
|
||||||
|
path = ("{api}/rooms/{room_id}/invite?"
|
||||||
|
"{query_parameters}").format(
|
||||||
|
api=MATRIX_API_PATH,
|
||||||
|
room_id=quote(room_id),
|
||||||
|
query_parameters=urlencode(query_parameters))
|
||||||
|
|
||||||
|
return HttpRequest(RequestType.POST, self.host, path, content)
|
||||||
|
|
||||||
|
|
||||||
class MatrixMessage:
|
class MatrixMessage:
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -193,8 +228,6 @@ class MatrixMessage:
|
||||||
self.send_time = None # type: float
|
self.send_time = None # type: float
|
||||||
self.receive_time = None # type: float
|
self.receive_time = None # type: float
|
||||||
|
|
||||||
host = ':'.join([server.address, str(server.port)])
|
|
||||||
|
|
||||||
if message_type == MessageType.LOGIN:
|
if message_type == MessageType.LOGIN:
|
||||||
self.request = server.client.login(
|
self.request = server.client.login(
|
||||||
server.user,
|
server.user,
|
||||||
|
@ -231,46 +264,13 @@ class MatrixMessage:
|
||||||
)
|
)
|
||||||
|
|
||||||
elif message_type == MessageType.JOIN:
|
elif message_type == MessageType.JOIN:
|
||||||
path = ("{api}/rooms/{room_id}/join?"
|
self.request = server.client.room_join(room_id)
|
||||||
"access_token={access_token}").format(
|
|
||||||
api=MATRIX_API_PATH,
|
|
||||||
room_id=room_id,
|
|
||||||
access_token=server.access_token)
|
|
||||||
|
|
||||||
self.request = HttpRequest(
|
|
||||||
RequestType.POST,
|
|
||||||
host,
|
|
||||||
path,
|
|
||||||
data
|
|
||||||
)
|
|
||||||
|
|
||||||
elif message_type == MessageType.PART:
|
elif message_type == MessageType.PART:
|
||||||
path = ("{api}/rooms/{room_id}/leave?"
|
self.request = server.client.room_leave(room_id)
|
||||||
"access_token={access_token}").format(
|
|
||||||
api=MATRIX_API_PATH,
|
|
||||||
room_id=room_id,
|
|
||||||
access_token=server.access_token)
|
|
||||||
|
|
||||||
self.request = HttpRequest(
|
|
||||||
RequestType.POST,
|
|
||||||
host,
|
|
||||||
path,
|
|
||||||
data
|
|
||||||
)
|
|
||||||
|
|
||||||
elif message_type == MessageType.INVITE:
|
elif message_type == MessageType.INVITE:
|
||||||
path = ("{api}/rooms/{room}/invite?"
|
self.request = server.client.room_invite(room_id, data)
|
||||||
"access_token={access_token}").format(
|
|
||||||
api=MATRIX_API_PATH,
|
|
||||||
room=room_id,
|
|
||||||
access_token=server.access_token)
|
|
||||||
|
|
||||||
self.request = HttpRequest(
|
|
||||||
RequestType.POST,
|
|
||||||
host,
|
|
||||||
path,
|
|
||||||
data
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MatrixUser:
|
class MatrixUser:
|
||||||
|
|
|
@ -256,14 +256,12 @@ def matrix_command_invite_cb(data, buffer, command):
|
||||||
_, invitee = split_args
|
_, invitee = split_args
|
||||||
room_id = key_from_value(server.buffers, buf)
|
room_id = key_from_value(server.buffers, buf)
|
||||||
|
|
||||||
body = {"user_id": invitee}
|
|
||||||
|
|
||||||
message = MatrixMessage(
|
message = MatrixMessage(
|
||||||
server,
|
server,
|
||||||
OPTIONS,
|
OPTIONS,
|
||||||
MessageType.INVITE,
|
MessageType.INVITE,
|
||||||
room_id=room_id,
|
room_id=room_id,
|
||||||
data=body
|
data=invitee
|
||||||
)
|
)
|
||||||
server.send_or_queue(message)
|
server.send_or_queue(message)
|
||||||
|
|
||||||
|
@ -319,8 +317,6 @@ def event_id_from_line(buf, target_number):
|
||||||
def matrix_redact_command_cb(data, buffer, args):
|
def matrix_redact_command_cb(data, buffer, args):
|
||||||
for server in SERVERS.values():
|
for server in SERVERS.values():
|
||||||
if buffer in server.buffers.values():
|
if buffer in server.buffers.values():
|
||||||
body = {}
|
|
||||||
|
|
||||||
room_id = key_from_value(server.buffers, buffer)
|
room_id = key_from_value(server.buffers, buffer)
|
||||||
|
|
||||||
matches = re.match(r"(\d+)(:\".*\")? ?(.*)?", args)
|
matches = re.match(r"(\d+)(:\".*\")? ?(.*)?", args)
|
||||||
|
|
|
@ -31,7 +31,6 @@ from matrix.globals import W, OPTIONS
|
||||||
from matrix.api import (
|
from matrix.api import (
|
||||||
MessageType,
|
MessageType,
|
||||||
matrix_sync,
|
matrix_sync,
|
||||||
MatrixMessage,
|
|
||||||
MatrixRoom,
|
MatrixRoom,
|
||||||
MatrixUser
|
MatrixUser
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue