FIX: when a topic is deleted, update the post count stats of all user who replied
This commit is contained in:
parent
51e74cb66e
commit
24af9b7d97
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue