From 1356228b05af05ef8b94e0a31f320a9fa11e6672 Mon Sep 17 00:00:00 2001 From: Nicolas Zermati Date: Sun, 30 May 2021 09:53:48 +0200 Subject: [PATCH] Throttle inactive user emails --- app/mailers/user_mailer.rb | 11 +++++--- app/models/user.rb | 1 + ...20_add_last_inactive_user_email_sent_at.rb | 5 ++++ db/schema.rb | 3 ++- spec/mailers/user_mailer_spec.rb | 27 +++++++++++++++++++ 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20210530071920_add_last_inactive_user_email_sent_at.rb create mode 100644 spec/mailers/user_mailer_spec.rb diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index c5b453ec9..fcbe645ca 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,13 +1,18 @@ class UserMailer < ApplicationMailer default from: "Covidliste " + 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 diff --git a/app/models/user.rb b/app/models/user.rb index 3d688b08e..75e4706a5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20210530071920_add_last_inactive_user_email_sent_at.rb b/db/migrate/20210530071920_add_last_inactive_user_email_sent_at.rb new file mode 100644 index 000000000..9e09688e5 --- /dev/null +++ b/db/migrate/20210530071920_add_last_inactive_user_email_sent_at.rb @@ -0,0 +1,5 @@ +class AddLastInactiveUserEmailSentAt < ActiveRecord::Migration[6.1] + def change + add_column :users, :last_inactive_user_email_sent_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index f90ea274e..6c5b8e1ae 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_05_29_122300) do +ActiveRecord::Schema.define(version: 2021_05_30_071920) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -297,6 +297,7 @@ t.string "email_domain" t.integer "grid_i" t.integer "grid_j" + t.datetime "last_inactive_user_email_sent_at" t.index ["anonymized_at"], name: "index_users_on_anonymized_at" t.index ["birthdate"], name: "index_users_on_birthdate" t.index ["city"], name: "index_users_on_city" diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb new file mode 100644 index 000000000..025423340 --- /dev/null +++ b/spec/mailers/user_mailer_spec.rb @@ -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(["inscription@covidliste.com"]) + 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