From b08bd00ae37365673644ed03d9c48b473c866d1e Mon Sep 17 00:00:00 2001 From: tooyosi Date: Tue, 2 Apr 2024 12:52:43 +0100 Subject: [PATCH 1/4] prevent notification emails from being sent to user's with invalid email --- app/workers/notification_email_worker.rb | 3 +++ .../20240402094638_add_valid_email_to_users.rb | 13 +++++++++++++ spec/workers/notification_email_worker_spec.rb | 10 ++++++++++ 3 files changed, 26 insertions(+) create mode 100644 db/migrate/20240402094638_add_valid_email_to_users.rb diff --git a/app/workers/notification_email_worker.rb b/app/workers/notification_email_worker.rb index b621be37..0a472bbd 100644 --- a/app/workers/notification_email_worker.rb +++ b/app/workers/notification_email_worker.rb @@ -14,6 +14,9 @@ class NotificationEmailWorker def perform(user_id, digest) user = ::User.find user_id + + return unless user.valid_email + digest_number = SubscriptionPreference.email_digests[digest] # Ensure there are categories delivered at this frequency diff --git a/db/migrate/20240402094638_add_valid_email_to_users.rb b/db/migrate/20240402094638_add_valid_email_to_users.rb new file mode 100644 index 00000000..c5a3c936 --- /dev/null +++ b/db/migrate/20240402094638_add_valid_email_to_users.rb @@ -0,0 +1,13 @@ +class AddValidEmailToUsers < ActiveRecord::Migration + def up + execute <<-SQL + ALTER FOREIGN TABLE users ADD COLUMN valid_email boolean DEFAULT true; + SQL + end + + def down + execute <<-SQL + ALTER FOREIGN TABLE users DROP COLUMN IF EXISTS valid_email; + SQL + end +end diff --git a/spec/workers/notification_email_worker_spec.rb b/spec/workers/notification_email_worker_spec.rb index fad97c80..9e3a050b 100644 --- a/spec/workers/notification_email_worker_spec.rb +++ b/spec/workers/notification_email_worker_spec.rb @@ -71,5 +71,15 @@ worker.perform user.id, :daily end end + + context 'when user email is invalid' do + it 'should not attempt to deliver email' do + user.update(valid_email: false) + mail = double deliver_now!: true + expect(NotificationMailer).not_to receive(:notify) + expect(mail).not_to receive :deliver_now! + worker.perform user.id, :daily + end + end end end From 296f39fe2eb4f4fb65465ca44afe7330f468ffd5 Mon Sep 17 00:00:00 2001 From: tooyosi Date: Wed, 3 Apr 2024 10:52:25 +0100 Subject: [PATCH 2/4] run migrations locally --- db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index c150aee2..a37c3281 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20231107211623) do +ActiveRecord::Schema.define(version: 20240402094638) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" From 953328b79312590321f7029387f29a587d65c946 Mon Sep 17 00:00:00 2001 From: tooyosi Date: Wed, 3 Apr 2024 11:10:57 +0100 Subject: [PATCH 3/4] add valid_email to rake.rb --- lib/tasks/db.rake | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index a6905f44..1a9332b5 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -80,6 +80,7 @@ namespace :panoptes do credited_name varchar(255), admin bool, banned bool, + valid_email bool, confirmed_at timestamp(6) ) server panoptes; From fa6aad27c162bfa6e5c12b541bce89c22e819b28 Mon Sep 17 00:00:00 2001 From: tooyosi Date: Wed, 3 Apr 2024 11:16:14 +0100 Subject: [PATCH 4/4] add valid_email user factory --- spec/factories/users.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 1c1e47f0..8b8c48ca 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -7,6 +7,7 @@ admin false banned false created_at Time.now - 1.year + valid_email true confirmed_at Time.now - 364.days factory :moderator do