From b1082924b9a0a1e706e53d9f6689fe03e5dbad32 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Fri, 13 Jul 2018 21:45:59 +0530 Subject: [PATCH] FIX: do not validate topic deletions --- lib/post_destroyer.rb | 12 ++++++------ spec/components/post_destroyer_spec.rb | 15 ++++++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 790d8132769..2493ba32407 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -168,12 +168,12 @@ class PostDestroyer def make_previous_post_the_last_one last_post = Post.where("topic_id = ? and id <> ?", @post.topic_id, @post.id).order('created_at desc').limit(1).first - if last_post.present? - @post.topic.update!( - last_posted_at: last_post.created_at, - last_post_user_id: last_post.user_id, - highest_post_number: last_post.post_number - ) + if last_post.present? && @post.topic.present? + topic = @post.topic + topic.last_posted_at = last_post.created_at + topic.last_post_user_id = last_post.user_id + topic.highest_post_number = last_post.post_number + topic.save!(validate: false) end end diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index e46c364ac2b..80c038777a2 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -265,9 +265,10 @@ describe PostDestroyer do end it "when topic is destroyed, it updates user_stats correctly" do - post + SiteSetting.min_topic_title_length = 5 + post.topic.update_column(:title, "xyz") + user1 = post.user - user1.reload user2 = Fabricate(:user) reply = create_post(topic_id: post.topic_id, user: user2) reply2 = create_post(topic_id: post.topic_id, user: user1) @@ -275,6 +276,7 @@ describe PostDestroyer do expect(user1.user_stat.post_count).to eq(1) expect(user2.user_stat.topic_count).to eq(0) expect(user2.user_stat.post_count).to eq(1) + PostDestroyer.new(Fabricate(:admin), post).destroy user1.reload user2.reload @@ -390,12 +392,13 @@ describe PostDestroyer do let(:user) { Fabricate(:user) } let!(:post) { create_post(user: user) } - let(:topic) { post.topic.reload } + let(:topic) { post.topic } let(:second_user) { Fabricate(:coding_horror) } let!(:second_post) { create_post(topic: topic, user: second_user) } before do PostDestroyer.new(moderator, second_post).destroy + topic.reload end it 'resets the last_poster_id back to the OP' do @@ -406,6 +409,10 @@ describe PostDestroyer do expect(topic.last_posted_at.to_i).to eq(post.created_at.to_i) end + it 'resets the highest_post_number' do + expect(topic.highest_post_number).to eq(post.post_number) + end + context 'topic_user' do let(:topic_user) { second_user.topic_users.find_by(topic_id: topic.id) } @@ -421,9 +428,7 @@ describe PostDestroyer do it "sets the second user's last_read_post_number back to 1" do expect(topic_user.highest_seen_post_number).to eq(1) end - end - end context "deleting a post belonging to a deleted topic" do