2017-06-05 16:07:44 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module StatusThreadingConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-04-11 12:35:09 +02:00
|
|
|
def ancestors(limit, account = nil)
|
|
|
|
find_statuses_from_tree_path(ancestor_ids(limit), account)
|
2017-06-05 16:07:44 +02:00
|
|
|
end
|
|
|
|
|
2018-04-23 19:27:35 +02:00
|
|
|
def descendants(limit, account = nil, max_child_id = nil, since_child_id = nil, depth = nil)
|
|
|
|
find_statuses_from_tree_path(descendant_ids(limit, max_child_id, since_child_id, depth), account)
|
2017-06-05 16:07:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-04-11 12:35:09 +02: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 16:07:44 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-11 12:35:09 +02:00
|
|
|
def ancestor_statuses(limit)
|
|
|
|
Status.find_by_sql([<<-SQL.squish, id: in_reply_to_id, limit: limit])
|
2017-06-05 16:07:44 +02: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 12:35:09 +02:00
|
|
|
ORDER BY path
|
|
|
|
LIMIT :limit
|
2017-06-05 16:07:44 +02:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2018-04-23 19:27:35 +02:00
|
|
|
def descendant_ids(limit, max_child_id, since_child_id, depth)
|
|
|
|
descendant_statuses(limit, max_child_id, since_child_id, depth).pluck(:id)
|
2017-06-05 16:07:44 +02:00
|
|
|
end
|
|
|
|
|
2018-04-23 19:27:35 +02:00
|
|
|
def descendant_statuses(limit, max_child_id, since_child_id, depth)
|
|
|
|
Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, max_child_id: max_child_id, since_child_id: since_child_id, depth: depth])
|
2017-06-05 16:07:44 +02:00
|
|
|
WITH RECURSIVE search_tree(id, path)
|
|
|
|
AS (
|
|
|
|
SELECT id, ARRAY[id]
|
|
|
|
FROM statuses
|
2018-04-23 19:27:35 +02:00
|
|
|
WHERE in_reply_to_id = :id AND COALESCE(id < :max_child_id, TRUE) AND COALESCE(id > :since_child_id, TRUE)
|
2017-06-05 16:07:44 +02:00
|
|
|
UNION ALL
|
|
|
|
SELECT statuses.id, path || statuses.id
|
|
|
|
FROM search_tree
|
|
|
|
JOIN statuses ON statuses.in_reply_to_id = search_tree.id
|
2018-04-23 19:27:35 +02:00
|
|
|
WHERE COALESCE(array_length(path, 1) < :depth, TRUE) AND NOT statuses.id = ANY(path)
|
2017-06-05 16:07:44 +02:00
|
|
|
)
|
|
|
|
SELECT id
|
|
|
|
FROM search_tree
|
|
|
|
ORDER BY path
|
2018-04-23 19:27:35 +02:00
|
|
|
LIMIT :limit
|
2017-06-05 16:07:44 +02:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_statuses_from_tree_path(ids, account)
|
2018-05-03 10:41:58 +02:00
|
|
|
statuses = statuses_with_accounts(ids).to_a
|
|
|
|
account_ids = statuses.map(&:account_id).uniq
|
|
|
|
domains = statuses.map(&:account_domain).compact.uniq
|
2017-06-05 16:07:44 +02:00
|
|
|
|
2018-05-03 10:41:58 +02:00
|
|
|
relations = if account.present?
|
|
|
|
{
|
|
|
|
blocking: Account.blocking_map(account_ids, account.id),
|
|
|
|
blocked_by: Account.blocked_by_map(account_ids, account.id),
|
|
|
|
muting: Account.muting_map(account_ids, account.id),
|
|
|
|
following: Account.following_map(account_ids, account.id),
|
|
|
|
domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
statuses.reject! { |status| filter_from_context?(status, account, relations) }
|
2017-06-05 16:07:44 +02:00
|
|
|
|
|
|
|
# Order ancestors/descendants by tree path
|
|
|
|
statuses.sort_by! { |status| ids.index(status.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def statuses_with_accounts(ids)
|
|
|
|
Status.where(id: ids).includes(:account)
|
|
|
|
end
|
|
|
|
|
2018-05-03 10:41:58 +02:00
|
|
|
def filter_from_context?(status, account, relations)
|
|
|
|
StatusFilter.new(status, account, relations).filtered?
|
2017-06-05 16:07:44 +02:00
|
|
|
end
|
|
|
|
end
|