This repository has been archived on 2023-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
takahe/users/views/identity.py

210 lines
7.7 KiB
Python
Raw Normal View History

2022-11-06 03:10:39 +01:00
import string
2022-11-05 21:17:27 +01:00
from django import forms
from django.contrib.auth.decorators import login_required
from django.core import validators
from django.http import Http404, JsonResponse
2022-11-05 21:17:27 +01:00
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views.generic import FormView, TemplateView, View
from core.ld import canonicalise
from core.models import Config
from users.decorators import identity_required
2022-11-17 06:23:32 +01:00
from users.models import Domain, Follow, FollowStates, Identity, IdentityStates
2022-11-05 21:17:27 +01:00
from users.shortcuts import by_handle_or_404
class ViewIdentity(TemplateView):
"""
Shows identity profile pages, and also acts as the Actor endpoint when
approached with the right Accept header.
"""
2022-11-05 21:17:27 +01:00
template_name = "identity/view.html"
def get(self, request, handle):
# Make sure we understand this handle
identity = by_handle_or_404(
self.request,
handle,
local=False,
fetch=True,
)
# If they're coming in looking for JSON, they want the actor
accept = request.META.get("HTTP_ACCEPT", "text/html").lower()
if (
"application/json" in accept
or "application/ld" in accept
or "application/activity" in accept
):
# Return actor info
return self.serve_actor(identity)
else:
# Show normal page
return super().get(request, identity=identity)
def serve_actor(self, identity):
# If this not a local actor, redirect to their canonical URI
if not identity.local:
return redirect(identity.actor_uri)
return JsonResponse(canonicalise(identity.to_ap(), include_security=True))
def get_context_data(self, identity):
2022-11-12 07:04:43 +01:00
posts = identity.posts.all()[:100]
if identity.data_age > Config.system.identity_max_age:
identity.transition_perform(IdentityStates.outdated)
2022-11-17 06:23:32 +01:00
follow = None
2022-11-18 04:04:01 +01:00
reverse_follow = None
2022-11-17 06:23:32 +01:00
if self.request.identity:
follow = Follow.maybe_get(self.request.identity, identity)
2022-11-18 04:04:01 +01:00
if follow and follow.state not in FollowStates.group_active():
2022-11-17 06:23:32 +01:00
follow = None
2022-11-18 04:04:01 +01:00
reverse_follow = Follow.maybe_get(identity, self.request.identity)
if (
reverse_follow
and reverse_follow.state not in FollowStates.group_active()
):
reverse_follow = None
2022-11-05 21:17:27 +01:00
return {
"identity": identity,
2022-11-12 07:04:43 +01:00
"posts": posts,
2022-11-17 06:23:32 +01:00
"follow": follow,
2022-11-18 04:04:01 +01:00
"reverse_follow": reverse_follow,
2022-11-05 21:17:27 +01: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)
2022-11-17 06:23:32 +01:00
elif existing_follow.state in [
FollowStates.undone,
FollowStates.undone_remotely,
]:
existing_follow.transition_perform(FollowStates.unrequested)
elif action == "unfollow":
existing_follow = Follow.maybe_get(self.request.identity, identity)
if existing_follow:
existing_follow.transition_perform(FollowStates.undone)
else:
raise ValueError(f"Cannot handle identity action {action}")
return redirect(identity.urls.view)
2022-11-05 21:17:27 +01: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-06 03:10:39 +01: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 21:17:27 +01:00
@method_decorator(login_required, name="dispatch")
class CreateIdentity(FormView):
template_name = "identity/create.html"
class form_class(forms.Form):
2022-11-14 00:14:38 +01:00
username = forms.CharField(
help_text="Must be unique on your domain. Cannot be changed easily. Use only: a-z 0-9 _ -"
)
domain = forms.ChoiceField(
help_text="Pick the domain to make this identity on. Cannot be changed later."
)
name = forms.CharField(
help_text="The display name other users see. You can change this easily."
)
2022-11-05 21:17:27 +01:00
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
2022-11-14 00:14:38 +01:00
self.fields["domain"].choices = [
(domain.domain, domain.domain)
for domain in Domain.available_for_user(user)
]
self.user = user
def clean_username(self):
2022-11-14 00:14:38 +01:00
# Remove any leading @ and force it lowercase
value = self.cleaned_data["username"].lstrip("@").lower()
if not self.user.admin:
# Apply username min length
limit = int(Config.system.identity_min_length)
validators.MinLengthValidator(limit)(value)
# Apply username restrictions
if value in Config.system.restricted_usernames.split():
raise forms.ValidationError(
"This username is restricted to administrators only."
)
2022-11-06 03:10:39 +01:00
# Validate it's all ascii characters
for character in value:
if character not in string.ascii_letters + string.digits + "_-":
raise forms.ValidationError(
2022-11-14 00:14:38 +01:00
"Only the letters a-z, numbers 0-9, dashes, and underscores are allowed."
2022-11-06 03:10:39 +01:00
)
2022-11-05 21:17:27 +01:00
return value
def clean(self):
# Check for existing users
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()
):
raise forms.ValidationError(f"{username}@{domain} is already taken")
if not self.user.admin and (
Identity.objects.filter(users=self.user).count()
>= Config.system.identity_max_per_user
):
raise forms.ValidationError(
f"You are not allowed more than {Config.system.identity_max_per_user} identities"
)
def get_form(self):
form_class = self.get_form_class()
return form_class(user=self.request.user, **self.get_form_kwargs())
2022-11-05 21:17:27 +01:00
def form_valid(self, form):
username = form.cleaned_data["username"]
domain = form.cleaned_data["domain"]
domain_instance = Domain.get_domain(domain)
2022-11-05 21:17:27 +01:00
new_identity = Identity.objects.create(
actor_uri=f"https://{domain_instance.uri_domain}/@{username}@{domain}/",
2022-11-17 07:10:55 +01:00
username=username.lower(),
domain_id=domain,
2022-11-05 21:17:27 +01: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)