You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. class TrendingTags
  3. EXPIRE_HISTORY_AFTER = 7.days.seconds
  4. class << self
  5. def record_use!(tag, account, at_time = Time.now.utc)
  6. return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot?
  7. increment_historical_use!(tag.id, at_time)
  8. increment_unique_use!(tag.id, account.id, at_time)
  9. end
  10. private
  11. def increment_historical_use!(tag_id, at_time)
  12. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}"
  13. redis.incrby(key, 1)
  14. redis.expire(key, EXPIRE_HISTORY_AFTER)
  15. end
  16. def increment_unique_use!(tag_id, account_id, at_time)
  17. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts"
  18. redis.pfadd(key, account_id)
  19. redis.expire(key, EXPIRE_HISTORY_AFTER)
  20. end
  21. def disallowed_hashtags
  22. return @disallowed_hashtags if defined?(@disallowed_hashtags)
  23. @disallowed_hashtags = Setting.disallowed_hashtags.nil? ? [] : Setting.disallowed_hashtags
  24. @disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String
  25. @disallowed_hashtags = @disallowed_hashtags.map(&:downcase)
  26. end
  27. def redis
  28. Redis.current
  29. end
  30. end
  31. end