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-12-04 07:20:50 -08:00
|
|
|
from django.contrib.syndication.views import Feed
|
2022-11-20 10:13:44 -08:00
|
|
|
from django.core import validators
|
2022-11-18 07:28:15 -08:00
|
|
|
from django.http import Http404, JsonResponse
|
2022-11-05 13:17:27 -07:00
|
|
|
from django.shortcuts import redirect
|
2022-12-31 09:29:51 -08:00
|
|
|
from django.utils import feedgenerator
|
2022-11-05 13:17:27 -07:00
|
|
|
from django.utils.decorators import method_decorator
|
2022-12-10 11:16:08 -08:00
|
|
|
from django.views.decorators.vary import vary_on_headers
|
2022-11-22 17:39:15 -08:00
|
|
|
from django.views.generic import FormView, ListView, TemplateView, View
|
2022-11-05 13:17:27 -07:00
|
|
|
|
2022-11-22 18:58:42 -08:00
|
|
|
from activities.models import Post, PostInteraction
|
2022-12-22 13:11:47 -08:00
|
|
|
from activities.services import TimelineService
|
2022-12-05 21:23:07 -08:00
|
|
|
from core.decorators import cache_page, cache_page_by_ap_json
|
2022-11-18 07:28:15 -08:00
|
|
|
from core.ld import canonicalise
|
2022-11-16 16:23:46 -08:00
|
|
|
from core.models import Config
|
2022-11-06 20:30:07 -08:00
|
|
|
from users.decorators import identity_required
|
2022-11-16 21:23:32 -08:00
|
|
|
from users.models import Domain, Follow, FollowStates, Identity, IdentityStates
|
2022-12-19 12:54:09 -08:00
|
|
|
from users.services import IdentityService
|
2022-11-05 13:17:27 -07:00
|
|
|
from users.shortcuts import by_handle_or_404
|
|
|
|
|
|
|
|
|
2022-12-10 11:16:08 -08:00
|
|
|
@method_decorator(vary_on_headers("Accept"), name="dispatch")
|
2022-12-05 21:23:07 -08:00
|
|
|
@method_decorator(cache_page_by_ap_json(public_only=True), name="dispatch")
|
2022-11-22 17:39:15 -08:00
|
|
|
class ViewIdentity(ListView):
|
2022-11-18 07:28:15 -08:00
|
|
|
"""
|
|
|
|
Shows identity profile pages, and also acts as the Actor endpoint when
|
|
|
|
approached with the right Accept header.
|
|
|
|
"""
|
2022-11-05 13:17:27 -07:00
|
|
|
|
|
|
|
template_name = "identity/view.html"
|
2022-12-21 12:36:10 -08:00
|
|
|
paginate_by = 25
|
2022-11-05 13:17:27 -07:00
|
|
|
|
2022-11-18 07:28:15 -08:00
|
|
|
def get(self, request, handle):
|
|
|
|
# Make sure we understand this handle
|
2022-11-22 17:39:15 -08:00
|
|
|
self.identity = by_handle_or_404(
|
2022-11-06 20:30:07 -08:00
|
|
|
self.request,
|
|
|
|
handle,
|
|
|
|
local=False,
|
|
|
|
fetch=True,
|
|
|
|
)
|
2022-11-22 17:39:15 -08:00
|
|
|
if (
|
|
|
|
not self.identity.local
|
|
|
|
and self.identity.data_age > Config.system.identity_max_age
|
|
|
|
):
|
|
|
|
self.identity.transition_perform(IdentityStates.outdated)
|
2022-11-18 07:28:15 -08:00
|
|
|
# If they're coming in looking for JSON, they want the actor
|
2022-12-05 19:02:35 -08:00
|
|
|
if request.ap_json:
|
2022-11-18 07:28:15 -08:00
|
|
|
# Return actor info
|
2022-11-22 17:39:15 -08:00
|
|
|
return self.serve_actor(self.identity)
|
2022-11-18 07:28:15 -08:00
|
|
|
else:
|
|
|
|
# Show normal page
|
2022-11-22 17:39:15 -08:00
|
|
|
return super().get(request, identity=self.identity)
|
2022-11-18 07:28:15 -08:00
|
|
|
|
|
|
|
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)
|
2022-11-27 13:43:46 -08:00
|
|
|
return JsonResponse(
|
|
|
|
canonicalise(identity.to_ap(), include_security=True),
|
|
|
|
content_type="application/activity+json",
|
|
|
|
)
|
2022-11-18 07:28:15 -08:00
|
|
|
|
2022-11-22 17:39:15 -08:00
|
|
|
def get_queryset(self):
|
2022-12-22 13:11:47 -08:00
|
|
|
return TimelineService(self.request.identity).identity_public(self.identity)
|
2022-11-22 17:39:15 -08:00
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
|
|
|
context["identity"] = self.identity
|
|
|
|
context["follow"] = None
|
|
|
|
context["reverse_follow"] = None
|
2022-11-22 18:58:42 -08:00
|
|
|
context["interactions"] = PostInteraction.get_post_interactions(
|
|
|
|
context["page_obj"],
|
|
|
|
self.request.identity,
|
|
|
|
)
|
2022-12-29 20:31:34 -08:00
|
|
|
context["post_count"] = self.identity.posts.count()
|
2022-12-14 09:15:46 -08:00
|
|
|
if self.identity.config_identity.visible_follows:
|
|
|
|
context["followers_count"] = self.identity.inbound_follows.filter(
|
|
|
|
state__in=FollowStates.group_active()
|
|
|
|
).count()
|
|
|
|
context["following_count"] = self.identity.outbound_follows.filter(
|
|
|
|
state__in=FollowStates.group_active()
|
|
|
|
).count()
|
2022-11-16 21:23:32 -08:00
|
|
|
if self.request.identity:
|
2022-11-22 17:39:15 -08:00
|
|
|
follow = Follow.maybe_get(self.request.identity, self.identity)
|
|
|
|
if follow and follow.state in FollowStates.group_active():
|
|
|
|
context["follow"] = follow
|
|
|
|
reverse_follow = Follow.maybe_get(self.identity, self.request.identity)
|
|
|
|
if reverse_follow and reverse_follow.state in FollowStates.group_active():
|
|
|
|
context["reverse_follow"] = reverse_follow
|
|
|
|
return context
|
2022-11-05 13:17:27 -07:00
|
|
|
|
|
|
|
|
2022-12-05 09:55:30 -08:00
|
|
|
@method_decorator(
|
2022-12-05 21:23:07 -08:00
|
|
|
cache_page("cache_timeout_identity_feed", public_only=True), name="__call__"
|
2022-12-05 09:55:30 -08:00
|
|
|
)
|
2022-12-04 07:20:50 -08:00
|
|
|
class IdentityFeed(Feed):
|
|
|
|
"""
|
|
|
|
Serves a local user's Public posts as an RSS feed
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_object(self, request, handle):
|
|
|
|
return by_handle_or_404(
|
|
|
|
request,
|
|
|
|
handle,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def title(self, identity: Identity):
|
|
|
|
return identity.name
|
|
|
|
|
|
|
|
def description(self, identity: Identity):
|
|
|
|
return f"Public posts from @{identity.handle}"
|
|
|
|
|
|
|
|
def link(self, identity: Identity):
|
|
|
|
return identity.absolute_profile_uri()
|
|
|
|
|
|
|
|
def items(self, identity: Identity):
|
2022-12-22 13:11:47 -08:00
|
|
|
return TimelineService(None).identity_public(identity)[:20]
|
2022-12-04 07:20:50 -08:00
|
|
|
|
|
|
|
def item_description(self, item: Post):
|
|
|
|
return item.safe_content_remote()
|
|
|
|
|
|
|
|
def item_link(self, item: Post):
|
|
|
|
return item.absolute_object_uri()
|
|
|
|
|
|
|
|
def item_pubdate(self, item: Post):
|
|
|
|
return item.published
|
|
|
|
|
2022-12-31 09:29:51 -08:00
|
|
|
def item_enclosures(self, item: Post):
|
|
|
|
attachment = item.attachments.first()
|
|
|
|
if attachment is None:
|
|
|
|
return []
|
|
|
|
|
|
|
|
enc = feedgenerator.Enclosure(
|
|
|
|
url=attachment.full_url().absolute,
|
|
|
|
length=str(attachment.file.size),
|
|
|
|
mime_type=attachment.mimetype,
|
|
|
|
)
|
|
|
|
return [enc]
|
|
|
|
|
2022-12-04 07:20:50 -08:00
|
|
|
|
2022-12-21 12:36:10 -08:00
|
|
|
class IdentityFollows(ListView):
|
|
|
|
"""
|
|
|
|
Shows following/followers for an identity.
|
|
|
|
"""
|
|
|
|
|
|
|
|
template_name = "identity/follows.html"
|
|
|
|
paginate_by = 25
|
|
|
|
inbound = False
|
|
|
|
|
|
|
|
def get(self, request, handle):
|
|
|
|
self.identity = by_handle_or_404(
|
|
|
|
self.request,
|
|
|
|
handle,
|
|
|
|
local=False,
|
|
|
|
)
|
|
|
|
if not Config.load_identity(self.identity).visible_follows:
|
|
|
|
raise Http404("Hidden follows")
|
|
|
|
return super().get(request, identity=self.identity)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
if self.inbound:
|
|
|
|
return IdentityService(self.identity).followers()
|
|
|
|
else:
|
|
|
|
return IdentityService(self.identity).following()
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
|
|
|
context["identity"] = self.identity
|
|
|
|
context["inbound"] = self.inbound
|
|
|
|
context["follows_page"] = True
|
|
|
|
context["followers_count"] = self.identity.inbound_follows.filter(
|
|
|
|
state__in=FollowStates.group_active()
|
|
|
|
).count()
|
|
|
|
context["following_count"] = self.identity.outbound_follows.filter(
|
|
|
|
state__in=FollowStates.group_active()
|
|
|
|
).count()
|
2022-12-29 20:31:34 -08:00
|
|
|
context["post_count"] = self.identity.posts.count()
|
2022-12-21 12:36:10 -08:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
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":
|
2022-12-19 12:54:09 -08:00
|
|
|
IdentityService(identity).follow_from(self.request.identity)
|
2022-11-16 21:23:32 -08:00
|
|
|
elif action == "unfollow":
|
2022-12-19 12:54:09 -08:00
|
|
|
IdentityService(identity).unfollow_from(self.request.identity)
|
2022-12-30 14:03:11 -08:00
|
|
|
elif action == "hide_boosts":
|
|
|
|
IdentityService(identity).follow_from(self.request.identity, boosts=False)
|
|
|
|
elif action == "show_boosts":
|
|
|
|
IdentityService(identity).follow_from(self.request.identity, boosts=True)
|
2022-11-06 20:30:07 -08:00
|
|
|
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-13 15:14:38 -08: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-25 17:32:45 -08:00
|
|
|
discoverable = forms.BooleanField(
|
|
|
|
help_text="If this user is visible on the frontpage and in user directories.",
|
|
|
|
initial=True,
|
|
|
|
widget=forms.Select(
|
|
|
|
choices=[(True, "Discoverable"), (False, "Not Discoverable")]
|
|
|
|
),
|
|
|
|
required=False,
|
|
|
|
)
|
2022-11-05 13:17:27 -07:00
|
|
|
|
2022-11-06 12:48:04 -08:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-11-13 15:14:38 -08:00
|
|
|
self.fields["domain"].choices = [
|
|
|
|
(domain.domain, domain.domain)
|
|
|
|
for domain in Domain.available_for_user(user)
|
|
|
|
]
|
2022-11-20 10:13:44 -08:00
|
|
|
self.user = user
|
2022-11-06 12:48:04 -08:00
|
|
|
|
|
|
|
def clean_username(self):
|
2022-11-13 15:14:38 -08:00
|
|
|
# Remove any leading @ and force it lowercase
|
|
|
|
value = self.cleaned_data["username"].lstrip("@").lower()
|
2022-11-20 10:13:44 -08:00
|
|
|
|
|
|
|
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-20 17:29:19 -08:00
|
|
|
if value in ["__system__"]:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
"This username is reserved for system use."
|
|
|
|
)
|
2022-11-20 10:13:44 -08:00
|
|
|
|
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(
|
2022-11-13 15:14:38 -08:00
|
|
|
"Only the letters a-z, numbers 0-9, dashes, and underscores are allowed."
|
2022-11-05 19:10:39 -07:00
|
|
|
)
|
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
|
2022-12-21 13:39:56 -08:00
|
|
|
and Identity.objects.filter(
|
|
|
|
username__iexact=username,
|
|
|
|
domain=domain.lower(),
|
|
|
|
).exists()
|
2022-11-09 22:48:31 -08:00
|
|
|
):
|
2022-11-06 12:48:04 -08:00
|
|
|
raise forms.ValidationError(f"{username}@{domain} is already taken")
|
|
|
|
|
2022-11-20 10:13:44 -08:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-11-06 12:48:04 -08:00
|
|
|
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-18 07:28:15 -08:00
|
|
|
actor_uri=f"https://{domain_instance.uri_domain}/@{username}@{domain}/",
|
2022-12-21 13:39:56 -08:00
|
|
|
username=username,
|
2022-11-06 12:48:04 -08:00
|
|
|
domain_id=domain,
|
2022-11-05 13:17:27 -07:00
|
|
|
name=form.cleaned_data["name"],
|
|
|
|
local=True,
|
2022-11-25 17:32:45 -08:00
|
|
|
discoverable=form.cleaned_data["discoverable"],
|
2022-11-05 13:17:27 -07:00
|
|
|
)
|
|
|
|
new_identity.users.add(self.request.user)
|
|
|
|
new_identity.generate_keypair()
|
2022-11-22 17:41:10 -08:00
|
|
|
self.request.session["identity_id"] = new_identity.id
|
2022-11-05 13:17:27 -07:00
|
|
|
return redirect(new_identity.urls.view)
|