threading_concern.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. module Status::ThreadingConcern
  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, depth = nil)
  8. find_statuses_from_tree_path(descendant_ids(limit, 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, depth)
  45. # use limit + 1 and depth + 1 because 'self' is included
  46. depth += 1 if depth.present?
  47. limit += 1 if limit.present?
  48. descendants_with_self = Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, depth: depth])
  49. WITH RECURSIVE search_tree(id, path) AS (
  50. SELECT id, ARRAY[id]
  51. FROM statuses
  52. WHERE id = :id
  53. UNION ALL
  54. SELECT statuses.id, path || statuses.id
  55. FROM search_tree
  56. JOIN statuses ON statuses.in_reply_to_id = search_tree.id
  57. WHERE COALESCE(array_length(path, 1) < :depth, TRUE) AND NOT statuses.id = ANY(path)
  58. )
  59. SELECT id
  60. FROM search_tree
  61. ORDER BY path
  62. LIMIT :limit
  63. SQL
  64. descendants_with_self.pluck(:id) - [id]
  65. end
  66. def find_statuses_from_tree_path(ids, account, promote: false)
  67. statuses = Status.with_accounts(ids).to_a
  68. account_ids = statuses.map(&:account_id).uniq
  69. domains = statuses.filter_map(&:account_domain).uniq
  70. relations = account&.relations_map(account_ids, domains) || {}
  71. statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }
  72. # Order ancestors/descendants by tree path
  73. statuses.sort_by! { |status| ids.index(status.id) }
  74. # Bring self-replies to the top
  75. if promote
  76. promote_by!(statuses) { |status| status.in_reply_to_account_id == status.account_id }
  77. else
  78. statuses
  79. end
  80. end
  81. def promote_by!(arr)
  82. insert_at = arr.find_index { |item| !yield(item) }
  83. return arr if insert_at.nil?
  84. arr.each_with_index do |item, index|
  85. next if index <= insert_at || !yield(item)
  86. arr.insert(insert_at, arr.delete_at(index))
  87. insert_at += 1
  88. end
  89. arr
  90. end
  91. end