mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Merge branch 'client_server_url_rename' into develop
This commit is contained in:
commit
6f0bba1934
@ -409,10 +409,9 @@ class SynapseCmd(cmd.Cmd):
|
|||||||
def do_send(self, line):
|
def do_send(self, line):
|
||||||
"""Sends a message. "send <roomid> <body>" """
|
"""Sends a message. "send <roomid> <body>" """
|
||||||
args = self._parse(line, ["roomid", "body"])
|
args = self._parse(line, ["roomid", "body"])
|
||||||
msg_id = "m%s" % int(time.time())
|
txn_id = "txn%s" % int(time.time())
|
||||||
path = "/rooms/%s/messages/%s/%s" % (urllib.quote(args["roomid"]),
|
path = "/rooms/%s/send/m.room.message/%s" % (urllib.quote(args["roomid"]),
|
||||||
self._usr(),
|
txn_id)
|
||||||
msg_id)
|
|
||||||
body_json = {
|
body_json = {
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": args["body"]
|
"body": args["body"]
|
||||||
|
@ -168,6 +168,46 @@ class RoomStateEventRestServlet(RestServlet):
|
|||||||
defer.returnValue((200, ""))
|
defer.returnValue((200, ""))
|
||||||
|
|
||||||
|
|
||||||
|
class RoomSendEventRestServlet(RestServlet):
|
||||||
|
|
||||||
|
def register(self, http_server):
|
||||||
|
# /rooms/$roomid/send/$event_type[/$txn_id]
|
||||||
|
PATTERN = ("/rooms/(?P<room_id>[^/]*)/send/(?P<event_type>[^/]*)")
|
||||||
|
register_txn_path(self, PATTERN, http_server, with_get=True)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def on_POST(self, request, room_id, event_type):
|
||||||
|
user = yield self.auth.get_user_by_req(request)
|
||||||
|
content = _parse_json(request)
|
||||||
|
|
||||||
|
event = self.event_factory.create_event(
|
||||||
|
etype=event_type,
|
||||||
|
room_id=urllib.unquote(room_id),
|
||||||
|
user_id=user.to_string(),
|
||||||
|
content=content
|
||||||
|
)
|
||||||
|
|
||||||
|
msg_handler = self.handlers.message_handler
|
||||||
|
yield msg_handler.send_message(event)
|
||||||
|
|
||||||
|
defer.returnValue((200, {"event_id": event.event_id}))
|
||||||
|
|
||||||
|
def on_GET(self, request, room_id, event_type, txn_id):
|
||||||
|
return (200, "Not implemented")
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def on_PUT(self, request, room_id, event_type, txn_id):
|
||||||
|
try:
|
||||||
|
defer.returnValue(self.txns.get_client_transaction(request, txn_id))
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
response = yield self.on_POST(request, room_id, event_type)
|
||||||
|
|
||||||
|
self.txns.store_client_transaction(request, txn_id, response)
|
||||||
|
defer.returnValue(response)
|
||||||
|
|
||||||
|
|
||||||
class JoinRoomAliasServlet(RestServlet):
|
class JoinRoomAliasServlet(RestServlet):
|
||||||
PATTERN = client_path_pattern("/join/(?P<room_alias>[^/]+)$")
|
PATTERN = client_path_pattern("/join/(?P<room_alias>[^/]+)$")
|
||||||
|
|
||||||
@ -188,54 +228,6 @@ class JoinRoomAliasServlet(RestServlet):
|
|||||||
defer.returnValue((200, ret_dict))
|
defer.returnValue((200, ret_dict))
|
||||||
|
|
||||||
|
|
||||||
class MessageRestServlet(RestServlet):
|
|
||||||
PATTERN = client_path_pattern("/rooms/(?P<room_id>[^/]*)/messages/"
|
|
||||||
+ "(?P<sender_id>[^/]*)/(?P<msg_id>[^/]*)$")
|
|
||||||
|
|
||||||
def get_event_type(self):
|
|
||||||
return MessageEvent.TYPE
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def on_GET(self, request, room_id, sender_id, msg_id):
|
|
||||||
user = yield self.auth.get_user_by_req(request)
|
|
||||||
|
|
||||||
msg_handler = self.handlers.message_handler
|
|
||||||
msg = yield msg_handler.get_message(room_id=urllib.unquote(room_id),
|
|
||||||
sender_id=urllib.unquote(sender_id),
|
|
||||||
msg_id=msg_id,
|
|
||||||
user_id=user.to_string(),
|
|
||||||
)
|
|
||||||
|
|
||||||
if not msg:
|
|
||||||
raise SynapseError(404, "Message not found.",
|
|
||||||
errcode=Codes.NOT_FOUND)
|
|
||||||
|
|
||||||
defer.returnValue((200, json.loads(msg.content)))
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def on_PUT(self, request, room_id, sender_id, msg_id):
|
|
||||||
user = yield self.auth.get_user_by_req(request)
|
|
||||||
|
|
||||||
if user.to_string() != urllib.unquote(sender_id):
|
|
||||||
raise SynapseError(403, "Must send messages as yourself.",
|
|
||||||
errcode=Codes.FORBIDDEN)
|
|
||||||
|
|
||||||
content = _parse_json(request)
|
|
||||||
|
|
||||||
event = self.event_factory.create_event(
|
|
||||||
etype=self.get_event_type(),
|
|
||||||
room_id=urllib.unquote(room_id),
|
|
||||||
user_id=user.to_string(),
|
|
||||||
msg_id=msg_id,
|
|
||||||
content=content
|
|
||||||
)
|
|
||||||
|
|
||||||
msg_handler = self.handlers.message_handler
|
|
||||||
yield msg_handler.send_message(event)
|
|
||||||
|
|
||||||
defer.returnValue((200, ""))
|
|
||||||
|
|
||||||
|
|
||||||
class FeedbackRestServlet(RestServlet):
|
class FeedbackRestServlet(RestServlet):
|
||||||
PATTERN = client_path_pattern(
|
PATTERN = client_path_pattern(
|
||||||
"/rooms/(?P<room_id>[^/]*)/messages/" +
|
"/rooms/(?P<room_id>[^/]*)/messages/" +
|
||||||
@ -402,7 +394,7 @@ class RoomMembershipRestServlet(RestServlet):
|
|||||||
def on_PUT(self, request, room_id, membership_action, txn_id):
|
def on_PUT(self, request, room_id, membership_action, txn_id):
|
||||||
try:
|
try:
|
||||||
defer.returnValue(self.txns.get_client_transaction(request, txn_id))
|
defer.returnValue(self.txns.get_client_transaction(request, txn_id))
|
||||||
except:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
response = yield self.on_POST(request, room_id, membership_action)
|
response = yield self.on_POST(request, room_id, membership_action)
|
||||||
@ -422,7 +414,7 @@ def _parse_json(request):
|
|||||||
raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
|
raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
|
||||||
|
|
||||||
|
|
||||||
def register_txn_path(servlet, regex_string, http_server):
|
def register_txn_path(servlet, regex_string, http_server, with_get=False):
|
||||||
"""Registers a transaction-based path.
|
"""Registers a transaction-based path.
|
||||||
|
|
||||||
This registers two paths:
|
This registers two paths:
|
||||||
@ -433,6 +425,7 @@ def register_txn_path(servlet, regex_string, http_server):
|
|||||||
regex_string (str): The regex string to register. Must NOT have a
|
regex_string (str): The regex string to register. Must NOT have a
|
||||||
trailing $ as this string will be appended to.
|
trailing $ as this string will be appended to.
|
||||||
http_server : The http_server to register paths with.
|
http_server : The http_server to register paths with.
|
||||||
|
with_get: True to also register respective GET paths for the PUTs.
|
||||||
"""
|
"""
|
||||||
http_server.register_path(
|
http_server.register_path(
|
||||||
"POST",
|
"POST",
|
||||||
@ -444,11 +437,16 @@ def register_txn_path(servlet, regex_string, http_server):
|
|||||||
client_path_pattern(regex_string + "/(?P<txn_id>[^/]*)$"),
|
client_path_pattern(regex_string + "/(?P<txn_id>[^/]*)$"),
|
||||||
servlet.on_PUT
|
servlet.on_PUT
|
||||||
)
|
)
|
||||||
|
if with_get:
|
||||||
|
http_server.register_path(
|
||||||
|
"GET",
|
||||||
|
client_path_pattern(regex_string + "/(?P<txn_id>[^/]*)$"),
|
||||||
|
servlet.on_GET
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def register_servlets(hs, http_server):
|
def register_servlets(hs, http_server):
|
||||||
RoomStateEventRestServlet(hs).register(http_server)
|
RoomStateEventRestServlet(hs).register(http_server)
|
||||||
MessageRestServlet(hs).register(http_server)
|
|
||||||
FeedbackRestServlet(hs).register(http_server)
|
FeedbackRestServlet(hs).register(http_server)
|
||||||
RoomCreateRestServlet(hs).register(http_server)
|
RoomCreateRestServlet(hs).register(http_server)
|
||||||
RoomMemberListRestServlet(hs).register(http_server)
|
RoomMemberListRestServlet(hs).register(http_server)
|
||||||
@ -456,3 +454,4 @@ def register_servlets(hs, http_server):
|
|||||||
JoinRoomAliasServlet(hs).register(http_server)
|
JoinRoomAliasServlet(hs).register(http_server)
|
||||||
RoomTriggerBackfill(hs).register(http_server)
|
RoomTriggerBackfill(hs).register(http_server)
|
||||||
RoomMembershipRestServlet(hs).register(http_server)
|
RoomMembershipRestServlet(hs).register(http_server)
|
||||||
|
RoomSendEventRestServlet(hs).register(http_server)
|
||||||
|
@ -181,7 +181,7 @@ class EventStreamPermissionsTestCase(RestTestCase):
|
|||||||
room_id = "!rid1:test"
|
room_id = "!rid1:test"
|
||||||
yield self.create_room_as(room_id, self.other_user,
|
yield self.create_room_as(room_id, self.other_user,
|
||||||
tok=self.other_token)
|
tok=self.other_token)
|
||||||
yield self.send(room_id, self.other_user, tok=self.other_token)
|
yield self.send(room_id, tok=self.other_token)
|
||||||
|
|
||||||
# invited to room (expect no content for room)
|
# invited to room (expect no content for room)
|
||||||
yield self.invite(room_id, src=self.other_user, targ=self.user_id,
|
yield self.invite(room_id, src=self.other_user, targ=self.user_id,
|
||||||
|
@ -83,8 +83,8 @@ class RoomPermissionsTestCase(RestTestCase):
|
|||||||
is_public=True)
|
is_public=True)
|
||||||
|
|
||||||
# send a message in one of the rooms
|
# send a message in one of the rooms
|
||||||
self.created_rmid_msg_path = ("/rooms/%s/messages/%s/midaaa1" %
|
self.created_rmid_msg_path = ("/rooms/%s/send/m.room.message/a1" %
|
||||||
(self.created_rmid, self.rmcreator_id))
|
(self.created_rmid))
|
||||||
(code, response) = yield self.mock_resource.trigger(
|
(code, response) = yield self.mock_resource.trigger(
|
||||||
"PUT",
|
"PUT",
|
||||||
self.created_rmid_msg_path,
|
self.created_rmid_msg_path,
|
||||||
@ -138,14 +138,14 @@ class RoomPermissionsTestCase(RestTestCase):
|
|||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_send_message(self):
|
def test_send_message(self):
|
||||||
msg_content = '{"msgtype":"m.text","body":"hello"}'
|
msg_content = '{"msgtype":"m.text","body":"hello"}'
|
||||||
send_msg_path = ("/rooms/%s/messages/%s/mid1" %
|
send_msg_path = ("/rooms/%s/send/m.room.message/mid1" %
|
||||||
(self.created_rmid, self.user_id))
|
(self.created_rmid))
|
||||||
|
|
||||||
# send message in uncreated room, expect 403
|
# send message in uncreated room, expect 403
|
||||||
(code, response) = yield self.mock_resource.trigger(
|
(code, response) = yield self.mock_resource.trigger(
|
||||||
"PUT",
|
"PUT",
|
||||||
"/rooms/%s/messages/%s/mid1" %
|
"/rooms/%s/send/m.room.message/mid2" %
|
||||||
(self.uncreated_rmid, self.user_id), msg_content)
|
(self.uncreated_rmid), msg_content)
|
||||||
self.assertEquals(403, code, msg=str(response))
|
self.assertEquals(403, code, msg=str(response))
|
||||||
|
|
||||||
# send message in created room not joined (no state), expect 403
|
# send message in created room not joined (no state), expect 403
|
||||||
@ -875,9 +875,8 @@ class RoomMessagesTestCase(RestTestCase):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_invalid_puts(self):
|
def test_invalid_puts(self):
|
||||||
path = "/rooms/%s/messages/%s/mid1" % (
|
path = "/rooms/%s/send/m.room.message/mid1" % (
|
||||||
urllib.quote(self.room_id), self.user_id
|
urllib.quote(self.room_id))
|
||||||
)
|
|
||||||
# missing keys or invalid json
|
# missing keys or invalid json
|
||||||
(code, response) = yield self.mock_resource.trigger("PUT",
|
(code, response) = yield self.mock_resource.trigger("PUT",
|
||||||
path, '{}')
|
path, '{}')
|
||||||
@ -905,9 +904,8 @@ class RoomMessagesTestCase(RestTestCase):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_rooms_messages_sent(self):
|
def test_rooms_messages_sent(self):
|
||||||
path = "/rooms/%s/messages/%s/mid1" % (
|
path = "/rooms/%s/send/m.room.message/mid1" % (
|
||||||
urllib.quote(self.room_id), self.user_id
|
urllib.quote(self.room_id))
|
||||||
)
|
|
||||||
|
|
||||||
content = '{"body":"test","msgtype":{"type":"a"}}'
|
content = '{"body":"test","msgtype":{"type":"a"}}'
|
||||||
(code, response) = yield self.mock_resource.trigger("PUT", path, content)
|
(code, response) = yield self.mock_resource.trigger("PUT", path, content)
|
||||||
@ -923,9 +921,8 @@ class RoomMessagesTestCase(RestTestCase):
|
|||||||
# self.assert_dict(json.loads(content), response)
|
# self.assert_dict(json.loads(content), response)
|
||||||
|
|
||||||
# m.text message type
|
# m.text message type
|
||||||
path = "/rooms/%s/messages/%s/mid2" % (
|
path = "/rooms/%s/send/m.room.message/mid2" % (
|
||||||
urllib.quote(self.room_id), self.user_id
|
urllib.quote(self.room_id))
|
||||||
)
|
|
||||||
content = '{"body":"test2","msgtype":"m.text"}'
|
content = '{"body":"test2","msgtype":"m.text"}'
|
||||||
(code, response) = yield self.mock_resource.trigger("PUT", path, content)
|
(code, response) = yield self.mock_resource.trigger("PUT", path, content)
|
||||||
self.assertEquals(200, code, msg=str(response))
|
self.assertEquals(200, code, msg=str(response))
|
||||||
@ -933,11 +930,3 @@ class RoomMessagesTestCase(RestTestCase):
|
|||||||
# (code, response) = yield self.mock_resource.trigger("GET", path, None)
|
# (code, response) = yield self.mock_resource.trigger("GET", path, None)
|
||||||
# self.assertEquals(200, code, msg=str(response))
|
# self.assertEquals(200, code, msg=str(response))
|
||||||
# self.assert_dict(json.loads(content), response)
|
# self.assert_dict(json.loads(content), response)
|
||||||
|
|
||||||
# trying to send message in different user path
|
|
||||||
path = "/rooms/%s/messages/%s/mid2" % (
|
|
||||||
urllib.quote(self.room_id), "invalid" + self.user_id
|
|
||||||
)
|
|
||||||
content = '{"body":"test2","msgtype":"m.text"}'
|
|
||||||
(code, response) = yield self.mock_resource.trigger("PUT", path, content)
|
|
||||||
self.assertEquals(403, code, msg=str(response))
|
|
||||||
|
@ -99,14 +99,14 @@ class RestTestCase(unittest.TestCase):
|
|||||||
defer.returnValue(response)
|
defer.returnValue(response)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def send(self, room_id, sender_id, body=None, msg_id=None, tok=None,
|
def send(self, room_id, body=None, txn_id=None, tok=None,
|
||||||
expect_code=200):
|
expect_code=200):
|
||||||
if msg_id is None:
|
if txn_id is None:
|
||||||
msg_id = "m%s" % (str(time.time()))
|
txn_id = "m%s" % (str(time.time()))
|
||||||
if body is None:
|
if body is None:
|
||||||
body = "body_text_here"
|
body = "body_text_here"
|
||||||
|
|
||||||
path = "/rooms/%s/messages/%s/%s" % (room_id, sender_id, msg_id)
|
path = "/rooms/%s/send/m.room.message/%s" % (room_id, txn_id)
|
||||||
content = '{"msgtype":"m.text","body":"%s"}' % body
|
content = '{"msgtype":"m.text","body":"%s"}' % body
|
||||||
if tok:
|
if tok:
|
||||||
path = path + "?access_token=%s" % tok
|
path = path + "?access_token=%s" % tok
|
||||||
|
@ -162,12 +162,12 @@ angular.module('matrixService', [])
|
|||||||
return doRequest("GET", path, undefined, {});
|
return doRequest("GET", path, undefined, {});
|
||||||
},
|
},
|
||||||
|
|
||||||
sendMessage: function(room_id, msg_id, content) {
|
sendMessage: function(room_id, txn_id, content) {
|
||||||
// The REST path spec
|
// The REST path spec
|
||||||
var path = "/rooms/$room_id/messages/$from/$msg_id";
|
var path = "/rooms/$room_id/send/m.room.message/$txn_id";
|
||||||
|
|
||||||
if (!msg_id) {
|
if (!txn_id) {
|
||||||
msg_id = "m" + new Date().getTime();
|
txn_id = "m" + new Date().getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Like the cmd client, escape room ids
|
// Like the cmd client, escape room ids
|
||||||
@ -175,8 +175,7 @@ angular.module('matrixService', [])
|
|||||||
|
|
||||||
// Customize it
|
// Customize it
|
||||||
path = path.replace("$room_id", room_id);
|
path = path.replace("$room_id", room_id);
|
||||||
path = path.replace("$from", config.user_id);
|
path = path.replace("$txn_id", txn_id);
|
||||||
path = path.replace("$msg_id", msg_id);
|
|
||||||
|
|
||||||
return doRequest("PUT", path, undefined, content);
|
return doRequest("PUT", path, undefined, content);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user