tags.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  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 TagListServlet(RestServlet):
  22. """
  23. GET /user/{user_id}/rooms/{room_id}/tags HTTP/1.1
  24. """
  25. PATTERNS = client_patterns("/user/(?P<user_id>[^/]*)/rooms/(?P<room_id>[^/]*)/tags")
  26. def __init__(self, hs):
  27. super(TagListServlet, self).__init__()
  28. self.auth = hs.get_auth()
  29. self.store = hs.get_datastore()
  30. @defer.inlineCallbacks
  31. def on_GET(self, request, user_id, room_id):
  32. requester = yield self.auth.get_user_by_req(request)
  33. if user_id != requester.user.to_string():
  34. raise AuthError(403, "Cannot get tags for other users.")
  35. tags = yield self.store.get_tags_for_room(user_id, room_id)
  36. return (200, {"tags": tags})
  37. class TagServlet(RestServlet):
  38. """
  39. PUT /user/{user_id}/rooms/{room_id}/tags/{tag} HTTP/1.1
  40. DELETE /user/{user_id}/rooms/{room_id}/tags/{tag} HTTP/1.1
  41. """
  42. PATTERNS = client_patterns(
  43. "/user/(?P<user_id>[^/]*)/rooms/(?P<room_id>[^/]*)/tags/(?P<tag>[^/]*)"
  44. )
  45. def __init__(self, hs):
  46. super(TagServlet, self).__init__()
  47. self.auth = hs.get_auth()
  48. self.store = hs.get_datastore()
  49. self.notifier = hs.get_notifier()
  50. @defer.inlineCallbacks
  51. def on_PUT(self, request, user_id, room_id, tag):
  52. requester = yield self.auth.get_user_by_req(request)
  53. if user_id != requester.user.to_string():
  54. raise AuthError(403, "Cannot add tags for other users.")
  55. body = parse_json_object_from_request(request)
  56. max_id = yield self.store.add_tag_to_room(user_id, room_id, tag, body)
  57. self.notifier.on_new_event("account_data_key", max_id, users=[user_id])
  58. return (200, {})
  59. @defer.inlineCallbacks
  60. def on_DELETE(self, request, user_id, room_id, tag):
  61. requester = yield self.auth.get_user_by_req(request)
  62. if user_id != requester.user.to_string():
  63. raise AuthError(403, "Cannot add tags for other users.")
  64. max_id = yield self.store.remove_tag_from_room(user_id, room_id, tag)
  65. self.notifier.on_new_event("account_data_key", max_id, users=[user_id])
  66. return (200, {})
  67. def register_servlets(hs, http_server):
  68. TagListServlet(hs).register(http_server)
  69. TagServlet(hs).register(http_server)