2021-03-08 08:49:10 -08:00
|
|
|
""" connections to external ActivityPub servers """
|
2021-04-10 11:38:57 -07:00
|
|
|
from urllib.parse import urlparse
|
2021-09-11 14:16:52 -07:00
|
|
|
|
2021-05-11 11:31:02 -07:00
|
|
|
from django.apps import apps
|
2020-09-17 13:09:11 -07:00
|
|
|
from django.db import models
|
2021-09-11 14:16:52 -07:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2020-09-21 08:16:34 -07:00
|
|
|
from .base_model import BookWyrmModel
|
2020-09-17 13:09:11 -07:00
|
|
|
|
2021-09-11 14:16:52 -07:00
|
|
|
FederationStatus = [
|
|
|
|
("federated", _("Federated")),
|
|
|
|
("blocked", _("Blocked")),
|
|
|
|
]
|
2021-04-05 15:16:21 -07:00
|
|
|
|
2020-09-17 13:09:11 -07:00
|
|
|
|
2020-09-21 08:16:34 -07:00
|
|
|
class FederatedServer(BookWyrmModel):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""store which servers we federate with"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
2024-04-07 17:19:48 +02:00
|
|
|
server_name = models.CharField(max_length=255, unique=True) # domain
|
2021-04-05 15:16:21 -07:00
|
|
|
status = models.CharField(
|
2021-09-11 14:16:52 -07:00
|
|
|
max_length=255, default="federated", choices=FederationStatus
|
2021-04-05 15:16:21 -07:00
|
|
|
)
|
2020-09-21 08:10:37 -07:00
|
|
|
# is it mastodon, bookwyrm, etc
|
2021-04-07 11:28:31 -07:00
|
|
|
application_type = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
application_version = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
notes = models.TextField(null=True, blank=True)
|
2020-09-17 13:09:11 -07:00
|
|
|
|
2021-04-05 15:16:21 -07:00
|
|
|
def block(self):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""block a server"""
|
2021-04-05 15:16:21 -07:00
|
|
|
self.status = "blocked"
|
2021-09-08 11:47:36 -07:00
|
|
|
self.save(update_fields=["status"])
|
2021-04-10 10:37:28 -07:00
|
|
|
|
|
|
|
# deactivate all associated users
|
2021-04-12 10:13:38 -07:00
|
|
|
self.user_set.filter(is_active=True).update(
|
|
|
|
is_active=False, deactivation_reason="domain_block"
|
|
|
|
)
|
2021-04-10 10:37:28 -07:00
|
|
|
|
2021-05-11 11:31:02 -07:00
|
|
|
# check for related connectors
|
|
|
|
if self.application_type == "bookwyrm":
|
2021-05-18 12:23:36 -07:00
|
|
|
connector_model = apps.get_model("bookwyrm.Connector", require_ready=True)
|
2021-05-11 11:31:02 -07:00
|
|
|
connector_model.objects.filter(
|
|
|
|
identifier=self.server_name, active=True
|
|
|
|
).update(active=False, deactivation_reason="domain_block")
|
|
|
|
|
2021-04-10 10:37:28 -07:00
|
|
|
def unblock(self):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""unblock a server"""
|
2021-04-10 10:37:28 -07:00
|
|
|
self.status = "federated"
|
2021-09-08 11:47:36 -07:00
|
|
|
self.save(update_fields=["status"])
|
2021-04-10 10:37:28 -07:00
|
|
|
|
2021-04-12 10:13:38 -07:00
|
|
|
self.user_set.filter(deactivation_reason="domain_block").update(
|
|
|
|
is_active=True, deactivation_reason=None
|
|
|
|
)
|
2021-04-10 11:38:57 -07:00
|
|
|
|
2021-05-11 11:31:02 -07:00
|
|
|
# check for related connectors
|
|
|
|
if self.application_type == "bookwyrm":
|
2021-05-18 12:23:36 -07:00
|
|
|
connector_model = apps.get_model("bookwyrm.Connector", require_ready=True)
|
2021-05-11 11:31:02 -07:00
|
|
|
connector_model.objects.filter(
|
|
|
|
identifier=self.server_name,
|
|
|
|
active=False,
|
|
|
|
deactivation_reason="domain_block",
|
|
|
|
).update(active=True, deactivation_reason=None)
|
|
|
|
|
2021-04-10 11:38:57 -07:00
|
|
|
@classmethod
|
2023-07-28 17:43:32 +02:00
|
|
|
def is_blocked(cls, url: str) -> bool:
|
2021-04-26 09:15:42 -07:00
|
|
|
"""look up if a domain is blocked"""
|
2021-04-10 11:38:57 -07:00
|
|
|
url = urlparse(url)
|
2024-04-07 17:19:48 +02:00
|
|
|
return cls.objects.filter(server_name=url.hostname, status="blocked").exists()
|