posts: rework post deletion to use dialog box.

Rework post deletion from using a separate page to using a dialog box,
like flagging.

* Add `DELETE /posts/:id` endpoint.
* Remove `POST /moderator/post/posts/:id/delete` endpoint.
This commit is contained in:
evazion
2020-08-03 19:28:10 -05:00
parent f1b0e31923
commit bca1f122d0
10 changed files with 69 additions and 62 deletions

View File

@@ -4,18 +4,6 @@ module Moderator
skip_before_action :api_check
respond_to :html, :json, :xml, :js
def confirm_delete
@post = ::Post.find(params[:id])
end
def delete
@post = authorize ::Post.find(params[:id])
if params[:commit] == "Delete"
@post.delete!(params[:reason], :move_favorites => params[:move_favorites].present?)
end
redirect_to(post_path(@post))
end
def confirm_move_favorites
@post = ::Post.find(params[:id])
end

View File

@@ -56,6 +56,18 @@ class PostsController < ApplicationController
respond_with_post_after_update(@post)
end
def destroy
@post = authorize Post.find(params[:id])
if params[:commit] == "Delete"
move_favorites = params.dig(:post, :move_favorites).to_s.truthy?
@post.delete!(params.dig(:post, :reason), move_favorites: move_favorites)
flash[:notice] = "Post deleted"
end
respond_with_post_after_update(@post)
end
def revert
@post = authorize Post.find(params[:id])
@version = @post.versions.find(params[:version_id])

View File

@@ -31,6 +31,10 @@ class PostPolicy < ApplicationPolicy
user.is_approver? && !record.is_deleted?
end
def destroy?
delete?
end
def ban?
user.is_approver? && !record.is_banned?
end

View File

@@ -1,26 +0,0 @@
<h1>Delete Post</h1>
<div>
<%= PostPresenter.preview(@post, show_deleted: true) %>
</div>
<%= form_tag(delete_moderator_post_post_path, :style => "clear: both;", :class => "simple_form") do %>
<% if @post.parent_id %>
<div class="input">
<label for="move_favorites">
<%= check_box_tag "move_favorites" %>
Move favorites to parent?
</label>
</div>
<% end %>
<p style="font-weight: bold;">Note: If the reason you are planning to delete this post is because it is from a banned artist, please <%= link_to "ban", confirm_ban_moderator_post_post_path(@post) %> this post instead of deleting it.</p>
<div class="input">
<label for="reason">Reason</label>
<%= text_area_tag "reason" %>
</div>
<%= submit_tag "Delete" %>
<%= submit_tag "Cancel" %>
<% end %>

View File

@@ -0,0 +1,5 @@
<% if params[:commit] == "Delete" %>
location.reload();
<% else %>
Danbooru.Utility.dialog("Delete Post", "<%= j render "posts/partials/show/delete_dialog", post: @post %>");
<% end %>

View File

@@ -0,0 +1,9 @@
<div class="delete-post-dialog-body">
<%= edit_form_for(post, method: :delete, remote: true) do |f| %>
<input type="hidden" name="commit" value="Delete">
<%= f.input :reason, as: :dtext, inline: true, input_html: { value: "" } %>
<% if post.parent_id.present? %>
<%= f.input :move_favorites, label: "Move favorites to parent", as: :boolean, input_html: { checked: false } %>
<% end %>
<% end %>
</div>

View File

@@ -64,7 +64,7 @@
<li id="post-option-move-favorites"><%= link_to "Move favorites", confirm_move_favorites_moderator_post_post_path(post_id: post.id) %></li>
<% end %>
<% elsif policy(post).delete? %>
<li id="post-option-delete"><%= link_to "Delete", confirm_delete_moderator_post_post_path(post_id: post.id) %></li>
<li id="post-option-delete"><%= link_to "Delete", post, method: :delete, remote: true %></li>
<% end %>
<% if post.is_approvable? && !post.is_deleted? %>

View File

@@ -13,9 +13,7 @@ Rails.application.routes.draw do
namespace :post do
resources :posts, :only => [:delete, :expunge, :confirm_delete] do
member do
get :confirm_delete
post :expunge
post :delete
get :confirm_move_favorites
post :move_favorites
get :confirm_ban
@@ -175,7 +173,7 @@ Rails.application.routes.draw do
end
resources :post_replacements, :only => [:index, :new, :create, :update]
resources :post_votes, only: [:index]
resources :posts, only: [:index, :show, :update] do
resources :posts, only: [:index, :show, :update, :destroy] do
resources :events, :only => [:index], :controller => "post_events"
resources :replacements, :only => [:index, :new, :create], :controller => "post_replacements"
resource :artist_commentary, :only => [:index, :show] do

View File

@@ -15,26 +15,6 @@ module Moderator
end
end
context "confirm_delete action" do
should "render" do
get_auth confirm_delete_moderator_post_post_path(@post), @admin
assert_response :success
end
end
context "delete action" do
should "render" do
post_auth delete_moderator_post_post_path(@post), @admin, params: {:reason => "xxx", :format => "js", :commit => "Delete"}
assert(@post.reload.is_deleted?)
end
should "work even if the deleter has flagged the post previously" do
create(:post_flag, post: @post, creator: @admin)
post_auth delete_moderator_post_post_path(@post), @admin, params: {:reason => "xxx", :format => "js", :commit => "Delete"}
assert(@post.reload.is_deleted?)
end
end
context "confirm_move_favorites action" do
should "render" do
get_auth confirm_move_favorites_moderator_post_post_path(@post), @admin

View File

@@ -633,6 +633,43 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
end
end
context "destroy action" do
setup do
@approver = create(:approver)
end
should "delete the post" do
delete_auth post_path(@post), @approver, params: { commit: "Delete", post: { reason: "test" } }
assert_redirected_to @post
assert_equal(true, @post.reload.is_deleted?)
assert_equal("test", @post.flags.last.reason)
end
should "delete the post even if the deleter has flagged the post previously" do
create(:post_flag, post: @post, creator: @approver)
delete_auth post_path(@post), @approver, params: { commit: "Delete", post: { reason: "test" } }
assert_redirected_to @post
assert_equal(true, @post.reload.is_deleted?)
end
should "not delete the post if the user is unauthorized" do
delete_auth post_path(@post), @user, params: { commit: "Delete" }
assert_response 403
assert_equal(false, @post.is_deleted?)
end
should "render the delete post dialog for an xhr request" do
delete_auth post_path(@post), @approver, xhr: true
assert_response :success
assert_equal(false, @post.is_deleted?)
end
end
context "revert action" do
setup do
PostVersion.sqs_service.stubs(:merge?).returns(false)