statuses.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # frozen_string_literal: true
  2. class Trends::Statuses < Trends::Base
  3. PREFIX = 'trending_statuses'
  4. self.default_options = {
  5. threshold: 5,
  6. review_threshold: 3,
  7. score_halflife: 2.hours.freeze,
  8. decay_threshold: 0.3,
  9. }
  10. class Query < Trends::Query
  11. def filtered_for!(account)
  12. @account = account
  13. self
  14. end
  15. def filtered_for(account)
  16. clone.filtered_for!(account)
  17. end
  18. private
  19. def apply_scopes(scope)
  20. if @account.nil?
  21. scope
  22. else
  23. scope.not_excluded_by_account(@account).not_domain_blocked_by_account(@account)
  24. end
  25. end
  26. end
  27. def register(status, at_time = Time.now.utc)
  28. add(status.proper, status.account_id, at_time) if eligible?(status.proper)
  29. end
  30. def add(status, _account_id, at_time = Time.now.utc)
  31. # We rely on the total reblogs and favourites count, so we
  32. # don't record which account did the what and when here
  33. record_used_id(status.id, at_time)
  34. end
  35. def query
  36. Query.new(key_prefix, klass)
  37. end
  38. def refresh(at_time = Time.now.utc)
  39. statuses = Status.where(id: (recently_used_ids(at_time) + currently_trending_ids(false, -1)).uniq).includes(:account, :media_attachments)
  40. calculate_scores(statuses, at_time)
  41. end
  42. def request_review
  43. statuses = Status.where(id: currently_trending_ids(false, -1)).includes(:account)
  44. statuses.filter_map do |status|
  45. next unless would_be_trending?(status.id) && !status.trendable? && status.requires_review_notification?
  46. status.account.touch(:requested_review_at)
  47. status
  48. end
  49. end
  50. protected
  51. def key_prefix
  52. PREFIX
  53. end
  54. def klass
  55. Status
  56. end
  57. private
  58. def eligible?(status)
  59. status.public_visibility? && status.account.discoverable? && !status.account.silenced? && status.spoiler_text.blank? && !status.sensitive? && !status.reply?
  60. end
  61. def calculate_scores(statuses, at_time)
  62. global_items = []
  63. locale_items = Hash.new { |h, key| h[key] = [] }
  64. statuses.each do |status|
  65. expected = 1.0
  66. observed = (status.reblogs_count + status.favourites_count).to_f
  67. score = begin
  68. if expected > observed || observed < options[:threshold]
  69. 0
  70. else
  71. ((observed - expected)**2) / expected
  72. end
  73. end
  74. decaying_score = score * (0.5**((at_time.to_f - status.created_at.to_f) / options[:score_halflife].to_f))
  75. next unless decaying_score >= options[:decay_threshold]
  76. global_items << { score: decaying_score, item: status }
  77. locale_items[status.language] << { account_id: status.account_id, score: decaying_score, item: status } if valid_locale?(status.language)
  78. end
  79. replace_items('', global_items)
  80. Trends.available_locales.each do |locale|
  81. replace_items(":#{locale}", locale_items[locale])
  82. end
  83. end
  84. def filter_for_allowed_items(items)
  85. # Show only one status per account, pick the one with the highest score
  86. # that's also eligible to trend
  87. items.group_by { |item| item[:account_id] }.values.filter_map { |account_items| account_items.select { |item| item[:item].trendable? && item[:item].account.discoverable? }.max_by { |item| item[:score] } }
  88. end
  89. def would_be_trending?(id)
  90. score(id) > score_at_rank(options[:review_threshold] - 1)
  91. end
  92. end