diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 659693ef6..75bd052dc 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,4 +1,3 @@ -# rubocop:disable Metrics/ClassLength # Provides mainly web actions for using and making comments. class CommentsController < ApplicationController before_action :authenticate_user!, except: [:post, :show, :thread, :thread_content] @@ -16,7 +15,7 @@ class CommentsController < ApplicationController before_action :check_restrict_access, only: [:thread_restrict] 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] def create_thread @@ -46,12 +45,9 @@ def create_thread if success notification = "New comment thread on #{@comment.root.title}: #{@comment_thread.title}" - unless @comment.post.user == current_user - @comment.post.user.create_notification(notification, helpers.comment_link(@comment)) - end ThreadFollower.where(post: @post).each do |tf| - unless tf.user == current_user || tf.user == @comment.post.user + unless tf.user == current_user tf.user.create_notification(notification, helpers.comment_link(@comment)) end ThreadFollower.create(user: tf.user, comment_thread: @comment_thread) @@ -253,8 +249,7 @@ def thread_unrestrict end @comment_thread.update(deleted: false, deleted_by: nil) when 'follow' - tf = ThreadFollower.find_by(comment_thread: @comment_thread, user: current_user) - tf&.destroy + ThreadFollower.where(comment_thread: @comment_thread, user: current_user).destroy_all else return not_found! end @@ -428,4 +423,3 @@ def audit(event_type, comment, audit_comment = '') user: current_user) end end -# rubocop:enable Metrics/ClassLength diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index e1af10966..354e45b2a 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -99,6 +99,7 @@ def create if @post.save @post.update(last_activity: @post.created_at, last_activity_by: current_user) + ThreadFollower.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 2508e88c5..3c9012405 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 - # Gets threads appropriately scoped for a given user & post # @param user [User, nil] user to check # @para post [Post] post to check @@ -78,15 +76,4 @@ def maximum_title_length errors.add(:title, "can't be more than #{max_len} characters") end 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/test/controllers/comments/post_follow_test.rb b/test/controllers/comments/post_follow_test.rb index 2547d6114..48f07791a 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, ThreadFollower.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, ThreadFollower.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, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count + + try_post_follow(question) + assert_response(:found) + + # Assert user follows post + assert_equal 1, ThreadFollower.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 255f50742..0cb3cb4d7 100644 --- a/test/controllers/posts/create_test.rb +++ b/test/controllers/posts/create_test.rb @@ -118,6 +118,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, ThreadFollower.where(['post_id = ? AND user_id = ?', assigns(:post), user]).count + end + private # Attempts to create a post