This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement account status endpoints (MSC3720) #12001
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f4a0ec7
Allow make_query to use unstable prefixes
babolivier 3bd6d05
Add an AccountHandler
babolivier 60458bf
Implement the federation endpoint from MSC3720
babolivier f739dfe
Add a configuration flag to enable the feature
babolivier 093067a
Implement the client-side endpoint and capability from MSC3720
babolivier bc73ae7
Add tests
babolivier 6b080d1
Merge branch 'develop' of github.com:matrix-org/synapse into babolivi…
babolivier 4fa53f9
Newsfile
babolivier 053843d
Lint
babolivier d8f11bd
Appease mypy
babolivier d37b31a
Lint
babolivier 8d61e96
Apply suggestions from code review
babolivier 63d76a7
Incorporate review and MSC changes
babolivier dddfee6
Merge branch 'develop' of github.com:matrix-org/synapse into babolivi…
babolivier 0889c89
Lint
babolivier 38cc18c
Filter failures received over federation
babolivier fb51368
Lint
babolivier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Implement experimental support for [MSC3720](https://github.com/matrix-org/matrix-doc/pull/3720) (account status endpoints). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # Copyright 2022 The Matrix.org Foundation C.I.C. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import TYPE_CHECKING, Dict, List, Tuple | ||
|
|
||
| from synapse.api.errors import Codes, SynapseError | ||
| from synapse.types import JsonDict, UserID | ||
|
|
||
| if TYPE_CHECKING: | ||
| from synapse.server import HomeServer | ||
|
|
||
|
|
||
| class AccountHandler: | ||
| def __init__(self, hs: "HomeServer"): | ||
| self._store = hs.get_datastore() | ||
| self._is_mine = hs.is_mine | ||
| self._federation_client = hs.get_federation_client() | ||
|
|
||
| async def get_account_statuses( | ||
| self, | ||
| user_ids: List[bytes], | ||
| allow_remote: bool, | ||
| ) -> Tuple[JsonDict, List[str]]: | ||
| """Get account statuses for a list of user IDs. | ||
|
|
||
| If one or more account(s) belong to remote homeservers, retrieve their status(es) | ||
| over federation if allowed. | ||
|
|
||
| Args: | ||
| user_ids: The list of accounts to retrieve the status of. | ||
| allow_remote: Whether to try to retrieve the status of remote accounts, if | ||
| any. | ||
|
|
||
| Returns: | ||
| The account statuses as well as the list of users whose statuses could not be | ||
| retrieved. | ||
babolivier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Raises: | ||
| SynapseError if a required parameter is missing or malformed, or if one of | ||
| the accounts isn't local to this homeserver and allow_remote is False. | ||
| """ | ||
| statuses = {} | ||
| failures = [] | ||
| remote_users: List[UserID] = [] | ||
|
|
||
| for user_id_bytes in user_ids: | ||
| try: | ||
| raw_user_id = user_id_bytes.decode("ascii") | ||
| user_id = UserID.from_string(raw_user_id) | ||
| except (AttributeError, SynapseError): | ||
babolivier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| raise SynapseError( | ||
| 400, | ||
| f"Not a valid Matrix user ID: {user_id_bytes.decode('utf8')}", | ||
babolivier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Codes.INVALID_PARAM, | ||
| ) | ||
|
|
||
| if self._is_mine(user_id): | ||
| status = await self._get_local_account_status(user_id) | ||
| statuses[user_id.to_string()] = status | ||
squahtx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| if not allow_remote: | ||
| raise SynapseError( | ||
| 400, | ||
| f"Not a local user: {raw_user_id}", | ||
| Codes.INVALID_PARAM, | ||
| ) | ||
|
|
||
| remote_users.append(user_id) | ||
|
|
||
| if allow_remote and len(remote_users) > 0: | ||
| remote_statuses, remote_failures = await self._get_remote_account_statuses( | ||
| remote_users, | ||
| ) | ||
|
|
||
| statuses.update(remote_statuses) | ||
| failures += remote_failures | ||
|
|
||
| return statuses, failures | ||
|
|
||
| async def _get_local_account_status(self, user_id: UserID) -> JsonDict: | ||
| """Retrieve the status of a local account. | ||
|
|
||
| Args: | ||
| user_id: The account to retrieve the status of. | ||
|
|
||
| Returns: | ||
| The account's status. | ||
| """ | ||
| status = {"exists": False} | ||
|
|
||
| userinfo = await self._store.get_userinfo_by_id(user_id.to_string()) | ||
|
|
||
| if userinfo is not None: | ||
| status = { | ||
| "exists": True, | ||
| "deactivated": userinfo.is_deactivated, | ||
| } | ||
|
|
||
| return status | ||
|
|
||
| async def _get_remote_account_statuses( | ||
| self, remote_users: List[UserID] | ||
| ) -> Tuple[JsonDict, List[str]]: | ||
| """Send out federation requests to retrieve the statuses of remote accounts. | ||
|
|
||
| Args: | ||
| remote_users: The accounts to retrieve the statuses of. | ||
|
|
||
| Returns: | ||
| The statuses of the accounts, and a list of accounts for which no status | ||
| could be retrieved. | ||
| """ | ||
| # Group remote users by destination, so we only send one request per remote | ||
| # homeserver. | ||
| by_destination: Dict[str, List[str]] = {} | ||
| for user in remote_users: | ||
| if user.domain not in by_destination: | ||
| by_destination[user.domain] = [] | ||
|
|
||
| by_destination[user.domain].append(user.to_string()) | ||
|
|
||
| # Retrieve the statuses and failures for remote accounts. | ||
| final_statuses: JsonDict = {} | ||
| final_failures: List[str] = [] | ||
| for destination, users in by_destination.items(): | ||
| statuses, failures = await self._federation_client.get_account_status( | ||
| destination, | ||
| users, | ||
| ) | ||
|
|
||
| final_statuses.update(statuses) | ||
| final_failures += failures | ||
babolivier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return final_statuses, final_failures | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| import logging | ||
| import random | ||
| from http import HTTPStatus | ||
| from typing import TYPE_CHECKING, Optional, Tuple | ||
| from typing import TYPE_CHECKING, Dict, List, Optional, Tuple | ||
| from urllib.parse import urlparse | ||
|
|
||
| from twisted.web.server import Request | ||
|
|
@@ -894,6 +894,37 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]: | |
| return 200, response | ||
|
|
||
|
|
||
| class AccountStatusRestServlet(RestServlet): | ||
| PATTERNS = client_patterns( | ||
| "/org.matrix.msc3720/account_status$", unstable=True, releases=() | ||
| ) | ||
|
|
||
| def __init__(self, hs: "HomeServer"): | ||
| super().__init__() | ||
| self._auth = hs.get_auth() | ||
| self._store = hs.get_datastore() | ||
| self._is_mine = hs.is_mine | ||
| self._federation_client = hs.get_federation_client() | ||
|
Comment on lines
+907
to
+909
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these things all seem to be unused. Please don't import things from HomeServer where they are not required - it increases the coupling of the code. Fixed in #12067.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gah, sorry about that. It came from a previous version of that implem that I refactored before opening the PR, but it looks like I forgot to remove some bits. Thanks for taking care of it! |
||
| self._account_handler = hs.get_account_handler() | ||
|
|
||
| async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]: | ||
| await self._auth.get_user_by_req(request) | ||
|
|
||
| args: Dict[bytes, List[bytes]] = request.args # type: ignore[assignment] | ||
| if b"user_id" not in args: | ||
| raise SynapseError( | ||
| 400, "Required parameter 'user_id' is missing", Codes.MISSING_PARAM | ||
| ) | ||
|
|
||
| user_ids: List[bytes] = args[b"user_id"] | ||
babolivier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| statuses, failures = await self._account_handler.get_account_statuses( | ||
| user_ids, | ||
| allow_remote=True, | ||
| ) | ||
|
|
||
| return 200, {"account_statuses": statuses, "failures": failures} | ||
|
|
||
|
|
||
| def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None: | ||
| EmailPasswordRequestTokenRestServlet(hs).register(http_server) | ||
| PasswordRestServlet(hs).register(http_server) | ||
|
|
@@ -908,3 +939,6 @@ def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None: | |
| ThreepidUnbindRestServlet(hs).register(http_server) | ||
| ThreepidDeleteRestServlet(hs).register(http_server) | ||
| WhoamiRestServlet(hs).register(http_server) | ||
|
|
||
| if hs.config.experimental.msc3720_enabled: | ||
| AccountStatusRestServlet(hs).register(http_server) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.