Modify email digest and create rake task (#151)

This commit is contained in:
Ben Halpern 2018-03-27 15:21:59 -04:00 committed by GitHub
parent 9d32af7b6c
commit 99fd30fa25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 54 additions and 14 deletions

View file

@ -17,7 +17,7 @@ class EmailDigest
user_email_heuristic = EmailLogic.new(user).analyze
next unless user_email_heuristic.should_receive_email?
articles = user_email_heuristic.articles_to_send
DigestMailer.daily_digest(user, articles).deliver
DigestMailer.digest_email(user, articles).deliver
end
end

View file

@ -58,7 +58,7 @@ class EmailLogic
end
def get_open_rate
past_sent_emails = @user.email_messages.where(mailer: "DigestMailer#daily_digest").limit(10)
past_sent_emails = @user.email_messages.where(mailer: "DigestMailer#digest_email").limit(10)
# Will stick with 50% open rate if @user has no/not-enough email digest history
return 0.5 if past_sent_emails.length < 10
@ -75,7 +75,7 @@ class EmailLogic
end
def get_last_digest_email_user_recieved
@user.email_messages.where(mailer: "DigestMailer#daily_digest").last&.sent_at
@user.email_messages.where(mailer: "DigestMailer#digest_email").last&.sent_at
end
def get_fresh_date

View file

@ -1,5 +1,14 @@
class DigestMailer < ApplicationMailer
def daily_digest(recipient)
@recipient = recipient
def digest_email(user, articles)
@user = if Rails.env.development?
User.first
else
user
end
@articles = articles.first(6)
@digest_email = true
mail(from: "yo@dev.to", to: @user.email, subject: "Emai!") do |format|
format.html { render "layouts/mailer" }
end
end
end

View file

@ -178,4 +178,18 @@
<br>
PBJ
</p>
<% elsif @digest_email %>
<h2>
Your Personal DEV Digest
</h2>
<h4>Recent posts you might find valuable based on who you follow ❤️</h4>
<% @articles.each do |article| %>
<ul>
<li>
<a href="https://dev.to<%= article.path %>" style="font-weight: bold;color:#0045ff"><%= article.title %></a> <%= truncate(article.description, length: 80) %>
</li>
</ul>
<% end %>
<center style="margin-top:50px;"><em>Thanks for being a valued member of the community.</em> 👩‍💻👨‍💻</center>
<% end %>

View file

@ -121,3 +121,6 @@ STREAM_URL: "REPLACEME"
STRIPE_PUBLISHABLE_KEY: "REPLACEME"
STRIPE_SECRET_KEY: "REPLACEME"
#Email digest frequency
PERIODIC_EMAIL_DIGEST_MAX: 10
PERIODIC_EMAIL_DIGEST_MIN: 2

View file

@ -60,4 +60,8 @@ end
task :github_repo_fetch_all => :environment do
GithubRepo.update_to_latest
end
end
task :send_email_digest => :environment do
EmailDigest.send_periodic_digest_email
end

View file

@ -11,7 +11,7 @@ RSpec.describe EmailDigest do
let(:mock_delegator) { instance_double("FakeDelegator") }
before do
allow(DigestMailer).to receive(:daily_digest) { mock_delegator }
allow(DigestMailer).to receive(:digest_email) { mock_delegator }
allow(mock_delegator).to receive(:deliver).and_return(true)
Delayed::Worker.delay_jobs = false
user
@ -21,14 +21,14 @@ RSpec.describe EmailDigest do
Delayed::Worker.delay_jobs = true
end
describe "::send_daily_digest" do
describe "::send_digest_email" do
context "when there's article to be sent" do
before { user.follow(author) }
it "send digest email when there's atleast 3 hot articles" do
3.times { create(:article, user_id: author.id, positive_reactions_count: 20) }
described_class.send_periodic_digest_email
expect(DigestMailer).to have_received(:daily_digest).with(
expect(DigestMailer).to have_received(:digest_email).with(
user, [instance_of(Article), instance_of(Article), instance_of(Article)]
)
end

View file

@ -16,13 +16,13 @@ RSpec.describe EmailLogic do
end
it "provides top 3 articles" do
3.times { create(:article, positive_reactions_count: 20) }
3.times { create(:article, positive_reactions_count: 40) }
h = described_class.new(user).analyze
expect(h.articles_to_send.length).to eq(3)
end
it "marks as not ready if there isn't atleast 3 articles" do
2.times { create(:article, positive_reactions_count: 20) }
2.times { create(:article, positive_reactions_count: 40) }
h = described_class.new(user).analyze
expect(h.should_receive_email?).to eq(false)
end
@ -34,7 +34,7 @@ RSpec.describe EmailLogic do
user.follow(author)
3.times { create(:article, user_id: author.id, positive_reactions_count: 20) }
10.times do
Ahoy::Message.create(mailer: "DigestMailer#daily_digest",
Ahoy::Message.create(mailer: "DigestMailer#digest_email",
user_id: user.id, sent_at: Time.now.utc)
end
end
@ -48,11 +48,11 @@ RSpec.describe EmailLogic do
context "when a user's open_percentage is high" do
before do
10.times do
Ahoy::Message.create(mailer: "DigestMailer#daily_digest", user_id: user.id,
Ahoy::Message.create(mailer: "DigestMailer#digest_email", user_id: user.id,
sent_at: Time.now.utc, opened_at: Time.now.utc)
author = create(:user)
user.follow(author)
3.times { create(:article, user_id: author.id, positive_reactions_count: 20) }
3.times { create(:article, user_id: author.id, positive_reactions_count: 40) }
end
end

View file

@ -0,0 +1,6 @@
# Preview all emails at http://localhost:3000/rails/mailers/notify_mailer
class DigestMailerPreview < ActionMailer::Preview
def digest_email
DigestMailer.digest_email(User.last, Article.all)
end
end

View file

@ -31,4 +31,8 @@ class NotifyMailerPreview < ActionMailer::Preview
def scholarship_awarded_email
NotifyMailer.scholarship_awarded_email(User.last)
end
def digest_email
NotifyMailer.digest_email(User.last, Article.all)
end
end