2017-06-05 07:07:44 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-01 03:00:41 -08:00
|
|
|
module Status::ThreadingConcern
|
2017-06-05 07:07:44 -07:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-04-11 03:35:09 -07:00
|
|
|
def ancestors(limit, account = nil)
|
|
|
|
find_statuses_from_tree_path(ancestor_ids(limit), account)
|
2017-06-05 07:07:44 -07:00
|
|
|
end
|
|
|
|
|
2022-11-10 13:30:00 -08:00
|
|
|
def descendants(limit, account = nil, depth = nil)
|
|
|
|
find_statuses_from_tree_path(descendant_ids(limit, depth), account, promote: true)
|
2017-06-05 07:07:44 -07:00
|
|
|
end
|
|
|
|
|
2019-02-28 06:22:21 -08:00
|
|
|
def self_replies(limit)
|
2024-04-16 06:16:54 -07:00
|
|
|
account.statuses.distributable_visibility.where(in_reply_to_id: id).reorder(id: :asc).limit(limit)
|
2019-02-28 06:22:21 -08:00
|
|
|
end
|
|
|
|
|
2017-06-05 07:07:44 -07:00
|
|
|
private
|
|
|
|
|
2018-04-11 03:35:09 -07:00
|
|
|
def ancestor_ids(limit)
|
|
|
|
key = "ancestors:#{id}"
|
|
|
|
ancestors = Rails.cache.fetch(key)
|
|
|
|
|
|
|
|
if ancestors.nil? || ancestors[:limit] < limit
|
|
|
|
ids = ancestor_statuses(limit).pluck(:id).reverse!
|
|
|
|
Rails.cache.write key, limit: limit, ids: ids
|
|
|
|
ids
|
|
|
|
else
|
|
|
|
ancestors[:ids].last(limit)
|
2017-06-05 07:07:44 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-11 03:35:09 -07:00
|
|
|
def ancestor_statuses(limit)
|
|
|
|
Status.find_by_sql([<<-SQL.squish, id: in_reply_to_id, limit: limit])
|
2017-06-05 07:07:44 -07:00
|
|
|
WITH RECURSIVE search_tree(id, in_reply_to_id, path)
|
|
|
|
AS (
|
|
|
|
SELECT id, in_reply_to_id, ARRAY[id]
|
|
|
|
FROM statuses
|
|
|
|
WHERE id = :id
|
|
|
|
UNION ALL
|
|
|
|
SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id
|
|
|
|
FROM search_tree
|
|
|
|
JOIN statuses ON statuses.id = search_tree.in_reply_to_id
|
|
|
|
WHERE NOT statuses.id = ANY(path)
|
|
|
|
)
|
|
|
|
SELECT id
|
|
|
|
FROM search_tree
|
2018-04-11 03:35:09 -07:00
|
|
|
ORDER BY path
|
|
|
|
LIMIT :limit
|
2017-06-05 07:07:44 -07:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2022-11-10 13:30:00 -08:00
|
|
|
def descendant_ids(limit, depth)
|
2018-06-04 13:20:12 -07:00
|
|
|
# use limit + 1 and depth + 1 because 'self' is included
|
|
|
|
depth += 1 if depth.present?
|
|
|
|
limit += 1 if limit.present?
|
|
|
|
|
2022-11-10 13:30:00 -08:00
|
|
|
descendants_with_self = Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, depth: depth])
|
|
|
|
WITH RECURSIVE search_tree(id, path) AS (
|
2017-06-05 07:07:44 -07:00
|
|
|
SELECT id, ARRAY[id]
|
|
|
|
FROM statuses
|
2022-11-10 13:30:00 -08:00
|
|
|
WHERE id = :id
|
|
|
|
UNION ALL
|
2017-06-05 07:07:44 -07:00
|
|
|
SELECT statuses.id, path || statuses.id
|
|
|
|
FROM search_tree
|
|
|
|
JOIN statuses ON statuses.in_reply_to_id = search_tree.id
|
2018-04-23 10:27:35 -07:00
|
|
|
WHERE COALESCE(array_length(path, 1) < :depth, TRUE) AND NOT statuses.id = ANY(path)
|
2017-06-05 07:07:44 -07:00
|
|
|
)
|
|
|
|
SELECT id
|
|
|
|
FROM search_tree
|
|
|
|
ORDER BY path
|
2018-04-23 10:27:35 -07:00
|
|
|
LIMIT :limit
|
2017-06-05 07:07:44 -07:00
|
|
|
SQL
|
2018-06-04 13:20:12 -07:00
|
|
|
|
2022-11-10 13:30:00 -08:00
|
|
|
descendants_with_self.pluck(:id) - [id]
|
2017-06-05 07:07:44 -07:00
|
|
|
end
|
|
|
|
|
2018-11-24 11:48:50 -08:00
|
|
|
def find_statuses_from_tree_path(ids, account, promote: false)
|
2020-01-11 02:55:33 -08:00
|
|
|
statuses = Status.with_accounts(ids).to_a
|
2018-05-03 01:41:58 -07:00
|
|
|
account_ids = statuses.map(&:account_id).uniq
|
2021-01-09 15:32:01 -08:00
|
|
|
domains = statuses.filter_map(&:account_domain).uniq
|
2023-03-21 02:32:58 -07:00
|
|
|
relations = account&.relations_map(account_ids, domains) || {}
|
2018-05-03 01:41:58 -07:00
|
|
|
|
2020-01-11 02:55:33 -08:00
|
|
|
statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }
|
2017-06-05 07:07:44 -07:00
|
|
|
|
|
|
|
# Order ancestors/descendants by tree path
|
|
|
|
statuses.sort_by! { |status| ids.index(status.id) }
|
2018-11-24 11:48:50 -08:00
|
|
|
|
|
|
|
# Bring self-replies to the top
|
|
|
|
if promote
|
|
|
|
promote_by!(statuses) { |status| status.in_reply_to_account_id == status.account_id }
|
|
|
|
else
|
|
|
|
statuses
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def promote_by!(arr)
|
|
|
|
insert_at = arr.find_index { |item| !yield(item) }
|
|
|
|
|
|
|
|
return arr if insert_at.nil?
|
|
|
|
|
|
|
|
arr.each_with_index do |item, index|
|
|
|
|
next if index <= insert_at || !yield(item)
|
|
|
|
|
|
|
|
arr.insert(insert_at, arr.delete_at(index))
|
|
|
|
insert_at += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
arr
|
2017-06-05 07:07:44 -07:00
|
|
|
end
|
|
|
|
end
|