diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 256c6275b..ffd4c8683 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -276,7 +276,7 @@ def post def post_follow @post = Post.find(params[:post_id]) - if CommentThread.post_followed?(@post, current_user) + if @post.followed_by?(current_user) ThreadFollower.where(post: @post, user: current_user).destroy_all else ThreadFollower.create(post: @post, user: current_user) diff --git a/app/models/comment_thread.rb b/app/models/comment_thread.rb index 6e19a835b..a07d4ade0 100644 --- a/app/models/comment_thread.rb +++ b/app/models/comment_thread.rb @@ -16,14 +16,6 @@ class CommentThread < ApplicationRecord after_create :create_follower - # Are there any threads on a given post that a given user follows? - # @param post [Post] post to check - # @param user [User] user to check - # @return [Boolean] check result - def self.post_followed?(post, user) - ThreadFollower.where(post: post, user: user).any? - end - # Is the thread read-only (can't be edited)? # @return [Boolean] check result def read_only? diff --git a/app/models/post.rb b/app/models/post.rb index 3f72411ea..4d8501c82 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -234,6 +234,14 @@ def reaction_list .to_h { |_k, v| [v.first.reaction_type, v] } end + # Are new threads on this post followed by a given user? + # @param post [Post] post to check + # @param user [User] user to check + # @return [Boolean] check result + def followed_by?(user) + ThreadFollower.where(post: self, user: user).any? + end + private ## diff --git a/app/views/posts/_expanded.html.erb b/app/views/posts/_expanded.html.erb index a9490abb1..d50ad23b4 100644 --- a/app/views/posts/_expanded.html.erb +++ b/app/views/posts/_expanded.html.erb @@ -532,7 +532,7 @@ <%= pluralize(public_count, 'comment thread') %> <% if user_signed_in? %> - <% if CommentThread.post_followed?(post, current_user) %> + <% if post.followed_by?(current_user) %> <%= link_to follow_post_comments_path(post_id: post.id), method: :post, class: "button is-muted is-outlined is-small", title: 'Don\'t follow new comment threads on this post', diff --git a/test/comments_test_helpers.rb b/test/comments_test_helpers.rb index 797a69f5f..e2b0c2365 100644 --- a/test/comments_test_helpers.rb +++ b/test/comments_test_helpers.rb @@ -71,6 +71,18 @@ def try_unfollow_thread(thread) post :thread_unrestrict, params: { id: thread.id, type: 'follow' } end + # Attempts to follow new threads on a given post + # @param post [Post] post to follow + def try_post_follow(test_post) + post :post_follow, params: { post_id: test_post.id } + end + + # Attempts to unfollow new threads on a given post + # @param post [Post] post to unfollow + def try_post_unfollow(test_post) + post :post_follow, params: { post_id: test_post.id } + end + # Attempts to lock a given comment thread # @param thread [CommentThread] thread to lock # @param duration [Integer] lock duration, in days diff --git a/test/controllers/comments/post_follow_test.rb b/test/controllers/comments/post_follow_test.rb new file mode 100644 index 000000000..c760b0d83 --- /dev/null +++ b/test/controllers/comments/post_follow_test.rb @@ -0,0 +1,37 @@ +require 'test_helper' +require 'comments_test_helpers' + +class CommentsControllerTest < ActionController::TestCase + include Devise::Test::ControllerHelpers + include CommentsControllerTestHelpers + + test 'post follower can unfollow post' 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 + end + + test 'non-follower can follow post' do + user = users(:basic_user) + sign_in user + question = posts(:question_one) + + # 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/fixtures/thread_followers.yml b/test/fixtures/thread_followers.yml index 893c177d7..b51e03b64 100644 --- a/test/fixtures/thread_followers.yml +++ b/test/fixtures/thread_followers.yml @@ -5,3 +5,7 @@ deleter_normal: standard_author_normal: user: standard_user comment_thread: normal + +standard_author_question_one: + user: standard_user + post: question_one