From 79002e1eaf96e9cfcd2993715b67cf3a61e02fb3 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 24 Nov 2022 22:31:45 -0700 Subject: [PATCH] Add initial delete UI --- activities/models/post.py | 1 + activities/views/posts.py | 56 ++++++++++++++++++++++++--- takahe/urls.py | 1 + templates/activities/_post.html | 2 +- templates/activities/post_delete.html | 14 +++++++ 5 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 templates/activities/post_delete.html diff --git a/activities/models/post.py b/activities/models/post.py index c8165d6..642842c 100644 --- a/activities/models/post.py +++ b/activities/models/post.py @@ -138,6 +138,7 @@ class Post(StatorModel): action_unlike = "{view}unlike/" action_boost = "{view}boost/" action_unboost = "{view}unboost/" + action_delete = "{view}delete/" action_reply = "/compose/?reply_to={self.id}" def get_scheme(self, url): diff --git a/activities/views/posts.py b/activities/views/posts.py index a53d401..268a3fe 100644 --- a/activities/views/posts.py +++ b/activities/views/posts.py @@ -1,4 +1,5 @@ from django import forms +from django.http import JsonResponse from django.shortcuts import get_object_or_404, redirect, render from django.utils.decorators import method_decorator from django.views.generic import FormView, TemplateView, View @@ -7,8 +8,10 @@ from activities.models import ( Post, PostInteraction, PostInteractionStates, + PostStates, TimelineEvent, ) +from core.ld import canonicalise from core.models import Config from users.decorators import identity_required from users.shortcuts import by_handle_or_404 @@ -18,18 +21,38 @@ class Individual(TemplateView): 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) + 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 + 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 post JSON + return self.serve_object() + else: + # Show normal page + return super().get(request) + + def get_context_data(self): return { - "identity": identity, - "post": post, + "identity": self.identity, + "post": self.post_obj, "interactions": PostInteraction.get_post_interactions( - [post], + [self.post_obj], self.request.identity, ), } + 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) + return JsonResponse(canonicalise(self.post_obj.to_ap(), include_security=True)) + @method_decorator(identity_required, name="dispatch") class Like(View): @@ -111,6 +134,27 @@ class Boost(View): return redirect(post.urls.view) +@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): + 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("/") + + @method_decorator(identity_required, name="dispatch") class Compose(FormView): diff --git a/takahe/urls.py b/takahe/urls.py index 614ec3b..ddffa68 100644 --- a/takahe/urls.py +++ b/takahe/urls.py @@ -94,6 +94,7 @@ urlpatterns = [ path("@/posts//unlike/", posts.Like.as_view(undo=True)), path("@/posts//boost/", posts.Boost.as_view()), path("@/posts//unboost/", posts.Boost.as_view(undo=True)), + path("@/posts//delete/", posts.Delete.as_view()), # Authentication path("auth/login/", auth.Login.as_view(), name="login"), path("auth/logout/", auth.Logout.as_view(), name="logout"), diff --git a/templates/activities/_post.html b/templates/activities/_post.html index e109e9c..06aa3f7 100644 --- a/templates/activities/_post.html +++ b/templates/activities/_post.html @@ -34,7 +34,7 @@ - + Delete diff --git a/templates/activities/post_delete.html b/templates/activities/post_delete.html new file mode 100644 index 0000000..1566399 --- /dev/null +++ b/templates/activities/post_delete.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block title %}Delete Post{% endblock %} + +{% block content %} +

Delete this post?

+
+ {% csrf_token %} + Cancel + +
+ + {% include "activities/_post.html" %} +{% endblock %}