From 8f94223d5a6fd105bd4879fabac5b074ae448b31 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Thu, 26 Mar 2026 17:22:20 +0000 Subject: [PATCH] Use Noticed to deliver notifications of direct messages --- app/controllers/api/messages_controller.rb | 2 +- app/controllers/messages_controller.rb | 2 +- app/mailers/user_mailer.rb | 8 ++++---- app/notifiers/direct_message_notifier.rb | 12 ++++++++++++ test/mailers/previews/user_mailer_preview.rb | 2 +- test/mailers/user_mailer_test.rb | 7 ++++--- 6 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 app/notifiers/direct_message_notifier.rb diff --git a/app/controllers/api/messages_controller.rb b/app/controllers/api/messages_controller.rb index ea5bd7ed1..ab97fb35c 100644 --- a/app/controllers/api/messages_controller.rb +++ b/app/controllers/api/messages_controller.rb @@ -52,7 +52,7 @@ module Api :body_format => "markdown") @message.save! - UserMailer.with(:message => @message).message_notification.deliver_later if @message.notify_recipient? + DirectMessageNotifier.with(:record => @message).deliver_later if @message.notify_recipient? # Return a copy of the new message respond_to do |format| diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 140f48390..2db2a9960 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -50,7 +50,7 @@ class MessagesController < ApplicationController render :action => "new" elsif @message.save flash[:notice] = t ".message_sent" - UserMailer.with(:message => @message).message_notification.deliver_later if @message.notify_recipient? + DirectMessageNotifier.with(:record => @message).deliver_later if @message.notify_recipient? redirect_to messages_outbox_path else @title = t "messages.new.title" diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index f2e27c3fa..0d33a14ad 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -85,10 +85,10 @@ class UserMailer < ApplicationMailer end def message_notification - message = params.fetch(:message) + message, recipient = params.fetch_values(:record, :recipient) - with_recipient_locale message.recipient do - @to_user = message.recipient.display_name + with_recipient_locale recipient do + @to_user = recipient.display_name @from_user = message.sender.display_name @text = message.body @title = message.title @@ -99,7 +99,7 @@ class UserMailer < ApplicationMailer attach_user_avatar(message.sender) mail :from => from_address(message.sender.display_name, "m", message.id, message.notification_token), - :to => message.recipient.email, + :to => recipient.email, :subject => t(".subject", :message_title => message.title) end end diff --git a/app/notifiers/direct_message_notifier.rb b/app/notifiers/direct_message_notifier.rb new file mode 100644 index 000000000..781419564 --- /dev/null +++ b/app/notifiers/direct_message_notifier.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class DirectMessageNotifier < ApplicationNotifier + recipients -> { record.recipient } + + validates :record, :presence => true + + deliver_by :email do |config| + config.mailer = "UserMailer" + config.method = "message_notification" + end +end diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb index c26350864..baf21c3ee 100644 --- a/test/mailers/previews/user_mailer_preview.rb +++ b/test/mailers/previews/user_mailer_preview.rb @@ -56,7 +56,7 @@ class UserMailerPreview < ActionMailer::Preview def message_notification recipient = create(:user, :languages => [I18n.locale]) message = create(:message, :recipient => recipient) - UserMailer.with(:message => message).message_notification + UserMailer.with(:record => message, :recipient => recipient).message_notification end def diary_comment_notification diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb index 29865f1b5..57aa22795 100644 --- a/test/mailers/user_mailer_test.rb +++ b/test/mailers/user_mailer_test.rb @@ -88,9 +88,10 @@ class UserMailerTest < ActionMailer::TestCase end def test_message_notification - user = create(:user, :display_name => "Jack & Jill
") - message = create(:message, :sender => user) - email = UserMailer.with(:message => message).message_notification + sender = create(:user, :display_name => "Jack & Jill
") + recipient = create(:user) + message = create(:message, :sender => sender, :recipient => recipient) + email = UserMailer.with(:record => message, :recipient => recipient).message_notification assert_match("Jack & Jill
", email.text_part.body.to_s) assert_match("Jack & Jill <br>", email.html_part.body.to_s) -- 2.47.3