about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-03-21 11:43:21 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-03-21 11:43:21 +0100
commit35aafdba961c90ecad04f38d4eec69100da36697 (patch)
tree294516de0da1177dd8bc28e774de3c4ab46ef80b /app/models
parent7e00a21ea6f76f8167f1070fbfe231bbd65c6cad (diff)
Ancestors and descendants of statuses
Diffstat (limited to 'app/models')
-rw-r--r--app/models/status.rb8
1 files changed, 8 insertions, 0 deletions
diff --git a/app/models/status.rb b/app/models/status.rb
index 08b5ee75e..2fdd8d123 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -76,6 +76,14 @@ class Status < ActiveRecord::Base
     m
   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])
+  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])
+  end
+
   after_create do
     self.account.stream_entries.create!(activity: self)
   end