mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 13:06:56 +08:00
FIX: user topic and post counts can become negative when staff deletes posts in personal messages
This commit is contained in:
@ -78,21 +78,23 @@ class PostDestroyer
|
|||||||
def staff_recovered
|
def staff_recovered
|
||||||
@post.recover!
|
@post.recover!
|
||||||
|
|
||||||
if author = @post.user
|
if @post.topic && !@post.topic.private_message?
|
||||||
if @post.is_first_post?
|
if author = @post.user
|
||||||
author.user_stat.topic_count += 1
|
if @post.is_first_post?
|
||||||
else
|
author.user_stat.topic_count += 1
|
||||||
author.user_stat.post_count += 1
|
else
|
||||||
|
author.user_stat.post_count += 1
|
||||||
|
end
|
||||||
|
author.user_stat.save!
|
||||||
end
|
end
|
||||||
author.user_stat.save!
|
|
||||||
end
|
|
||||||
|
|
||||||
if @post.is_first_post? && @post.topic && !@post.topic.private_message?
|
if @post.is_first_post?
|
||||||
# Update stats of all people who replied
|
# Update stats of all people who replied
|
||||||
counts = Post.where(post_type: Post.types[:regular], topic_id: @post.topic_id).where('post_number > 1').group(:user_id).count
|
counts = Post.where(post_type: Post.types[:regular], topic_id: @post.topic_id).where('post_number > 1').group(:user_id).count
|
||||||
counts.each do |user_id, count|
|
counts.each do |user_id, count|
|
||||||
if user_stat = UserStat.where(user_id: user_id).first
|
if user_stat = UserStat.where(user_id: user_id).first
|
||||||
user_stat.update_attributes(post_count: user_stat.post_count + count)
|
user_stat.update_attributes(post_count: user_stat.post_count + count)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -248,10 +250,12 @@ class PostDestroyer
|
|||||||
author.user_stat.first_post_created_at = author.posts.order('created_at ASC').first.try(:created_at)
|
author.user_stat.first_post_created_at = author.posts.order('created_at ASC').first.try(:created_at)
|
||||||
end
|
end
|
||||||
|
|
||||||
if @post.post_type == Post.types[:regular] && !@post.is_first_post? && !@topic.nil?
|
if @post.topic && !@post.topic.private_message?
|
||||||
author.user_stat.post_count -= 1
|
if @post.post_type == Post.types[:regular] && !@post.is_first_post? && !@topic.nil?
|
||||||
|
author.user_stat.post_count -= 1
|
||||||
|
end
|
||||||
|
author.user_stat.topic_count -= 1 if @post.is_first_post?
|
||||||
end
|
end
|
||||||
author.user_stat.topic_count -= 1 if @post.is_first_post?
|
|
||||||
|
|
||||||
# We don't count replies to your own topics in topic_reply_count
|
# We don't count replies to your own topics in topic_reply_count
|
||||||
if @topic && author.id != @topic.user_id
|
if @topic && author.id != @topic.user_id
|
||||||
|
@ -360,6 +360,32 @@ describe PostDestroyer do
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'private message' do
|
||||||
|
let(:author) { Fabricate(:user) }
|
||||||
|
let(:private_message) { Fabricate(:private_message_topic, user: author) }
|
||||||
|
let!(:first_post) { Fabricate(:post, topic: private_message, user: author) }
|
||||||
|
let!(:second_post) { Fabricate(:post, topic: private_message, user: author, post_number: 2)}
|
||||||
|
|
||||||
|
it "doesn't update post_count for a reply" do
|
||||||
|
expect {
|
||||||
|
PostDestroyer.new(admin, second_post).destroy
|
||||||
|
author.reload
|
||||||
|
}.to_not change { author.post_count }
|
||||||
|
|
||||||
|
expect {
|
||||||
|
PostDestroyer.new(admin, second_post).recover
|
||||||
|
}.to_not change { author.post_count }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't update topic_count for first post" do
|
||||||
|
expect {
|
||||||
|
PostDestroyer.new(admin, first_post).destroy
|
||||||
|
author.reload
|
||||||
|
}.to_not change { author.topic_count }
|
||||||
|
expect(author.post_count).to eq(0) # also unchanged
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'deleting the second post in a topic' do
|
context 'deleting the second post in a topic' do
|
||||||
|
|
||||||
let(:user) { Fabricate(:user) }
|
let(:user) { Fabricate(:user) }
|
||||||
|
@ -56,8 +56,10 @@ describe TopicConverter do
|
|||||||
first_post
|
first_post
|
||||||
topic_user = TopicUser.create!(user_id: author.id, topic_id: private_message.id, posted: true)
|
topic_user = TopicUser.create!(user_id: author.id, topic_id: private_message.id, posted: true)
|
||||||
expect(private_message.user.user_stat.topic_count).to eq(0)
|
expect(private_message.user.user_stat.topic_count).to eq(0)
|
||||||
|
expect(private_message.user.user_stat.post_count).to eq(0)
|
||||||
private_message.convert_to_public_topic(admin)
|
private_message.convert_to_public_topic(admin)
|
||||||
expect(private_message.reload.user.user_stat.topic_count).to eq(1)
|
expect(private_message.reload.user.user_stat.topic_count).to eq(1)
|
||||||
|
expect(private_message.user.user_stat.post_count).to eq(1)
|
||||||
expect(topic_user.reload.notification_level).to eq(TopicUser.notification_levels[:watching])
|
expect(topic_user.reload.notification_level).to eq(TopicUser.notification_levels[:watching])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user