groups_local.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Dict, List, Tuple, Type
  15. from synapse.api.errors import SynapseError
  16. from synapse.federation.transport.server._base import (
  17. Authenticator,
  18. BaseFederationServlet,
  19. )
  20. from synapse.handlers.groups_local import GroupsLocalHandler
  21. from synapse.server import HomeServer
  22. from synapse.types import JsonDict, get_domain_from_id
  23. from synapse.util.ratelimitutils import FederationRateLimiter
  24. class BaseGroupsLocalServlet(BaseFederationServlet):
  25. """Abstract base class for federation servlet classes which provides a groups local handler.
  26. See BaseFederationServlet for more information.
  27. """
  28. def __init__(
  29. self,
  30. hs: HomeServer,
  31. authenticator: Authenticator,
  32. ratelimiter: FederationRateLimiter,
  33. server_name: str,
  34. ):
  35. super().__init__(hs, authenticator, ratelimiter, server_name)
  36. self.handler = hs.get_groups_local_handler()
  37. class FederationGroupsLocalInviteServlet(BaseGroupsLocalServlet):
  38. """A group server has invited a local user"""
  39. PATH = "/groups/local/(?P<group_id>[^/]*)/users/(?P<user_id>[^/]*)/invite"
  40. async def on_POST(
  41. self,
  42. origin: str,
  43. content: JsonDict,
  44. query: Dict[bytes, List[bytes]],
  45. group_id: str,
  46. user_id: str,
  47. ) -> Tuple[int, JsonDict]:
  48. if get_domain_from_id(group_id) != origin:
  49. raise SynapseError(403, "group_id doesn't match origin")
  50. assert isinstance(
  51. self.handler, GroupsLocalHandler
  52. ), "Workers cannot handle group invites."
  53. new_content = await self.handler.on_invite(group_id, user_id, content)
  54. return 200, new_content
  55. class FederationGroupsRemoveLocalUserServlet(BaseGroupsLocalServlet):
  56. """A group server has removed a local user"""
  57. PATH = "/groups/local/(?P<group_id>[^/]*)/users/(?P<user_id>[^/]*)/remove"
  58. async def on_POST(
  59. self,
  60. origin: str,
  61. content: JsonDict,
  62. query: Dict[bytes, List[bytes]],
  63. group_id: str,
  64. user_id: str,
  65. ) -> Tuple[int, None]:
  66. if get_domain_from_id(group_id) != origin:
  67. raise SynapseError(403, "user_id doesn't match origin")
  68. assert isinstance(
  69. self.handler, GroupsLocalHandler
  70. ), "Workers cannot handle group removals."
  71. await self.handler.user_removed_from_group(group_id, user_id, content)
  72. return 200, None
  73. class FederationGroupsBulkPublicisedServlet(BaseGroupsLocalServlet):
  74. """Get roles in a group"""
  75. PATH = "/get_groups_publicised"
  76. async def on_POST(
  77. self, origin: str, content: JsonDict, query: Dict[bytes, List[bytes]]
  78. ) -> Tuple[int, JsonDict]:
  79. resp = await self.handler.bulk_get_publicised_groups(
  80. content["user_ids"], proxy=False
  81. )
  82. return 200, resp
  83. GROUP_LOCAL_SERVLET_CLASSES: Tuple[Type[BaseFederationServlet], ...] = (
  84. FederationGroupsLocalInviteServlet,
  85. FederationGroupsRemoveLocalUserServlet,
  86. FederationGroupsBulkPublicisedServlet,
  87. )