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.

101 lines
3.3 KiB

  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)
  9. end
  10. private
  11. def ancestor_ids(limit)
  12. key = "ancestors:#{id}"
  13. ancestors = Rails.cache.fetch(key)
  14. if ancestors.nil? || ancestors[:limit] < limit
  15. ids = ancestor_statuses(limit).pluck(:id).reverse!
  16. Rails.cache.write key, limit: limit, ids: ids
  17. ids
  18. else
  19. ancestors[:ids].last(limit)
  20. end
  21. end
  22. def ancestor_statuses(limit)
  23. Status.find_by_sql([<<-SQL.squish, id: in_reply_to_id, limit: limit])
  24. WITH RECURSIVE search_tree(id, in_reply_to_id, path)
  25. AS (
  26. SELECT id, in_reply_to_id, ARRAY[id]
  27. FROM statuses
  28. WHERE id = :id
  29. UNION ALL
  30. SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id
  31. FROM search_tree
  32. JOIN statuses ON statuses.id = search_tree.in_reply_to_id
  33. WHERE NOT statuses.id = ANY(path)
  34. )
  35. SELECT id
  36. FROM search_tree
  37. ORDER BY path
  38. LIMIT :limit
  39. SQL
  40. end
  41. def descendant_ids(limit, max_child_id, since_child_id, depth)
  42. descendant_statuses(limit, max_child_id, since_child_id, depth).pluck(:id)
  43. end
  44. def descendant_statuses(limit, max_child_id, since_child_id, depth)
  45. Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, max_child_id: max_child_id, since_child_id: since_child_id, depth: depth])
  46. WITH RECURSIVE search_tree(id, path)
  47. AS (
  48. SELECT id, ARRAY[id]
  49. FROM statuses
  50. WHERE in_reply_to_id = :id AND COALESCE(id < :max_child_id, TRUE) AND COALESCE(id > :since_child_id, TRUE)
  51. UNION ALL
  52. SELECT statuses.id, path || statuses.id
  53. FROM search_tree
  54. JOIN statuses ON statuses.in_reply_to_id = search_tree.id
  55. WHERE COALESCE(array_length(path, 1) < :depth, TRUE) AND NOT statuses.id = ANY(path)
  56. )
  57. SELECT id
  58. FROM search_tree
  59. ORDER BY path
  60. LIMIT :limit
  61. SQL
  62. end
  63. def find_statuses_from_tree_path(ids, account)
  64. statuses = statuses_with_accounts(ids).to_a
  65. account_ids = statuses.map(&:account_id).uniq
  66. domains = statuses.map(&:account_domain).compact.uniq
  67. relations = if account.present?
  68. {
  69. blocking: Account.blocking_map(account_ids, account.id),
  70. blocked_by: Account.blocked_by_map(account_ids, account.id),
  71. muting: Account.muting_map(account_ids, account.id),
  72. following: Account.following_map(account_ids, account.id),
  73. domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
  74. }
  75. end
  76. statuses.reject! { |status| filter_from_context?(status, account, relations) }
  77. # Order ancestors/descendants by tree path
  78. statuses.sort_by! { |status| ids.index(status.id) }
  79. end
  80. def statuses_with_accounts(ids)
  81. Status.where(id: ids).includes(:account)
  82. end
  83. def filter_from_context?(status, account, relations)
  84. StatusFilter.new(status, account, relations).filtered?
  85. end
  86. end