Skip to content

Commit

Permalink
Throttle inactive user emails
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoolas25 committed May 30, 2021
1 parent ed299ad commit 1356228
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
11 changes: 8 additions & 3 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
class UserMailer < ApplicationMailer
default from: "Covidliste <[email protected]>"

MIN_SEND_INTERVAL = 7.days

def send_inactive_user_unsubscription_request
user = User.find(params[:user_id])
@user = User.find(params[:user_id])

return if @user.email.blank?
return if @user.last_inactive_user_email_sent_at && @user.last_inactive_user_email_sent_at >= MIN_SEND_INTERVAL.ago

return if user.email.blank?
@user.touch(:last_inactive_user_email_sent_at)

mail(
to: user.email,
to: @user.email,
subject: "Attendez-vous toujours une dose de vaccin ?"
)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def anonymize!
self.birthdate = nil
self.grid_i = nil
self.grid_j = nil
self.last_inactive_user_email_sent_at = nil
self.anonymized_at = Time.now.utc
save(validate: false)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLastInactiveUserEmailSentAt < ActiveRecord::Migration[6.1]
def change
add_column :users, :last_inactive_user_email_sent_at, :datetime
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "rails_helper"

RSpec.describe UserMailer, type: :mailer do
describe "#send_inactive_user_unsubscription_request" do
let(:mail) { described_class.with(user_id: user.id).send_inactive_user_unsubscription_request }
let(:user) { create(:user) }

it "renders the headers" do
expect(mail.subject).to eq("Attendez-vous toujours une dose de vaccin ?")
expect(mail.to).to eq([user.email])
expect(mail.from).to eq(["[email protected]"])
end

it "updates the last_inactive_user_email_sent_at" do
expect { mail.deliver_now }
.to change { user.reload.last_inactive_user_email_sent_at }
.from(nil)
.to(be_within(1.minute).of(Time.zone.now))
end

it "includes a signed link to the confirm_destroy_profile URL" do
match_data = mail.body.encoded.match(%r{/users/profile/confirm_destroy\?authentication_token=([^"]+)"})
token = CGI.unescape(match_data.captures.first)
expect(User.find_signed(token, purpose: "users.destroy")).to eq(user)
end
end
end

0 comments on commit 1356228

Please sign in to comment.