Only send digests to active users

This commit is contained in:
Robin Ward 2013-07-11 18:47:06 -04:00
parent 6c3c77df66
commit 89152116c6
5 changed files with 28 additions and 9 deletions

View File

@ -12,7 +12,7 @@ module Jobs
def target_users
# Users who want to receive emails and haven't been emailed int he last day
query = User.select(:id)
.where(email_digests: true)
.where(email_digests: true, active: true)
.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)")

View File

@ -7,7 +7,7 @@ describe Jobs::EnqueueDigestEmails do
describe '#target_users' do
context 'disabled digests' do
let!(:user_no_digests) { Fabricate(:user, email_digests: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
let!(:user_no_digests) { Fabricate(:active_user, email_digests: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
it "doesn't return users with email disabled" do
Jobs::EnqueueDigestEmails.new.target_users.include?(user_no_digests).should be_false
@ -15,7 +15,7 @@ describe Jobs::EnqueueDigestEmails do
end
context 'unapproved users' do
Given!(:unapproved_user) { Fabricate(:user, approved: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
Given!(:unapproved_user) { Fabricate(:active_user, approved: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
When { SiteSetting.stubs(:must_approve_users?).returns(true) }
Then { expect(Jobs::EnqueueDigestEmails.new.target_users.include?(unapproved_user)).to eq(false) }
@ -33,15 +33,24 @@ describe Jobs::EnqueueDigestEmails do
end
context 'recently emailed' do
let!(:user_emailed_recently) { Fabricate(:user, last_emailed_at: 6.days.ago) }
let!(:user_emailed_recently) { Fabricate(:active_user, last_emailed_at: 6.days.ago) }
it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_users.include?(user_emailed_recently).should be_false
end
end
context "inactive user" do
let!(:inactive_user) { Fabricate(:user) }
it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_users.include?(inactive_user).should be_false
end
end
context 'visited the site today' do
let!(:user_visited_today) { Fabricate(:user, last_seen_at: 6.days.ago) }
let!(:user_visited_today) { Fabricate(:active_user, last_seen_at: 6.days.ago) }
it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_users.include?(user_visited_today).should be_false
@ -50,7 +59,7 @@ describe Jobs::EnqueueDigestEmails do
context 'regular users' do
let!(:user) { Fabricate(:user) }
let!(:user) { Fabricate(:active_user) }
it "returns the user" do
Jobs::EnqueueDigestEmails.new.target_users.should == [user]

View File

@ -48,3 +48,13 @@ Fabricator(:newuser, from: :user) do
email 'newbie@new.com'
trust_level TrustLevel.levels[:newuser]
end
Fabricator(:active_user, from: :user) do
name 'Luke Skywalker'
username { sequence(:username) { |i| "luke#{i}" } }
email { sequence(:email) { |i| "luke#{i}@skywalker.com" } }
password 'myawesomepassword'
trust_level TrustLevel.levels[:basic]
active true
bio_raw "Don't as me about my dad!"
end

View File

@ -8,7 +8,7 @@ describe EmailToken do
context '#create' do
let(:user) { Fabricate(:user) }
let(:user) { Fabricate(:user, active: false) }
let!(:original_token) { user.email_tokens.first }
let!(:email_token) { user.email_tokens.create(email: 'bubblegum@adevnturetime.ooo') }
@ -42,7 +42,7 @@ describe EmailToken do
context '#confirm' do
let(:user) { Fabricate(:user) }
let(:user) { Fabricate(:user, active: false) }
let(:email_token) { user.email_tokens.first }
it 'returns nil with a nil token' do

View File

@ -567,7 +567,7 @@ describe User do
describe 'passwords' do
before do
@user = Fabricate.build(:user)
@user = Fabricate.build(:user, active: false)
@user.password = "ilovepasta"
@user.save!
end