FIX: Recover public actions (likes) when recovering a post (#6412)

This commit is contained in:
Penar Musaraj 2018-10-02 11:25:08 -04:00 committed by GitHub
parent 324b57c207
commit 34516c72bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 4 deletions

View File

@ -189,6 +189,7 @@ class Post < ActiveRecord::Base
def recover! def recover!
super super
update_flagged_posts_count update_flagged_posts_count
recover_public_post_actions
TopicLink.extract_from(self) TopicLink.extract_from(self)
QuotedPost.extract_from(self) QuotedPost.extract_from(self)
if topic && topic.category_id && topic.category if topic && topic.category_id && topic.category
@ -375,6 +376,19 @@ class Post < ActiveRecord::Base
PostAction.update_flagged_posts_count PostAction.update_flagged_posts_count
end end
def recover_public_post_actions
PostAction.publics
.with_deleted
.where(post_id: self.id, id: self.custom_fields["deleted_public_actions"])
.find_each do |post_action|
post_action.recover!
post_action.save!
end
self.custom_fields.delete("deleted_public_actions")
self.save_custom_fields
end
def filter_quotes(parent_post = nil) def filter_quotes(parent_post = nil)
return cooked if parent_post.blank? return cooked if parent_post.blank?

View File

@ -190,11 +190,15 @@ class PostDestroyer
end end
def trash_public_post_actions def trash_public_post_actions
public_post_actions = PostAction.publics.where(post_id: @post.id) if public_post_actions = PostAction.publics.where(post_id: @post.id)
public_post_actions.each { |pa| pa.trash!(@user) } public_post_actions.each { |pa| pa.trash!(@user) }
f = PostActionType.public_types.map { |k, _| ["#{k}_count", 0] } @post.custom_fields["deleted_public_actions"] = public_post_actions.ids
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten]) @post.save_custom_fields
f = PostActionType.public_types.map { |k, _| ["#{k}_count", 0] }
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
end
end end
def agree_with_flags def agree_with_flags

View File

@ -217,6 +217,38 @@ describe PostDestroyer do
end end
end end
describe "recovery and post actions" do
let(:codinghorror) { Fabricate(:coding_horror) }
let!(:like) { PostAction.act(codinghorror, post, PostActionType.types[:like]) }
let!(:another_like) { PostAction.act(moderator, post, PostActionType.types[:like]) }
it "restores public post actions" do
PostDestroyer.new(moderator, post).destroy
expect(PostAction.exists?(id: like.id)).to eq(false)
PostDestroyer.new(moderator, post).recover
expect(PostAction.exists?(id: like.id)).to eq(true)
end
it "does not recover previously-deleted actions" do
PostAction.remove_act(codinghorror, post, PostActionType.types[:like])
expect(PostAction.exists?(id: like.id)).to eq(false)
PostDestroyer.new(moderator, post).destroy
PostDestroyer.new(moderator, post).recover
expect(PostAction.exists?(id: another_like.id)).to eq(true)
expect(PostAction.exists?(id: like.id)).to eq(false)
end
it "updates post like count" do
PostDestroyer.new(moderator, post).destroy
PostDestroyer.new(moderator, post).recover
post.reload
expect(post.like_count).to eq(2)
expect(post.custom_fields["deleted_public_actions"]).to be_nil
end
end
describe 'basic destroying' do describe 'basic destroying' do
it "as the creator of the post, doesn't delete the post" do it "as the creator of the post, doesn't delete the post" do
begin begin
@ -622,6 +654,11 @@ describe PostDestroyer do
Topic.where(title: I18n.t('system_messages.flags_agreed_and_post_deleted.subject_template')).exists? Topic.where(title: I18n.t('system_messages.flags_agreed_and_post_deleted.subject_template')).exists?
).to eq(false) ).to eq(false)
end end
it "should set the deleted_public_actions custom field" do
PostDestroyer.new(moderator, second_post).destroy
expect(second_post.custom_fields["deleted_public_actions"]).to eq("#{bookmark.id}")
end
end end
describe "user actions" do describe "user actions" do