Skip to content

Commit 204adaf

Browse files
committed
Added guard to help prevent double sending emails and restored the ability to display featured jobs in the Popular Protip mailer
1 parent 47743b3 commit 204adaf

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

app/mailers/mail_preview.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def popular_protips
55
from = 60.days.ago
66
to = 0.days.ago
77
user = User.with_username(USERNAME)
8+
REDIS.srem(ProtipMailer::CAMPAIGN_ID, user.id.to_s)
89
protips = ProtipMailer::Queries.popular_protips(from, to)
910
ProtipMailer.popular_protips(user, protips, from, to).deliver
1011
end

app/mailers/notifier_mailer.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class NothingToSendException < Exception
2626
NEW_APPLICANT_EVENT = 'new_applicant'
2727
INVOICE_EVENT = 'invoice'
2828

29-
ACTIVITY_SUBJECT_PREFIX = "[Coderwall]"
30-
3129
def welcome_email(username)
3230
headers['X-Mailgun-Variables'] = {email_type: WELCOME_EVENT}.to_json
3331

app/mailers/protip_mailer.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ class ProtipMailer < ActionMailer::Base
1717
protips_count: 'protips'
1818
}
1919
ACTIVITY_SUBJECT_PREFIX = '[Coderwall]'
20+
CAMPAIGN_ID = 'protip_mailer-popular_protips'
2021

2122
#################################################################################
2223
def popular_protips(user, protips, from, to)
23-
fail 'Protips are required.' if protips.nil? || protips.empty?
2424
fail 'User is required.' unless user
25+
# Skip if this user has already been sent and email for this campaign id.
26+
fail "Already sent email to #{user.id} please check Redis SET #{CAMPAIGN_ID}." unless REDIS.sadd(CAMPAIGN_ID, user.id.to_s)
27+
28+
fail 'Protips are required.' if protips.nil? || protips.empty?
2529
fail 'From date is required.' unless from
2630
fail 'To date is required.' unless to
2731

28-
headers['X-Mailgun-Campaign-Id'] = 'protip_mailer-popular_protips'
32+
headers['X-Mailgun-Campaign-Id'] = CAMPAIGN_ID
2933

3034
@user = user
3135
@protips = protips
@@ -44,7 +48,7 @@ def popular_protips(user, protips, from, to)
4448
end.first
4549
@most = nil if @most && (@most[@star_stat] <= 0)
4650

47-
mail(to: @user.email, subject: "#{ACTIVITY_SUBJECT_PREFIX} protips just for you, algorithmically picked w/:heart:")
51+
mail(to: @user.email, subject: "protips just for you, algorithmically picked w/:heart:")
4852
rescue Exception => ex
4953
abort_delivery(ex)
5054
end

app/views/protip_mailer/popular_protips.html.haml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
Coderwall is Open Source!
3434
%h3{style: "#{nopad} #{sans_serif} font-size: 16px; line-height: 22px; color: #48494E; font-weight: normal;"}
3535
Coderwall is an open product on Assembly. Every software product on Assembly is a collaborative effort including the vision, development, design, and marketing. Each month a product's revenue is collected & split amongst all involved. Want to help make Coderwall better?
36-
%a{href: 'https://assembly.com/coderwall', style: "#{nopad} color: #3d8dcc;"} Jump in and get started!
36+
%a{href: 'http://hackernoons.com/all-our-coderwall-are-belong-to-you', style: "#{nopad} color: #3d8dcc;"} Read the announcement
37+
or
38+
%a{href: 'https://assembly.com/coderwall', style: "#{nopad} color: #3d8dcc;"} jump in and get started!
3739

3840

3941
%table.outside{border: 0, cellpadding: 0, cellspacing: 0, style: "margin: 0 auto; padding: 0 40px 20px 40px; width: 600px; background: #fff;", width: 600}

0 commit comments

Comments
 (0)