2022-11-16 14:53:39 +01:00
|
|
|
from django import forms
|
2022-11-16 02:30:30 +01:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.utils.decorators import method_decorator
|
2022-11-16 14:53:39 +01:00
|
|
|
from django.views.generic import FormView, TemplateView, View
|
2022-11-16 02:30:30 +01:00
|
|
|
|
2022-11-23 02:51:01 +01:00
|
|
|
from activities.models import (
|
|
|
|
Post,
|
|
|
|
PostInteraction,
|
|
|
|
PostInteractionStates,
|
|
|
|
TimelineEvent,
|
|
|
|
)
|
2022-11-18 02:52:00 +01:00
|
|
|
from core.models import Config
|
2022-11-16 02:30:30 +01:00
|
|
|
from users.decorators import identity_required
|
|
|
|
from users.shortcuts import by_handle_or_404
|
|
|
|
|
|
|
|
|
2022-11-16 14:53:39 +01:00
|
|
|
class Individual(TemplateView):
|
2022-11-16 02:30:30 +01:00
|
|
|
|
|
|
|
template_name = "activities/post.html"
|
|
|
|
|
|
|
|
def get_context_data(self, handle, post_id):
|
|
|
|
identity = by_handle_or_404(self.request, handle, local=False)
|
|
|
|
post = get_object_or_404(identity.posts, pk=post_id)
|
|
|
|
return {
|
|
|
|
"identity": identity,
|
|
|
|
"post": post,
|
|
|
|
"interactions": PostInteraction.get_post_interactions(
|
|
|
|
[post],
|
|
|
|
self.request.identity,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class Like(View):
|
|
|
|
"""
|
|
|
|
Adds/removes a like from the current identity to the post
|
|
|
|
"""
|
|
|
|
|
|
|
|
undo = False
|
|
|
|
|
|
|
|
def post(self, request, handle, post_id):
|
|
|
|
identity = by_handle_or_404(self.request, handle, local=False)
|
2022-11-17 07:00:10 +01:00
|
|
|
post = get_object_or_404(
|
|
|
|
identity.posts.prefetch_related("attachments"), pk=post_id
|
|
|
|
)
|
2022-11-16 02:30:30 +01:00
|
|
|
if self.undo:
|
|
|
|
# Undo any likes on the post
|
|
|
|
for interaction in PostInteraction.objects.filter(
|
|
|
|
type=PostInteraction.Types.like,
|
|
|
|
identity=request.identity,
|
|
|
|
post=post,
|
|
|
|
):
|
|
|
|
interaction.transition_perform(PostInteractionStates.undone)
|
|
|
|
else:
|
|
|
|
# Make a like on this post if we didn't already
|
|
|
|
PostInteraction.objects.get_or_create(
|
|
|
|
type=PostInteraction.Types.like,
|
|
|
|
identity=request.identity,
|
|
|
|
post=post,
|
|
|
|
)
|
|
|
|
# Return either a redirect or a HTMX snippet
|
|
|
|
if request.htmx:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"activities/_like.html",
|
|
|
|
{
|
|
|
|
"post": post,
|
|
|
|
"interactions": {"like": set() if self.undo else {post.pk}},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return redirect(post.urls.view)
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class Boost(View):
|
|
|
|
"""
|
|
|
|
Adds/removes a boost from the current identity to the post
|
|
|
|
"""
|
|
|
|
|
|
|
|
undo = False
|
|
|
|
|
|
|
|
def post(self, request, handle, post_id):
|
|
|
|
identity = by_handle_or_404(self.request, handle, local=False)
|
|
|
|
post = get_object_or_404(identity.posts, pk=post_id)
|
|
|
|
if self.undo:
|
|
|
|
# Undo any boosts on the post
|
|
|
|
for interaction in PostInteraction.objects.filter(
|
|
|
|
type=PostInteraction.Types.boost,
|
|
|
|
identity=request.identity,
|
|
|
|
post=post,
|
|
|
|
):
|
|
|
|
interaction.transition_perform(PostInteractionStates.undone)
|
|
|
|
else:
|
|
|
|
# Make a boost on this post if we didn't already
|
|
|
|
PostInteraction.objects.get_or_create(
|
|
|
|
type=PostInteraction.Types.boost,
|
|
|
|
identity=request.identity,
|
|
|
|
post=post,
|
|
|
|
)
|
|
|
|
# Return either a redirect or a HTMX snippet
|
|
|
|
if request.htmx:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"activities/_boost.html",
|
|
|
|
{
|
|
|
|
"post": post,
|
|
|
|
"interactions": {"boost": set() if self.undo else {post.pk}},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return redirect(post.urls.view)
|
2022-11-16 14:53:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class Compose(FormView):
|
|
|
|
|
|
|
|
template_name = "activities/compose.html"
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
2022-11-18 02:52:00 +01:00
|
|
|
|
2022-11-16 14:53:39 +01:00
|
|
|
text = forms.CharField(
|
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
"placeholder": "What's on your mind?",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
visibility = forms.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
(Post.Visibilities.public, "Public"),
|
|
|
|
(Post.Visibilities.unlisted, "Unlisted"),
|
|
|
|
(Post.Visibilities.followers, "Followers & Mentioned Only"),
|
|
|
|
(Post.Visibilities.mentioned, "Mentioned Only"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
content_warning = forms.CharField(
|
|
|
|
required=False,
|
2022-11-23 03:52:40 +01:00
|
|
|
label=Config.lazy_system_value("content_warning_text"),
|
2022-11-16 14:53:39 +01:00
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
2022-11-23 03:52:40 +01:00
|
|
|
"placeholder": Config.lazy_system_value("content_warning_text"),
|
2022-11-16 14:53:39 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
help_text="Optional - Post will be hidden behind this text until clicked",
|
|
|
|
)
|
|
|
|
|
2022-11-18 02:52:00 +01:00
|
|
|
def clean_text(self):
|
|
|
|
text = self.cleaned_data.get("text")
|
|
|
|
if not text:
|
|
|
|
return text
|
|
|
|
length = len(text)
|
|
|
|
if length > Config.system.post_length:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
f"Maximum post length is {Config.system.post_length} characters (you have {length})"
|
|
|
|
)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
form = super().get_form_class()
|
|
|
|
form.declared_fields["text"]
|
|
|
|
return form
|
|
|
|
|
2022-11-16 14:53:39 +01:00
|
|
|
def form_valid(self, form):
|
2022-11-23 02:51:01 +01:00
|
|
|
post = Post.create_local(
|
2022-11-16 14:53:39 +01:00
|
|
|
author=self.request.identity,
|
2022-11-22 05:18:13 +01:00
|
|
|
content=form.cleaned_data["text"],
|
2022-11-16 14:53:39 +01:00
|
|
|
summary=form.cleaned_data.get("content_warning"),
|
|
|
|
visibility=form.cleaned_data["visibility"],
|
|
|
|
)
|
2022-11-23 02:51:01 +01:00
|
|
|
# Add their own timeline event for immediate visibility
|
|
|
|
TimelineEvent.add_post(self.request.identity, post)
|
2022-11-16 14:53:39 +01:00
|
|
|
return redirect("/")
|