mirror of
https://github.com/discourse/discourse.git
synced 2025-02-09 13:44:16 +08:00
![Alan Guo Xiang Tan](/assets/img/avatar_default.png)
Ensures that `UserStat#post_count` and `UserStat#topic_count` does not go below 0. When it does like it did now, we tend to have bugs in our code since we're usually coding with the assumption that the count isn't negative. In order to support the constraints, our post and topic fabricators in tests will now automatically increment the count for the respective user's `UserStat` as well. We have to do this because our fabricators bypasss `PostCreator` which holds the responsibility of updating `UserStat#post_count` and `UserStat#topic_count`.
28 lines
671 B
Ruby
28 lines
671 B
Ruby
# frozen_string_literal: true
|
|
|
|
class UserStatCountUpdater
|
|
class << self
|
|
def increment!(post, user_stat: nil)
|
|
update!(post, user_stat: user_stat)
|
|
end
|
|
|
|
def decrement!(post, user_stat: nil)
|
|
update!(post, user_stat: user_stat, action: :decrement!)
|
|
end
|
|
|
|
private
|
|
|
|
def update!(post, user_stat: nil, action: :increment!)
|
|
return if !post.topic
|
|
return if post.topic.private_message?
|
|
stat = user_stat || post.user.user_stat
|
|
|
|
if post.is_first_post?
|
|
stat.public_send(action, :topic_count)
|
|
elsif post.post_type == Post.types[:regular]
|
|
stat.public_send(action, :post_count)
|
|
end
|
|
end
|
|
end
|
|
end
|