presence_resource.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.http.server import respond_with_json_bytes, request_handler
  15. from synapse.http.servlet import parse_json_object_from_request
  16. from twisted.web.resource import Resource
  17. from twisted.web.server import NOT_DONE_YET
  18. from twisted.internet import defer
  19. class PresenceResource(Resource):
  20. """
  21. HTTP endpoint for marking users as syncing.
  22. POST /_synapse/replication/presence HTTP/1.1
  23. Content-Type: application/json
  24. {
  25. "process_id": "<process_id>",
  26. "syncing_users": ["<user_id>"]
  27. }
  28. """
  29. def __init__(self, hs):
  30. Resource.__init__(self) # Resource is old-style, so no super()
  31. self.version_string = hs.version_string
  32. self.clock = hs.get_clock()
  33. self.presence_handler = hs.get_presence_handler()
  34. def render_POST(self, request):
  35. self._async_render_POST(request)
  36. return NOT_DONE_YET
  37. @request_handler()
  38. @defer.inlineCallbacks
  39. def _async_render_POST(self, request):
  40. content = parse_json_object_from_request(request)
  41. process_id = content["process_id"]
  42. syncing_user_ids = content["syncing_users"]
  43. yield self.presence_handler.update_external_syncs(
  44. process_id, set(syncing_user_ids)
  45. )
  46. respond_with_json_bytes(request, 200, "{}")