diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 99e3af8ac..906890818 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -25,7 +25,7 @@ class CommentsController < ApplicationController before_action :check_lock_thread_access, only: [:lock_thread] before_action :check_thread_access, only: [:thread, :thread_content, :thread_followers] before_action :check_unrestrict_access, only: [:thread_unrestrict] - before_action :check_if_target_post_locked, only: [:create, :post_follow] + before_action :check_if_target_post_locked, only: [:create, :create_thread] before_action :check_if_parent_post_locked, only: [:update, :destroy] before_action :verify_moderator, only: [:thread_followers] @@ -56,12 +56,9 @@ def create_thread if success notification = "New comment thread on #{@comment.root.title}: #{@comment_thread.title}" - unless @comment.post.user.same_as?(current_user) - @comment.post.user.create_notification(notification, helpers.comment_link(@comment)) - end NewThreadFollower.where(post: @post).each do |ntf| - unless ntf.user.same_as?(current_user) || ntf.user.same_as?(@comment.post.user) + unless ntf.user.same_as?(current_user) ntf.user.create_notification(notification, helpers.comment_link(@comment)) end ThreadFollower.create(user: ntf.user, comment_thread: @comment_thread) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index a7f653d15..8202bcaf7 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -110,6 +110,7 @@ def create if @post.save @post.update(last_activity: @post.created_at, last_activity_by: current_user) + NewThreadFollower.create user: @post.user, post: @post if @post_type.has_parent? unless @post.user_id == @post.parent.user_id @post.parent.user.create_notification("New response to your post #{@post.parent.title}", diff --git a/app/models/comment_thread.rb b/app/models/comment_thread.rb index 68d40af67..eb58e1c79 100644 --- a/app/models/comment_thread.rb +++ b/app/models/comment_thread.rb @@ -14,8 +14,6 @@ class CommentThread < ApplicationRecord validate :maximum_title_length validates :title, presence: { message: I18n.t('comments.errors.title_presence') } - after_create :create_follower - before_save :bump_last_activity # Gets threads appropriately scoped for a given user & post @@ -121,15 +119,4 @@ def remove_follower(user) ThreadFollower.where(comment_thread: self, user: user).destroy_all.any? end - - private - - # Comment author and post author are automatically followed to the thread. Question author is NOT - # automatically followed on new answer comment threads. Comment author follower creation is done - # on the Comment model. - def create_follower - if post.user.preference('auto_follow_comment_threads') == 'true' - ThreadFollower.create comment_thread: self, user: post.user - end - end end diff --git a/config/config/preferences.yml b/config/config/preferences.yml index c860403b0..240b901ba 100644 --- a/config/config/preferences.yml +++ b/config/config/preferences.yml @@ -71,7 +71,7 @@ display_import_labels: auto_follow_comment_threads: type: boolean description: > - Automatically follow any comment thread you participate in. + Automatically follow any comment thread you create or participate in. default: 'true' global: true diff --git a/db/migrate/20260208223211_follow_own_posts.rb b/db/migrate/20260208223211_follow_own_posts.rb new file mode 100644 index 000000000..98847d109 --- /dev/null +++ b/db/migrate/20260208223211_follow_own_posts.rb @@ -0,0 +1,10 @@ +class FollowOwnPosts < ActiveRecord::Migration[7.2] + def change + to_insert = Post.where.not(post_type_id: [PolicyDoc.post_type_id, HelpDoc.post_type_id]) + .where.not(user_id: nil) + .pluck(:id, :user_id) + .map { |post_id, user_id| { post_id: post_id, user_id: user_id } } + + NewThreadFollower.insert_all(to_insert) + end +end diff --git a/test/controllers/comments/post_follow_test.rb b/test/controllers/comments/post_follow_test.rb index bd2932b16..2479fc49b 100644 --- a/test/controllers/comments/post_follow_test.rb +++ b/test/controllers/comments/post_follow_test.rb @@ -49,4 +49,25 @@ class CommentsControllerTest < ActionController::TestCase # Assert user still only follows post once assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count end + + test 'post author can unfollow post then follow' do + user = users(:standard_user) + sign_in user + question = posts(:question_one) + + # Assert user follows post + assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count + + try_post_unfollow(question) + assert_response(:found) + + # Assert user does not follow post + assert_equal 0, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count + + try_post_follow(question) + assert_response(:found) + + # Assert user follows post + assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count + end end diff --git a/test/controllers/posts/create_test.rb b/test/controllers/posts/create_test.rb index 3f53dd5e3..ff88b9053 100644 --- a/test/controllers/posts/create_test.rb +++ b/test/controllers/posts/create_test.rb @@ -132,6 +132,16 @@ class PostsControllerTest < ActionController::TestCase assert_redirected_to_sign_in end + test 'should make post author a thread follower of the post' do + user = users(:standard_user) + sign_in user + try_create_post + assert_response(:found) + + # Assert user follows post + assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', assigns(:post), user]).count + end + private # Attempts to create a post