2023-07-24 16:59:50 -07:00
|
|
|
import csv
|
|
|
|
|
2022-11-25 16:52:43 -08:00
|
|
|
from django import forms
|
2023-07-24 16:59:50 -07:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.core.validators import FileExtensionValidator, ValidationError
|
2022-11-25 16:52:43 -08:00
|
|
|
from django.db import models
|
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
from django.utils.decorators import method_decorator
|
2022-12-05 18:54:47 -08:00
|
|
|
from django.views.generic import FormView, ListView
|
2022-11-25 16:52:43 -08:00
|
|
|
|
2022-12-20 01:16:03 -08:00
|
|
|
from users.decorators import admin_required
|
2022-11-25 16:52:43 -08:00
|
|
|
from users.models import Domain
|
2023-07-24 16:59:50 -07:00
|
|
|
from users.services import DomainService
|
|
|
|
from users.views.admin.domains import DomainValidator
|
2022-11-25 16:52:43 -08:00
|
|
|
|
|
|
|
|
2022-12-20 01:16:03 -08:00
|
|
|
@method_decorator(admin_required, name="dispatch")
|
2022-12-05 18:54:47 -08:00
|
|
|
class FederationRoot(ListView):
|
2022-11-25 16:52:43 -08:00
|
|
|
template_name = "admin/federation.html"
|
2022-12-05 18:54:47 -08:00
|
|
|
paginate_by = 50
|
2022-11-25 16:52:43 -08:00
|
|
|
|
2022-12-05 18:54:47 -08:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.query = request.GET.get("query")
|
|
|
|
self.extra_context = {
|
2022-11-25 16:52:43 -08:00
|
|
|
"section": "federation",
|
2022-12-05 18:54:47 -08:00
|
|
|
"query": self.query or "",
|
2022-11-25 16:52:43 -08:00
|
|
|
}
|
2022-12-05 18:54:47 -08:00
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
domains = (
|
|
|
|
Domain.objects.filter(local=False)
|
|
|
|
.annotate(num_users=models.Count("identities"))
|
|
|
|
.order_by("domain")
|
|
|
|
)
|
|
|
|
if self.query:
|
|
|
|
domains = domains.filter(domain__icontains=self.query)
|
|
|
|
return domains
|
2022-11-25 16:52:43 -08:00
|
|
|
|
|
|
|
|
2022-12-20 01:16:03 -08:00
|
|
|
@method_decorator(admin_required, name="dispatch")
|
2022-11-25 16:52:43 -08:00
|
|
|
class FederationEdit(FormView):
|
|
|
|
template_name = "admin/federation_edit.html"
|
|
|
|
extra_context = {"section": "federation"}
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
|
|
|
blocked = forms.BooleanField(
|
2022-11-25 17:11:31 -08:00
|
|
|
help_text="If this domain is blocked from interacting with this server.\nAll incoming posts from this domain will be irrecoverably dropped.",
|
2022-11-25 16:52:43 -08:00
|
|
|
widget=forms.Select(choices=[(True, "Blocked"), (False, "Not Blocked")]),
|
|
|
|
required=False,
|
|
|
|
)
|
2023-03-06 15:37:05 -08:00
|
|
|
notes = forms.CharField(
|
|
|
|
label="Notes",
|
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
"rows": 3,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
required=False,
|
|
|
|
)
|
2022-11-25 16:52:43 -08:00
|
|
|
|
|
|
|
def dispatch(self, request, domain):
|
|
|
|
self.domain = get_object_or_404(
|
|
|
|
Domain.objects.filter(local=False), domain=domain
|
|
|
|
)
|
|
|
|
return super().dispatch(request)
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super().get_context_data(*args, **kwargs)
|
|
|
|
context["domain"] = self.domain
|
2023-06-14 10:15:29 -07:00
|
|
|
context["page"] = self.request.GET.get("page")
|
2022-11-25 16:52:43 -08:00
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.domain.blocked = form.cleaned_data["blocked"]
|
2023-03-06 15:37:05 -08:00
|
|
|
self.domain.notes = form.cleaned_data["notes"] or None
|
2022-11-25 16:52:43 -08:00
|
|
|
self.domain.save()
|
|
|
|
return redirect(Domain.urls.root_federation)
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
return {
|
|
|
|
"blocked": self.domain.blocked,
|
2023-03-06 15:37:05 -08:00
|
|
|
"notes": self.domain.notes,
|
2022-11-25 16:52:43 -08:00
|
|
|
}
|
2023-07-24 16:59:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(admin_required, name="dispatch")
|
|
|
|
class FederationBlocklist(FormView):
|
|
|
|
template_name = "admin/federation_blocklist.html"
|
|
|
|
extra_context = {"section": "federation"}
|
|
|
|
error_msg = "The uploaded file has an invalid blocklist CSV format."
|
|
|
|
success_msg = "The blocklist CSV was processed processed with success!"
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
|
|
|
blocklist = forms.FileField(
|
|
|
|
help_text=(
|
|
|
|
"Blocklist file with one domain per line. "
|
2023-08-03 09:41:47 -07:00
|
|
|
"CSVs with 'domain' and '#domain' headers are also supported."
|
2023-07-24 16:59:50 -07:00
|
|
|
),
|
|
|
|
validators=[FileExtensionValidator(allowed_extensions=["txt", "csv"])],
|
|
|
|
)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
validator = DomainValidator()
|
|
|
|
domains = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
lines = form.cleaned_data["blocklist"].read().decode("utf-8").splitlines()
|
|
|
|
|
2023-08-03 09:41:47 -07:00
|
|
|
if "#domain" in lines[0] or "domain" in lines[0]:
|
2023-07-24 16:59:50 -07:00
|
|
|
reader = csv.DictReader(lines)
|
|
|
|
else:
|
|
|
|
reader = csv.DictReader(lines, fieldnames=["#domain"])
|
|
|
|
|
|
|
|
for row in reader:
|
2023-08-03 09:41:47 -07:00
|
|
|
domain = row.get("#domain", row.get("domain", "")).strip()
|
2023-07-24 16:59:50 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
validator(domain)
|
|
|
|
except ValidationError:
|
|
|
|
# skip adding invalid domain
|
|
|
|
# to the blocklist
|
|
|
|
continue
|
|
|
|
|
|
|
|
domains.append(domain)
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
messages.error(self.request, self.error_msg)
|
|
|
|
return redirect(".")
|
|
|
|
|
|
|
|
if not domains:
|
|
|
|
messages.error(self.request, self.error_msg)
|
|
|
|
return redirect(".")
|
|
|
|
|
|
|
|
DomainService.block(domains)
|
|
|
|
|
|
|
|
messages.success(self.request, self.success_msg)
|
|
|
|
return redirect("admin_federation")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["page"] = self.request.GET.get("page")
|
|
|
|
return context
|