about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2020-09-07 11:02:04 +0200
committerGitHub <noreply@github.com>2020-09-07 11:02:04 +0200
commite8bc187845b78e4a94894c69ecf930a524ad2056 (patch)
tree5d406773ebc580f361996222bd636bdaac108b2b /app/models
parenta6121a159c5305ea9faa95743a50babb23ab41cd (diff)
Refactor how public and tag timelines are queried (#14728)
Diffstat (limited to 'app/models')
-rw-r--r--app/models/public_feed.rb90
-rw-r--r--app/models/status.rb67
-rw-r--r--app/models/tag_feed.rb57
3 files changed, 148 insertions, 66 deletions
diff --git a/app/models/public_feed.rb b/app/models/public_feed.rb
new file mode 100644
index 000000000..c8ce1a140
--- /dev/null
+++ b/app/models/public_feed.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+class PublicFeed < Feed
+  # @param [Account] account
+  # @param [Hash] options
+  # @option [Boolean] :with_replies
+  # @option [Boolean] :with_reblogs
+  # @option [Boolean] :local
+  # @option [Boolean] :remote
+  # @option [Boolean] :only_media
+  def initialize(account, options = {})
+    @account = account
+    @options = options
+  end
+
+  # @param [Integer] limit
+  # @param [Integer] max_id
+  # @param [Integer] since_id
+  # @param [Integer] min_id
+  # @return [Array<Status>]
+  def get(limit, max_id = nil, since_id = nil, min_id = nil)
+    scope = public_scope
+
+    scope.merge!(without_replies_scope) unless with_replies?
+    scope.merge!(without_reblogs_scope) unless with_reblogs?
+    scope.merge!(local_only_scope) if local_only?
+    scope.merge!(remote_only_scope) if remote_only?
+    scope.merge!(account_filters_scope) if account?
+    scope.merge!(media_only_scope) if media_only?
+
+    scope.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id)
+  end
+
+  private
+
+  def with_reblogs?
+    @options[:with_reblogs]
+  end
+
+  def with_replies?
+    @options[:with_replies]
+  end
+
+  def local_only?
+    @options[:local]
+  end
+
+  def remote_only?
+    @options[:remote]
+  end
+
+  def account?
+    @account.present?
+  end
+
+  def media_only?
+    @options[:only_media]
+  end
+
+  def public_scope
+    Status.with_public_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
+  end
+
+  def local_only_scope
+    Status.local
+  end
+
+  def remote_only_scope
+    Status.remote
+  end
+
+  def without_replies_scope
+    Status.without_replies
+  end
+
+  def without_reblogs_scope
+    Status.without_reblogs
+  end
+
+  def media_only_scope
+    Status.joins(:media_attachments).group(:id)
+  end
+
+  def account_filters_scope
+    Status.not_excluded_by_account(@account).tap do |scope|
+      scope.merge!(Status.not_domain_blocked_by_account(@account)) unless local_only?
+      scope.merge!(Status.in_chosen_languages(@account)) if @account.chosen_languages.present?
+    end
+  end
+end
diff --git a/app/models/status.rb b/app/models/status.rb
index 71596ec2f..c6e16ff75 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -85,12 +85,12 @@ class Status < ApplicationRecord
   scope :recent, -> { reorder(id: :desc) }
   scope :remote, -> { where(local: false).where.not(uri: nil) }
   scope :local,  -> { where(local: true).or(where(uri: nil)) }
-
   scope :with_accounts, ->(ids) { where(id: ids).includes(:account) }
   scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
   scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
   scope :with_public_visibility, -> { where(visibility: :public) }
   scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) }
+  scope :in_chosen_languages, ->(account) { where(language: nil).or where(language: account.chosen_languages) }
   scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced_at: nil }) }
   scope :including_silenced_accounts, -> { left_outer_joins(:account).where.not(accounts: { silenced_at: nil }) }
   scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
@@ -277,26 +277,6 @@ class Status < ApplicationRecord
       visibilities.keys - %w(direct limited)
     end
 
