about summary refs log tree commit diff
path: root/app/models/status.rb
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-06-05 10:07:44 -0400
committerEugen Rochko <eugen@zeonfederated.com>2017-06-05 16:07:44 +0200
commit6201f96b8a49d58b5b2d73fac9fa1fa93f5890ed (patch)
tree57e2f3b751dab120705459f5b9d9c39e2f7e63bb /app/models/status.rb
parentc26cea262b7673b0b239dd1da6754e7788aa08d8 (diff)
Introduce StatusThreadingConcern (#3490)
* Add a StatusFilter class to identify visibility of statuses by accounts

* Extract StatusThreadingConcern from Status

* Clarify purpose of checking for nil account
Diffstat (limited to 'app/models/status.rb')
-rw-r--r--app/models/status.rb30
1 files changed, 1 insertions, 29 deletions
diff --git a/app/models/status.rb b/app/models/status.rb
index e75ac7070..af9f7524e 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -28,6 +28,7 @@ class Status < ApplicationRecord
   include Paginable
   include Streamable
   include Cacheable
+  include StatusThreadingConcern
 
   enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
 
@@ -115,16 +116,6 @@ class Status < ApplicationRecord
     private_visibility? || direct_visibility?
   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) }
-    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)
-    find_statuses_from_tree_path(ids, account)
-  end
-
   def non_sensitive_with_media?
     !sensitive? && media_attachments.any?
   end
@@ -277,23 +268,4 @@ class Status < ApplicationRecord
       thread.account_id
     end
   end
-
-  def find_statuses_from_tree_path(ids, account)
-    statuses = Status.where(id: ids).includes(:account).to_a
-
-    # FIXME: n+1 bonanza
-    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)
-    should_filter   = account&.blocking?(status.account_id)
-    should_filter ||= account&.domain_blocking?(status.account_domain)
-    should_filter ||= account&.muting?(status.account_id)
-    should_filter ||= (status.account.silenced? && !account&.following?(status.account_id))
-    should_filter ||= !StatusPolicy.new(account, status).show?
-    should_filter
-  end
 end