tags.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 synapse.api.errors import AuthError
  17. from synapse.http.servlet import RestServlet, parse_json_object_from_request
  18. from ._base import client_patterns
  19. logger = logging.getLogger(__name__)
  20. class TagListServlet(RestServlet):
  21. """
  22. GET /user/{user_id}/rooms/{room_id}/tags HTTP/1.1
  23. """
  24. PATTERNS = client_patterns("/user/(?P<user_id>[^/]*)/rooms/(?P<room_id>[^/]*)/tags")
  25. def __init__(self, hs):
  26. super().__init__()
  27. self.auth = hs.get_auth()
  28. self.store = hs.get_datastore()
  29. async def on_GET(self, request, user_id, room_id):
  30. requester = await self.auth.get_user_by_req(request)
  31. if user_id != requester.user.to_string():
  32. raise AuthError(403, "Cannot get tags for other users.")
  33. tags = await self.store.get_tags_for_room(user_id, room_id)
  34. return 200, {"tags": tags}
  35. class TagServlet(RestServlet):
  36. """
  37. PUT /user/{user_id}/rooms/{room_id}/tags/{tag} HTTP/1.1
  38. DELETE /user/{user_id}/rooms/{room_id}/tags/{tag} HTTP/1.1
  39. """
  40. PATTERNS = client_patterns(
  41. "/user/(?P<user_id>[^/]*)/rooms/(?P<room_id>[^/]*)/tags/(?P<tag>[^/]*)"
  42. )
  43. def __init__(self, hs):
  44. super().__init__()
  45. self.auth = hs.get_auth()
  46. self.handler = hs.get_account_data_handler()
  47. async def on_PUT(self, request, user_id, room_id, tag):
  48. requester = await self.auth.get_user_by_req(request)
  49. if user_id != requester.user.to_string():
  50. raise AuthError(403, "Cannot add tags for other users.")
  51. body = parse_json_object_from_request(request)
  52. await self.handler.add_tag_to_room(user_id, room_id, tag, body)
  53. return 200, {}
  54. async def on_DELETE(self, request, user_id, room_id, tag):
  55. requester = await self.auth.get_user_by_req(request)
  56. if user_id != requester.user.to_string():
  57. raise AuthError(403, "Cannot add tags for other users.")
  58. await self.handler.remove_tag_from_room(user_id, room_id, tag)
  59. return 200, {}
  60. def register_servlets(hs, http_server):
  61. TagListServlet(hs).register(http_server)
  62. TagServlet(hs).register(http_server)