Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add GET account data routes #4303

Merged
merged 4 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4303.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add routes for reading account data.
34 changes: 33 additions & 1 deletion synapse/rest/client/v2_alpha/account_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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 ._base import client_v2_patterns
Expand All @@ -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<user_id>[^/]*)/account_data/(?P<account_data_type>[^/]*)"
Expand Down Expand Up @@ -57,10 +58,26 @@ 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 NotFoundError("Account data 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<user_id>[^/]*)"
Expand Down Expand Up @@ -99,6 +116,21 @@ 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 NotFoundError("Room account data not found")

defer.returnValue((200, event))


def register_servlets(hs, http_server):
AccountDataServlet(hs).register(http_server)
Expand Down