From eedf222d4eec21c2050be43c725b875351ebca61 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 24 Mar 2026 18:45:44 +0000 Subject: [PATCH] Deliver note comment notifications using Noticed --- app/controllers/api/notes_controller.rb | 6 +----- app/mailers/user_mailer.rb | 2 +- app/models/note.rb | 4 ++++ app/models/note_comment.rb | 4 ++++ app/notifiers/note_comment_notifier.rb | 12 ++++++++++++ test/mailers/previews/user_mailer_preview.rb | 2 +- test/mailers/user_mailer_test.rb | 2 +- test/models/note_comment_test.rb | 15 +++++++++++++++ test/models/note_test.rb | 12 ++++++++++++ 9 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 app/notifiers/note_comment_notifier.rb diff --git a/app/controllers/api/notes_controller.rb b/app/controllers/api/notes_controller.rb index d16b158c9..167bfbbd9 100644 --- a/app/controllers/api/notes_controller.rb +++ b/app/controllers/api/notes_controller.rb @@ -376,11 +376,7 @@ module Api comment = note.comments.create!(attributes) - if notify - note.subscribers.visible.each do |user| - UserMailer.with(:comment => comment, :recipient => user).note_comment_notification.deliver_later if current_user != user - end - end + NoteCommentNotifier.with(:record => comment).deliver_later if notify NoteSubscription.find_or_create_by(:note => note, :user => current_user) if current_user end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 65c703f8c..567cbe459 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -152,7 +152,7 @@ class UserMailer < ApplicationMailer end def note_comment_notification - comment, recipient = params.fetch_values(:comment, :recipient) + comment, recipient = params.fetch_values(:record, :recipient) with_recipient_locale recipient do @noteurl = note_url(comment.note) diff --git a/app/models/note.rb b/app/models/note.rb index c1b93a2e8..9cdadb045 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -100,6 +100,10 @@ class Note < ApplicationRecord RichText.new("text", super) end + def visible_subscribers + subscribers.visible + end + private # Fill in default values for new notes diff --git a/app/models/note_comment.rb b/app/models/note_comment.rb index 972d25de0..75557449b 100644 --- a/app/models/note_comment.rb +++ b/app/models/note_comment.rb @@ -42,4 +42,8 @@ class NoteComment < ApplicationRecord def body RichText.new("text", self[:body]) end + + def notifiable_subscribers + note.visible_subscribers.where.not(:id => author_id) + end end diff --git a/app/notifiers/note_comment_notifier.rb b/app/notifiers/note_comment_notifier.rb new file mode 100644 index 000000000..03cbb1794 --- /dev/null +++ b/app/notifiers/note_comment_notifier.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class NoteCommentNotifier < ApplicationNotifier + recipients -> { record.notifiable_subscribers } + + validates :record, :presence => true + + deliver_by :email do |config| + config.mailer = "UserMailer" + config.method = "note_comment_notification" + end +end diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb index 7db83337d..0fa9db335 100644 --- a/test/mailers/previews/user_mailer_preview.rb +++ b/test/mailers/previews/user_mailer_preview.rb @@ -76,7 +76,7 @@ class UserMailerPreview < ActionMailer::Preview recipient = create(:user, :languages => [I18n.locale]) commenter = create(:user) comment = create(:note_comment, :author => commenter) - UserMailer.with(:comment => comment, :recipient => recipient).note_comment_notification + UserMailer.with(:record => comment, :recipient => recipient).note_comment_notification end def changeset_comment_notification diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb index 8e69dc7d6..13152a0dc 100644 --- a/test/mailers/user_mailer_test.rb +++ b/test/mailers/user_mailer_test.rb @@ -129,7 +129,7 @@ class UserMailerTest < ActionMailer::TestCase commenter = create(:user) note = create(:note, :lat => 51.7632, :lon => -0.0076) comment = create(:note_comment, :author => commenter, :note => note) - email = UserMailer.with(:comment => comment, :recipient => recipient).note_comment_notification + email = UserMailer.with(:record => comment, :recipient => recipient).note_comment_notification html_body = Nominatim.stub :describe_location, "The End of the Rainbow" do parse_html_body(email) diff --git a/test/models/note_comment_test.rb b/test/models/note_comment_test.rb index 6cc216da2..bdd729f23 100644 --- a/test/models/note_comment_test.rb +++ b/test/models/note_comment_test.rb @@ -38,4 +38,19 @@ class NoteCommentTest < ActiveSupport::TestCase assert_not_predicate note_comment, :valid?, "#{body} is valid when it shouldn't be" end end + + def test_notifiable_subscribers + commenter1 = create(:user) + commenter2 = create(:user, :suspended) + commenter3 = create(:user) + commenter4 = create(:user) + note = create(:note) + create(:note_subscription, :note => note, :user => commenter1) + create(:note_subscription, :note => note, :user => commenter2) + create(:note_subscription, :note => note, :user => commenter3) + create(:note_subscription, :note => note, :user => commenter4) + comment = create(:note_comment, :note => note, :author => commenter4) + + assert_equal comment.notifiable_subscribers.sort, [commenter1, commenter3].sort + end end diff --git a/test/models/note_test.rb b/test/models/note_test.rb index bfe167464..c17a0ca63 100644 --- a/test/models/note_test.rb +++ b/test/models/note_test.rb @@ -83,4 +83,16 @@ class NoteTest < ActiveSupport::TestCase assert_equal "0.0000400", note.lat.to_s assert_equal "0.0000800", note.lon.to_s end + + def test_visible_subscribers + commenter1 = create(:user) + commenter2 = create(:user, :suspended) + commenter3 = create(:user) + note = create(:note) + create(:note_subscription, :note => note, :user => commenter1) + create(:note_subscription, :note => note, :user => commenter2) + create(:note_subscription, :note => note, :user => commenter3) + + assert_equal note.visible_subscribers.sort, [commenter1, commenter3].sort + end end -- 2.39.5