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.

119 lines
3.7 KiB

  1. # frozen_string_literal: true
  2. class Trends::Links < Trends::Base
  3. PREFIX = 'trending_links'
  4. self.default_options = {
  5. threshold: 5,
  6. review_threshold: 3,
  7. max_score_cooldown: 2.days.freeze,
  8. max_score_halflife: 8.hours.freeze,
  9. }
  10. def register(status, at_time = Time.now.utc)
  11. original_status = status.reblog? ? status.reblog : status
  12. return unless original_status.public_visibility? && status.public_visibility? &&
  13. !original_status.account.silenced? && !status.account.silenced? &&
  14. !original_status.spoiler_text?
  15. original_status.preview_cards.each do |preview_card|
  16. add(preview_card, status.account_id, at_time) if preview_card.appropriate_for_trends?
  17. end
  18. end
  19. def add(preview_card, account_id, at_time = Time.now.utc)
  20. preview_card.history.add(account_id, at_time)
  21. record_used_id(preview_card.id, at_time)
  22. end
  23. def refresh(at_time = Time.now.utc)
  24. preview_cards = PreviewCard.where(id: (recently_used_ids(at_time) + currently_trending_ids(false, -1)).uniq)
  25. calculate_scores(preview_cards, at_time)
  26. trim_older_items
  27. end
  28. def request_review
  29. preview_cards = PreviewCard.where(id: currently_trending_ids(false, -1))
  30. preview_cards.filter_map do |preview_card|
  31. next unless would_be_trending?(preview_card.id) && !preview_card.trendable? && preview_card.requires_review_notification?
  32. if preview_card.provider.nil?
  33. preview_card.provider = PreviewCardProvider.create(domain: preview_card.domain, requested_review_at: Time.now.utc)
  34. else
  35. preview_card.provider.touch(:requested_review_at)
  36. end
  37. preview_card
  38. end
  39. end
  40. protected
  41. def key_prefix
  42. PREFIX
  43. end
  44. def klass
  45. PreviewCard
  46. end
  47. private
  48. def calculate_scores(preview_cards, at_time)
  49. preview_cards.each do |preview_card|
  50. expected = preview_card.history.get(at_time - 1.day).accounts.to_f
  51. expected = 1.0 if expected.zero?
  52. observed = preview_card.history.get(at_time).accounts.to_f
  53. max_time = preview_card.max_score_at
  54. max_score = preview_card.max_score
  55. max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown])
  56. score = begin
  57. if expected > observed || observed < options[:threshold]
  58. 0
  59. else
  60. ((observed - expected)**2) / expected
  61. end
  62. end
  63. if score > max_score
  64. max_score = score
  65. max_time = at_time
  66. # Not interested in triggering any callbacks for this
  67. preview_card.update_columns(max_score: max_score, max_score_at: max_time)
  68. end
  69. decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f))
  70. add_to_and_remove_from_subsets(preview_card.id, decaying_score, {
  71. all: true,
  72. allowed: preview_card.trendable?,
  73. })
  74. next unless valid_locale?(preview_card.language)
  75. add_to_and_remove_from_subsets(preview_card.id, decaying_score, {
  76. "all:#{preview_card.language}" => true,
  77. "allowed:#{preview_card.language}" => preview_card.trendable?,
  78. })
  79. end
  80. # Clean up localized sets by calculating the intersection with the main
  81. # set. We do this instead of just deleting the localized sets to avoid
  82. # having moments where the API returns empty results
  83. redis.pipelined do
  84. Trends.available_locales.each do |locale|
  85. redis.zinterstore("#{key_prefix}:all:#{locale}", ["#{key_prefix}:all:#{locale}", "#{key_prefix}:all"], aggregate: 'max')
  86. redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:all"], aggregate: 'max')
  87. end
  88. end
  89. end
  90. def would_be_trending?(id)
  91. score(id) > score_at_rank(options[:review_threshold] - 1)
  92. end
  93. end