Use classmethod for disable federation check

This commit is contained in:
Mouse Reeve 2024-08-29 10:01:01 -07:00
parent 595cc24c04
commit c6d34b5f3a
6 changed files with 14 additions and 17 deletions

View file

@ -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)

View file

@ -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:

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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