presence.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 typing import TYPE_CHECKING
  17. from synapse.http.servlet import parse_json_object_from_request
  18. from synapse.replication.http._base import ReplicationEndpoint
  19. from synapse.types import UserID
  20. if TYPE_CHECKING:
  21. from synapse.server import HomeServer
  22. logger = logging.getLogger(__name__)
  23. class ReplicationBumpPresenceActiveTime(ReplicationEndpoint):
  24. """We've seen the user do something that indicates they're interacting
  25. with the app.
  26. The POST looks like:
  27. POST /_synapse/replication/bump_presence_active_time/<user_id>
  28. 200 OK
  29. {}
  30. """
  31. NAME = "bump_presence_active_time"
  32. PATH_ARGS = ("user_id",)
  33. METHOD = "POST"
  34. CACHE = False
  35. def __init__(self, hs: "HomeServer"):
  36. super().__init__(hs)
  37. self._presence_handler = hs.get_presence_handler()
  38. @staticmethod
  39. async def _serialize_payload(user_id):
  40. return {}
  41. async def _handle_request(self, request, user_id):
  42. await self._presence_handler.bump_presence_active_time(
  43. UserID.from_string(user_id)
  44. )
  45. return (
  46. 200,
  47. {},
  48. )
  49. class ReplicationPresenceSetState(ReplicationEndpoint):
  50. """Set the presence state for a user.
  51. The POST looks like:
  52. POST /_synapse/replication/presence_set_state/<user_id>
  53. {
  54. "state": { ... },
  55. "ignore_status_msg": false,
  56. }
  57. 200 OK
  58. {}
  59. """
  60. NAME = "presence_set_state"
  61. PATH_ARGS = ("user_id",)
  62. METHOD = "POST"
  63. CACHE = False
  64. def __init__(self, hs: "HomeServer"):
  65. super().__init__(hs)
  66. self._presence_handler = hs.get_presence_handler()
  67. @staticmethod
  68. async def _serialize_payload(user_id, state, ignore_status_msg=False):
  69. return {
  70. "state": state,
  71. "ignore_status_msg": ignore_status_msg,
  72. }
  73. async def _handle_request(self, request, user_id):
  74. content = parse_json_object_from_request(request)
  75. await self._presence_handler.set_state(
  76. UserID.from_string(user_id), content["state"], content["ignore_status_msg"]
  77. )
  78. return (
  79. 200,
  80. {},
  81. )
  82. def register_servlets(hs, http_server):
  83. ReplicationBumpPresenceActiveTime(hs).register(http_server)
  84. ReplicationPresenceSetState(hs).register(http_server)