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

Commit

Permalink
Update usages of ThirdPartyEventRules module callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed May 3, 2023
1 parent 287f5d2 commit 80174f4
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 44 deletions.
4 changes: 3 additions & 1 deletion synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
from synapse.config.server import ListenerConfig, ManholeConfig, TCPListenerConfig
from synapse.crypto import context_factory
from synapse.events.presence_router import load_legacy_presence_router
from synapse.events.third_party_rules import load_legacy_third_party_event_rules
from synapse.handlers.auth import load_legacy_password_auth_providers
from synapse.http.site import SynapseSite
from synapse.logging.context import PreserveLoggingContext
Expand All @@ -73,6 +72,9 @@
from synapse.metrics.background_process_metrics import wrap_as_background_process
from synapse.metrics.jemalloc import setup_jemalloc_stats
from synapse.module_api.callbacks.spamchecker_callbacks import load_legacy_spam_checkers
from synapse.module_api.callbacks.third_party_event_rules import (
load_legacy_third_party_event_rules,
)
from synapse.types import ISynapseReactor
from synapse.util import SYNAPSE_VERSION
from synapse.util.caches.lrucache import setup_expire_lru_cache_entries
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def __init__(self, hs: "HomeServer"):
self._password_enabled_for_login = hs.config.auth.password_enabled_for_login
self._password_enabled_for_reauth = hs.config.auth.password_enabled_for_reauth
self._password_localdb_enabled = hs.config.auth.password_localdb_enabled
self._third_party_rules = hs.get_third_party_event_rules()
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules

# Ratelimiter for failed auth during UIA. Uses same ratelimit config
# as per `rc_login.failed_attempts`.
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def __init__(self, hs: "HomeServer"):
self._profile_handler = hs.get_profile_handler()
self.user_directory_handler = hs.get_user_directory_handler()
self._server_name = hs.hostname
self._third_party_rules = hs.get_third_party_event_rules()
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules

# Flag that indicates whether the process to part users from rooms is running
self._user_parter_running = False
self._third_party_rules = hs.get_third_party_event_rules()
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules

# Start the user parter loop so it can resume parting users from rooms where
# it left off (if it has work left to do).
Expand Down
6 changes: 4 additions & 2 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def __init__(self, hs: "HomeServer"):
self.config = hs.config
self.enable_room_list_search = hs.config.roomdirectory.enable_room_list_search
self.require_membership = hs.config.server.require_membership_for_aliases
self.third_party_event_rules = hs.get_third_party_event_rules()
self._third_party_event_rules = (
hs.get_module_api_callbacks().third_party_event_rules
)
self.server_name = hs.hostname

