diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb index 51ca626c3ae..f0b01fad498 100644 --- a/app/models/user_stat.rb +++ b/app/models/user_stat.rb @@ -52,6 +52,7 @@ class UserStat < ActiveRecord::Base ", seen_at: last_seen end + # topic_reply_count is a count of posts in other users' topics def update_topic_reply_count self.topic_reply_count = Topic diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 61595ab8f6e..0b53a9b6724 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -245,6 +245,16 @@ class PostDestroyer author.last_posted_at = author.posts.order('created_at DESC').first.try(:created_at) author.save! end + + if @post.is_first_post? && @post.topic && !@post.topic.private_message? + # Update stats of all people who replied + counts = Post.where(post_type: Post.types[:regular]).where(topic_id: @post.topic_id).group(:user_id).count + counts.each do |user_id, count| + if user_stat = UserStat.where(user_id: user_id).first + user_stat.update_attributes(post_count: user_stat.post_count - count) + end + end + end end end diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index 6b6ba44d89b..857f4821f10 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -207,6 +207,7 @@ describe PostDestroyer do DiscourseEvent.on(:topic_destroyed, &topic_destroyed) @orig = post2.cooked + # Guardian.new(post2.user).can_delete_post?(post2) == false PostDestroyer.new(post2.user, post2).destroy post2.reload @@ -239,6 +240,26 @@ describe PostDestroyer do end end + it "when topic is destroyed, it updates user_stats correctly" do + post + 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) + expect(user1.user_stat.topic_count).to eq(1) + expect(user1.user_stat.post_count).to eq(2) + 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 + expect(user1.user_stat.topic_count).to eq(0) + expect(user1.user_stat.post_count).to eq(0) + expect(user2.user_stat.topic_count).to eq(0) + expect(user2.user_stat.post_count).to eq(0) + end + it "accepts a delete_removed_posts_after option" do SiteSetting.delete_removed_posts_after = 0