You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

104 lines
3.9 KiB

  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  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 typing import TYPE_CHECKING, Dict, Iterable, Set, Union
  15. from synapse.api.presence import UserPresenceState
  16. if TYPE_CHECKING:
  17. from synapse.server import HomeServer
  18. class PresenceRouter:
  19. """
  20. A module that the homeserver will call upon to help route user presence updates to
  21. additional destinations. If a custom presence router is configured, calls will be
  22. passed to that instead.
  23. """
  24. ALL_USERS = "ALL"
  25. def __init__(self, hs: "HomeServer"):
  26. self.custom_presence_router = None
  27. # Check whether a custom presence router module has been configured
  28. if hs.config.presence_router_module_class:
  29. # Initialise the module
  30. self.custom_presence_router = hs.config.presence_router_module_class(
  31. config=hs.config.presence_router_config, module_api=hs.get_module_api()
  32. )
  33. # Ensure the module has implemented the required methods
  34. required_methods = ["get_users_for_states", "get_interested_users"]
  35. for method_name in required_methods:
  36. if not hasattr(self.custom_presence_router, method_name):
  37. raise Exception(
  38. "PresenceRouter module '%s' must implement all required methods: %s"
  39. % (
  40. hs.config.presence_router_module_class.__name__,
  41. ", ".join(required_methods),
  42. )
  43. )
  44. async def get_users_for_states(
  45. self,
  46. state_updates: Iterable[UserPresenceState],
  47. ) -> Dict[str, Set[UserPresenceState]]:
  48. """
  49. Given an iterable of user presence updates, determine where each one
  50. needs to go.
  51. Args:
  52. state_updates: An iterable of user presence state updates.
  53. Returns:
  54. A dictionary of user_id -> set of UserPresenceState, indicating which
  55. presence updates each user should receive.
  56. """
  57. if self.custom_presence_router is not None:
  58. # Ask the custom module
  59. return await self.custom_presence_router.get_users_for_states(
  60. state_updates=state_updates
  61. )
  62. # Don't include any extra destinations for presence updates
  63. return {}
  64. async def get_interested_users(self, user_id: str) -> Union[Set[str], ALL_USERS]:
  65. """
  66. Retrieve a list of users that `user_id` is interested in receiving the
  67. presence of. This will be in addition to those they share a room with.
  68. Optionally, the object PresenceRouter.ALL_USERS can be returned to indicate
  69. that this user should receive all incoming local and remote presence updates.
  70. Note that this method will only be called for local users, but can return users
  71. that are local or remote.
  72. Args:
  73. user_id: A user requesting presence updates.
  74. Returns:
  75. A set of user IDs to return presence updates for, or ALL_USERS to return all
  76. known updates.
  77. """
  78. if self.custom_presence_router is not None:
  79. # Ask the custom module for interested users
  80. return await self.custom_presence_router.get_interested_users(
  81. user_id=user_id
  82. )
  83. # A custom presence router is not defined.
  84. # Don't report any additional interested users
  85. return set()