about summary refs log tree commit diff
diff options
context:
space:
mode:
authoralpaca-tc <alpaca-tc@alpaca.tc>2017-05-17 22:38:16 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-17 15:38:16 +0200
commit59ceeae8ea3bc0135c87f12ae9d232d84c2b6671 (patch)
tree7e53edd559129028f1f673a762e8269c7464d59f
parentb397f6963338bf1572677771d2bd1fba3ec66c82 (diff)
Refactor Status#ancestors/descendants (#3092)
-rw-r--r--app/models/status.rb24
1 files changed, 12 insertions, 12 deletions
diff --git a/app/models/status.rb b/app/models/status.rb
index 8d827e980..fd1049116 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -122,21 +122,13 @@ class Status < ApplicationRecord
   end
 
   def ancestors(account = nil)
-    ids      = Rails.cache.fetch("ancestors:#{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', id]) - [self]).pluck(:id) }
-    statuses = Status.where(id: ids).group_by(&:id)
-    results  = ids.map { |id| statuses[id]&.first }.compact
-    results  = results.reject { |status| filter_from_context?(status, account) }
-
-    results
+    ids = Rails.cache.fetch("ancestors:#{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', id]) - [self]).pluck(:id) }
+    find_statuses_from_tree_path(ids, account)
   end
 
   def descendants(account = nil)
-    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', id]) - [self]).pluck(:id)
-    statuses = Status.where(id: ids).group_by(&:id)
-    results  = ids.map { |id| statuses[id].first }
-    results  = results.reject { |status| filter_from_context?(status, account) }
-
-    results
+    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', id]) - [self]).pluck(:id)
+    find_statuses_from_tree_path(ids, account)
   end
 
   def non_sensitive_with_media?
@@ -291,6 +283,14 @@ class Status < ApplicationRecord
     end
   end
 
+  def find_statuses_from_tree_path(ids, account)
+    statuses = Status.where(id: ids).to_a
+    statuses.reject! { |status| filter_from_context?(status, account) }
+
+    # Order ancestors/descendants by tree path
+    statuses.sort_by! { |status| ids.index(status.id) }
+  end
+
   def filter_from_context?(status, account)
     account&.blocking?(status.account_id) || account&.muting?(status.account_id) || (status.account.silenced? && !account&.following?(status.account_id)) || !status.permitted?(account)
   end