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

Remove the deprecated Handlers object #8494

Merged
merged 11 commits into from
Oct 9, 2020
1 change: 1 addition & 0 deletions changelog.d/8494.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove the deprecated `Handlers` object.
2 changes: 1 addition & 1 deletion synapse/app/admin_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def export_data_command(hs, args):
user_id = args.user_id
directory = args.output_directory

res = await hs.get_handlers().admin_handler.export_user_data(
res = await hs.get_admin_handler().export_user_data(
user_id, FileExfiltrationWriter(user_id, directory=directory)
)
print(res)
Expand Down
7 changes: 6 additions & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,15 @@ def __init__(self, hs):
super().__init__(hs)

self.auth = hs.get_auth()
self.handler = hs.get_handlers().federation_handler
self.handler = hs.get_federation_handler()
self.state = hs.get_state_handler()

self.device_handler = hs.get_device_handler()

# Ensure the following handlers are loaded since they register callbacks
# with FederationHandlerRegistry.
hs.get_directory_handler()
Comment on lines +107 to +109
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary because previously get_handlers() would instantiate the directory handler (and a few other handlers). Only the directory handler called register_edu_handler or register_query_handler.


self._federation_ratelimiter = hs.get_federation_ratelimiter()

self._server_linearizer = Linearizer("fed_server")
Expand Down
33 changes: 0 additions & 33 deletions synapse/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,3 @@
# 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.

clokep marked this conversation as resolved.
Show resolved Hide resolved
from .admin import AdminHandler
from .directory import DirectoryHandler
from .federation import FederationHandler
from .identity import IdentityHandler
from .search import SearchHandler


class Handlers:

""" Deprecated. A collection of handlers.

At some point most of the classes whose name ended "Handler" were
accessed through this class.

However this makes it painful to unit test the handlers and to run cut
down versions of synapse that only use specific handlers because using a
single handler required creating all of the handlers. So some of the
handlers have been lifted out of the Handlers object and are now accessed
directly through the homeserver object itself.

Any new handlers should follow the new pattern of being accessed through
the homeserver object and should not be added to the Handlers object.

The remaining handlers should be moved out of the handlers object.
"""

def __init__(self, hs):
self.federation_handler = FederationHandler(hs)
self.directory_handler = DirectoryHandler(hs)
self.admin_handler = AdminHandler(hs)
self.identity_handler = IdentityHandler(hs)
self.search_handler = SearchHandler(hs)
2 changes: 1 addition & 1 deletion synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ async def delete_threepid(
if medium == "email":
address = canonicalise_email(address)

identity_handler = self.hs.get_handlers().identity_handler
identity_handler = self.hs.get_identity_handler()
result = await identity_handler.try_unbind_threepid(
user_id, {"medium": medium, "address": address, "id_server": id_server}
)
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, hs):
self._auth_handler = hs.get_auth_handler()
self._device_handler = hs.get_device_handler()
self._room_member_handler = hs.get_room_member_handler()
self._identity_handler = hs.get_handlers().identity_handler
self._identity_handler = hs.get_identity_handler()
self.user_directory_handler = hs.get_user_directory_handler()

# Flag that indicates whether the process to part users from rooms is running
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ async def persist_and_notify_client_event(

# Check the alias is currently valid (if it has changed).
room_alias_str = event.content.get("alias", None)
directory_handler = self.hs.get_handlers().directory_handler
directory_handler = self.hs.get_directory_handler()
if room_alias_str and room_alias_str != original_alias:
await self._validate_canonical_alias(
directory_handler, room_alias_str, event.room_id
Expand All @@ -1040,7 +1040,7 @@ async def persist_and_notify_client_event(
directory_handler, alias_str, event.room_id
)

federation_handler = self.hs.get_handlers().federation_handler
federation_handler = self.hs.get_federation_handler()

if event.type == EventTypes.Member:
if event.content["membership"] == Membership.INVITE:
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ async def get_messages(
"room_key", leave_token
)

await self.hs.get_handlers().federation_handler.maybe_backfill(
await self.hs.get_federation_handler().maybe_backfill(
room_id, curr_topo, limit=pagin_config.limit,
)

Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, hs):
self._auth_handler = hs.get_auth_handler()
self.profile_handler = hs.get_profile_handler()
self.user_directory_handler = hs.get_user_directory_handler()
self.identity_handler = self.hs.get_handlers().identity_handler
self.identity_handler = self.hs.get_identity_handler()
self.ratelimiter = hs.get_registration_ratelimiter()
self.macaroon_gen = hs.get_macaroon_generator()
self._server_notices_mxid = hs.config.server_notices_mxid
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ async def create_room(
if not allowed_by_third_party_rules:
raise SynapseError(403, "Room visibility value not allowed.")

directory_handler = self.hs.get_handlers().directory_handler
directory_handler = self.hs.get_directory_handler()
if room_alias:
await directory_handler.create_association(
requester=requester,
Expand Down
6 changes: 3 additions & 3 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def __init__(self, hs: "HomeServer"):
self.state_handler = hs.get_state_handler()
self.config = hs.config

self.federation_handler = hs.get_handlers().federation_handler
self.directory_handler = hs.get_handlers().directory_handler
self.identity_handler = hs.get_handlers().identity_handler
self.federation_handler = hs.get_federation_handler()
self.directory_handler = hs.get_directory_handler()
self.identity_handler = hs.get_identity_handler()
self.registration_handler = hs.get_registration_handler()
self.profile_handler = hs.get_profile_handler()
self.event_creation_handler = hs.get_event_creation_handler()
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/ui_auth/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def _check_threepid(self, medium, authdict):

threepid_creds = authdict["threepid_creds"]

identity_handler = self.hs.get_handlers().identity_handler
identity_handler = self.hs.get_identity_handler()

logger.info("Getting validated threepid. threepidcreds: %r", (threepid_creds,))

Expand Down
2 changes: 1 addition & 1 deletion synapse/replication/http/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, hs):
self.store = hs.get_datastore()
self.storage = hs.get_storage()
self.clock = hs.get_clock()
self.federation_handler = hs.get_handlers().federation_handler
self.federation_handler = hs.get_federation_handler()

@staticmethod
async def _serialize_payload(store, room_id, event_and_contexts, backfilled):
Expand Down
2 changes: 1 addition & 1 deletion synapse/replication/http/membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ReplicationRemoteJoinRestServlet(ReplicationEndpoint):
def __init__(self, hs):
super().__init__(hs)

self.federation_handler = hs.get_handlers().federation_handler
self.federation_handler = hs.get_federation_handler()
self.store = hs.get_datastore()
self.clock = hs.get_clock()

Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/admin/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ListRoomRestServlet(RestServlet):
def __init__(self, hs):
self.store = hs.get_datastore()
self.auth = hs.get_auth()
self.admin_handler = hs.get_handlers().admin_handler
self.admin_handler = hs.get_admin_handler()

async def on_GET(self, request):
requester = await self.auth.get_user_by_req(request)
Expand Down Expand Up @@ -273,7 +273,7 @@ def __init__(self, hs):
self.hs = hs
self.auth = hs.get_auth()
self.room_member_handler = hs.get_room_member_handler()
self.admin_handler = hs.get_handlers().admin_handler
self.admin_handler = hs.get_admin_handler()
self.state_handler = hs.get_state_handler()

async def on_POST(self, request, room_identifier):
Expand Down
13 changes: 6 additions & 7 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, hs):
self.hs = hs
self.store = hs.get_datastore()
self.auth = hs.get_auth()
self.admin_handler = hs.get_handlers().admin_handler
self.admin_handler = hs.get_admin_handler()

async def on_GET(self, request, user_id):
target_user = UserID.from_string(user_id)
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self, hs):
self.hs = hs
self.store = hs.get_datastore()
self.auth = hs.get_auth()
self.admin_handler = hs.get_handlers().admin_handler
self.admin_handler = hs.get_admin_handler()

async def on_GET(self, request):
await assert_requester_is_admin(self.auth, request)
Expand Down Expand Up @@ -135,7 +135,7 @@ class UserRestServletV2(RestServlet):
def __init__(self, hs):
self.hs = hs
self.auth = hs.get_auth()
self.admin_handler = hs.get_handlers().admin_handler
self.admin_handler = hs.get_admin_handler()
self.store = hs.get_datastore()
self.auth_handler = hs.get_auth_handler()
self.profile_handler = hs.get_profile_handler()
Expand Down Expand Up @@ -448,7 +448,7 @@ class WhoisRestServlet(RestServlet):
def __init__(self, hs):
self.hs = hs
self.auth = hs.get_auth()
self.handlers = hs.get_handlers()
self.admin_handler = hs.get_admin_handler()

async def on_GET(self, request, user_id):
target_user = UserID.from_string(user_id)
Expand All @@ -461,7 +461,7 @@ async def on_GET(self, request, user_id):
if not self.hs.is_mine(target_user):
raise SynapseError(400, "Can only whois a local user")

ret = await self.handlers.admin_handler.get_whois(target_user)
ret = await self.admin_handler.get_whois(target_user)

return 200, ret

Expand Down Expand Up @@ -591,7 +591,6 @@ def __init__(self, hs):
self.hs = hs
self.store = hs.get_datastore()
self.auth = hs.get_auth()
self.handlers = hs.get_handlers()

async def on_GET(self, request, target_user_id):
"""Get request to search user table for specific users according to
Expand All @@ -612,7 +611,7 @@ async def on_GET(self, request, target_user_id):
term = parse_string(request, "term", required=True)
logger.info("term: %s ", term)

ret = await self.handlers.store.search_users(term)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code was just broken...

ret = await self.store.search_users(term)
return 200, ret


Expand Down
25 changes: 12 additions & 13 deletions synapse/rest/client/v1/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ class ClientDirectoryServer(RestServlet):
def __init__(self, hs):
super().__init__()
self.store = hs.get_datastore()
self.handlers = hs.get_handlers()
self.directory_handler = hs.get_directory_handler()
self.auth = hs.get_auth()

async def on_GET(self, request, room_alias):
room_alias = RoomAlias.from_string(room_alias)

dir_handler = self.handlers.directory_handler
res = await dir_handler.get_association(room_alias)
res = await self.directory_handler.get_association(room_alias)

return 200, res

Expand Down Expand Up @@ -79,19 +78,19 @@ async def on_PUT(self, request, room_alias):

requester = await self.auth.get_user_by_req(request)

await self.handlers.directory_handler.create_association(
await self.directory_handler.create_association(
requester, room_alias, room_id, servers
)

return 200, {}

async def on_DELETE(self, request, room_alias):
dir_handler = self.handlers.directory_handler

try:
service = self.auth.get_appservice_by_req(request)
room_alias = RoomAlias.from_string(room_alias)
await dir_handler.delete_appservice_association(service, room_alias)
await self.directory_handler.delete_appservice_association(
service, room_alias
)
logger.info(
"Application service at %s deleted alias %s",
service.url,
Expand All @@ -107,7 +106,7 @@ async def on_DELETE(self, request, room_alias):

room_alias = RoomAlias.from_string(room_alias)

await dir_handler.delete_association(requester, room_alias)
await self.directory_handler.delete_association(requester, room_alias)

logger.info(
"User %s deleted alias %s", user.to_string(), room_alias.to_string()
Expand All @@ -122,7 +121,7 @@ class ClientDirectoryListServer(RestServlet):
def __init__(self, hs):
super().__init__()
self.store = hs.get_datastore()
self.handlers = hs.get_handlers()
self.directory_handler = hs.get_directory_handler()
self.auth = hs.get_auth()

async def on_GET(self, request, room_id):
Expand All @@ -138,7 +137,7 @@ async def on_PUT(self, request, room_id):
content = parse_json_object_from_request(request)
visibility = content.get("visibility", "public")

await self.handlers.directory_handler.edit_published_room_list(
await self.directory_handler.edit_published_room_list(
requester, room_id, visibility
)

Expand All @@ -147,7 +146,7 @@ async def on_PUT(self, request, room_id):
async def on_DELETE(self, request, room_id):
requester = await self.auth.get_user_by_req(request)

await self.handlers.directory_handler.edit_published_room_list(
await self.directory_handler.edit_published_room_list(
requester, room_id, "private"
)

Expand All @@ -162,7 +161,7 @@ class ClientAppserviceDirectoryListServer(RestServlet):
def __init__(self, hs):
super().__init__()
self.store = hs.get_datastore()
self.handlers = hs.get_handlers()
self.directory_handler = hs.get_directory_handler()
self.auth = hs.get_auth()

def on_PUT(self, request, network_id, room_id):
Expand All @@ -180,7 +179,7 @@ async def _edit(self, request, network_id, room_id, visibility):
403, "Only appservices can edit the appservice published room list"
)

await self.handlers.directory_handler.edit_published_appservice_room_list(
await self.directory_handler.edit_published_appservice_room_list(
requester.app_service.id, network_id, room_id, visibility
)

Expand Down
1 change: 0 additions & 1 deletion synapse/rest/client/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def __init__(self, hs):

self.auth_handler = self.hs.get_auth_handler()
self.registration_handler = hs.get_registration_handler()
self.handlers = hs.get_handlers()
self._well_known_builder = WellKnownBuilder(hs)
self._address_ratelimiter = Ratelimiter(
clock=hs.get_clock(),
Expand Down
10 changes: 3 additions & 7 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def on_OPTIONS(self, request):
class RoomStateEventRestServlet(TransactionRestServlet):
def __init__(self, hs):
super().__init__(hs)
self.handlers = hs.get_handlers()
self.event_creation_handler = hs.get_event_creation_handler()
self.room_member_handler = hs.get_room_member_handler()
self.message_handler = hs.get_message_handler()
Expand Down Expand Up @@ -798,7 +797,6 @@ def on_PUT(self, request, room_id, membership_action, txn_id):
class RoomRedactEventRestServlet(TransactionRestServlet):
def __init__(self, hs):
super().__init__(hs)
self.handlers = hs.get_handlers()
self.event_creation_handler = hs.get_event_creation_handler()
self.auth = hs.get_auth()

Expand Down Expand Up @@ -903,7 +901,7 @@ class RoomAliasListServlet(RestServlet):
def __init__(self, hs: "synapse.server.HomeServer"):
super().__init__()
self.auth = hs.get_auth()
self.directory_handler = hs.get_handlers().directory_handler
self.directory_handler = hs.get_directory_handler()

async def on_GET(self, request, room_id):
requester = await self.auth.get_user_by_req(request)
Expand All @@ -920,7 +918,7 @@ class SearchRestServlet(RestServlet):

def __init__(self, hs):
super().__init__()
self.handlers = hs.get_handlers()
self.search_handler = hs.get_search_handler()
self.auth = hs.get_auth()

async def on_POST(self, request):
Expand All @@ -929,9 +927,7 @@ async def on_POST(self, request):
content = parse_json_object_from_request(request)

batch = parse_string(request, "next_batch")
results = await self.handlers.search_handler.search(
requester.user, content, batch
)
results = await self.search_handler.search(requester.user, content, batch)

return 200, results

Expand Down
Loading