REFACTOR: Allow `delete_removed_posts_after` SiteSetting to be bypassed.

This commit is contained in:
Guo Xiang Tan 2017-06-16 12:27:51 +09:00
parent d82dbd565b
commit a6450f7377
2 changed files with 22 additions and 4 deletions

View File

@ -44,10 +44,12 @@ class PostDestroyer
end
def destroy
if @user.staff? || SiteSetting.delete_removed_posts_after < 1
delete_removed_posts_after = @opts[:delete_removed_posts_after] || SiteSetting.delete_removed_posts_after
if @user.staff? || delete_removed_posts_after < 1
perform_delete
elsif @user.id == @post.user_id
mark_for_deletion
mark_for_deletion(delete_removed_posts_after)
end
DiscourseEvent.trigger(:post_destroyed, @post, @opts, @user)
@ -115,11 +117,14 @@ class PostDestroyer
end
# When a user 'deletes' their own post. We just change the text.
def mark_for_deletion
def mark_for_deletion(delete_removed_posts_after = SiteSetting.delete_removed_posts_after)
I18n.with_locale(SiteSetting.default_locale) do
# don't call revise from within transaction, high risk of deadlock
@post.revise(@user, { raw: I18n.t('js.post.deleted_by_author', count: SiteSetting.delete_removed_posts_after) }, force_new_version: true)
@post.revise(@user,
{ raw: I18n.t('js.post.deleted_by_author', count: delete_removed_posts_after) },
force_new_version: true
)
Post.transaction do
@post.update_column(:user_deleted, true)

View File

@ -239,6 +239,19 @@ describe PostDestroyer do
end
end
it "accepts a delete_removed_posts_after option" do
SiteSetting.delete_removed_posts_after = 0
PostDestroyer.new(post.user, post, delete_removed_posts_after: 1).destroy
post.reload
expect(post.deleted_at).to eq(nil)
expect(post.user_deleted).to eq(true)
expect(post.raw).to eq(I18n.t('js.post.deleted_by_author', count: 1))
end
context "as a moderator" do
it "deletes the post" do
author = post.user