From 3afcb128a3797d584994738a284837334dcb54e3 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Wed, 5 Mar 2025 16:49:09 +0000 Subject: [PATCH 1/6] Move followed_by? method to post model to simplify calling (cherry picked from commit 2dc903527c30253addc2d5b0e312936081b79d75) --- app/controllers/comments_controller.rb | 2 +- app/models/comment_thread.rb | 3 +++ app/models/post.rb | 4 ++++ app/views/posts/_expanded.html.erb | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) 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..558417362 100644 --- a/app/models/comment_thread.rb +++ b/app/models/comment_thread.rb @@ -44,6 +44,7 @@ def can_access?(user) (!deleted? || user&.privilege?('flag_curate') || user&.post_privilege?('flag_curate', post)) && post.can_access?(user) end +<<<<<<< HEAD # Gets a list of user IDs who should be pingable in the thread. # @return [Array] @@ -85,4 +86,6 @@ def create_follower ThreadFollower.create comment_thread: self, user: post.user end end +======= +>>>>>>> 2dc90352 (Move followed_by? method to post model to simplify calling) end diff --git a/app/models/post.rb b/app/models/post.rb index 3f72411ea..2a3127304 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -234,6 +234,10 @@ def reaction_list .to_h { |_k, v| [v.first.reaction_type, v] } end + 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', From b76c91b240318d010bcb0ba2898c6ff5ff88cfb2 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Fri, 15 Aug 2025 15:20:38 +0100 Subject: [PATCH 2/6] Remove accidentally remaining merge conflict markers --- app/models/comment_thread.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/models/comment_thread.rb b/app/models/comment_thread.rb index 558417362..6e19a835b 100644 --- a/app/models/comment_thread.rb +++ b/app/models/comment_thread.rb @@ -44,7 +44,6 @@ def can_access?(user) (!deleted? || user&.privilege?('flag_curate') || user&.post_privilege?('flag_curate', post)) && post.can_access?(user) end -<<<<<<< HEAD # Gets a list of user IDs who should be pingable in the thread. # @return [Array] @@ -86,6 +85,4 @@ def create_follower ThreadFollower.create comment_thread: self, user: post.user end end -======= ->>>>>>> 2dc90352 (Move followed_by? method to post model to simplify calling) end From 7abcf8608abae8b78e71a0aac6dbf401e5d476c8 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Fri, 15 Aug 2025 15:21:58 +0100 Subject: [PATCH 3/6] Remove method duplicated during cherry-pick and add comment --- app/models/comment_thread.rb | 8 -------- app/models/post.rb | 4 ++++ 2 files changed, 4 insertions(+), 8 deletions(-) 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 2a3127304..bb3b84a5f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -234,6 +234,10 @@ def reaction_list .to_h { |_k, v| [v.first.reaction_type, v] } end + # Are new threads on this post followed by this 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 From a385b7fe74da2ec4484f097c40507fbfbef01e92 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Fri, 15 Aug 2025 15:50:55 +0100 Subject: [PATCH 4/6] Make comment consistent with others in Post model --- app/models/post.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index bb3b84a5f..4d8501c82 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -234,7 +234,7 @@ def reaction_list .to_h { |_k, v| [v.first.reaction_type, v] } end - # Are new threads on this post followed by this user? + # 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 From 13bbd829df34c80d88e67f9ddaa3748aa5161ea8 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Fri, 15 Aug 2025 22:55:57 +0100 Subject: [PATCH 5/6] Add tests for following new threads on a post --- test/comments_test_helpers.rb | 12 +++++++ test/controllers/comments/post_follow_test.rb | 34 +++++++++++++++++++ test/fixtures/thread_followers.yml | 4 +++ 3 files changed, 50 insertions(+) create mode 100644 test/controllers/comments/post_follow_test.rb 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..066ca9bc2 --- /dev/null +++ b/test/controllers/comments/post_follow_test.rb @@ -0,0 +1,34 @@ +require 'test_helper' +require 'comments_test_helpers' + +class CommentsControllerTest < ActionController::TestCase + 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 From 55760b9c7aa6752be9bf74842ef7ceab64eef936 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Sat, 16 Aug 2025 01:03:21 +0100 Subject: [PATCH 6/6] Add missing includes for test --- test/controllers/comments/post_follow_test.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/controllers/comments/post_follow_test.rb b/test/controllers/comments/post_follow_test.rb index 066ca9bc2..c760b0d83 100644 --- a/test/controllers/comments/post_follow_test.rb +++ b/test/controllers/comments/post_follow_test.rb @@ -2,6 +2,9 @@ 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