about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--app/lib/vacuum/statuses_vacuum.rb43
-rw-r--r--app/models/status.rb15
2 files changed, 26 insertions, 32 deletions
diff --git a/app/lib/vacuum/statuses_vacuum.rb b/app/lib/vacuum/statuses_vacuum.rb
index 41d6ba270..d1c4e7197 100644
--- a/app/lib/vacuum/statuses_vacuum.rb
+++ b/app/lib/vacuum/statuses_vacuum.rb
@@ -8,47 +8,40 @@ class Vacuum::StatusesVacuum
   end
 
   def perform
-    vacuum_statuses! if retention_period?
+    vacuum_statuses! if @retention_period.present?
   end
 
   private
 
   def vacuum_statuses!
-    statuses_scope.find_in_batches do |statuses|
+    statuses_scope.in_batches do |statuses|
       # Side-effects not covered by foreign keys, such
       # as the search index, must be handled first.
-
-      remove_from_account_conversations(statuses)
-      remove_from_search_index(statuses)
-
-      # Foreign keys take care of most associated records
-      # for us. Media attachments will be orphaned.
-
-      Status.where(id: statuses.map(&:id)).delete_all
+      statuses.direct_visibility
+              .includes(mentions: :account)
+              .find_each do |status|
+        # TODO: replace temporary solution - call of private model method
+        status.send(:unlink_from_conversations)
+      end
+      remove_from_search_index(statuses.ids) if Chewy.enabled?
+
+      # Foreign keys take care of most associated records for us.
+      # Media attachments will be orphaned.
+      statuses.delete_all
     end
   end
 
   def statuses_scope
-    Status.unscoped.kept.where(account: Account.remote).where(Status.arel_table[:id].lt(retention_period_as_id)).select(:id, :visibility)
+    Status.unscoped.kept
+          .joins(:account).merge(Account.remote)
+          .where('statuses.id < ?', retention_period_as_id)
   end
 
   def retention_period_as_id
     Mastodon::Snowflake.id_at(@retention_period.ago, with_random: false)
   end
 
-  def analyze_statuses!
-    ActiveRecord::Base.connection.execute('ANALYZE statuses')
-  end
-
-  def remove_from_account_conversations(statuses)
-    Status.where(id: statuses.select(&:direct_visibility?).map(&:id)).includes(:account, mentions: :account).each(&:unlink_from_conversations)
-  end
-
-  def remove_from_search_index(statuses)
-    with_redis { |redis| redis.sadd('chewy:queue:StatusesIndex', statuses.map(&:id)) } if Chewy.enabled?
-  end
-
-  def retention_period?
-    @retention_period.present?
+  def remove_from_search_index(status_ids)
+    with_redis { |redis| redis.sadd('chewy:queue:StatusesIndex', status_ids) }
   end
 end
diff --git a/app/models/status.rb b/app/models/status.rb
index 8bdb5e8db..2fe9f2de0 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -66,6 +66,7 @@ class Status < ApplicationRecord
   has_many :reblogged_by_accounts, through: :reblogs, class_name: 'Account', source: :account
   has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
   has_many :mentions, dependent: :destroy, inverse_of: :status
+  has_many :mentioned_accounts, through: :mentions, source: :account, class_name: 'Account'
   has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status
   has_many :media_attachments, dependent: :nullify
 
@@ -141,11 +142,11 @@ class Status < ApplicationRecord
     ids << account_id if local?
 
     if preloaded.nil?
-      ids += mentions.where(account: Account.local, silent: false).pluck(:account_id)
-      ids += favourites.where(account: Account.local).pluck(:account_id)
-      ids += reblogs.where(account: Account.local).pluck(:account_id)
-      ids += bookmarks.where(account: Account.local).pluck(:account_id)
-      ids += poll.votes.where(account: Account.local).pluck(:account_id) if poll.present?
+      ids += mentions.joins(:account).merge(Account.local).active.pluck(:account_id)
+      ids += favourites.joins(:account).merge(Account.local).pluck(:account_id)
+      ids += reblogs.joins(:account).merge(Account.local).pluck(:account_id)
+      ids += bookmarks.joins(:account).merge(Account.local).pluck(:account_id)
+      ids += poll.votes.joins(:account).merge(Account.local).pluck(:account_id) if poll.present?
     else
       ids += preloaded.mentions[id] || []
       ids += preloaded.favourites[id] || []
@@ -527,8 +528,8 @@ class Status < ApplicationRecord
   def unlink_from_conversations
     return unless direct_visibility?
 
-    mentioned_accounts = (association(:mentions).loaded? ? mentions : mentions.includes(:account)).map(&:account)
-    inbox_owners       = mentioned_accounts.select(&:local?) + (account.local? ? [account] : [])
+    inbox_owners = mentioned_accounts.local
+    inbox_owners += [account] if account.local?
 
     inbox_owners.each do |inbox_owner|
       AccountConversation.remove_status(inbox_owner, self)