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
Move Account Validity callbacks to a dedicated file #15237
Merged
anoadragon453
merged 5 commits into
develop
from
anoa/module_callbacks_split_account_data
Mar 16, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
53680ae
Move Account Validity callbacks to a dedicated file
anoadragon453 ee9bcd1
changelog
anoadragon453 449d01d
Use an absolute import for AccountValidityModuleApiCallbacks
anoadragon453 11f97b3
Fix method name in comment
anoadragon453 d6c14fb
lint
anoadragon453 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 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 @@ | ||
Move various module API callback registration methods to a dedicated class. |
This file contains 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 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 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,22 @@ | ||
# Copyright 2023 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 synapse.module_api.callbacks.account_validity_callbacks import ( | ||
AccountValidityModuleApiCallbacks, | ||
) | ||
|
||
|
||
class ModuleApiCallbacks: | ||
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. I'm assuming by how you set this up you're planning to add other module APIs to this too? 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. Correct - see this poorly named branch for the commits that move the rest of the callback classes: https://github.com/matrix-org/synapse/compare/anoa/public_rooms_module_api_backup |
||
def __init__(self) -> None: | ||
self.account_validity = AccountValidityModuleApiCallbacks() |
93 changes: 93 additions & 0 deletions
93
synapse/module_api/callbacks/account_validity_callbacks.py
This file contains 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,93 @@ | ||
# Copyright 2023 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. | ||
|
||
import logging | ||
from typing import Awaitable, Callable, List, Optional, Tuple | ||
|
||
from twisted.web.http import Request | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# Types for callbacks to be registered via the module api | ||
IS_USER_EXPIRED_CALLBACK = Callable[[str], Awaitable[Optional[bool]]] | ||
ON_USER_REGISTRATION_CALLBACK = Callable[[str], Awaitable] | ||
# Temporary hooks to allow for a transition from `/_matrix/client` endpoints | ||
# to `/_synapse/client/account_validity`. See `register_callbacks` below. | ||
ON_LEGACY_SEND_MAIL_CALLBACK = Callable[[str], Awaitable] | ||
ON_LEGACY_RENEW_CALLBACK = Callable[[str], Awaitable[Tuple[bool, bool, int]]] | ||
ON_LEGACY_ADMIN_REQUEST = Callable[[Request], Awaitable] | ||
|
||
|
||
class AccountValidityModuleApiCallbacks: | ||
def __init__(self) -> None: | ||
self.is_user_expired_callbacks: List[IS_USER_EXPIRED_CALLBACK] = [] | ||
self.on_user_registration_callbacks: List[ON_USER_REGISTRATION_CALLBACK] = [] | ||
self.on_legacy_send_mail_callback: Optional[ON_LEGACY_SEND_MAIL_CALLBACK] = None | ||
self.on_legacy_renew_callback: Optional[ON_LEGACY_RENEW_CALLBACK] = None | ||
|
||
# The legacy admin requests callback isn't a protected attribute because we need | ||
# to access it from the admin servlet, which is outside of this handler. | ||
self.on_legacy_admin_request_callback: Optional[ON_LEGACY_ADMIN_REQUEST] = None | ||
|
||
def register_callbacks( | ||
self, | ||
is_user_expired: Optional[IS_USER_EXPIRED_CALLBACK] = None, | ||
on_user_registration: Optional[ON_USER_REGISTRATION_CALLBACK] = None, | ||
on_legacy_send_mail: Optional[ON_LEGACY_SEND_MAIL_CALLBACK] = None, | ||
on_legacy_renew: Optional[ON_LEGACY_RENEW_CALLBACK] = None, | ||
on_legacy_admin_request: Optional[ON_LEGACY_ADMIN_REQUEST] = None, | ||
) -> None: | ||
"""Register callbacks from module for each hook.""" | ||
if is_user_expired is not None: | ||
self.is_user_expired_callbacks.append(is_user_expired) | ||
|
||
if on_user_registration is not None: | ||
self.on_user_registration_callbacks.append(on_user_registration) | ||
|
||
# The builtin account validity feature exposes 3 endpoints (send_mail, renew, and | ||
# an admin one). As part of moving the feature into a module, we need to change | ||
# the path from /_matrix/client/unstable/account_validity/... to | ||
# /_synapse/client/account_validity, because: | ||
# | ||
# * the feature isn't part of the Matrix spec thus shouldn't live under /_matrix | ||
# * the way we register servlets means that modules can't register resources | ||
# under /_matrix/client | ||
# | ||
# We need to allow for a transition period between the old and new endpoints | ||
# in order to allow for clients to update (and for emails to be processed). | ||
# | ||
# Once the email-account-validity module is loaded, it will take control of account | ||
# validity by moving the rows from our `account_validity` table into its own table. | ||
# | ||
# Therefore, we need to allow modules (in practice just the one implementing the | ||
# email-based account validity) to temporarily hook into the legacy endpoints so we | ||
# can route the traffic coming into the old endpoints into the module, which is | ||
# why we have the following three temporary hooks. | ||
if on_legacy_send_mail is not None: | ||
if self.on_legacy_send_mail_callback is not None: | ||
raise RuntimeError("Tried to register on_legacy_send_mail twice") | ||
|
||
self.on_legacy_send_mail_callback = on_legacy_send_mail | ||
|
||
if on_legacy_renew is not None: | ||
if self.on_legacy_renew_callback is not None: | ||
raise RuntimeError("Tried to register on_legacy_renew twice") | ||
|
||
self.on_legacy_renew_callback = on_legacy_renew | ||
|
||
if on_legacy_admin_request is not None: | ||
if self.on_legacy_admin_request_callback is not None: | ||
raise RuntimeError("Tried to register on_legacy_admin_request twice") | ||
|
||
self.on_legacy_admin_request_callback = on_legacy_admin_request |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea if people import these in modules somewhere (e.g. for type hints)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users should really only import things from the
synapse.module_api
module. That being said, one can import classes that thesynapse.module_api
module itself imports, by doing:for example, and this would not be affected by these changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I know some of the spam checker and media bits don't do this as cleanly.