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

Standardise the module interface #10062

Merged
merged 38 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e388397
First cut at a standardised module interface
babolivier May 25, 2021
11f525c
Don't use a centralised handler and let modules register what they ne…
babolivier May 26, 2021
ceb9904
Specify where the new methods need to be called from
babolivier May 26, 2021
c4d09a8
Implement new module interface for the spam checker
babolivier May 26, 2021
f5098c9
Don't centralise registration of hooks and web resources
babolivier May 26, 2021
7da2fd3
Don't use a class if a simple function works just as well
babolivier May 26, 2021
f1c0889
Fix CI
babolivier May 26, 2021
817fc75
Lint
babolivier May 26, 2021
a988b8c
Incorporate comments
babolivier May 27, 2021
ba4e678
Lint
babolivier May 27, 2021
a06649c
Don't inhibit rejection reason from spamchecker
babolivier May 28, 2021
d55b17b
Make mypy happy
babolivier May 28, 2021
2c8d6d5
Fix tests
babolivier May 28, 2021
10153fc
Lint
babolivier May 28, 2021
eda9658
Merge branch 'develop' into babolivier/modules
babolivier May 28, 2021
1c9e3d4
Document the new module interface
babolivier Jun 4, 2021
870647d
Merge branch 'develop' into babolivier/modules
babolivier Jun 4, 2021
b92965c
Add new doc to the summary, and add a deprecation notice to the spam …
babolivier Jun 4, 2021
d440297
Fix a typo in registration docs
babolivier Jun 4, 2021
ce4347b
Point to the new docs in the sample configuration
babolivier Jun 4, 2021
79ee967
Improve example
babolivier Jun 4, 2021
7bf8fdb
Apply suggestions from code review
babolivier Jun 16, 2021
a63a060
Merge branch 'develop' into babolivier/modules
babolivier Jun 16, 2021
c6ed049
Incorporate review comments
babolivier Jun 16, 2021
39a02b1
Lint
babolivier Jun 17, 2021
8e28b3e
Use async callbacks in tests
babolivier Jun 17, 2021
9c5bffd
Correctly wrap check_registration_for_spam
babolivier Jun 17, 2021
468b900
Lint
babolivier Jun 17, 2021
5a9f391
Move support for 3-arg check_registration_for_spam to legacy code
babolivier Jun 18, 2021
6a326f9
Remove unused import
babolivier Jun 18, 2021
575556f
Remove other unused import
babolivier Jun 18, 2021
12774dc
Explicitely type legacy callback as not None
babolivier Jun 18, 2021
b12855c
Don't import cast again
babolivier Jun 18, 2021
cd596f5
Be more vague in upgrade notes and add deprecation notice to changelog
babolivier Jun 18, 2021
3a28f6a
Phrasing
babolivier Jun 18, 2021
9cbe1e6
Merge branch 'develop' into babolivier/modules
babolivier Jun 18, 2021
387d41b
Types don't like commas
babolivier Jun 18, 2021
249c607
Fix tests and phrasing
babolivier Jun 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don't centralise registration of hooks and web resources
  • Loading branch information
babolivier committed May 26, 2021
commit f5098c9113e5d20ffdc8ef0abe3a09f33795773c
3 changes: 3 additions & 0 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from synapse.app.phone_stats_home import start_phone_stats_home
from synapse.config.homeserver import HomeServerConfig
from synapse.crypto import context_factory
from synapse.events.spamcheck import LegacySpamCheckerWrapper
from synapse.logging.context import PreserveLoggingContext
from synapse.metrics.background_process_metrics import wrap_as_background_process
from synapse.metrics.jemalloc import setup_jemalloc_stats
Expand Down Expand Up @@ -343,6 +344,8 @@ def run_sighup(*args, **kwargs):
for module, config in hs.config.modules.loaded_modules:
module(config=config, api=module_api)

LegacySpamCheckerWrapper.load_modules(hs)

