From c6d34b5f3a0ccbca81d4de5ead4eeb63c5ec3e95 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 29 Aug 2024 10:01:01 -0700 Subject: [PATCH] Use classmethod for disable federation check --- bookwyrm/connectors/abstract_connector.py | 2 +- bookwyrm/connectors/bookwyrm_connector.py | 6 +----- bookwyrm/decorators.py | 2 +- bookwyrm/models/activitypub_mixin.py | 4 ++-- bookwyrm/models/site.py | 11 ++++++----- bookwyrm/views/helpers.py | 6 +++--- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 306bac3b3..40513c761 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -315,7 +315,7 @@ def get_data( """wrapper for request.get""" # make sure this isn't a forbidden federated request if is_activitypub: - models.SiteSettings.objects.get().raise_federation_disabled() + models.SiteSettings.raise_federation_disabled() # check if the url is blocked raise_not_valid_url(url) diff --git a/bookwyrm/connectors/bookwyrm_connector.py b/bookwyrm/connectors/bookwyrm_connector.py index 5040041a6..9aaa56264 100644 --- a/bookwyrm/connectors/bookwyrm_connector.py +++ b/bookwyrm/connectors/bookwyrm_connector.py @@ -5,17 +5,13 @@ from typing import Any, Iterator from bookwyrm import activitypub, models from bookwyrm.book_search import SearchResult from .abstract_connector import AbstractMinimalConnector -from .connector_manager import ConnectorException class Connector(AbstractMinimalConnector): """this is basically just for search""" def __init__(self, identifier: str): - if models.SiteSettings.objects.get().disable_federation: - raise ConnectorException( - "Federation is disabled, cannot load BookWyrm connector", identifier - ) + models.SiteSettings.raise_federation_disabled() super().__init__(identifier) def get_or_create_book(self, remote_id: str) -> models.Edition: diff --git a/bookwyrm/decorators.py b/bookwyrm/decorators.py index 56eef8fda..470746d63 100644 --- a/bookwyrm/decorators.py +++ b/bookwyrm/decorators.py @@ -8,6 +8,6 @@ def require_federation(function): @wraps(function) def wrap(request, *args, **kwargs): # pylint: disable=unused-argument - SiteSettings.objects.get().raise_federation_disabled() + SiteSettings.raise_federation_disabled() return wrap diff --git a/bookwyrm/models/activitypub_mixin.py b/bookwyrm/models/activitypub_mixin.py index 581106a05..ac548c77a 100644 --- a/bookwyrm/models/activitypub_mixin.py +++ b/bookwyrm/models/activitypub_mixin.py @@ -132,7 +132,7 @@ class ActivitypubMixin: """send out an activity""" site_model = apps.get_model("bookwyrm.SiteSettings", require_ready=True) try: - site_model.objects.get().raise_federation_disabled() + site_model.raise_federation_disabled() except PermissionDenied: return @@ -524,7 +524,7 @@ def broadcast_task(sender_id: int, activity: str, recipients: list[str]): # checking this here ought to be redundant unless there are already-spawned tasks # when federation is turned off. In that case this should prevent them from running. site_model = apps.get_model("bookwyrm.SiteSettings", require_ready=True) - site_model.objects.get().raise_federation_disabled() + site_model.raise_federation_disabled() user_model = apps.get_model("bookwyrm.User", require_ready=True) sender = user_model.objects.select_related("key_pair").get(id=sender_id) diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index 95e0c5e49..cd7d8aaf0 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -119,6 +119,12 @@ class SiteSettings(SiteModel): default_settings.save() return default_settings + @classmethod + def raise_federation_disabled(cls) -> None: + """Don't connect to the outside world""" + if cls.objects.get().disable_federation: + raise PermissionDenied("Federation is disabled") + @property def logo_url(self) -> Any: """helper to build the logo url""" @@ -134,11 +140,6 @@ class SiteSettings(SiteModel): """helper to build the logo url""" return self.get_url("favicon", "images/favicon.png") - def raise_federation_disabled(self) -> None: - """Don't connect to the outside world""" - if self.disable_federation: - raise PermissionDenied("Federation is disabled") - def get_url(self, field: str, default_path: str) -> Any: """get a media url or a default static path""" uploaded = getattr(self, field, None) diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index b60e87815..d32147dbe 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -54,7 +54,7 @@ def is_api_request(request): if is_api: # don't allow API requests if federation is disabled - models.SiteSettings.objects.get().raise_federation_disabled() + models.SiteSettings.raise_federation_disabled() return is_api @@ -69,7 +69,7 @@ def is_bookwyrm_request(request): def handle_remote_webfinger(query, unknown_only=False, refresh=False): """webfingerin' other servers""" # SHOULD we do a remote webfinger? Is it allowed? - models.SiteSettings.objects.get().raise_federation_disabled() + models.SiteSettings.raise_federation_disabled() user = None @@ -116,7 +116,7 @@ def handle_remote_webfinger(query, unknown_only=False, refresh=False): def subscribe_remote_webfinger(query): """get subscribe template from other servers""" # SHOULD we do a remote webfinger? Is it allowed? - models.SiteSettings.objects.get().raise_federation_disabled() + models.SiteSettings.raise_federation_disabled() template = None # usernames could be @user@domain or user@domain