account_data.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. import logging
  16. from twisted.internet import defer
  17. from synapse.api.errors import AuthError, NotFoundError, SynapseError
  18. from synapse.http.servlet import RestServlet, parse_json_object_from_request
  19. from ._base import client_patterns
  20. logger = logging.getLogger(__name__)
  21. class AccountDataServlet(RestServlet):
  22. """
  23. PUT /user/{user_id}/account_data/{account_dataType} HTTP/1.1
  24. GET /user/{user_id}/account_data/{account_dataType} HTTP/1.1
  25. """
  26. PATTERNS = client_patterns(
  27. "/user/(?P<user_id>[^/]*)/account_data/(?P<account_data_type>[^/]*)"
  28. )
  29. def __init__(self, hs):
  30. super(AccountDataServlet, self).__init__()
  31. self.auth = hs.get_auth()
  32. self.store = hs.get_datastore()
  33. self.notifier = hs.get_notifier()
  34. @defer.inlineCallbacks
  35. def on_PUT(self, request, user_id, account_data_type):
  36. requester = yield self.auth.get_user_by_req(request)
  37. if user_id != requester.user.to_string():
  38. raise AuthError(403, "Cannot add account data for other users.")
  39. body = parse_json_object_from_request(request)
  40. max_id = yield self.store.add_account_data_for_user(
  41. user_id, account_data_type, body
  42. )
  43. self.notifier.on_new_event("account_data_key", max_id, users=[user_id])
  44. return (200, {})
  45. @defer.inlineCallbacks
  46. def on_GET(self, request, user_id, account_data_type):
  47. requester = yield self.auth.get_user_by_req(request)
  48. if user_id != requester.user.to_string():
  49. raise AuthError(403, "Cannot get account data for other users.")
  50. event = yield self.store.get_global_account_data_by_type_for_user(
  51. account_data_type, user_id
  52. )
  53. if event is None:
  54. raise NotFoundError("Account data not found")
  55. return (200, event)
  56. class RoomAccountDataServlet(RestServlet):
  57. """
  58. PUT /user/{user_id}/rooms/{room_id}/account_data/{account_dataType} HTTP/1.1
  59. GET /user/{user_id}/rooms/{room_id}/account_data/{account_dataType} HTTP/1.1
  60. """
  61. PATTERNS = client_patterns(
  62. "/user/(?P<user_id>[^/]*)"
  63. "/rooms/(?P<room_id>[^/]*)"
  64. "/account_data/(?P<account_data_type>[^/]*)"
  65. )
  66. def __init__(self, hs):
  67. super(RoomAccountDataServlet, self).__init__()
  68. self.auth = hs.get_auth()
  69. self.store = hs.get_datastore()
  70. self.notifier = hs.get_notifier()
  71. @defer.inlineCallbacks
  72. def on_PUT(self, request, user_id, room_id, account_data_type):
  73. requester = yield self.auth.get_user_by_req(request)
  74. if user_id != requester.user.to_string():
  75. raise AuthError(403, "Cannot add account data for other users.")
  76. body = parse_json_object_from_request(request)
  77. if account_data_type == "m.fully_read":
  78. raise SynapseError(
  79. 405,
  80. "Cannot set m.fully_read through this API."
  81. " Use /rooms/!roomId:server.name/read_markers",
  82. )
  83. max_id = yield self.store.add_account_data_to_room(
  84. user_id, room_id, account_data_type, body
  85. )
  86. self.notifier.on_new_event("account_data_key", max_id, users=[user_id])
  87. return (200, {})
  88. @defer.inlineCallbacks
  89. def on_GET(self, request, user_id, room_id, account_data_type):
  90. requester = yield self.auth.get_user_by_req(request)
  91. if user_id != requester.user.to_string():
  92. raise AuthError(403, "Cannot get account data for other users.")
  93. event = yield self.store.get_account_data_for_room_and_type(
  94. user_id, room_id, account_data_type
  95. )
  96. if event is None:
  97. raise NotFoundError("Room account data not found")
  98. return (200, event)
  99. def register_servlets(hs, http_server):
  100. AccountDataServlet(hs).register(http_server)
  101. RoomAccountDataServlet(hs).register(http_server)