status_threading_concern.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # frozen_string_literal: true
  2. module StatusThreadingConcern
  3. extend ActiveSupport::Concern
  4. def ancestors(limit, account = nil)
  5. find_statuses_from_tree_path(ancestor_ids(limit), account)
  6. end
  7. def descendants(limit, account = nil, max_child_id = nil, since_child_id = nil, depth = nil)
  8. find_statuses_from_tree_path(descendant_ids(limit, max_child_id, since_child_id, depth), account, promote: true)
  9. end
  10. def self_replies(limit)
  11. account.statuses.where(in_reply_to_id: id, visibility: [:public, :unlisted]).reorder(id: :asc).limit(limit)
  12. end
  13. private
  14. def ancestor_ids(limit)
  15. key = "ancestors:#{id}"
  16. ancestors = Rails.cache.fetch(key)
  17. if ancestors.nil? || ancestors[:limit] < limit
  18. ids = ancestor_statuses(limit).pluck(:id).reverse!
  19. Rails.cache.write key, limit: limit, ids: ids
  20. ids
  21. else
  22. ancestors[:ids].last(limit)
  23. end
  24. end
  25. def ancestor_statuses(limit)
  26. Status.find_by_sql([<<-SQL.squish, id: in_reply_to_id, limit: limit])
  27. WITH RECURSIVE search_tree(id, in_reply_to_id, path)
  28. AS (
  29. SELECT id, in_reply_to_id, ARRAY[id]
  30. FROM statuses
  31. WHERE id = :id
  32. UNION ALL
  33. SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id
  34. FROM search_tree
  35. JOIN statuses ON statuses.id = search_tree.in_reply_to_id
  36. WHERE NOT statuses.id = ANY(path)
  37. )
  38. SELECT id
  39. FROM search_tree
  40. ORDER BY path
  41. LIMIT :limit
  42. SQL
  43. end
  44. def descendant_ids(limit, max_child_id, since_child_id, depth)
  45. descendant_statuses(limit, max_child_id, since_child_id, depth).pluck(:id)
  46. end
  47. def descendant_statuses(limit, max_child_id, since_child_id, depth)
  48. # use limit + 1 and depth + 1 because 'self' is included
  49. depth += 1 if depth.present?
  50. limit += 1 if limit.present?
  51. descendants_with_self = Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, max_child_id: max_child_id, since_child_id: since_child_id, depth: depth])
  52. WITH RECURSIVE search_tree(id, path)
  53. AS (
  54. SELECT id, ARRAY[id]
  55. FROM statuses
  56. WHERE id = :id AND COALESCE(id < :max_child_id, TRUE) AND COALESCE(id > :since_child_id, TRUE)
  57. UNION ALL
  58. SELECT statuses.id, path || statuses.id
  59. FROM search_tree
  60. JOIN statuses ON statuses.in_reply_to_id = search_tree.id
  61. WHERE COALESCE(array_length(path, 1) < :depth, TRUE) AND NOT statuses.id = ANY(path)
  62. )
  63. SELECT id
  64. FROM search_tree
  65. ORDER BY path
  66. LIMIT :limit
  67. SQL
  68. descendants_with_self - [self]
  69. end
  70. def find_statuses_from_tree_path(ids, account, promote: false)
  71. statuses = Status.with_accounts(ids).to_a
  72. account_ids = statuses.map(&:account_id).uniq
  73. domains = statuses.map(&:account_domain).compact.uniq
  74. relations = relations_map_for_account(account, account_ids, domains)
  75. statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }
  76. # Order ancestors/descendants by tree path
  77. statuses.sort_by! { |status| ids.index(status.id) }
  78. # Bring self-replies to the top
  79. if promote
  80. promote_by!(statuses) { |status| status.in_reply_to_account_id == status.account_id }
  81. else
  82. statuses
  83. end
  84. end
  85. def promote_by!(arr)
  86. insert_at = arr.find_index { |item| !yield(item) }
  87. return arr if insert_at.nil?
  88. arr.each_with_index do |item, index|
  89. next if index <= insert_at || !yield(item)
  90. arr.insert(insert_at, arr.delete_at(index))
  91. insert_at += 1
  92. end
  93. arr
  94. end
  95. def relations_map_for_account(account, account_ids, domains)
  96. return {} if account.nil?
  97. {
  98. blocking: Account.blocking_map(account_ids, account.id),
  99. blocked_by: Account.blocked_by_map(account_ids, account.id),
  100. muting: Account.muting_map(account_ids, account.id),
  101. following: Account.following_map(account_ids, account.id),
  102. domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
  103. }
  104. end
  105. end