# It is now safe to start your Synapse.
hs.start_listening()
hs.get_datastore().db_pool.start_profiling()
Expand Down
2 changes: 1 addition & 1 deletion synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _listener_http(self, config: HomeServerConfig, listener_config: ListenerConf
resources[path] = resource

# Attach additional resources registered by modules.
resources.update(module_api.get_registered_web_resources())
resources.update(self._module_web_resources)

# try to find something useful to redirect '/' to
if WEB_CLIENT_PREFIX in resources:
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ModulesConfig(Config):
def read_config(self, config: dict, **kwargs):
self.loaded_modules = []

configured_modules = config.get("modules", [])
configured_modules = config.get("modules") or []
for i, module in enumerate(configured_modules):
config_path = ("modules", "<item %i>" % i)
if not isinstance(module, dict):
Expand Down
52 changes: 7 additions & 45 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ModuleApi:
can register new users etc if necessary.
"""

def __init__(self, hs, auth_handler):
def __init__(self, hs: "HomeServer", auth_handler):
self._hs = hs

self._store = hs.get_datastore()
Expand All @@ -67,8 +67,7 @@ def __init__(self, hs, auth_handler):
self._http_client = hs.get_simple_http_client() # type: SimpleHttpClient
self._public_room_list_manager = PublicRoomListManager(hs)

self._callbacks: Dict[str, List[Callable]] = {}
self._web_resources: Dict[str, IResource] = {}
self._spam_checker = hs.get_spam_checker()

@property
def http_client(self):
Expand All @@ -87,35 +86,10 @@ def public_room_list_manager(self):
"""
return self._public_room_list_manager

def register_hook(self, fn_name: str, callback: Callable):
"""Registers the given callback as a hook with the given name.

This function must be called in the __init__ method of the modules using it.

If multiple modules register the same hook, their callbacks will be run in the
same order as the modules appear in the configuration file (highest first).

Args:
fn_name: The hook name.
callback: The function to call for this hook.
"""
if fn_name not in self._callbacks.keys():
self._callbacks[fn_name] = [callback]
return

self._callbacks[fn_name].append(callback)

def get_registered_hook(self, fn_name: str) -> List[Callable]:
"""Get the callbacks registered by modules for the given hook name.

Args:
fn_name: The name of the hook.

Returns:
The callbacks implementing the hook, or an empty list if there is no callback
for this hook.
"""
return self._callbacks.get(fn_name, [])
@property
def register_spam_checker_callbacks(self):
"""Registers callbacks for spam checking capabilities."""
return self._spam_checker.register_callbacks

def register_web_resource(self, path: str, resource: IResource):
"""Registers a web resource to be served at the given path.
Expand All @@ -129,19 +103,7 @@ def register_web_resource(self, path: str, resource: IResource):
path: The path to register the resource for.
resource: The resource to attach to this path.
"""
# Don't register a resource that's already been registered. Like with hooks, we
# want to give priority to the module that's the highest in the configuration
# file, so in case of a conflict we keep the resource registered by this module.
if path not in self._web_resources.keys():
self._web_resources[path] = resource

def get_registered_web_resources(self) -> Dict[str, IResource]:
"""Get the web resources registered by modules.

Returns:
A dict associating a path with the resource to attach to it.
"""
return self._web_resources
self._hs.register_module_web_resource(path, resource)

def get_user_by_req(self, req, allow_guest=False):
"""Check the access_token provided for a request
Expand Down
17 changes: 17 additions & 0 deletions synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from twisted.internet import defer
from twisted.mail.smtp import sendmail
from twisted.web.iweb import IPolicyForHTTPS
from twisted.web.resource import IResource

from synapse.api.auth import Auth
from synapse.api.filtering import Filtering
Expand Down Expand Up @@ -257,6 +258,22 @@ def __init__(

self.datastores = None # type: Optional[Databases]

self._module_web_resources: Dict[str, IResource] = {}

def register_module_web_resource(self, path: str, resource: IResource):
"""Allows a module to register a web resource to be served at the given path.

If multiple modules register a resource for the same path, the module that
appears the highest in the configuration file takes priority.

Args:
path: The path to register the resource for.
resource: The resource to attach to this path.
"""
# Don't register a resource that's already been registered.
if path not in self._module_web_resources.keys():
self._module_web_resources[path] = resource

def get_instance_id(self) -> str:
"""A unique ID for this synapse process instance.

Expand Down