about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--app/models/media_attachment.rb1
-rw-r--r--app/models/user.rb2
-rw-r--r--app/workers/scheduler/feed_cleanup_scheduler.rb2
-rw-r--r--app/workers/scheduler/media_cleanup_scheduler.rb2
-rw-r--r--spec/workers/scheduler/feed_cleanup_scheduler_spec.rb19
-rw-r--r--spec/workers/scheduler/media_cleanup_scheduler_spec.rb15
6 files changed, 39 insertions, 2 deletions
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index c29a66eb2..cf73c22b8 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -55,6 +55,7 @@ class MediaAttachment < ApplicationRecord
   validates :account, presence: true
 
   scope :attached, -> { where.not(status_id: nil) }
+  scope :unattached, -> { where(status_id: nil) }
   scope :local, -> { where(remote_url: '') }
   default_scope { order(id: :asc) }
 
diff --git a/app/models/user.rb b/app/models/user.rb
index 2ad68f9ae..d0732ed59 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -35,6 +35,7 @@
 
 class User < ApplicationRecord
   include Settings::Extend
+  ACTIVE_DURATION = 14.days
 
   devise :registerable, :recoverable,
          :rememberable, :trackable, :validatable, :confirmable,
@@ -51,6 +52,7 @@ class User < ApplicationRecord
   scope :recent,    -> { order(id: :desc) }
   scope :admins,    -> { where(admin: true) }
   scope :confirmed, -> { where.not(confirmed_at: nil) }
+  scope :inactive, -> { where(arel_table[:current_sign_in_at].lt(ACTIVE_DURATION.ago)) }
 
   before_validation :sanitize_languages
 
diff --git a/app/workers/scheduler/feed_cleanup_scheduler.rb b/app/workers/scheduler/feed_cleanup_scheduler.rb
index c7df363a4..402eed7c6 100644
--- a/app/workers/scheduler/feed_cleanup_scheduler.rb
+++ b/app/workers/scheduler/feed_cleanup_scheduler.rb
@@ -17,7 +17,7 @@ class Scheduler::FeedCleanupScheduler
   private
 
   def inactive_users
-    User.confirmed.where('current_sign_in_at < ?', 14.days.ago)
+    User.confirmed.inactive
   end
 
   def redis
diff --git a/app/workers/scheduler/media_cleanup_scheduler.rb b/app/workers/scheduler/media_cleanup_scheduler.rb
index 885ee9afc..a95f512be 100644
--- a/app/workers/scheduler/media_cleanup_scheduler.rb
+++ b/app/workers/scheduler/media_cleanup_scheduler.rb
@@ -12,6 +12,6 @@ class Scheduler::MediaCleanupScheduler
   private
 
   def unattached_media
-    MediaAttachment.reorder(nil).where(status_id: nil).where('created_at < ?', 1.day.ago)
+    MediaAttachment.reorder(nil).unattached.where('created_at < ?', 1.day.ago)
   end
 end
diff --git a/spec/workers/scheduler/feed_cleanup_scheduler_spec.rb b/spec/workers/scheduler/feed_cleanup_scheduler_spec.rb
new file mode 100644
index 000000000..4c709a2c9
--- /dev/null
+++ b/spec/workers/scheduler/feed_cleanup_scheduler_spec.rb
@@ -0,0 +1,19 @@
+require 'rails_helper'
+
+describe Scheduler::FeedCleanupScheduler do
+  subject { described_class.new }
+
+  let!(:active_user) { Fabricate(:user, current_sign_in_at: 2.days.ago) }
+  let!(:inactive_user) { Fabricate(:user, current_sign_in_at: 22.days.ago) }
+
+  it 'clears feeds of inactives' do
+    expect_any_instance_of(Redis).to receive(:del).with(feed_key_for(inactive_user))
+    expect_any_instance_of(Redis).not_to receive(:del).with(feed_key_for(active_user))
+
+    subject.perform
+  end
+
+  def feed_key_for(user)
+    FeedManager.instance.key(:home, user.account_id)
+  end
+end
diff --git a/spec/workers/scheduler/media_cleanup_scheduler_spec.rb b/spec/workers/scheduler/media_cleanup_scheduler_spec.rb
new file mode 100644
index 000000000..8a0da67e1
--- /dev/null
+++ b/spec/workers/scheduler/media_cleanup_scheduler_spec.rb
@@ -0,0 +1,15 @@
+require 'rails_helper'
+
+describe Scheduler::MediaCleanupScheduler do
+  subject { described_class.new }
+
+  let!(:old_media) { Fabricate(:media_attachment, account_id: nil, created_at: 10.days.ago) }
+  let!(:new_media) { Fabricate(:media_attachment, account_id: nil, created_at: 1.hour.ago) }
+
+  it 'removes old media records' do
+    subject.perform
+
+    expect { old_media.reload }.to raise_error(ActiveRecord::RecordNotFound)
+    expect(new_media.reload).to be_persisted
+  end
+end