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.
 
 
 
 
 
 

291 lines
8.0 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. import logging
  15. from typing import TYPE_CHECKING, Tuple
  16. from twisted.web.server import Request
  17. from synapse.http.server import HttpServer
  18. from synapse.replication.http._base import ReplicationEndpoint
  19. from synapse.types import JsonDict
  20. if TYPE_CHECKING:
  21. from synapse.server import HomeServer
  22. logger = logging.getLogger(__name__)
  23. class ReplicationAddUserAccountDataRestServlet(ReplicationEndpoint):
  24. """Add user account data on the appropriate account data worker.
  25. Request format:
  26. POST /_synapse/replication/add_user_account_data/:user_id/:type
  27. {
  28. "content": { ... },
  29. }
  30. """
  31. NAME = "add_user_account_data"
  32. PATH_ARGS = ("user_id", "account_data_type")
  33. CACHE = False
  34. def __init__(self, hs: "HomeServer"):
  35. super().__init__(hs)
  36. self.handler = hs.get_account_data_handler()
  37. @staticmethod
  38. async def _serialize_payload( # type: ignore[override]
  39. user_id: str, account_data_type: str, content: JsonDict
  40. ) -> JsonDict:
  41. payload = {
  42. "content": content,
  43. }
  44. return payload
  45. async def _handle_request( # type: ignore[override]
  46. self, request: Request, content: JsonDict, user_id: str, account_data_type: str
  47. ) -> Tuple[int, JsonDict]:
  48. max_stream_id = await self.handler.add_account_data_for_user(
  49. user_id, account_data_type, content["content"]
  50. )
  51. return 200, {"max_stream_id": max_stream_id}
  52. class ReplicationRemoveUserAccountDataRestServlet(ReplicationEndpoint):
  53. """Remove user account data on the appropriate account data worker.
  54. Request format:
  55. POST /_synapse/replication/remove_user_account_data/:user_id/:type
  56. {
  57. "content": { ... },
  58. }
  59. """
  60. NAME = "remove_user_account_data"
  61. PATH_ARGS = ("user_id", "account_data_type")
  62. CACHE = False
  63. def __init__(self, hs: "HomeServer"):
  64. super().__init__(hs)
  65. self.handler = hs.get_account_data_handler()
  66. @staticmethod
  67. async def _serialize_payload( # type: ignore[override]
  68. user_id: str, account_data_type: str
  69. ) -> JsonDict:
  70. return {}
  71. async def _handle_request( # type: ignore[override]
  72. self, request: Request, content: JsonDict, user_id: str, account_data_type: str
  73. ) -> Tuple[int, JsonDict]:
  74. max_stream_id = await self.handler.remove_account_data_for_user(
  75. user_id, account_data_type
  76. )
  77. return 200, {"max_stream_id": max_stream_id}
  78. class ReplicationAddRoomAccountDataRestServlet(ReplicationEndpoint):
  79. """Add room account data on the appropriate account data worker.
  80. Request format:
  81. POST /_synapse/replication/add_room_account_data/:user_id/:room_id/:account_data_type
  82. {
  83. "content": { ... },
  84. }
  85. """
  86. NAME = "add_room_account_data"
  87. PATH_ARGS = ("user_id", "room_id", "account_data_type")
  88. CACHE = False
  89. def __init__(self, hs: "HomeServer"):
  90. super().__init__(hs)
  91. self.handler = hs.get_account_data_handler()
  92. @staticmethod
  93. async def _serialize_payload( # type: ignore[override]
  94. user_id: str, room_id: str, account_data_type: str, content: JsonDict
  95. ) -> JsonDict:
  96. payload = {
  97. "content": content,
  98. }
  99. return payload
  100. async def _handle_request( # type: ignore[override]
  101. self,
  102. request: Request,
  103. content: JsonDict,
  104. user_id: str,
  105. room_id: str,
  106. account_data_type: str,
  107. ) -> Tuple[int, JsonDict]:
  108. max_stream_id = await self.handler.add_account_data_to_room(
  109. user_id, room_id, account_data_type, content["content"]
  110. )
  111. return 200, {"max_stream_id": max_stream_id}
  112. class ReplicationRemoveRoomAccountDataRestServlet(ReplicationEndpoint):
  113. """Remove room account data on the appropriate account data worker.
  114. Request format:
  115. POST /_synapse/replication/remove_room_account_data/:user_id/:room_id/:account_data_type
  116. {
  117. "content": { ... },
  118. }
  119. """
  120. NAME = "remove_room_account_data"
  121. PATH_ARGS = ("user_id", "room_id", "account_data_type")
  122. CACHE = False
  123. def __init__(self, hs: "HomeServer"):
  124. super().__init__(hs)
  125. self.handler = hs.get_account_data_handler()
  126. @staticmethod
  127. async def _serialize_payload( # type: ignore[override]
  128. user_id: str, room_id: str, account_data_type: str, content: JsonDict
  129. ) -> JsonDict:
  130. return {}
  131. async def _handle_request( # type: ignore[override]
  132. self,
  133. request: Request,
  134. content: JsonDict,
  135. user_id: str,
  136. room_id: str,
  137. account_data_type: str,
  138. ) -> Tuple[int, JsonDict]:
  139. max_stream_id = await self.handler.remove_account_data_for_room(
  140. user_id, room_id, account_data_type
  141. )
  142. return 200, {"max_stream_id": max_stream_id}
  143. class ReplicationAddTagRestServlet(ReplicationEndpoint):
  144. """Add tag on the appropriate account data worker.
  145. Request format:
  146. POST /_synapse/replication/add_tag/:user_id/:room_id/:tag
  147. {
  148. "content": { ... },
  149. }
  150. """
  151. NAME = "add_tag"
  152. PATH_ARGS = ("user_id", "room_id", "tag")
  153. CACHE = False
  154. def __init__(self, hs: "HomeServer"):
  155. super().__init__(hs)
  156. self.handler = hs.get_account_data_handler()
  157. @staticmethod
  158. async def _serialize_payload( # type: ignore[override]
  159. user_id: str, room_id: str, tag: str, content: JsonDict
  160. ) -> JsonDict:
  161. payload = {
  162. "content": content,
  163. }
  164. return payload
  165. async def _handle_request( # type: ignore[override]
  166. self, request: Request, content: JsonDict, user_id: str, room_id: str, tag: str
  167. ) -> Tuple[int, JsonDict]:
  168. max_stream_id = await self.handler.add_tag_to_room(
  169. user_id, room_id, tag, content["content"]
  170. )
  171. return 200, {"max_stream_id": max_stream_id}
  172. class ReplicationRemoveTagRestServlet(ReplicationEndpoint):
  173. """Remove tag on the appropriate account data worker.
  174. Request format:
  175. POST /_synapse/replication/remove_tag/:user_id/:room_id/:tag
  176. {}
  177. """
  178. NAME = "remove_tag"
  179. PATH_ARGS = (
  180. "user_id",
  181. "room_id",
  182. "tag",
  183. )
  184. CACHE = False
  185. def __init__(self, hs: "HomeServer"):
  186. super().__init__(hs)
  187. self.handler = hs.get_account_data_handler()
  188. @staticmethod
  189. async def _serialize_payload(user_id: str, room_id: str, tag: str) -> JsonDict: # type: ignore[override]
  190. return {}
  191. async def _handle_request( # type: ignore[override]
  192. self, request: Request, content: JsonDict, user_id: str, room_id: str, tag: str
  193. ) -> Tuple[int, JsonDict]:
  194. max_stream_id = await self.handler.remove_tag_from_room(
  195. user_id,
  196. room_id,
  197. tag,
  198. )
  199. return 200, {"max_stream_id": max_stream_id}
  200. def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
  201. ReplicationAddUserAccountDataRestServlet(hs).register(http_server)
  202. ReplicationAddRoomAccountDataRestServlet(hs).register(http_server)
  203. ReplicationAddTagRestServlet(hs).register(http_server)
  204. ReplicationRemoveTagRestServlet(hs).register(http_server)
  205. if hs.config.experimental.msc3391_enabled:
  206. ReplicationRemoveUserAccountDataRestServlet(hs).register(http_server)
  207. ReplicationRemoveRoomAccountDataRestServlet(hs).register(http_server)