Skip to content
Snippets Groups Projects
Commit c0e9603c authored by Eugen Rochko's avatar Eugen Rochko
Browse files

Fix #50 - Order ancestors/descendants by tree path

parent d7091517
No related branches found
No related tags found
No related merge requests found
......@@ -8,8 +8,8 @@ class Api::StatusesController < ApiController
def context
@status = Status.find(params[:id])
@ancestors = @status.ancestors.with_includes.with_counters.to_a
@descendants = @status.descendants.with_includes.with_counters.to_a
@ancestors = @status.ancestors
@descendants = @status.descendants
end
def create
......
......@@ -9,8 +9,8 @@ class StreamEntriesController < ApplicationController
@type = @stream_entry.activity_type.downcase
if @stream_entry.activity_type == 'Status'
@ancestors = @stream_entry.activity.ancestors.with_includes.with_counters
@descendants = @stream_entry.activity.descendants.with_includes.with_counters
@ancestors = @stream_entry.activity.ancestors
@descendants = @stream_entry.activity.descendants
if user_signed_in?
status_ids = [@stream_entry.activity_id] + @ancestors.map { |s| s.id } + @descendants.map { |s| s.id }
......
......@@ -61,11 +61,17 @@ class Status < ApplicationRecord
end
def ancestors
Status.where(id: Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE 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 ORDER BY path DESC', self.id]) - [self])
ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE 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 ORDER BY path DESC', self.id]) - [self]).pluck(:id)
statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
ids.map { |id| statuses[id].first }
end
def descendants
Status.where(id: Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', self.id]) - [self])
ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', self.id]) - [self]).pluck(:id)
statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
ids.map { |id| statuses[id].first }
end
def self.as_home_timeline(account)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment