account_data.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from ._base import client_v2_patterns
  16. from synapse.http.servlet import RestServlet, parse_json_object_from_request
  17. from synapse.api.errors import AuthError
  18. from twisted.internet import defer
  19. import logging
  20. logger = logging.getLogger(__name__)
  21. class AccountDataServlet(RestServlet):
  22. """
  23. PUT /user/{user_id}/account_data/{account_dataType} HTTP/1.1
  24. """
  25. PATTERNS = client_v2_patterns(
  26. "/user/(?P<user_id>[^/]*)/account_data/(?P<account_data_type>[^/]*)"
  27. )
  28. def __init__(self, hs):
  29. super(AccountDataServlet, self).__init__()
  30. self.auth = hs.get_auth()
  31. self.store = hs.get_datastore()
  32. self.notifier = hs.get_notifier()
  33. @defer.inlineCallbacks
  34. def on_PUT(self, request, user_id, account_data_type):
  35. requester = yield self.auth.get_user_by_req(request)
  36. if user_id != requester.user.to_string():
  37. raise AuthError(403, "Cannot add account data for other users.")
  38. body = parse_json_object_from_request(request)
  39. max_id = yield self.store.add_account_data_for_user(
  40. user_id, account_data_type, body
  41. )
  42. self.notifier.on_new_event(
  43. "account_data_key", max_id, users=[user_id]
  44. )
  45. defer.returnValue((200, {}))
  46. class RoomAccountDataServlet(RestServlet):
  47. """
  48. PUT /user/{user_id}/rooms/{room_id}/account_data/{account_dataType} HTTP/1.1
  49. """
  50. PATTERNS = client_v2_patterns(
  51. "/user/(?P<user_id>[^/]*)"
  52. "/rooms/(?P<room_id>[^/]*)"
  53. "/account_data/(?P<account_data_type>[^/]*)"
  54. )
  55. def __init__(self, hs):
  56. super(RoomAccountDataServlet, self).__init__()
  57. self.auth = hs.get_auth()
  58. self.store = hs.get_datastore()
  59. self.notifier = hs.get_notifier()
  60. @defer.inlineCallbacks
  61. def on_PUT(self, request, user_id, room_id, account_data_type):
  62. requester = yield self.auth.get_user_by_req(request)
  63. if user_id != requester.user.to_string():
  64. raise AuthError(403, "Cannot add account data for other users.")
  65. body = parse_json_object_from_request(request)
  66. max_id = yield self.store.add_account_data_to_room(
  67. user_id, room_id, account_data_type, body
  68. )
  69. self.notifier.on_new_event(
  70. "account_data_key", max_id, users=[user_id]
  71. )
  72. defer.returnValue((200, {}))
  73. def register_servlets(hs, http_server):
  74. AccountDataServlet(hs).register(http_server)
  75. RoomAccountDataServlet(hs).register(http_server)