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