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

Commit

Permalink
Pydantic for experimental account_status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Sep 1, 2022
1 parent b5cb8b1 commit 1faf04f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions synapse/handlers/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING, Dict, List, Tuple
from typing import TYPE_CHECKING, Dict, List, Sequence, Tuple

from synapse.api.errors import Codes, SynapseError
from synapse.types import JsonDict, UserID
Expand All @@ -33,7 +33,7 @@ def __init__(self, hs: "HomeServer"):

async def get_account_statuses(
self,
user_ids: List[str],
user_ids: Sequence[str],
allow_remote: bool,
) -> Tuple[JsonDict, List[str]]:
"""Get account statuses for a list of user IDs.
Expand Down
20 changes: 12 additions & 8 deletions synapse/rest/client/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# limitations under the License.
import logging
import random
from typing import TYPE_CHECKING, Optional, Tuple
from typing import TYPE_CHECKING, Optional, Sequence, Tuple
from urllib.parse import urlparse

from pydantic import StrictBool, StrictStr, constr
from pydantic import StrictBool, StrictStr, conlist, constr

from twisted.web.server import Request

Expand Down Expand Up @@ -842,17 +842,21 @@ def __init__(self, hs: "HomeServer"):
self._auth = hs.get_auth()
self._account_handler = hs.get_account_handler()

class PostBody(RequestBodyModel):
# TODO: we could validate that each user id is an mxid here, and/or parse it
# as a UserID
if TYPE_CHECKING:
user_ids: Sequence[StrictStr]
else:
user_ids: conlist(item_type=StrictStr, min_items=1)

async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
await self._auth.get_user_by_req(request)

body = parse_json_object_from_request(request)
if "user_ids" not in body:
raise SynapseError(
400, "Required parameter 'user_ids' is missing", Codes.MISSING_PARAM
)
body = parse_and_validate_json_object_from_request(request, self.PostBody)

statuses, failures = await self._account_handler.get_account_statuses(
body["user_ids"],
body.user_ids,
allow_remote=True,
)

Expand Down

0 comments on commit 1faf04f

Please sign in to comment.