about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/models/user.rb4
-rw-r--r--app/workers/digest_mailer_worker.rb6
-rw-r--r--app/workers/scheduler/email_scheduler.rb24
3 files changed, 29 insertions, 5 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 9459db7fe..892a07bba 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -170,6 +170,10 @@ class User < ApplicationRecord
     settings.default_privacy || (account.locked? ? 'private' : 'public')
   end
 
+  def allows_digest_emails?
+    settings.notification_emails['digest']
+  end
+
   def token_for_app(a)
     return nil if a.nil? || a.owner != self
     Doorkeeper::AccessToken
diff --git a/app/workers/digest_mailer_worker.rb b/app/workers/digest_mailer_worker.rb
index 028db89a9..21f1c357a 100644
--- a/app/workers/digest_mailer_worker.rb
+++ b/app/workers/digest_mailer_worker.rb
@@ -9,7 +9,7 @@ class DigestMailerWorker
 
   def perform(user_id)
     @user = User.find(user_id)
-    deliver_digest if user_receives_digest?
+    deliver_digest if @user.allows_digest_emails?
   end
 
   private
@@ -18,8 +18,4 @@ class DigestMailerWorker
     NotificationMailer.digest(user.account).deliver_now!
     user.touch(:last_emailed_at)
   end
-
-  def user_receives_digest?
-    user.settings.notification_emails['digest']
-  end
 end
diff --git a/app/workers/scheduler/email_scheduler.rb b/app/workers/scheduler/email_scheduler.rb
new file mode 100644
index 000000000..24d0c0ebe
--- /dev/null
+++ b/app/workers/scheduler/email_scheduler.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+require 'sidekiq-scheduler'
+
+class Scheduler::EmailScheduler
+  include Sidekiq::Worker
+
+  def perform
+    eligible_users.find_each do |user|
+      next unless user.allows_digest_emails?
+      DigestMailerWorker.perform_async(user.id)
+    end
+  end
+
+  private
+
+  def eligible_users
+    User.confirmed
+        .joins(:account)
+        .where(accounts: { silenced: false, suspended: false })
+        .where(disabled: false)
+        .where('current_sign_in_at < ?', 20.days.ago)
+        .where('last_emailed_at IS NULL OR last_emailed_at < ?', 20.days.ago)
+  end
+end