2022-11-27 19:09:46 +01:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2022-11-27 20:37:52 +01:00
|
|
|
from django.db import models
|
2022-11-27 19:09:46 +01:00
|
|
|
from django.http import JsonResponse
|
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-12-02 02:46:49 +01:00
|
|
|
from django.views.generic import TemplateView, View
|
2022-11-16 02:30:30 +01:00
|
|
|
|
2022-12-02 02:46:49 +01:00
|
|
|
from activities.models import Post, PostInteraction, PostInteractionStates, PostStates
|
2022-12-06 06:23:07 +01:00
|
|
|
from core.decorators import cache_page_by_ap_json
|
2022-11-25 06:31:45 +01:00
|
|
|
from core.ld import canonicalise
|
2022-11-16 02:30:30 +01:00
|
|
|
from users.decorators import identity_required
|
|
|
|
from users.shortcuts import by_handle_or_404
|
|
|
|
|
|
|
|
|
2022-12-06 06:23:07 +01:00
|
|
|
@method_decorator(
|
|
|
|
cache_page_by_ap_json("cache_timeout_page_post", public_only=True), name="dispatch"
|
|
|
|
)
|
2022-11-16 14:53:39 +01:00
|
|
|
class Individual(TemplateView):
|
2022-11-16 02:30:30 +01:00
|
|
|
|
|
|
|
template_name = "activities/post.html"
|
|
|
|
|
2022-11-25 06:31:45 +01:00
|
|
|
def get(self, request, handle, post_id):
|
|
|
|
self.identity = by_handle_or_404(self.request, handle, local=False)
|
|
|
|
self.post_obj = get_object_or_404(self.identity.posts, pk=post_id)
|
|
|
|
# If they're coming in looking for JSON, they want the actor
|
2022-12-06 04:02:35 +01:00
|
|
|
if request.ap_json:
|
2022-11-25 06:31:45 +01:00
|
|
|
# Return post JSON
|
|
|
|
return self.serve_object()
|
|
|
|
else:
|
|
|
|
# Show normal page
|
|
|
|
return super().get(request)
|
|
|
|
|
|
|
|
def get_context_data(self):
|
2022-12-06 00:07:18 +01:00
|
|
|
parent = None
|
2022-11-29 06:34:03 +01:00
|
|
|
if self.post_obj.in_reply_to:
|
2022-12-05 05:22:20 +01:00
|
|
|
try:
|
|
|
|
parent = Post.by_object_uri(self.post_obj.in_reply_to, fetch=True)
|
|
|
|
except Post.DoesNotExist:
|
2022-12-06 00:07:18 +01:00
|
|
|
pass
|
2022-11-16 02:30:30 +01:00
|
|
|
return {
|
2022-11-25 06:31:45 +01:00
|
|
|
"identity": self.identity,
|
|
|
|
"post": self.post_obj,
|
2022-11-16 02:30:30 +01:00
|
|
|
"interactions": PostInteraction.get_post_interactions(
|
2022-11-25 06:31:45 +01:00
|
|
|
[self.post_obj],
|
2022-11-16 02:30:30 +01:00
|
|
|
self.request.identity,
|
|
|
|
),
|
2022-11-27 22:25:09 +01:00
|
|
|
"link_original": True,
|
2022-11-29 06:34:03 +01:00
|
|
|
"parent": parent,
|
2022-11-27 20:37:52 +01:00
|
|
|
"replies": Post.objects.filter(
|
|
|
|
models.Q(
|
|
|
|
visibility__in=[
|
|
|
|
Post.Visibilities.public,
|
|
|
|
Post.Visibilities.local_only,
|
|
|
|
Post.Visibilities.unlisted,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
| models.Q(
|
|
|
|
visibility=Post.Visibilities.followers,
|
|
|
|
author__inbound_follows__source=self.identity,
|
|
|
|
)
|
|
|
|
| models.Q(
|
|
|
|
visibility=Post.Visibilities.mentioned,
|
|
|
|
mentions=self.identity,
|
|
|
|
),
|
|
|
|
in_reply_to=self.post_obj.object_uri,
|
|
|
|
)
|
|
|
|
.distinct()
|
|
|
|
.order_by("published", "created"),
|
2022-11-16 02:30:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 06:31:45 +01:00
|
|
|
def serve_object(self):
|
|
|
|
# If this not a local post, redirect to its canonical URI
|
|
|
|
if not self.post_obj.local:
|
|
|
|
return redirect(self.post_obj.object_uri)
|
2022-11-27 22:43:46 +01:00
|
|
|
return JsonResponse(
|
|
|
|
canonicalise(self.post_obj.to_ap(), include_security=True),
|
|
|
|
content_type="application/activity+json",
|
|
|
|
)
|
2022-11-25 06:31:45 +01:00
|
|
|
|
2022-11-16 02:30:30 +01:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
2022-11-25 06:31:45 +01:00
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class Delete(TemplateView):
|
|
|
|
"""
|
|
|
|
Deletes a post
|
|
|
|
"""
|
|
|
|
|
|
|
|
template_name = "activities/post_delete.html"
|
|
|
|
|
|
|
|
def dispatch(self, request, handle, post_id):
|
2022-11-27 19:09:46 +01:00
|
|
|
# Make sure the request identity owns the post!
|
|
|
|
if handle != request.identity.handle:
|
|
|
|
raise PermissionDenied("Post author is not requestor")
|
2022-11-25 06:31:45 +01:00
|
|
|
self.identity = by_handle_or_404(self.request, handle, local=False)
|
|
|
|
self.post_obj = get_object_or_404(self.identity.posts, pk=post_id)
|
|
|
|
return super().dispatch(request)
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
return {"post": self.post_obj}
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
self.post_obj.transition_perform(PostStates.deleted)
|
|
|
|
return redirect("/")
|