2018-05-27 15:45:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TrendingTags
|
2018-07-16 13:21:27 -04:00
|
|
|
KEY = 'trending_tags'
|
2018-05-27 15:45:30 -04:00
|
|
|
EXPIRE_HISTORY_AFTER = 7.days.seconds
|
2018-07-16 13:21:27 -04:00
|
|
|
EXPIRE_TRENDS_AFTER = 1.day.seconds
|
|
|
|
THRESHOLD = 5
|
2018-05-27 15:45:30 -04:00
|
|
|
|
|
|
|
class << self
|
2019-02-02 13:11:38 -05:00
|
|
|
include Redisable
|
|
|
|
|
2018-05-27 15:45:30 -04:00
|
|
|
def record_use!(tag, account, at_time = Time.now.utc)
|
2018-06-01 13:19:44 -04:00
|
|
|
return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot?
|
2018-05-27 15:45:30 -04:00
|
|
|
|
|
|
|
increment_historical_use!(tag.id, at_time)
|
|
|
|
increment_unique_use!(tag.id, account.id, at_time)
|
2018-07-16 13:21:27 -04:00
|
|
|
increment_vote!(tag.id, at_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(limit)
|
|
|
|
key = "#{KEY}:#{Time.now.utc.beginning_of_day.to_i}"
|
2018-07-16 13:54:14 -04:00
|
|
|
tag_ids = redis.zrevrange(key, 0, limit - 1).map(&:to_i)
|
2018-11-16 09:02:18 -05:00
|
|
|
tags = Tag.where(id: tag_ids).to_a.each_with_object({}) { |tag, h| h[tag.id] = tag }
|
2018-07-16 13:21:27 -04:00
|
|
|
tag_ids.map { |tag_id| tags[tag_id] }.compact
|
2018-05-27 15:45:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def increment_historical_use!(tag_id, at_time)
|
|
|
|
key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}"
|
|
|
|
redis.incrby(key, 1)
|
|
|
|
redis.expire(key, EXPIRE_HISTORY_AFTER)
|
|
|
|
end
|
|
|
|
|
|
|
|
def increment_unique_use!(tag_id, account_id, at_time)
|
|
|
|
key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts"
|
|
|
|
redis.pfadd(key, account_id)
|
|
|
|
redis.expire(key, EXPIRE_HISTORY_AFTER)
|
|
|
|
end
|
|
|
|
|
2018-07-16 13:21:27 -04:00
|
|
|
def increment_vote!(tag_id, at_time)
|
|
|
|
key = "#{KEY}:#{at_time.beginning_of_day.to_i}"
|
|
|
|
expected = redis.pfcount("activity:tags:#{tag_id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts").to_f
|
|
|
|
expected = 1.0 if expected.zero?
|
|
|
|
observed = redis.pfcount("activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts").to_f
|
|
|
|
|
|
|
|
if expected > observed || observed < THRESHOLD
|
|
|
|
redis.zrem(key, tag_id.to_s)
|
|
|
|
else
|
|
|
|
score = ((observed - expected)**2) / expected
|
2019-07-30 07:10:40 -04:00
|
|
|
added = redis.zadd(key, score, tag_id.to_s)
|
2019-08-01 13:57:09 -04:00
|
|
|
bump_tag_score!(tag_id) if added
|
2018-07-16 13:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
redis.expire(key, EXPIRE_TRENDS_AFTER)
|
|
|
|
end
|
|
|
|
|
2019-07-30 07:10:40 -04:00
|
|
|
def bump_tag_score!(tag_id)
|
|
|
|
Tag.where(id: tag_id).update_all('score = COALESCE(score, 0) + 1')
|
|
|
|
end
|
|
|
|
|
2018-05-27 15:45:30 -04:00
|
|
|
def disallowed_hashtags
|
|
|
|
return @disallowed_hashtags if defined?(@disallowed_hashtags)
|
|
|
|
|
|
|
|
@disallowed_hashtags = Setting.disallowed_hashtags.nil? ? [] : Setting.disallowed_hashtags
|
|
|
|
@disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String
|
|
|
|
@disallowed_hashtags = @disallowed_hashtags.map(&:downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|