receipts.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 SynapseError
  17. from synapse.http.servlet import RestServlet
  18. from ._base import client_patterns
  19. logger = logging.getLogger(__name__)
  20. class ReceiptRestServlet(RestServlet):
  21. PATTERNS = client_patterns(
  22. "/rooms/(?P<room_id>[^/]*)"
  23. "/receipt/(?P<receipt_type>[^/]*)"
  24. "/(?P<event_id>[^/]*)$"
  25. )
  26. def __init__(self, hs):
  27. super().__init__()
  28. self.hs = hs
  29. self.auth = hs.get_auth()
  30. self.receipts_handler = hs.get_receipts_handler()
  31. self.presence_handler = hs.get_presence_handler()
  32. async def on_POST(self, request, room_id, receipt_type, event_id):
  33. requester = await self.auth.get_user_by_req(request)
  34. if receipt_type != "m.read":
  35. raise SynapseError(400, "Receipt type must be 'm.read'")
  36. await self.presence_handler.bump_presence_active_time(requester.user)
  37. await self.receipts_handler.received_client_receipt(
  38. room_id, receipt_type, user_id=requester.user.to_string(), event_id=event_id
  39. )
  40. return 200, {}
  41. def register_servlets(hs, http_server):
  42. ReceiptRestServlet(hs).register(http_server)