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.

110 lines
3.5 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. # 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, max_child_id: max_child_id, since_child_id: since_child_id, depth: depth])
  49. WITH RECURSIVE search_tree(id, path)
  50. AS (
  51. SELECT id, ARRAY[id]
  52. FROM statuses
  53. WHERE id = :id AND COALESCE(id < :max_child_id, TRUE) AND COALESCE(id > :since_child_id, TRUE)
  54. UNION ALL
  55. SELECT statuses.id, path || statuses.id
  56. FROM search_tree
  57. JOIN statuses ON statuses.in_reply_to_id = search_tree.id
  58. WHERE COALESCE(array_length(path, 1) < :depth, TRUE) AND NOT statuses.id = ANY(path)
  59. )
  60. SELECT id
  61. FROM search_tree
  62. ORDER BY path
  63. LIMIT :limit
  64. SQL
  65. descendants_with_self - [self]
  66. end
  67. def find_statuses_from_tree_path(ids, account)
  68. statuses = statuses_with_accounts(ids).to_a
  69. account_ids = statuses.map(&:account_id).uniq
  70. domains = statuses.map(&:account_domain).compact.uniq
  71. relations = relations_map_for_account(account, account_ids, domains)
  72. statuses.reject! { |status| filter_from_context?(status, account, relations) }
  73. # Order ancestors/descendants by tree path
  74. statuses.sort_by! { |status| ids.index(status.id) }
  75. end
  76. def relations_map_for_account(account, account_ids, domains)
  77. return {} if account.nil?
  78. {
  79. blocking: Account.blocking_map(account_ids, account.id),
  80. blocked_by: Account.blocked_by_map(account_ids, account.id),
  81. muting: Account.muting_map(account_ids, account.id),
  82. following: Account.following_map(account_ids, account.id),
  83. domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
  84. }
  85. end
  86. def statuses_with_accounts(ids)
  87. Status.where(id: ids).includes(:account)
  88. end
  89. def filter_from_context?(status, account, relations)
  90. StatusFilter.new(status, account, relations).filtered?
  91. end
  92. end