Modify email digest and create rake task (#151)
This commit is contained in:
parent
9d32af7b6c
commit
99fd30fa25
10 changed files with 54 additions and 14 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 %>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
6
spec/mailers/previews/digest_mailer_preview.rb
Normal file
6
spec/mailers/previews/digest_mailer_preview.rb
Normal 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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue