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.
 
 
 
 
 
 

212 lines
7.3 KiB

  1. # Copyright 2014-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. import logging
  15. from typing import TYPE_CHECKING, List, Optional, Tuple
  16. from synapse._pydantic_compat import HAS_PYDANTIC_V2
  17. if TYPE_CHECKING or HAS_PYDANTIC_V2:
  18. from pydantic.v1 import StrictStr
  19. else:
  20. from pydantic import StrictStr
  21. from typing_extensions import Literal
  22. from twisted.web.server import Request
  23. from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
  24. from synapse.http.server import HttpServer
  25. from synapse.http.servlet import (
  26. RestServlet,
  27. parse_and_validate_json_object_from_request,
  28. )
  29. from synapse.http.site import SynapseRequest
  30. from synapse.rest.client._base import client_patterns
  31. from synapse.rest.models import RequestBodyModel
  32. from synapse.types import JsonDict, RoomAlias
  33. if TYPE_CHECKING:
  34. from synapse.server import HomeServer
  35. logger = logging.getLogger(__name__)
  36. def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
  37. ClientDirectoryServer(hs).register(http_server)
  38. if hs.config.worker.worker_app is None:
  39. ClientDirectoryListServer(hs).register(http_server)
  40. ClientAppserviceDirectoryListServer(hs).register(http_server)
  41. class ClientDirectoryServer(RestServlet):
  42. PATTERNS = client_patterns("/directory/room/(?P<room_alias>[^/]*)$", v1=True)
  43. CATEGORY = "Client API requests"
  44. def __init__(self, hs: "HomeServer"):
  45. super().__init__()
  46. self.store = hs.get_datastores().main
  47. self.directory_handler = hs.get_directory_handler()
  48. self.auth = hs.get_auth()
  49. async def on_GET(self, request: Request, room_alias: str) -> Tuple[int, JsonDict]:
  50. if not RoomAlias.is_valid(room_alias):
  51. raise SynapseError(400, "Room alias invalid", errcode=Codes.INVALID_PARAM)
  52. room_alias_obj = RoomAlias.from_string(room_alias)
  53. res = await self.directory_handler.get_association(room_alias_obj)
  54. return 200, res
  55. class PutBody(RequestBodyModel):
  56. # TODO: get Pydantic to validate that this is a valid room id?
  57. room_id: StrictStr
  58. # `servers` is unspecced
  59. servers: Optional[List[StrictStr]] = None
  60. async def on_PUT(
  61. self, request: SynapseRequest, room_alias: str
  62. ) -> Tuple[int, JsonDict]:
  63. if not RoomAlias.is_valid(room_alias):
  64. raise SynapseError(400, "Room alias invalid", errcode=Codes.INVALID_PARAM)
  65. room_alias_obj = RoomAlias.from_string(room_alias)
  66. content = parse_and_validate_json_object_from_request(request, self.PutBody)
  67. logger.debug("Got content: %s", content)
  68. logger.debug("Got room name: %s", room_alias_obj.to_string())
  69. logger.debug("Got room_id: %s", content.room_id)
  70. logger.debug("Got servers: %s", content.servers)
  71. room = await self.store.get_room(content.room_id)
  72. if room is None:
  73. raise SynapseError(400, "Room does not exist")
  74. requester = await self.auth.get_user_by_req(request)
  75. await self.directory_handler.create_association(
  76. requester, room_alias_obj, content.room_id, content.servers
  77. )
  78. return 200, {}
  79. async def on_DELETE(
  80. self, request: SynapseRequest, room_alias: str
  81. ) -> Tuple[int, JsonDict]:
  82. if not RoomAlias.is_valid(room_alias):
  83. raise SynapseError(400, "Room alias invalid", errcode=Codes.INVALID_PARAM)
  84. room_alias_obj = RoomAlias.from_string(room_alias)
  85. requester = await self.auth.get_user_by_req(request)
  86. if requester.app_service:
  87. await self.directory_handler.delete_appservice_association(
  88. requester.app_service, room_alias_obj
  89. )
  90. logger.info(
  91. "Application service at %s deleted alias %s",
  92. requester.app_service.url,
  93. room_alias_obj.to_string(),
  94. )
  95. else:
  96. await self.directory_handler.delete_association(requester, room_alias_obj)
  97. logger.info(
  98. "User %s deleted alias %s",
  99. requester.user.to_string(),
  100. room_alias_obj.to_string(),
  101. )
  102. return 200, {}
  103. class ClientDirectoryListServer(RestServlet):
  104. PATTERNS = client_patterns("/directory/list/room/(?P<room_id>[^/]*)$", v1=True)
  105. def __init__(self, hs: "HomeServer"):
  106. super().__init__()
  107. self.store = hs.get_datastores().main
  108. self.directory_handler = hs.get_directory_handler()
  109. self.auth = hs.get_auth()
  110. async def on_GET(self, request: Request, room_id: str) -> Tuple[int, JsonDict]:
  111. room = await self.store.get_room(room_id)
  112. if room is None:
  113. raise NotFoundError("Unknown room")
  114. return 200, {"visibility": "public" if room["is_public"] else "private"}
  115. class PutBody(RequestBodyModel):
  116. visibility: Literal["public", "private"] = "public"
  117. async def on_PUT(
  118. self, request: SynapseRequest, room_id: str
  119. ) -> Tuple[int, JsonDict]:
  120. requester = await self.auth.get_user_by_req(request)
  121. content = parse_and_validate_json_object_from_request(request, self.PutBody)
  122. await self.directory_handler.edit_published_room_list(
  123. requester, room_id, content.visibility
  124. )
  125. return 200, {}
  126. class ClientAppserviceDirectoryListServer(RestServlet):
  127. PATTERNS = client_patterns(
  128. "/directory/list/appservice/(?P<network_id>[^/]*)/(?P<room_id>[^/]*)$", v1=True
  129. )
  130. def __init__(self, hs: "HomeServer"):
  131. super().__init__()
  132. self.store = hs.get_datastores().main
  133. self.directory_handler = hs.get_directory_handler()
  134. self.auth = hs.get_auth()
  135. class PutBody(RequestBodyModel):
  136. visibility: Literal["public", "private"] = "public"
  137. async def on_PUT(
  138. self, request: SynapseRequest, network_id: str, room_id: str
  139. ) -> Tuple[int, JsonDict]:
  140. content = parse_and_validate_json_object_from_request(request, self.PutBody)
  141. return await self._edit(request, network_id, room_id, content.visibility)
  142. async def on_DELETE(
  143. self, request: SynapseRequest, network_id: str, room_id: str
  144. ) -> Tuple[int, JsonDict]:
  145. return await self._edit(request, network_id, room_id, "private")
  146. async def _edit(
  147. self,
  148. request: SynapseRequest,
  149. network_id: str,
  150. room_id: str,
  151. visibility: Literal["public", "private"],
  152. ) -> Tuple[int, JsonDict]:
  153. requester = await self.auth.get_user_by_req(request)
  154. if not requester.app_service:
  155. raise AuthError(
  156. 403, "Only appservices can edit the appservice published room list"
  157. )
  158. await self.directory_handler.edit_published_appservice_room_list(
  159. requester.app_service.id, network_id, room_id, visibility
  160. )
  161. return 200, {}