2022-11-05 19:10:39 -07:00
|
|
|
import string
|
|
|
|
|
2022-11-05 13:17:27 -07:00
|
|
|
from django import forms
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2022-11-12 20:14:21 -08:00
|
|
|
from django.http import Http404
|
2022-11-05 13:17:27 -07:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.generic import FormView, TemplateView, View
|
|
|
|
|
2022-11-12 21:10:06 -08:00
|
|
|
from core.config import Config
|
2022-11-05 13:17:27 -07:00
|
|
|
from core.forms import FormHelper
|
2022-11-06 20:30:07 -08:00
|
|
|
from users.decorators import identity_required
|
2022-11-12 20:14:21 -08:00
|
|
|
from users.models import Domain, Follow, Identity, IdentityStates
|
2022-11-05 13:17:27 -07:00
|
|
|
from users.shortcuts import by_handle_or_404
|
|
|
|
|
|
|
|
|
|
|
|
class ViewIdentity(TemplateView):
|
|
|
|
|
|
|
|
template_name = "identity/view.html"
|
|
|
|
|
|
|
|
def get_context_data(self, handle):
|
2022-11-06 20:30:07 -08:00
|
|
|
identity = by_handle_or_404(
|
|
|
|
self.request,
|
|
|
|
handle,
|
|
|
|
local=False,
|
|
|
|
fetch=True,
|
|
|
|
)
|
2022-11-11 22:04:43 -08:00
|
|
|
posts = identity.posts.all()[:100]
|
2022-11-13 12:57:27 -08:00
|
|
|
if identity.data_age > Config.load().identity_max_age:
|
2022-11-09 21:29:33 -08:00
|
|
|
identity.transition_perform(IdentityStates.outdated)
|
2022-11-05 13:17:27 -07:00
|
|
|
return {
|
|
|
|
"identity": identity,
|
2022-11-11 22:04:43 -08:00
|
|
|
"posts": posts,
|
2022-11-06 20:30:07 -08:00
|
|
|
"follow": Follow.maybe_get(self.request.identity, identity)
|
|
|
|
if self.request.identity
|
|
|
|
else None,
|
2022-11-05 13:17:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-06 20:30:07 -08:00
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class ActionIdentity(View):
|
|
|
|
def post(self, request, handle):
|
|
|
|
identity = by_handle_or_404(self.request, handle, local=False)
|
|
|
|
# See what action we should perform
|
|
|
|
action = self.request.POST["action"]
|
|
|
|
if action == "follow":
|
|
|
|
existing_follow = Follow.maybe_get(self.request.identity, identity)
|
|
|
|
if not existing_follow:
|
|
|
|
Follow.create_local(self.request.identity, identity)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Cannot handle identity action {action}")
|
|
|
|
return redirect(identity.urls.view)
|
|
|
|
|
|
|
|
|
2022-11-05 13:17:27 -07:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class SelectIdentity(TemplateView):
|
|
|
|
|
|
|
|
template_name = "identity/select.html"
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
return {
|
|
|
|
"identities": Identity.objects.filter(users__pk=self.request.user.pk),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-05 19:10:39 -07:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class ActivateIdentity(View):
|
|
|
|
def get(self, request, handle):
|
|
|
|
identity = by_handle_or_404(request, handle)
|
|
|
|
if not identity.users.filter(pk=request.user.pk).exists():
|
|
|
|
raise Http404()
|
|
|
|
request.session["identity_id"] = identity.id
|
|
|
|
# Get next URL, not allowing offsite links
|
|
|
|
next = request.GET.get("next") or "/"
|
|
|
|
if ":" in next:
|
|
|
|
next = "/"
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
2022-11-05 13:17:27 -07:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class CreateIdentity(FormView):
|
|
|
|
|
|
|
|
template_name = "identity/create.html"
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
2022-11-06 12:48:04 -08:00
|
|
|
username = forms.CharField()
|
2022-11-05 13:17:27 -07:00
|
|
|
name = forms.CharField()
|
|
|
|
|
|
|
|
helper = FormHelper(submit_text="Create")
|
|
|
|
|
2022-11-06 12:48:04 -08:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["domain"] = forms.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
(domain.domain, domain.domain)
|
|
|
|
for domain in Domain.available_for_user(user)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def clean_username(self):
|
2022-11-05 13:17:27 -07:00
|
|
|
# Remove any leading @
|
2022-11-06 12:48:04 -08:00
|
|
|
value = self.cleaned_data["username"].lstrip("@")
|
2022-11-05 19:10:39 -07:00
|
|
|
# Validate it's all ascii characters
|
|
|
|
for character in value:
|
|
|
|
if character not in string.ascii_letters + string.digits + "_-":
|
|
|
|
raise forms.ValidationError(
|
|
|
|
"Only the letters a-z, numbers 0-9, dashes and underscores are allowed."
|
|
|
|
)
|
2022-11-05 13:17:27 -07:00
|
|
|
return value
|
|
|
|
|
2022-11-06 12:48:04 -08:00
|
|
|
def clean(self):
|
|
|
|
# Check for existing users
|
2022-11-09 22:48:31 -08:00
|
|
|
username = self.cleaned_data.get("username")
|
|
|
|
domain = self.cleaned_data.get("domain")
|
|
|
|
if (
|
|
|
|
username
|
|
|
|
and domain
|
|
|
|
and Identity.objects.filter(username=username, domain=domain).exists()
|
|
|
|
):
|
2022-11-06 12:48:04 -08:00
|
|
|
raise forms.ValidationError(f"{username}@{domain} is already taken")
|
|
|
|
|
|
|
|
def get_form(self):
|
|
|
|
form_class = self.get_form_class()
|
|
|
|
return form_class(user=self.request.user, **self.get_form_kwargs())
|
|
|
|
|
2022-11-05 13:17:27 -07:00
|
|
|
def form_valid(self, form):
|
2022-11-06 12:48:04 -08:00
|
|
|
username = form.cleaned_data["username"]
|
|
|
|
domain = form.cleaned_data["domain"]
|
2022-11-09 21:29:33 -08:00
|
|
|
domain_instance = Domain.get_domain(domain)
|
2022-11-05 13:17:27 -07:00
|
|
|
new_identity = Identity.objects.create(
|
2022-11-06 23:19:00 -08:00
|
|
|
actor_uri=f"https://{domain_instance.uri_domain}/@{username}@{domain}/actor/",
|
2022-11-06 12:48:04 -08:00
|
|
|
username=username,
|
|
|
|
domain_id=domain,
|
2022-11-05 13:17:27 -07:00
|
|
|
name=form.cleaned_data["name"],
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
new_identity.users.add(self.request.user)
|
|
|
|
new_identity.generate_keypair()
|
|
|
|
return redirect(new_identity.urls.view)
|