From 19f788b9aa36004e872aac3dd3adbb7b620241e5 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 11:31:02 -0700 Subject: [PATCH] Deactivate connectors related to blocked federated servers --- ...10511_1651.py => 0074_auto_20210511_1829.py} | 16 +++++++++++++++- bookwyrm/models/base_model.py | 10 ++++++++++ bookwyrm/models/connector.py | 5 ++++- bookwyrm/models/federated_server.py | 17 +++++++++++++++++ bookwyrm/models/user.py | 12 +----------- 5 files changed, 47 insertions(+), 13 deletions(-) rename bookwyrm/migrations/{0074_auto_20210511_1651.py => 0074_auto_20210511_1829.py} (60%) diff --git a/bookwyrm/migrations/0074_auto_20210511_1651.py b/bookwyrm/migrations/0074_auto_20210511_1829.py similarity index 60% rename from bookwyrm/migrations/0074_auto_20210511_1651.py rename to bookwyrm/migrations/0074_auto_20210511_1829.py index 51e9e09c0..287a51ad6 100644 --- a/bookwyrm/migrations/0074_auto_20210511_1651.py +++ b/bookwyrm/migrations/0074_auto_20210511_1829.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2 on 2021-05-11 16:51 +# Generated by Django 3.2 on 2021-05-11 18:29 from django.db import migrations, models @@ -31,4 +31,18 @@ class Migration(migrations.Migration): name="active", field=models.BooleanField(default=True), ), + migrations.AddField( + model_name="connector", + name="deactivation_reason", + field=models.CharField( + blank=True, + choices=[ + ("self_deletion", "Self Deletion"), + ("moderator_deletion", "Moderator Deletion"), + ("domain_block", "Domain Block"), + ], + max_length=255, + null=True, + ), + ), ] diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index e85ff7338..2cb7c0365 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -6,6 +6,16 @@ from bookwyrm.settings import DOMAIN from .fields import RemoteIdField +DeactivationReason = models.TextChoices( + "DeactivationReason", + [ + "self_deletion", + "moderator_deletion", + "domain_block", + ], +) + + class BookWyrmModel(models.Model): """shared fields""" diff --git a/bookwyrm/models/connector.py b/bookwyrm/models/connector.py index 4debcc3d6..2d6717908 100644 --- a/bookwyrm/models/connector.py +++ b/bookwyrm/models/connector.py @@ -2,7 +2,7 @@ from django.db import models from bookwyrm.connectors.settings import CONNECTORS -from .base_model import BookWyrmModel +from .base_model import BookWyrmModel, DeactivationReason ConnectorFiles = models.TextChoices("ConnectorFiles", CONNECTORS) @@ -18,6 +18,9 @@ class Connector(BookWyrmModel): connector_file = models.CharField(max_length=255, choices=ConnectorFiles.choices) api_key = models.CharField(max_length=255, null=True, blank=True) active = models.BooleanField(default=True) + deactivation_reason = models.CharField( + max_length=255, choices=DeactivationReason.choices, null=True, blank=True + ) base_url = models.CharField(max_length=255) books_url = models.CharField(max_length=255) diff --git a/bookwyrm/models/federated_server.py b/bookwyrm/models/federated_server.py index 7d446ca0d..86635ab00 100644 --- a/bookwyrm/models/federated_server.py +++ b/bookwyrm/models/federated_server.py @@ -1,5 +1,6 @@ """ connections to external ActivityPub servers """ from urllib.parse import urlparse +from django.apps import apps from django.db import models from .base_model import BookWyrmModel @@ -34,6 +35,13 @@ class FederatedServer(BookWyrmModel): is_active=False, deactivation_reason="domain_block" ) + # check for related connectors + if self.application_type == "bookwyrm": + connector_model = apps.get_model("bookwyrm.Connector", require_read=True) + connector_model.objects.filter( + identifier=self.server_name, active=True + ).update(active=False, deactivation_reason="domain_block") + def unblock(self): """unblock a server""" self.status = "federated" @@ -43,6 +51,15 @@ class FederatedServer(BookWyrmModel): is_active=True, deactivation_reason=None ) + # check for related connectors + if self.application_type == "bookwyrm": + connector_model = apps.get_model("bookwyrm.Connector", require_read=True) + connector_model.objects.filter( + identifier=self.server_name, + active=False, + deactivation_reason="domain_block", + ).update(active=True, deactivation_reason=None) + @classmethod def is_blocked(cls, url): """look up if a domain is blocked""" diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 5f0e64e3b..7d821c5ba 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -19,21 +19,11 @@ from bookwyrm.signatures import create_key_pair from bookwyrm.tasks import app from bookwyrm.utils import regex from .activitypub_mixin import OrderedCollectionPageMixin, ActivitypubMixin -from .base_model import BookWyrmModel +from .base_model import BookWyrmModel, DeactivationReason from .federated_server import FederatedServer from . import fields, Review -DeactivationReason = models.TextChoices( - "DeactivationReason", - [ - "self_deletion", - "moderator_deletion", - "domain_block", - ], -) - - class User(OrderedCollectionPageMixin, AbstractUser): """a user who wants to read books"""