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
|
2019-08-07 11:08:30 -04:00
|
|
|
LIMIT = 10
|
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)
|
2019-08-05 13:54:29 -04:00
|
|
|
return if account.silenced? || account.bot? || !tag.usable? || !(tag.trendable? || tag.requires_review?)
|
2018-05-27 15:45:30 -04:00
|
|
|
|
|
|
|
increment_historical_use!(tag.id, at_time)
|
|
|
|
increment_unique_use!(tag.id, account.id, at_time)
|
2019-08-05 13:54:29 -04:00
|
|
|
increment_vote!(tag, at_time)
|
2018-07-16 13:21:27 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 13:54:29 -04:00
|
|
|
def get(limit, filtered: true)
|
2019-08-07 11:08:30 -04:00
|
|
|
tag_ids = redis.zrevrange("#{KEY}:#{Time.now.utc.beginning_of_day.to_i}", 0, LIMIT - 1).map(&:to_i)
|
2019-08-05 13:54:29 -04:00
|
|
|
|
|
|
|
tags = Tag.where(id: tag_ids)
|
|
|
|
tags = tags.where(trendable: true) if filtered
|
|
|
|
tags = tags.each_with_object({}) { |tag, h| h[tag.id] = tag }
|
|
|
|
|
2019-08-07 11:08:30 -04:00
|
|
|
tag_ids.map { |tag_id| tags[tag_id] }.compact.take(limit)
|
2018-05-27 15:45:30 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 13:54:29 -04:00
|
|
|
def trending?(tag)
|
|
|
|
rank = redis.zrevrank("#{KEY}:#{Time.now.utc.beginning_of_day.to_i}", tag.id)
|
2019-08-07 11:08:30 -04:00
|
|
|
rank.present? && rank <= LIMIT
|
2019-08-05 13:54:29 -04:00
|
|
|
end
|
|
|
|
|
2018-05-27 15:45:30 -04:00
|
|
|
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
|
|
|
|
|
2019-08-05 13:54:29 -04:00
|
|
|
def increment_vote!(tag, at_time)
|
2018-07-16 13:21:27 -04:00
|
|
|
key = "#{KEY}:#{at_time.beginning_of_day.to_i}"
|
2019-08-05 13:54:29 -04:00
|
|
|
expected = redis.pfcount("activity:tags:#{tag.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts").to_f
|
2018-07-16 13:21:27 -04:00
|
|
|
expected = 1.0 if expected.zero?
|
2019-08-05 13:54:29 -04:00
|
|
|
observed = redis.pfcount("activity:tags:#{tag.id}:#{at_time.beginning_of_day.to_i}:accounts").to_f
|
2018-07-16 13:21:27 -04:00
|
|
|
|
|
|
|
if expected > observed || observed < THRESHOLD
|
2019-08-05 13:54:29 -04:00
|
|
|
redis.zrem(key, tag.id)
|
2018-07-16 13:21:27 -04:00
|
|
|
else
|
2019-08-05 13:54:29 -04:00
|
|
|
score = ((observed - expected)**2) / expected
|
|
|
|
old_rank = redis.zrevrank(key, tag.id)
|
|
|
|
|
|
|
|
redis.zadd(key, score, tag.id)
|
2019-08-07 11:08:30 -04:00
|
|
|
request_review!(tag) if (old_rank.nil? || old_rank > LIMIT) && redis.zrevrank(key, tag.id) <= LIMIT && !tag.trendable? && tag.requires_review? && !tag.requested_review?
|
2018-07-16 13:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
redis.expire(key, EXPIRE_TRENDS_AFTER)
|
|
|
|
end
|
|
|
|
|
2019-08-05 13:54:29 -04:00
|
|
|
def request_review!(tag)
|
2019-08-06 11:57:52 -04:00
|
|
|
return unless Setting.trends
|
|
|
|
|
|
|
|
tag.touch(:requested_review_at)
|
|
|
|
|
2019-08-05 13:54:29 -04:00
|
|
|
User.staff.includes(:account).find_each { |u| AdminMailer.new_trending_tag(u.account, tag).deliver_later! if u.allows_trending_tag_emails? }
|
2018-05-27 15:45:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|