From 467f7b9720c86adb2f97b63f596346a3dadd113b Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Thu, 26 Mar 2026 15:56:02 +0000 Subject: [PATCH] Use Noticed to deliver a "new follower" notification --- app/controllers/follows_controller.rb | 2 +- app/mailers/user_mailer.rb | 6 +++--- app/notifiers/new_follower_notifier.rb | 12 ++++++++++++ test/mailers/previews/user_mailer_preview.rb | 2 +- test/mailers/user_mailer_test.rb | 2 +- 5 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 app/notifiers/new_follower_notifier.rb diff --git a/app/controllers/follows_controller.rb b/app/controllers/follows_controller.rb index 479c288d9..467b496a4 100644 --- a/app/controllers/follows_controller.rb +++ b/app/controllers/follows_controller.rb @@ -28,7 +28,7 @@ class FollowsController < ApplicationController flash[:error] = t ".limit_exceeded" elsif follow.save flash[:notice] = t ".success", :name => @user.display_name - UserMailer.with(:follow => follow).follow_notification.deliver_later + NewFollowerNotifier.with(:record => follow).deliver_later else follow.add_error(t(".failed", :name => @user.display_name)) end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 96d027af7..f2e27c3fa 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -137,16 +137,16 @@ class UserMailer < ApplicationMailer end def follow_notification - follow = params.fetch(:follow) + follow, recipient = params.fetch_values(:record, :recipient) - with_recipient_locale follow.following do + with_recipient_locale recipient do @follow = follow @viewurl = user_url(@follow.follower) @followurl = follow_url(@follow.follower) @author = @follow.follower.display_name attach_user_avatar(@follow.follower) - mail :to => follow.following.email, + mail :to => recipient.email, :subject => t(".subject", :user => follow.follower.display_name) end end diff --git a/app/notifiers/new_follower_notifier.rb b/app/notifiers/new_follower_notifier.rb new file mode 100644 index 000000000..1508e2064 --- /dev/null +++ b/app/notifiers/new_follower_notifier.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class NewFollowerNotifier < ApplicationNotifier + recipients -> { record.following } + + validates :record, :presence => true + + deliver_by :email do |config| + config.mailer = "UserMailer" + config.method = "follow_notification" + end +end diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb index bf4023e4f..c26350864 100644 --- a/test/mailers/previews/user_mailer_preview.rb +++ b/test/mailers/previews/user_mailer_preview.rb @@ -69,7 +69,7 @@ class UserMailerPreview < ActionMailer::Preview def follow_notification following = create(:user, :languages => [I18n.locale]) follow = create(:follow, :following => following) - UserMailer.with(:follow => follow).follow_notification + UserMailer.with(:record => follow, :recipient => following).follow_notification end def note_comment_notification diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb index 15e64f935..29865f1b5 100644 --- a/test/mailers/user_mailer_test.rb +++ b/test/mailers/user_mailer_test.rb @@ -114,7 +114,7 @@ class UserMailerTest < ActionMailer::TestCase def test_follow_notification follow = create(:follow) - email = UserMailer.with(:follow => follow).follow_notification + email = UserMailer.with(:record => follow, :recipient => follow.following).follow_notification follower_profile_url = url_helpers.user_url(follow.follower) follow_follower_url = url_helpers.follow_url(follow.follower) -- 2.39.5