Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit e69bf14

Browse files
author
David Robertson
committed
Use Pydantic to validate PUT /directory/list/appservice/NETWORK/ROOM_ID
1 parent fb9b361 commit e69bf14

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

synapse/handlers/directory.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,11 @@ async def edit_published_room_list(
505505
await self.store.set_room_is_public(room_id, making_public)
506506

507507
async def edit_published_appservice_room_list(
508-
self, appservice_id: str, network_id: str, room_id: str, visibility: str
508+
self,
509+
appservice_id: str,
510+
network_id: str,
511+
room_id: str,
512+
visibility: Literal["public", "private"],
509513
) -> None:
510514
"""Add or remove a room from the appservice/network specific public
511515
room list.
@@ -516,9 +520,6 @@ async def edit_published_appservice_room_list(
516520
room_id
517521
visibility: either "public" or "private"
518522
"""
519-
if visibility not in ["public", "private"]:
520-
raise SynapseError(400, "Invalid visibility setting")
521-
522523
await self.store.set_room_is_public_appservice(
523524
room_id, appservice_id, network_id, visibility == "public"
524525
)

synapse/rest/client/directory.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from synapse.http.servlet import (
2626
RestServlet,
2727
parse_and_validate_json_object_from_request,
28-
parse_json_object_from_request,
2928
)
3029
from synapse.http.site import SynapseRequest
3130
from synapse.rest.client._base import client_patterns
@@ -170,20 +169,26 @@ def __init__(self, hs: "HomeServer"):
170169
self.directory_handler = hs.get_directory_handler()
171170
self.auth = hs.get_auth()
172171

172+
class PutBody(RequestBodyModel):
173+
visibility: Literal["public", "private"] = "public"
174+
173175
async def on_PUT(
174176
self, request: SynapseRequest, network_id: str, room_id: str
175177
) -> Tuple[int, JsonDict]:
176-
content = parse_json_object_from_request(request)
177-
visibility = content.get("visibility", "public")
178-
return await self._edit(request, network_id, room_id, visibility)
178+
content = parse_and_validate_json_object_from_request(request, self.PutBody)
179+
return await self._edit(request, network_id, room_id, content.visibility)
179180

180181
async def on_DELETE(
181182
self, request: SynapseRequest, network_id: str, room_id: str
182183
) -> Tuple[int, JsonDict]:
183184
return await self._edit(request, network_id, room_id, "private")
184185

185186
async def _edit(
186-
self, request: SynapseRequest, network_id: str, room_id: str, visibility: str
187+
self,
188+
request: SynapseRequest,
189+
network_id: str,
190+
room_id: str,
191+
visibility: Literal["public", "private"],
187192
) -> Tuple[int, JsonDict]:
188193
requester = await self.auth.get_user_by_req(request)
189194
if not requester.app_service:

0 commit comments

Comments
 (0)