receipts.py 1.9 KB

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