mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 11:14:42 +08:00
FIX: defer flags (only) when handling a flag and deleting replies (#6702)
This commit is contained in:
@ -136,7 +136,7 @@ export default Post.extend({
|
|||||||
label: I18n.t("yes_value"),
|
label: I18n.t("yes_value"),
|
||||||
class: "btn-danger",
|
class: "btn-danger",
|
||||||
callback() {
|
callback() {
|
||||||
Post.deleteMany(replies.map(r => r.id))
|
Post.deleteMany(replies.map(r => r.id), { deferFlags: true })
|
||||||
.then(action)
|
.then(action)
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
@ -369,10 +369,10 @@ Post.reopenClass({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteMany(post_ids) {
|
deleteMany(post_ids, { deferFlags = false } = {}) {
|
||||||
return ajax("/posts/destroy_many", {
|
return ajax("/posts/destroy_many", {
|
||||||
type: "DELETE",
|
type: "DELETE",
|
||||||
data: { post_ids }
|
data: { post_ids, defer_flags: deferFlags }
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -336,6 +336,7 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
def destroy_many
|
def destroy_many
|
||||||
params.require(:post_ids)
|
params.require(:post_ids)
|
||||||
|
defer_flags = params[:defer_flags] || false
|
||||||
|
|
||||||
posts = Post.where(id: post_ids_including_replies)
|
posts = Post.where(id: post_ids_including_replies)
|
||||||
raise Discourse::InvalidParameters.new(:post_ids) if posts.blank?
|
raise Discourse::InvalidParameters.new(:post_ids) if posts.blank?
|
||||||
@ -344,7 +345,7 @@ class PostsController < ApplicationController
|
|||||||
posts.each { |p| guardian.ensure_can_delete!(p) }
|
posts.each { |p| guardian.ensure_can_delete!(p) }
|
||||||
|
|
||||||
Post.transaction do
|
Post.transaction do
|
||||||
posts.each { |p| PostDestroyer.new(current_user, p).destroy }
|
posts.each { |p| PostDestroyer.new(current_user, p, defer_flags: defer_flags).destroy }
|
||||||
end
|
end
|
||||||
|
|
||||||
render body: nil
|
render body: nil
|
||||||
|
@ -147,7 +147,11 @@ class PostDestroyer
|
|||||||
update_user_counts
|
update_user_counts
|
||||||
TopicUser.update_post_action_cache(post_id: @post.id)
|
TopicUser.update_post_action_cache(post_id: @post.id)
|
||||||
DB.after_commit do
|
DB.after_commit do
|
||||||
agree_with_flags
|
if @opts[:defer_flags].to_s == "true"
|
||||||
|
defer_flags
|
||||||
|
else
|
||||||
|
agree_with_flags
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -225,7 +229,7 @@ class PostDestroyer
|
|||||||
if @post.has_active_flag? && @user.id > 0 && @user.staff?
|
if @post.has_active_flag? && @user.id > 0 && @user.staff?
|
||||||
Jobs.enqueue(
|
Jobs.enqueue(
|
||||||
:send_system_message,
|
:send_system_message,
|
||||||
user_id: @post.user.id,
|
user_id: @post.user_id,
|
||||||
message_type: :flags_agreed_and_post_deleted,
|
message_type: :flags_agreed_and_post_deleted,
|
||||||
message_options: {
|
message_options: {
|
||||||
url: @post.url,
|
url: @post.url,
|
||||||
@ -241,6 +245,10 @@ class PostDestroyer
|
|||||||
PostAction.agree_flags!(@post, @user, delete_post: true)
|
PostAction.agree_flags!(@post, @user, delete_post: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def defer_flags
|
||||||
|
PostAction.defer_flags!(@post, @user, delete_post: true)
|
||||||
|
end
|
||||||
|
|
||||||
def trash_user_actions
|
def trash_user_actions
|
||||||
UserAction.where(target_post_id: @post.id).each do |ua|
|
UserAction.where(target_post_id: @post.id).each do |ua|
|
||||||
row = {
|
row = {
|
||||||
|
@ -632,7 +632,7 @@ describe PostDestroyer do
|
|||||||
let!(:flag) { PostAction.act(moderator, second_post, PostActionType.types[:off_topic]) }
|
let!(:flag) { PostAction.act(moderator, second_post, PostActionType.types[:off_topic]) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
SiteSetting.queue_jobs = false
|
Jobs::SendSystemMessage.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should delete public post actions and agree with flags" do
|
it "should delete public post actions and agree with flags" do
|
||||||
@ -650,36 +650,39 @@ describe PostDestroyer do
|
|||||||
expect(second_post.bookmark_count).to eq(0)
|
expect(second_post.bookmark_count).to eq(0)
|
||||||
expect(second_post.off_topic_count).to eq(1)
|
expect(second_post.off_topic_count).to eq(1)
|
||||||
|
|
||||||
notification = second_post.user.notifications.where(notification_type: Notification.types[:private_message]).last
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(1)
|
||||||
expect(notification).to be_present
|
|
||||||
expect(notification.topic.title).to eq(I18n.t('system_messages.flags_agreed_and_post_deleted.subject_template'))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not send the flags_agreed_and_post_deleted message if it was deleted by system" do
|
it "should not send the flags_agreed_and_post_deleted message if it was deleted by system" do
|
||||||
second_post.expects(:update_flagged_posts_count)
|
expect(PostAction.flagged_posts_count).to eq(1)
|
||||||
PostDestroyer.new(Discourse.system_user, second_post).destroy
|
PostDestroyer.new(Discourse.system_user, second_post).destroy
|
||||||
expect(
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
||||||
Topic.where(title: I18n.t('system_messages.flags_agreed_and_post_deleted.subject_template')).exists?
|
expect(PostAction.flagged_posts_count).to eq(0)
|
||||||
).to eq(false)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not send the flags_agreed_and_post_deleted message if it was deleted by author" do
|
it "should not send the flags_agreed_and_post_deleted message if it was deleted by author" do
|
||||||
SiteSetting.delete_removed_posts_after = 0
|
SiteSetting.delete_removed_posts_after = 0
|
||||||
second_post.expects(:update_flagged_posts_count)
|
expect(PostAction.flagged_posts_count).to eq(1)
|
||||||
PostDestroyer.new(second_post.user, second_post).destroy
|
PostDestroyer.new(second_post.user, second_post).destroy
|
||||||
expect(
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
||||||
Topic.where(title: I18n.t('system_messages.flags_agreed_and_post_deleted.subject_template')).exists?
|
expect(PostAction.flagged_posts_count).to eq(0)
|
||||||
).to eq(false)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not send the flags_agreed_and_post_deleted message if flags were deferred" do
|
it "should not send the flags_agreed_and_post_deleted message if flags were deferred" do
|
||||||
second_post.expects(:update_flagged_posts_count)
|
expect(PostAction.flagged_posts_count).to eq(1)
|
||||||
PostAction.defer_flags!(second_post, moderator)
|
PostAction.defer_flags!(second_post, moderator)
|
||||||
second_post.reload
|
second_post.reload
|
||||||
|
expect(PostAction.flagged_posts_count).to eq(0)
|
||||||
|
|
||||||
PostDestroyer.new(moderator, second_post).destroy
|
PostDestroyer.new(moderator, second_post).destroy
|
||||||
expect(
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
||||||
Topic.where(title: I18n.t('system_messages.flags_agreed_and_post_deleted.subject_template')).exists?
|
end
|
||||||
).to eq(false)
|
|
||||||
|
it "should not send the flags_agreed_and_post_deleted message if defer_flags is true" do
|
||||||
|
expect(PostAction.flagged_posts_count).to eq(1)
|
||||||
|
PostDestroyer.new(moderator, second_post, defer_flags: true).destroy
|
||||||
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
||||||
|
expect(PostAction.flagged_posts_count).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should set the deleted_public_actions custom field" do
|
it "should set the deleted_public_actions custom field" do
|
||||||
|
@ -243,6 +243,24 @@ describe PostsController do
|
|||||||
delete "/posts/destroy_many.json", params: { post_ids: [post1.id], reply_post_ids: [post1.id] }
|
delete "/posts/destroy_many.json", params: { post_ids: [post1.id], reply_post_ids: [post1.id] }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "deleting flagged posts" do
|
||||||
|
let(:moderator) { Fabricate(:moderator) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
PostAction.act(moderator, post1, PostActionType.types[:off_topic])
|
||||||
|
PostAction.act(moderator, post2, PostActionType.types[:off_topic])
|
||||||
|
Jobs::SendSystemMessage.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
it "defers the posts" do
|
||||||
|
sign_in(moderator)
|
||||||
|
expect(PostAction.flagged_posts_count).to eq(2)
|
||||||
|
delete "/posts/destroy_many.json", params: { post_ids: [post1.id, post2.id], defer_flags: true }
|
||||||
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
||||||
|
expect(PostAction.flagged_posts_count).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user