diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2016-09-21 23:18:28 +0200 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2016-09-21 23:18:28 +0200 |
commit | c0e9603c921b66b927f9380968c70ac89253f30c (patch) | |
tree | 995e7f89759c550d91abd4af9eabf162943fa6aa /app/models | |
parent | d709151781ae67290456a1fe7c1bd1346254cd8c (diff) |
Fix #50 - Order ancestors/descendants by tree path
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/status.rb | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/app/models/status.rb b/app/models/status.rb index 4adf2944c..e336cfe62 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -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) |