diff --git a/app/labor/email_digest.rb b/app/labor/email_digest.rb index 16232123a..c06af3da3 100644 --- a/app/labor/email_digest.rb +++ b/app/labor/email_digest.rb @@ -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 diff --git a/app/labor/email_logic.rb b/app/labor/email_logic.rb index e7883c10e..7bf1b8a3a 100644 --- a/app/labor/email_logic.rb +++ b/app/labor/email_logic.rb @@ -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 diff --git a/app/mailers/digest_mailer.rb b/app/mailers/digest_mailer.rb index 4c410cc20..97306f16b 100644 --- a/app/mailers/digest_mailer.rb +++ b/app/mailers/digest_mailer.rb @@ -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 diff --git a/app/views/layouts/_mailer_content.html.erb b/app/views/layouts/_mailer_content.html.erb index cb5ae8752..0565e3bbe 100644 --- a/app/views/layouts/_mailer_content.html.erb +++ b/app/views/layouts/_mailer_content.html.erb @@ -178,4 +178,18 @@
PBJ

+<% elsif @digest_email %> +

+ Your Personal DEV Digest +

+

Recent posts you might find valuable based on who you follow ❤️

+ <% @articles.each do |article| %> + + <% end %> + +
Thanks for being a valued member of the community. 👩‍💻👨‍💻
<% end %> diff --git a/config/sample_application.yml b/config/sample_application.yml index 8445683c3..81cb4fa06 100644 --- a/config/sample_application.yml +++ b/config/sample_application.yml @@ -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 \ No newline at end of file diff --git a/lib/tasks/fetch.rake b/lib/tasks/fetch.rake index 637e2acfd..cf40ef94b 100644 --- a/lib/tasks/fetch.rake +++ b/lib/tasks/fetch.rake @@ -60,4 +60,8 @@ end task :github_repo_fetch_all => :environment do GithubRepo.update_to_latest -end \ No newline at end of file +end + +task :send_email_digest => :environment do + EmailDigest.send_periodic_digest_email +end diff --git a/spec/labor/email_digest_spec.rb b/spec/labor/email_digest_spec.rb index 1bbe3ed62..90c8fdac1 100644 --- a/spec/labor/email_digest_spec.rb +++ b/spec/labor/email_digest_spec.rb @@ -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 diff --git a/spec/labor/email_logic_spec.rb b/spec/labor/email_logic_spec.rb index 448168702..4de7ab425 100644 --- a/spec/labor/email_logic_spec.rb +++ b/spec/labor/email_logic_spec.rb @@ -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 diff --git a/spec/mailers/previews/digest_mailer_preview.rb b/spec/mailers/previews/digest_mailer_preview.rb new file mode 100644 index 000000000..f48215f73 --- /dev/null +++ b/spec/mailers/previews/digest_mailer_preview.rb @@ -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 diff --git a/spec/mailers/previews/notify_mailer_preview.rb b/spec/mailers/previews/notify_mailer_preview.rb index 6af59bbb6..a356ab85f 100644 --- a/spec/mailers/previews/notify_mailer_preview.rb +++ b/spec/mailers/previews/notify_mailer_preview.rb @@ -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