Quick unflag options for posts

This also loosens the restrictions on what can be flagged and how
frequently. Previously pending posts could not be flagged, which
was not in line with our current flagging system purpose.
This commit is contained in:
Kira 2019-08-01 16:03:13 -07:00
parent 8aebb2d567
commit ccbbe39338
7 changed files with 56 additions and 6 deletions

View File

@ -22,10 +22,21 @@ class PostFlagsController < ApplicationController
respond_with(@post_flag)
end
def destroy
@post = Post.find(params[:post_id])
@post.unflag!
if params[:approval] == 'unapprove'
@post.unapprove!
elsif params[:approval] == 'approve'
@post.approve!
end
respond_with(nil)
end
def show
@post_flag = PostFlag.find(params[:id])
respond_with(@post_flag) do |fmt|
fmt.html { redirect_to post_flags_path(search: { id: @post_flag.id }) }
fmt.html {redirect_to post_flags_path(search: {id: @post_flag.id})}
end
end

View File

@ -584,6 +584,21 @@ Post.delete_with_reason = function(post_id, reason) {
});
}
Post.unflag = function(post_id, approval) {
let modApproval = approval || 'none';
$.ajax({
type: "DELETE",
url: `/posts/${post_id}/flag.json`,
data: {approval: modApproval}
}).fail(function(data) {
var message = $.map(data.responseJSON.errors, function(msg, attr) { return msg; }).join('; ');
$(window).trigger('danbooru:error', "Error: " + message);
}).done(function(data) {
$(window).trigger("danbooru:notice", "Unflagged post");
location.reload();
});
}
Post.unapprove = function(post_id) {
$.ajax({
type: "DELETE",

View File

@ -311,6 +311,11 @@ class Post < ApplicationRecord
end
end
def unflag!
flags.each(&:resolve!)
update(is_flagged: false)
end
def appeal!(reason)
if is_status_locked?
raise PostAppeal::Error.new("Post is locked and cannot be appealed")
@ -1381,6 +1386,15 @@ class Post < ApplicationRecord
return false
end
if reason.blank?
last_flag = flags.unresolved.order(id: :desc).first
if last_flag.blank?
self.errors.add(:base, "Cannot flag with blank reason when no active flag exists.")
return false
end
reason = last_flag.reason
end
Post.transaction do
flag!(reason, is_deletion: true)

View File

@ -15,7 +15,7 @@ class PostFlag < ApplicationRecord
validates_presence_of :reason
validate :validate_creator_is_not_limited, on: :create
validate :validate_post
validates_uniqueness_of :creator_id, :scope => :post_id, :on => :create, :unless => :is_deletion, :message => "have already flagged this post"
validates_uniqueness_of :creator_id, :scope => :post_id, :on => :create, :unless => :bypass_unique, :message => "have already flagged this post"
before_save :update_post
scope :by_users, -> { where.not(creator: User.system) }
@ -138,6 +138,10 @@ class PostFlag < ApplicationRecord
post.update_column(:is_flagged, true) unless post.is_flagged?
end
def bypass_unique
is_deletion || creator.is_janitor?
end
def validate_creator_is_not_limited
return if is_deletion
@ -145,6 +149,8 @@ class PostFlag < ApplicationRecord
errors[:creator] << "cannot flag posts"
end
return if creator.is_janitor?
if creator_id != User.system.id && PostFlag.for_creator(creator_id).where("created_at > ?", 30.days.ago).count >= CREATION_THRESHOLD
report = Reports::PostFlags.new(user_id: post.uploader_id, date_range: 90.days.ago)
@ -171,7 +177,6 @@ class PostFlag < ApplicationRecord
end
def validate_post
errors[:post] << "is pending and cannot be flagged" if post.is_pending? && !is_deletion
errors[:post] << "is locked and cannot be flagged" if post.is_status_locked?
errors[:post] << "is deleted" if post.is_deleted?
end

View File

@ -6,7 +6,11 @@
<% if CurrentUser.can_approve_posts? %>
<div>
<!-- unflag options for flagged posts -->
<%= link_to "Delete", confirm_delete_moderator_post_post_path(post) %> |
<%= link_to_function "Unflag", "Danbooru.Post.unflag(#{post.id}, 'none')" %> |
<%= link_to_function "Unflag+unapprove", "Danbooru.Post.unflag(#{post.id}, 'unapprove')" %> |
<%= link_to_function "Unflag+approve", "Danbooru.Post.unflag(#{post.id}, 'approve')" %> |
<%= link_to_function "Delete with given reason", "Danbooru.Post.delete_with_reason(#{post.id}, null)" %>
</div>
<% end %>
</div>

View File

@ -19,7 +19,7 @@
<li><span id="status-locked-notice">Status locked</span></li>
<% else %>
<% unless CurrentUser.no_flagging? %>
<% if !post.is_deleted? && !post.is_pending? && !post.is_flagged? %>
<% if !post.is_deleted? && !post.is_flagged? %>
<li><%= link_to "Flag", new_post_flag_path(post_flag: {post_id: post.id}), id: "flag", remote: true %></li>
<% elsif post.is_flagged? || post.is_deleted? %>
<li><%= link_to "Appeal", new_post_appeal_path(post_appeal: {post_id: post.id}), id: "appeal", remote: true %></li>

View File

@ -242,6 +242,7 @@ Rails.application.routes.draw do
resources :events, :only => [:index], :controller => "post_events"
resources :replacements, :only => [:index, :new, :create], :controller => "post_replacements"
resource :votes, :controller => "post_votes", :only => [:create, :destroy]
resource :flag, controller: 'post_flags', only: [:destroy]
collection do
get :random
end
@ -260,7 +261,7 @@ Rails.application.routes.draw do
end
end
resources :post_appeals
resources :post_flags
resources :post_flags, except: [:destroy]
resources :post_approvals, only: [:index]
resources :post_versions, :only => [:index, :search] do
member do