From e3fa2bb924838996198645eb0aa720debffa1919 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Tue, 19 May 2026 12:28:46 +0100 Subject: [PATCH 1/3] Include post author in comment pings --- app/controllers/comments_controller.rb | 2 -- test/controllers/comments/create_test.rb | 32 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index bb22c7796..e0364cceb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -477,8 +477,6 @@ def apply_pings(pings) user = User.where(id: p).first next if user.nil? - next if user.same_as?(@comment.post.user) - title = @post.parent.nil? ? @post.title : @post.parent.title user.create_notification("You were mentioned in a comment in the thread '#{@comment_thread.title}' " \ "on the post '#{title}'", diff --git a/test/controllers/comments/create_test.rb b/test/controllers/comments/create_test.rb index ca738e1a0..89594e7a9 100644 --- a/test/controllers/comments/create_test.rb +++ b/test/controllers/comments/create_test.rb @@ -26,6 +26,23 @@ class CommentsControllerTest < ActionController::TestCase assert assigns(:comment_thread).followed_by?(users(:editor)), 'Follower record not created for thread author' end + test 'should correctly notify post author of created thread pinging post author' do + sign_in users(:editor) + + before_author_notifs = users(:standard_user).notifications.count + + try_create_thread(posts(:question_one), mentions: [users(:standard_user)]) + + assert_response(:found) + assert_redirected_to post_path(assigns(:post)) + assert_not_nil assigns(:post) + assert_not_nil assigns(:comment)&.id + assert_not_nil assigns(:comment_thread)&.id + assert_nil flash[:danger] + assert_equal before_author_notifs + 1, users(:standard_user).notifications.count, + 'Author notification not created when it should have been' + end + test 'should correctly default thread title if not provided' do sign_in users(:editor) @@ -119,6 +136,21 @@ class CommentsControllerTest < ActionController::TestCase assert assigns(:comment_thread).followed_by?(users(:editor)), 'Follower record not created for comment author' end + test 'should correctly notify post author of comment pinging post author in thread' do + sign_in users(:editor) + before_author_notifs = users(:standard_user).notifications.count + + try_create_comment(comment_threads(:normal), mentions: [users(:standard_user)]) + + assert_response(:found) + assert_redirected_to comment_thread_path(assigns(:comment_thread)) + assert_not_nil assigns(:post) + assert_not_nil assigns(:comment_thread) + assert_not_nil assigns(:comment)&.id + assert_equal before_author_notifs + 1, users(:standard_user).notifications.count, + 'Post author notification not created when it should have been' + end + test 'should correctly redirect depending on the inline parameter' do thread = comment_threads(:normal) editor = users(:editor) From 1fbe995d9b25a9246c5658516d8d4defbdc6b89e Mon Sep 17 00:00:00 2001 From: trichoplax Date: Tue, 19 May 2026 17:09:03 +0100 Subject: [PATCH 2/3] Prevent double notification on new thread for new thread followers --- app/controllers/comments_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e0364cceb..8e091001c 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -58,7 +58,7 @@ def create_thread notification = "New comment thread on #{@comment.root.title}: #{@comment_thread.title}" NewThreadFollower.where(post: @post).each do |ntf| - unless ntf.user.same_as?(current_user) + unless ntf.user.same_as?(current_user) || pings.include?(ntf.user_id) ntf.user.create_notification(notification, helpers.comment_link(@comment)) end ThreadFollower.create(user: ntf.user, comment_thread: @comment_thread) From 793c54aa64f97bb7af9304b75a4bd24e68e26128 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Tue, 19 May 2026 17:39:27 +0100 Subject: [PATCH 3/3] Avoid unnecessary calculation of post title for new comment --- app/controllers/comments_controller.rb | 3 +-- test/controllers/comments/create_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 8e091001c..c39e5887e 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -92,9 +92,8 @@ def create .where('link LIKE ?', "#{thread_url}%") next if existing_notification.exists? - title = @post.parent.nil? ? @post.title : @post.parent.title follower.user.create_notification("There are new comments in a followed thread '#{@comment_thread.title}' " \ - "on the post '#{title}'", + "on the post '#{@comment.root.title}'", helpers.comment_link(@comment)) end else diff --git a/test/controllers/comments/create_test.rb b/test/controllers/comments/create_test.rb index 89594e7a9..52e413356 100644 --- a/test/controllers/comments/create_test.rb +++ b/test/controllers/comments/create_test.rb @@ -24,6 +24,9 @@ class CommentsControllerTest < ActionController::TestCase assert_equal before_uninvolved_notifs, users(:moderator).notifications.count, 'Uninvolved notification created when it should not have been' assert assigns(:comment_thread).followed_by?(users(:editor)), 'Follower record not created for thread author' + assert_equal users(:standard_user).notifications.last.content, + 'New comment thread on Q1 - This is test question number one: sample thread title', + 'Post author notification of new thread has incorrect wording' end test 'should correctly notify post author of created thread pinging post author' do @@ -41,6 +44,9 @@ class CommentsControllerTest < ActionController::TestCase assert_nil flash[:danger] assert_equal before_author_notifs + 1, users(:standard_user).notifications.count, 'Author notification not created when it should have been' + assert_equal users(:standard_user).notifications.last.content, + "You were mentioned in a comment in the thread 'sample thread title' on the post 'Q1 - This is test question number one'", + 'Post author notification of mention in new thread has incorrect wording' end test 'should correctly default thread title if not provided' do @@ -134,6 +140,9 @@ class CommentsControllerTest < ActionController::TestCase assert_equal before_uninvolved_notifs, users(:moderator).notifications.count, 'Uninvolved notification created when it should not have been' assert assigns(:comment_thread).followed_by?(users(:editor)), 'Follower record not created for comment author' + assert_equal users(:standard_user).notifications.last.content, + "There are new comments in a followed thread 'sample' on the post 'Q1 - This is test question number one'", + 'Post author notification of new comment has incorrect wording' end test 'should correctly notify post author of comment pinging post author in thread' do @@ -149,6 +158,9 @@ class CommentsControllerTest < ActionController::TestCase assert_not_nil assigns(:comment)&.id assert_equal before_author_notifs + 1, users(:standard_user).notifications.count, 'Post author notification not created when it should have been' + assert_equal users(:standard_user).notifications.last.content, + "You were mentioned in a comment in the thread 'sample' on the post 'Q1 - This is test question number one'", + 'Post author notification of mention in comment has incorrect wording' end test 'should correctly redirect depending on the inline parameter' do