2022-02-07 11:23:34 +08:00
|
|
|
# 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!)
|
2022-02-11 09:00:58 +08:00
|
|
|
return if !post&.topic
|
|
|
|
return if action == :increment! && post.topic.private_message?
|
|
|
|
stat = user_stat || post.user&.user_stat
|
|
|
|
|
|
|
|
return if stat.blank?
|
2022-02-07 11:23:34 +08:00
|
|
|
|
2022-02-09 21:48:18 +08:00
|
|
|
column =
|
|
|
|
if post.is_first_post?
|
|
|
|
:topic_count
|
|
|
|
elsif post.post_type == Post.types[:regular]
|
|
|
|
:post_count
|
|
|
|
end
|
|
|
|
|
|
|
|
return if column.blank?
|
|
|
|
|
2022-02-11 09:00:58 +08:00
|
|
|
# There are lingering bugs in the code base that does not properly increase the count when the status of the post
|
|
|
|
# changes. Since we have Job::DirectoryRefreshOlder which runs daily to reconcile the count, there is no need
|
|
|
|
# to trigger an error.
|
2022-02-09 21:48:18 +08:00
|
|
|
if action == :decrement! && stat.public_send(column) < 1
|
2022-02-11 09:00:58 +08:00
|
|
|
if SiteSetting.verbose_user_stat_count_logging
|
2022-02-11 11:51:51 +08:00
|
|
|
Rails.logger.warn("Attempted to insert negative count into UserStat##{column} for post with id '#{post.id}'")
|
2022-02-11 09:00:58 +08:00
|
|
|
end
|
|
|
|
|
2022-02-09 21:48:18 +08:00
|
|
|
return
|
2022-02-09 12:51:13 +08:00
|
|
|
end
|
2022-02-09 21:48:18 +08:00
|
|
|
|
|
|
|
stat.public_send(action, column)
|
2022-02-07 11:23:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|