-    def in_chosen_languages(account)
-      where(language: nil).or where(language: account.chosen_languages)
-    end
-
-    def as_public_timeline(account = nil, local_only = false)
-      query = timeline_scope(local_only).without_replies
-
-      apply_timeline_filters(query, account, [:local, true].include?(local_only))
-    end
-
-    def as_tag_timeline(tag, account = nil, local_only = false)
-      query = timeline_scope(local_only).tagged_with(tag)
-
-      apply_timeline_filters(query, account, local_only)
-    end
-
-    def as_outbox_timeline(account)
-      where(account: account, visibility: :public)
-    end
-
     def favourites_map(status_ids, account_id)
       Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true }
     end
@@ -373,51 +353,6 @@ class Status < ApplicationRecord
         status&.distributable? ? status : nil
       end.compact
     end
-
-    private
-
-    def timeline_scope(scope = false)
-      starting_scope = case scope
-                       when :local, true
-                         Status.local
-                       when :remote
-                         Status.remote
-                       else
-                         Status
-                       end
-
-      starting_scope
-        .with_public_visibility
-        .without_reblogs
-    end
-
-    def apply_timeline_filters(query, account, local_only)
-      if account.nil?
-        filter_timeline_default(query)
-      else
-        filter_timeline_for_account(query, account, local_only)
-      end
-    end
-
-    def filter_timeline_for_account(query, account, local_only)
-      query = query.not_excluded_by_account(account)
-      query = query.not_domain_blocked_by_account(account) unless local_only
-      query = query.in_chosen_languages(account) if account.chosen_languages.present?
-      query.merge(account_silencing_filter(account))
-    end
-
-    def filter_timeline_default(query)
-      query.excluding_silenced_accounts
-    end
-
-    def account_silencing_filter(account)
-      if account.silenced?
-        including_myself = left_outer_joins(:account).where(account_id: account.id).references(:accounts)
-        excluding_silenced_accounts.or(including_myself)
-      else
-        excluding_silenced_accounts
-      end
-    end
   end
 
   def status_stat
diff --git a/app/models/tag_feed.rb b/app/models/tag_feed.rb
new file mode 100644
index 000000000..50634fe83
--- /dev/null
+++ b/app/models/tag_feed.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+class TagFeed < PublicFeed
+  LIMIT_PER_MODE = 4
+
+  # @param [Tag] tag
+  # @param [Account] account
+  # @param [Hash] options
+  # @option [Enumerable<String>] :any
+  # @option [Enumerable<String>] :all
+  # @option [Enumerable<String>] :none
+  # @option [Boolean] :local
+  # @option [Boolean] :remote
+  # @option [Boolean] :only_media
+  def initialize(tag, account, options = {})
+    @tag     = tag
+    @account = account
+    @options = options
+  end
+
+  # @param [Integer] limit
+  # @param [Integer] max_id
+  # @param [Integer] since_id
+  # @param [Integer] min_id
+  # @return [Array<Status>]
+  def get(limit, max_id = nil, since_id = nil, min_id = nil)
+    scope = public_scope
+
+    scope.merge!(tagged_with_any_scope)
+    scope.merge!(tagged_with_all_scope)
+    scope.merge!(tagged_with_none_scope)
+    scope.merge!(local_only_scope) if local_only?
+    scope.merge!(remote_only_scope) if remote_only?
+    scope.merge!(account_filters_scope) if account?
+    scope.merge!(media_only_scope) if media_only?
+
+    scope.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id)
+  end
+
+  private
+
+  def tagged_with_any_scope
+    Status.group(:id).tagged_with(tags_for(Array(@tag.name) | Array(@options[:any])))
+  end
+
+  def tagged_with_all_scope
+    Status.group(:id).tagged_with_all(tags_for(@options[:all]))
+  end
+
+  def tagged_with_none_scope
+    Status.group(:id).tagged_with_none(tags_for(@options[:none]))
+  end
+
+  def tags_for(names)
+    Tag.matching_name(Array(names).take(LIMIT_PER_MODE)) if names.present?
+  end
+end