2022-11-27 10:09:46 -08:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2022-12-16 18:42:48 -08:00
|
|
|
from django.http import Http404, JsonResponse
|
2022-11-15 17:30:30 -08:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
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-12-01 17:46:49 -08:00
|
|
|
from django.views.generic import TemplateView, View
|
2022-11-15 17:30:30 -08:00
|
|
|
|
2022-12-26 09:42:02 -08:00
|
|
|
from activities.models import Post, PostInteraction, PostStates
|
2022-12-20 01:59:06 -08:00
|
|
|
from activities.services import PostService
|
2022-12-05 21:23:07 -08:00
|
|
|
from core.decorators import cache_page_by_ap_json
|
2022-11-24 21:31:45 -08:00
|
|
|
from core.ld import canonicalise
|
2022-11-15 17:30:30 -08:00
|
|
|
from users.decorators import identity_required
|
2022-12-26 09:42:02 -08:00
|
|
|
from users.models import Identity
|
2022-11-15 17:30:30 -08:00
|
|
|
from users.shortcuts import by_handle_or_404
|
|
|
|
|
|
|
|
|
2022-12-05 21:23:07 -08:00
|
|
|
@method_decorator(
|
|
|
|
cache_page_by_ap_json("cache_timeout_page_post", public_only=True), name="dispatch"
|
|
|
|
)
|
2022-12-10 11:16:08 -08:00
|
|
|
@method_decorator(vary_on_headers("Accept"), name="dispatch")
|
2022-11-16 05:53:39 -08:00
|
|
|
class Individual(TemplateView):
|
2022-11-15 17:30:30 -08:00
|
|
|
|
|
|
|
template_name = "activities/post.html"
|
|
|
|
|
2022-12-26 09:42:02 -08:00
|
|
|
identity: Identity
|
|
|
|
post_obj: Post
|
|
|
|
|
2022-11-24 21:31:45 -08:00
|
|
|
def get(self, request, handle, post_id):
|
|
|
|
self.identity = by_handle_or_404(self.request, handle, local=False)
|
2022-12-16 18:42:48 -08:00
|
|
|
if self.identity.blocked:
|
|
|
|
raise Http404("Blocked user")
|
2022-12-24 09:50:01 -08:00
|
|
|
self.post_obj = get_object_or_404(
|
2023-01-02 16:15:38 -08:00
|
|
|
PostService.queryset()
|
|
|
|
.filter(author=self.identity)
|
2023-01-02 16:42:38 -08:00
|
|
|
.visible_to(request.identity, include_replies=True),
|
2022-12-24 09:50:01 -08:00
|
|
|
pk=post_id,
|
|
|
|
)
|
2022-12-16 20:32:08 -08:00
|
|
|
if self.post_obj.state in [PostStates.deleted, PostStates.deleted_fanned_out]:
|
|
|
|
raise Http404("Deleted post")
|
2022-11-24 21:31:45 -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-24 21:31:45 -08:00
|
|
|
# Return post JSON
|
|
|
|
return self.serve_object()
|
|
|
|
else:
|
|
|
|
# Show normal page
|
|
|
|
return super().get(request)
|
|
|
|
|
2022-12-26 09:39:33 -08:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
2022-12-20 01:59:06 -08:00
|
|
|
ancestors, descendants = PostService(self.post_obj).context(
|
|
|
|
self.request.identity
|
|
|
|
)
|
2022-12-26 09:39:33 -08:00
|
|
|
|
|
|
|
context.update(
|
|
|
|
{
|
|
|
|
"identity": self.identity,
|
|
|
|
"post": self.post_obj,
|
|
|
|
"interactions": PostInteraction.get_post_interactions(
|
|
|
|
[self.post_obj] + ancestors + descendants,
|
|
|
|
self.request.identity,
|
|
|
|
),
|
|
|
|
"link_original": True,
|
|
|
|
"ancestors": ancestors,
|
|
|
|
"descendants": descendants,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return context
|
2022-11-15 17:30:30 -08:00
|
|
|
|
2022-11-24 21:31:45 -08: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 13:43:46 -08:00
|
|
|
return JsonResponse(
|
|
|
|
canonicalise(self.post_obj.to_ap(), include_security=True),
|
|
|
|
content_type="application/activity+json",
|
|
|
|
)
|
2022-11-24 21:31:45 -08:00
|
|
|
|
2022-11-15 17:30:30 -08: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-16 22:00:10 -08:00
|
|
|
post = get_object_or_404(
|
2023-01-02 16:42:38 -08:00
|
|
|
PostService.queryset()
|
|
|
|
.filter(author=identity)
|
|
|
|
.visible_to(request.identity, include_replies=True),
|
2022-12-24 09:50:01 -08:00
|
|
|
pk=post_id,
|
2022-11-16 22:00:10 -08:00
|
|
|
)
|
2022-12-20 01:59:06 -08:00
|
|
|
service = PostService(post)
|
2022-11-15 17:30:30 -08:00
|
|
|
if self.undo:
|
2022-12-24 09:50:01 -08:00
|
|
|
service.unlike_as(request.identity)
|
2022-11-15 17:30:30 -08:00
|
|
|
else:
|
2022-12-24 09:50:01 -08:00
|
|
|
service.like_as(request.identity)
|
2022-11-15 17:30:30 -08:00
|
|
|
# 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)
|
2022-12-24 09:50:01 -08:00
|
|
|
post = get_object_or_404(
|
2023-01-02 16:42:38 -08:00
|
|
|
PostService.queryset()
|
|
|
|
.filter(author=identity)
|
|
|
|
.visible_to(request.identity, include_replies=True),
|
2022-12-24 09:50:01 -08:00
|
|
|
pk=post_id,
|
|
|
|
)
|
2022-12-20 01:59:06 -08:00
|
|
|
service = PostService(post)
|
2022-11-15 17:30:30 -08:00
|
|
|
if self.undo:
|
2022-12-20 01:59:06 -08:00
|
|
|
service.unboost_as(request.identity)
|
2022-11-15 17:30:30 -08:00
|
|
|
else:
|
2022-12-20 01:59:06 -08:00
|
|
|
service.boost_as(request.identity)
|
2022-11-15 17:30:30 -08:00
|
|
|
# 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 05:53:39 -08:00
|
|
|
|
|
|
|
|
2023-03-11 10:17:20 -08:00
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class Bookmark(View):
|
|
|
|
"""
|
|
|
|
Adds/removes a bookmark 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(
|
|
|
|
PostService.queryset()
|
|
|
|
.filter(author=identity)
|
|
|
|
.visible_to(request.identity, include_replies=True),
|
|
|
|
pk=post_id,
|
|
|
|
)
|
|
|
|
if self.undo:
|
|
|
|
request.identity.bookmarks.filter(post=post).delete()
|
|
|
|
else:
|
|
|
|
request.identity.bookmarks.get_or_create(post=post)
|
|
|
|
# Return either a redirect or a HTMX snippet
|
|
|
|
if request.htmx:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"activities/_bookmark.html",
|
|
|
|
{
|
|
|
|
"post": post,
|
|
|
|
"bookmarks": set() if self.undo else {post.pk},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return redirect(post.urls.view)
|
|
|
|
|
|
|
|
|
2022-11-24 21:31:45 -08: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 10:09:46 -08:00
|
|
|
# Make sure the request identity owns the post!
|
|
|
|
if handle != request.identity.handle:
|
|
|
|
raise PermissionDenied("Post author is not requestor")
|
2022-11-24 21:31:45 -08: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):
|
2022-12-27 10:53:12 -08:00
|
|
|
PostService(self.post_obj).delete()
|
2022-11-24 21:31:45 -08:00
|
|
|
return redirect("/")
|