From acecfeb37f7fc1008ee6270029cb23866b07dbea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 6 Nov 2015 19:19:13 +0100 Subject: [PATCH] Add 'staged' boolean to User --- app/jobs/regular/user_email.rb | 3 +++ app/jobs/scheduled/enqueue_digest_emails.rb | 2 +- db/migrate/20151105181635_add_staged_to_user.rb | 5 +++++ spec/jobs/enqueue_digest_emails_spec.rb | 8 ++++++++ spec/jobs/user_email_spec.rb | 6 ++++++ 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20151105181635_add_staged_to_user.rb diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rb index 0ef4811ea12..da5703e360c 100644 --- a/app/jobs/regular/user_email.rb +++ b/app/jobs/regular/user_email.rb @@ -19,6 +19,9 @@ module Jobs return skip(I18n.t("email_log.anonymous_user")) if @user.anonymous? return skip(I18n.t("email_log.suspended_not_pm")) if @user.suspended? && args[:type] != :user_private_message + # ensure we *never* send a digest to a staged user + return if @user.staged && args[:type] == :digest + seen_recently = (@user.last_seen_at.present? && @user.last_seen_at > SiteSetting.email_time_window_mins.minutes.ago) seen_recently = false if @user.email_always diff --git a/app/jobs/scheduled/enqueue_digest_emails.rb b/app/jobs/scheduled/enqueue_digest_emails.rb index 0e373a1496f..c38f5053dbd 100644 --- a/app/jobs/scheduled/enqueue_digest_emails.rb +++ b/app/jobs/scheduled/enqueue_digest_emails.rb @@ -15,7 +15,7 @@ module Jobs def target_user_ids # Users who want to receive emails and haven't been emailed in the last day query = User.real - .where(email_digests: true, active: true) + .where(email_digests: true, active: true, staged: false) .not_suspended .where("COALESCE(last_emailed_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 DAY'::INTERVAL * digest_after_days)") .where("(COALESCE(last_seen_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 DAY'::INTERVAL * digest_after_days)) AND diff --git a/db/migrate/20151105181635_add_staged_to_user.rb b/db/migrate/20151105181635_add_staged_to_user.rb new file mode 100644 index 00000000000..16d7984a7a8 --- /dev/null +++ b/db/migrate/20151105181635_add_staged_to_user.rb @@ -0,0 +1,5 @@ +class AddStagedToUser < ActiveRecord::Migration + def change + add_column :users, :staged, :boolean, null: false, default: false + end +end diff --git a/spec/jobs/enqueue_digest_emails_spec.rb b/spec/jobs/enqueue_digest_emails_spec.rb index d3f6b354d92..a6a4d77f406 100644 --- a/spec/jobs/enqueue_digest_emails_spec.rb +++ b/spec/jobs/enqueue_digest_emails_spec.rb @@ -32,6 +32,14 @@ describe Jobs::EnqueueDigestEmails do And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) } end + context 'staged users' do + let!(:staged_user) { Fabricate(:active_user, staged: true, last_emailed_at: 1.year.ago, last_seen_at: 1.year.ago) } + + it "doesn't return staged users" do + expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(staged_user.id)).to eq(false) + end + end + context 'recently emailed' do let!(:user_emailed_recently) { Fabricate(:active_user, last_emailed_at: 6.days.ago) } diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rb index d9ac680445e..a06db067a7f 100644 --- a/spec/jobs/user_email_spec.rb +++ b/spec/jobs/user_email_spec.rb @@ -8,6 +8,7 @@ describe Jobs::UserEmail do end let(:user) { Fabricate(:user, last_seen_at: 11.minutes.ago ) } + let(:staged) { Fabricate(:user, staged: true, last_seen_at: 11.minutes.ago ) } let(:suspended) { Fabricate(:user, last_seen_at: 10.minutes.ago, suspended_at: 5.minutes.ago, suspended_till: 7.days.from_now ) } let(:anonymous) { Fabricate(:anonymous, last_seen_at: 11.minutes.ago ) } let(:mailer) { Mail::Message.new(to: user.email) } @@ -29,6 +30,11 @@ describe Jobs::UserEmail do Jobs::UserEmail.new.execute(type: :digest, user_id: 1234) end + it "doesn't call the mailer when the user is staged" do + UserNotifications.expects(:digest).never + Jobs::UserEmail.new.execute(type: :digest, user_id: staged.id) + end + context 'to_address' do it 'overwrites a to_address when present' do