From e8df9e7ffe9600534d1cb3ad2f5389dbc73e9406 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 17 Dec 2018 19:27:07 -0700 Subject: [PATCH 1/4] Add GET account data routes As per https://github.com/matrix-org/matrix-doc/issues/1339 --- synapse/rest/client/v2_alpha/account_data.py | 38 +++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/synapse/rest/client/v2_alpha/account_data.py b/synapse/rest/client/v2_alpha/account_data.py index 371e9aa354b9..2fe9f86f2fec 100644 --- a/synapse/rest/client/v2_alpha/account_data.py +++ b/synapse/rest/client/v2_alpha/account_data.py @@ -17,7 +17,7 @@ from twisted.internet import defer -from synapse.api.errors import AuthError, SynapseError +from synapse.api.errors import AuthError, SynapseError, Codes from synapse.http.servlet import RestServlet, parse_json_object_from_request from ._base import client_v2_patterns @@ -28,6 +28,7 @@ class AccountDataServlet(RestServlet): """ PUT /user/{user_id}/account_data/{account_dataType} HTTP/1.1 + GET /user/{user_id}/account_data/{account_dataType} HTTP/1.1 """ PATTERNS = client_v2_patterns( "/user/(?P[^/]*)/account_data/(?P[^/]*)" @@ -57,10 +58,28 @@ def on_PUT(self, request, user_id, account_data_type): defer.returnValue((200, {})) + @defer.inlineCallbacks + def on_GET(self, request, user_id, account_data_type): + requester = yield self.auth.get_user_by_req(request) + if user_id != requester.user.to_string(): + raise AuthError(403, "Cannot get account data for other users.") + + event = yield self.store.get_global_account_data_by_type_for_user( + account_data_type, user_id, + ) + + if event is None: + raise SynapseError( + 404, "Account data not found", Codes.NOT_FOUND + ) + + defer.returnValue((200, event)) + class RoomAccountDataServlet(RestServlet): """ PUT /user/{user_id}/rooms/{room_id}/account_data/{account_dataType} HTTP/1.1 + GET /user/{user_id}/rooms/{room_id}/account_data/{account_dataType} HTTP/1.1 """ PATTERNS = client_v2_patterns( "/user/(?P[^/]*)" @@ -99,6 +118,23 @@ def on_PUT(self, request, user_id, room_id, account_data_type): defer.returnValue((200, {})) + @defer.inlineCallbacks + def on_GET(self, request, user_id, room_id, account_data_type): + requester = yield self.auth.get_user_by_req(request) + if user_id != requester.user.to_string(): + raise AuthError(403, "Cannot get account data for other users.") + + event = yield self.store.get_account_data_for_room_and_type( + user_id, room_id, account_data_type, + ) + + if event is None: + raise SynapseError( + 404, "Room account data not found", Codes.NOT_FOUND + ) + + defer.returnValue((200, event)) + def register_servlets(hs, http_server): AccountDataServlet(hs).register(http_server) From 3b0c98a9bae43dc63ee8deb6c1ebf4f066ecba92 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 17 Dec 2018 19:37:18 -0700 Subject: [PATCH 2/4] Changelog --- changelog.d/4303.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/4303.feature diff --git a/changelog.d/4303.feature b/changelog.d/4303.feature new file mode 100644 index 000000000000..1a563d4e646f --- /dev/null +++ b/changelog.d/4303.feature @@ -0,0 +1 @@ +Add routes for reading account data. From b4f84a63d0ebb4c3076c0ba843bf465fbad6f365 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 17 Dec 2018 19:53:39 -0700 Subject: [PATCH 3/4] Fix imports --- synapse/rest/client/v2_alpha/account_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/client/v2_alpha/account_data.py b/synapse/rest/client/v2_alpha/account_data.py index 2fe9f86f2fec..b9fa7828d5b0 100644 --- a/synapse/rest/client/v2_alpha/account_data.py +++ b/synapse/rest/client/v2_alpha/account_data.py @@ -17,7 +17,7 @@ from twisted.internet import defer -from synapse.api.errors import AuthError, SynapseError, Codes +from synapse.api.errors import AuthError, Codes, SynapseError from synapse.http.servlet import RestServlet, parse_json_object_from_request from ._base import client_v2_patterns From bfba9287cedf7def6c2916a78af4b7596f753bf2 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 18 Dec 2018 10:57:10 -0700 Subject: [PATCH 4/4] Use NotFoundError instead of constructing a 404 ourselves --- synapse/rest/client/v2_alpha/account_data.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/synapse/rest/client/v2_alpha/account_data.py b/synapse/rest/client/v2_alpha/account_data.py index b9fa7828d5b0..f171b8d6269e 100644 --- a/synapse/rest/client/v2_alpha/account_data.py +++ b/synapse/rest/client/v2_alpha/account_data.py @@ -17,7 +17,7 @@ from twisted.internet import defer -from synapse.api.errors import AuthError, Codes, SynapseError +from synapse.api.errors import AuthError, NotFoundError, SynapseError from synapse.http.servlet import RestServlet, parse_json_object_from_request from ._base import client_v2_patterns @@ -69,9 +69,7 @@ def on_GET(self, request, user_id, account_data_type): ) if event is None: - raise SynapseError( - 404, "Account data not found", Codes.NOT_FOUND - ) + raise NotFoundError("Account data not found") defer.returnValue((200, event)) @@ -129,9 +127,7 @@ def on_GET(self, request, user_id, room_id, account_data_type): ) if event is None: - raise SynapseError( - 404, "Room account data not found", Codes.NOT_FOUND - ) + raise NotFoundError("Room account data not found") defer.returnValue((200, event))