Add GET account data routes (#4303)

As per https://github.com/matrix-org/matrix-doc/issues/1339
This commit is contained in:
Travis Ralston 2019-01-07 03:27:54 -07:00 committed by Richard van der Hoff
parent b7c0218812
commit bc1fa8cd01
2 changed files with 34 additions and 1 deletions

1
changelog.d/4303.feature Normal file
View File

@ -0,0 +1 @@
Add routes for reading account data.

View File

@ -17,7 +17,7 @@ import logging
from twisted.internet import defer from twisted.internet import defer
from synapse.api.errors import AuthError, SynapseError from synapse.api.errors import AuthError, NotFoundError, SynapseError
from synapse.http.servlet import RestServlet, parse_json_object_from_request from synapse.http.servlet import RestServlet, parse_json_object_from_request
from ._base import client_v2_patterns from ._base import client_v2_patterns
@ -28,6 +28,7 @@ logger = logging.getLogger(__name__)
class AccountDataServlet(RestServlet): class AccountDataServlet(RestServlet):
""" """
PUT /user/{user_id}/account_data/{account_dataType} HTTP/1.1 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( PATTERNS = client_v2_patterns(
"/user/(?P<user_id>[^/]*)/account_data/(?P<account_data_type>[^/]*)" "/user/(?P<user_id>[^/]*)/account_data/(?P<account_data_type>[^/]*)"
@ -57,10 +58,26 @@ class AccountDataServlet(RestServlet):
defer.returnValue((200, {})) 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 NotFoundError("Account data not found")
defer.returnValue((200, event))
class RoomAccountDataServlet(RestServlet): class RoomAccountDataServlet(RestServlet):
""" """
PUT /user/{user_id}/rooms/{room_id}/account_data/{account_dataType} HTTP/1.1 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( PATTERNS = client_v2_patterns(
"/user/(?P<user_id>[^/]*)" "/user/(?P<user_id>[^/]*)"
@ -99,6 +116,21 @@ class RoomAccountDataServlet(RestServlet):
defer.returnValue((200, {})) 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 NotFoundError("Room account data not found")
defer.returnValue((200, event))
def register_servlets(hs, http_server): def register_servlets(hs, http_server):
AccountDataServlet(hs).register(http_server) AccountDataServlet(hs).register(http_server)