1
0

status_stat.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: status_stats
  5. #
  6. # id :bigint(8) not null, primary key
  7. # status_id :bigint(8) not null
  8. # replies_count :bigint(8) default(0), not null
  9. # reblogs_count :bigint(8) default(0), not null
  10. # favourites_count :bigint(8) default(0), not null
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. # untrusted_favourites_count :bigint(8)
  14. # untrusted_reblogs_count :bigint(8)
  15. #
  16. class StatusStat < ApplicationRecord
  17. belongs_to :status, inverse_of: :status_stat
  18. before_validation :clamp_untrusted_counts
  19. MAX_UNTRUSTED_COUNT = 100_000_000
  20. def replies_count
  21. [attributes['replies_count'], 0].max
  22. end
  23. def reblogs_count
  24. [attributes['reblogs_count'], 0].max
  25. end
  26. def favourites_count
  27. [attributes['favourites_count'], 0].max
  28. end
  29. private
  30. def clamp_untrusted_counts
  31. self.untrusted_favourites_count = untrusted_favourites_count.to_i.clamp(0, MAX_UNTRUSTED_COUNT) if untrusted_favourites_count.present?
  32. self.untrusted_reblogs_count = untrusted_reblogs_count.to_i.clamp(0, MAX_UNTRUSTED_COUNT) if untrusted_reblogs_count.present?
  33. end
  34. end