self.federation = hs.get_federation_client()
Expand Down Expand Up @@ -503,7 +505,7 @@ async def edit_published_room_list(
# Check if publishing is blocked by a third party module
allowed_by_third_party_rules = (
await (
self.third_party_event_rules.check_visibility_can_be_modified(
self._third_party_event_rules.check_visibility_can_be_modified(
room_id, visibility
)
)
Expand Down
6 changes: 4 additions & 2 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def __init__(self, hs: "HomeServer"):

self._room_backfill = Linearizer("room_backfill")

self.third_party_event_rules = hs.get_third_party_event_rules()
self._third_party_event_rules = (
hs.get_module_api_callbacks().third_party_event_rules
)

# Tracks running partial state syncs by room ID.
# Partial state syncs currently only run on the main process, so it's okay to
Expand Down Expand Up @@ -1253,7 +1255,7 @@ async def on_make_knock_request(
unpersisted_context,
) = await self.event_creation_handler.create_new_client_event(builder=builder)

event_allowed, _ = await self.third_party_event_rules.check_event_allowed(
event_allowed, _ = await self._third_party_event_rules.check_event_allowed(
event, unpersisted_context
)
if not event_allowed:
Expand Down
4 changes: 3 additions & 1 deletion synapse/handlers/federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def __init__(self, hs: "HomeServer"):
self._get_room_member_handler = hs.get_room_member_handler

self._federation_client = hs.get_federation_client()
self._third_party_event_rules = hs.get_third_party_event_rules()
self._third_party_event_rules = (
hs.get_module_api_callbacks().third_party_event_rules
)
self._notifier = hs.get_notifier()

self._is_mine_id = hs.is_mine_id
Expand Down
7 changes: 3 additions & 4 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
from synapse.visibility import get_effective_room_visibility_from_state

if TYPE_CHECKING:
from synapse.events.third_party_rules import ThirdPartyEventRules
from synapse.server import HomeServer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -509,8 +508,8 @@ def __init__(self, hs: "HomeServer"):
self._bulk_push_rule_evaluator = hs.get_bulk_push_rule_evaluator()

self._spam_checker_module_callbacks = hs.get_module_api_callbacks().spam_checker
self.third_party_event_rules: "ThirdPartyEventRules" = (
self.hs.get_third_party_event_rules()
self._third_party_event_rules = (
self.hs.get_module_api_callbacks().third_party_event_rules
)

self._block_events_without_consent_error = (
Expand Down Expand Up @@ -1314,7 +1313,7 @@ async def create_new_client_event(
if requester:
context.app_service = requester.app_service

res, new_content = await self.third_party_event_rules.check_event_allowed(
res, new_content = await self._third_party_event_rules.check_event_allowed(
event, context
)
if res is False:
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, hs: "HomeServer"):

self.server_name = hs.config.server.server_name

self._third_party_rules = hs.get_third_party_event_rules()
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules

async def get_profile(self, user_id: str, ignore_backoff: bool = True) -> JsonDict:
target_user = UserID.from_string(user_id)
Expand Down
10 changes: 6 additions & 4 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ def __init__(self, hs: "HomeServer"):
)
self._server_notices_mxid = hs.config.servernotices.server_notices_mxid

self.third_party_event_rules = hs.get_third_party_event_rules()
self._third_party_event_rules = (
hs.get_module_api_callbacks().third_party_event_rules
)

async def upgrade_room(
self, requester: Requester, old_room_id: str, new_version: RoomVersion
Expand Down Expand Up @@ -742,7 +744,7 @@ async def create_room(

# Let the third party rules modify the room creation config if needed, or abort
# the room creation entirely with an exception.
await self.third_party_event_rules.on_create_room(
await self._third_party_event_rules.on_create_room(
requester, config, is_requester_admin=is_requester_admin
)

Expand Down Expand Up @@ -879,7 +881,7 @@ async def create_room(
# Check whether this visibility value is blocked by a third party module
allowed_by_third_party_rules = (
await (
self.third_party_event_rules.check_visibility_can_be_modified(
self._third_party_event_rules.check_visibility_can_be_modified(
room_id, visibility
)
)
Expand Down Expand Up @@ -1731,7 +1733,7 @@ def __init__(self, hs: "HomeServer"):
self.room_member_handler = hs.get_room_member_handler()
self._room_creation_handler = hs.get_room_creation_handler()
self._replication = hs.get_replication_data_handler()
self._third_party_rules = hs.get_third_party_event_rules()
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules
self.event_creation_handler = hs.get_event_creation_handler()
self.store = hs.get_datastores().main

Expand Down
6 changes: 4 additions & 2 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def __init__(self, hs: "HomeServer"):

self.clock = hs.get_clock()
self._spam_checker_module_callbacks = hs.get_module_api_callbacks().spam_checker
self.third_party_event_rules = hs.get_third_party_event_rules()
self._third_party_event_rules = (
hs.get_module_api_callbacks().third_party_event_rules
)
self._server_notices_mxid = self.config.servernotices.server_notices_mxid
self._enable_lookup = hs.config.registration.enable_3pid_lookup
self.allow_per_room_profiles = self.config.server.allow_per_room_profiles
Expand Down Expand Up @@ -1560,7 +1562,7 @@ async def do_3pid_invite(
# can't just rely on the standard ratelimiting of events.
await self._third_party_invite_limiter.ratelimit(requester)

can_invite = await self.third_party_event_rules.check_threepid_can_be_invited(
can_invite = await self._third_party_event_rules.check_threepid_can_be_invited(
medium, address, room_id
)
if not can_invite:
Expand Down
2 changes: 1 addition & 1 deletion synapse/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def __init__(self, hs: "HomeServer"):

self._federation_client = hs.get_federation_http_client()

self._third_party_rules = hs.get_third_party_event_rules()
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules

self.clock = hs.get_clock()
self.appservice_handler = hs.get_application_service_handler()
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/admin/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self, hs: "HomeServer"):
self._auth = hs.get_auth()
self._store = hs.get_datastores().main
self._pagination_handler = hs.get_pagination_handler()
self._third_party_rules = hs.get_third_party_event_rules()
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules

async def on_DELETE(
self, request: SynapseRequest, room_id: str
Expand Down
56 changes: 35 additions & 21 deletions tests/rest/client/test_third_party_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
from synapse.api.room_versions import RoomVersion
from synapse.config.homeserver import HomeServerConfig
from synapse.events import EventBase
from synapse.events.third_party_rules import load_legacy_third_party_event_rules
from synapse.module_api.callbacks.third_party_event_rules import (
load_legacy_third_party_event_rules,
)
from synapse.rest import admin
from synapse.rest.client import account, login, profile, room
from synapse.server import HomeServer
Expand Down Expand Up @@ -146,7 +148,7 @@ async def check(
return ev.type != "foo.bar.forbidden", None

callback = Mock(spec=[], side_effect=check)
self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [
self.hs.get_module_api_callbacks().third_party_event_rules._check_event_allowed_callbacks = [
callback
]

Expand Down Expand Up @@ -202,7 +204,9 @@ async def check(
) -> Tuple[bool, Optional[JsonDict]]:
raise NastyHackException(429, "message")

self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
self.hs.get_module_api_callbacks().third_party_event_rules._check_event_allowed_callbacks = [
check
]

# Make a request
channel = self.make_request(
Expand All @@ -229,7 +233,9 @@ async def check(
ev.content = {"x": "y"}
return True, None

self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
self.hs.get_module_api_callbacks().third_party_event_rules._check_event_allowed_callbacks = [
check
]

# now send the event
channel = self.make_request(
Expand All @@ -253,7 +259,9 @@ async def check(
d["content"] = {"x": "y"}
return True, d

self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
self.hs.get_module_api_callbacks().third_party_event_rules._check_event_allowed_callbacks = [
check
]

# now send the event
channel = self.make_request(
Expand Down Expand Up @@ -289,7 +297,9 @@ async def check(
}
return True, d

self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
self.hs.get_module_api_callbacks().third_party_event_rules._check_event_allowed_callbacks = [
check
]

# Send an event, then edit it.
channel = self.make_request(
Expand Down Expand Up @@ -440,7 +450,9 @@ async def test_fn(
)
return True, None

self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [test_fn]
self.hs.get_module_api_callbacks().third_party_event_rules._check_event_allowed_callbacks = [
test_fn
]

# Sometimes the bug might not happen the first time the event type is added
# to the state but might happen when an event updates the state of the room for
Expand All @@ -466,7 +478,7 @@ async def test_fn(
def test_on_new_event(self) -> None:
"""Test that the on_new_event callback is called on new events"""
on_new_event = Mock(make_awaitable(None))
self.hs.get_third_party_event_rules()._on_new_event_callbacks.append(
self.hs.get_module_api_callbacks().third_party_event_rules._on_new_event_callbacks.append(
on_new_event
)

Expand Down Expand Up @@ -569,7 +581,9 @@ def test_on_profile_update(self) -> None:

# Register a mock callback.
m = Mock(return_value=make_awaitable(None))
self.hs.get_third_party_event_rules()._on_profile_update_callbacks.append(m)
self.hs.get_module_api_callbacks().third_party_event_rules._on_profile_update_callbacks.append(
m
)

# Change the display name.
channel = self.make_request(
Expand Down Expand Up @@ -628,7 +642,9 @@ def test_on_profile_update_admin(self) -> None:

# Register a mock callback.
m = Mock(return_value=make_awaitable(None))
self.hs.get_third_party_event_rules()._on_profile_update_callbacks.append(m)
self.hs.get_module_api_callbacks().third_party_event_rules._on_profile_update_callbacks.append(
m
)

# Register an admin user.
self.register_user("admin", "password", admin=True)
Expand Down Expand Up @@ -667,15 +683,15 @@ def test_on_user_deactivation_status_changed(self) -> None:
"""
# Register a mocked callback.
deactivation_mock = Mock(return_value=make_awaitable(None))
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules = self.hs.get_module_api_callbacks().third_party_event_rules
third_party_rules._on_user_deactivation_status_changed_callbacks.append(
deactivation_mock,
)
# Also register a mocked callback for profile updates, to check that the
# deactivation code calls it in a way that let modules know the user is being
# deactivated.
profile_mock = Mock(return_value=make_awaitable(None))
self.hs.get_third_party_event_rules()._on_profile_update_callbacks.append(
self.hs.get_module_api_callbacks().third_party_event_rules._on_profile_update_callbacks.append(
profile_mock,
)

Expand Down Expand Up @@ -725,7 +741,7 @@ def test_on_user_deactivation_status_changed_admin(self) -> None:
"""
# Register a mock callback.
m = Mock(return_value=make_awaitable(None))
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules = self.hs.get_module_api_callbacks().third_party_event_rules
third_party_rules._on_user_deactivation_status_changed_callbacks.append(m)

# Register an admin user.
Expand Down Expand Up @@ -779,7 +795,7 @@ def test_check_can_deactivate_user(self) -> None:
"""
# Register a mocked callback.
deactivation_mock = Mock(return_value=make_awaitable(False))
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules = self.hs.get_module_api_callbacks().third_party_event_rules
third_party_rules._check_can_deactivate_user_callbacks.append(
deactivation_mock,
)
Expand Down Expand Up @@ -825,7 +841,7 @@ def test_check_can_deactivate_user_admin(self) -> None:
"""
# Register a mocked callback.
deactivation_mock = Mock(return_value=make_awaitable(False))
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules = self.hs.get_module_api_callbacks().third_party_event_rules
third_party_rules._check_can_deactivate_user_callbacks.append(
deactivation_mock,
)
Expand Down Expand Up @@ -864,7 +880,7 @@ def test_check_can_shutdown_room(self) -> None:
"""
# Register a mocked callback.
shutdown_mock = Mock(return_value=make_awaitable(False))
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules = self.hs.get_module_api_callbacks().third_party_event_rules
third_party_rules._check_can_shutdown_room_callbacks.append(
shutdown_mock,
)
Expand Down Expand Up @@ -900,7 +916,7 @@ def test_on_threepid_bind(self) -> None:
"""
# Register a mocked callback.
threepid_bind_mock = Mock(return_value=make_awaitable(None))
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules = self.hs.get_module_api_callbacks().third_party_event_rules
third_party_rules._on_threepid_bind_callbacks.append(threepid_bind_mock)

# Register an admin user.
Expand Down Expand Up @@ -947,8 +963,7 @@ def test_on_add_and_remove_user_third_party_identifier(self) -> None:
on_remove_user_third_party_identifier_callback_mock = Mock(
return_value=make_awaitable(None)
)
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules.register_third_party_rules_callbacks(
self.hs.get_module_api().register_third_party_rules_callbacks(
on_add_user_third_party_identifier=on_add_user_third_party_identifier_callback_mock,
on_remove_user_third_party_identifier=on_remove_user_third_party_identifier_callback_mock,
)
Expand Down Expand Up @@ -1009,8 +1024,7 @@ def test_on_remove_user_third_party_identifier_is_called_on_deactivate(
on_remove_user_third_party_identifier_callback_mock = Mock(
return_value=make_awaitable(None)
)
third_party_rules = self.hs.get_third_party_event_rules()
third_party_rules.register_third_party_rules_callbacks(
self.hs.get_module_api().register_third_party_rules_callbacks(
on_remove_user_third_party_identifier=on_remove_user_third_party_identifier_callback_mock,
)

Expand Down
Loading

0 comments on commit 80174f4

Please sign in to comment.