A platform for community discussion. Free, open, simple.
FIX: Recover public actions (likes) when recovering a post (#6412)
Penar Musaraj34516c72bd29
diff --git a/app/models/post.rb b/app/models/post.rbindex 51d83227276..5e220246135 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -189,6 +189,7 @@ class Post < ActiveRecord::Basedef recover!superupdate_flagged_posts_count+ recover_public_post_actionsTopicLink.extract_from(self)QuotedPost.extract_from(self)if topic && topic.category_id && topic.category@@ -375,6 +376,19 @@ class Post < ActiveRecord::BasePostAction.update_flagged_posts_countend+ 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)return cooked if parent_post.blank?diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rbindex 94146ffb323..bd932b46aa7 100644--- a/lib/post_destroyer.rb+++ b/lib/post_destroyer.rb@@ -190,11 +190,15 @@ class PostDestroyerenddef trash_public_post_actions- public_post_actions = PostAction.publics.where(post_id: @post.id)- public_post_actions.each { |pa| pa.trash!(@user) }+ if public_post_actions = PostAction.publics.where(post_id: @post.id)+ public_post_actions.each { |pa| pa.trash!(@user) }- f = PostActionType.public_types.map { |k, _| ["#{k}_count", 0] }- Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])+ @post.custom_fields["deleted_public_actions"] = public_post_actions.ids+ @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])+ endenddef agree_with_flagsdiff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rbindex ddd00be3079..96603a4f3bf 100644--- a/spec/components/post_destroyer_spec.rb+++ b/spec/components/post_destroyer_spec.rb@@ -217,6 +217,38 @@ describe PostDestroyer doendend+ 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' doit "as the creator of the post, doesn't delete the post" dobegin@@ -622,6 +654,11 @@ describe PostDestroyer doTopic.where(title: I18n.t('system_messages.flags_agreed_and_post_deleted.subject_template')).exists?).to eq(false)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}")+ endenddescribe "user actions" do