Skip to content

Commit caff149

Browse files
authored
Merge pull request #1796 from codidact/trichoplax/split-post-follow-and-unfollow
Separate post unfollow to avoid reversal on accidental second call
2 parents d9256f4 + 338497e commit caff149

5 files changed

Lines changed: 31 additions & 9 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rubocop:disable Metrics/ClassLength
12
# Provides mainly web actions for using and making comments.
23
class CommentsController < ApplicationController
34
before_action :authenticate_user!, except: [:post, :show, :thread, :thread_content]
@@ -276,14 +277,18 @@ def post
276277

277278
def post_follow
278279
@post = Post.find(params[:post_id])
279-
if @post.followed_by?(current_user)
280-
ThreadFollower.where(post: @post, user: current_user).destroy_all
281-
else
280+
if ThreadFollower.where(post: @post, user: current_user).none?
282281
ThreadFollower.create(post: @post, user: current_user)
283282
end
284283
redirect_to post_path(@post)
285284
end
286285

286+
def post_unfollow
287+
@post = Post.find(params[:post_id])
288+
ThreadFollower.where(post: @post, user: current_user).destroy_all
289+
redirect_to post_path(@post)
290+
end
291+
287292
def pingable
288293
thread = params[:id] == '-1' ? CommentThread.new(post_id: params[:post]) : CommentThread.find(params[:id])
289294
users = User.where(id: thread.pingable)
@@ -419,3 +424,4 @@ def audit(event_type, comment, audit_comment = '')
419424
user: current_user)
420425
end
421426
end
427+
# rubocop:enable Metrics/ClassLength

app/views/posts/_expanded.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
? @post_type : PostType of the post
77
88
Parameters:
9-
? params[:comment_id] : Comment ID to show even if it would be hidden otherwise
9+
? params[:comment_id] : Comment ID to show even if it would be hidden otherwise
1010
? params[:thread_id] : CommentThread ID to render expanded view for
1111
? params[:sort] : sorting type for the post's children
1212
@@ -243,8 +243,8 @@
243243
<div class="tools">
244244
<%= render "shared/copy_link", classes: ["tools--item"],
245245
desc: "Copy a permanent link to this post",
246-
id: post.id,
247-
md: generic_share_link_md(post),
246+
id: post.id,
247+
md: generic_share_link_md(post),
248248
raw: generic_share_link(post) %>
249249
<%= link_to post_history_path(post), class: 'tools--item', 'aria-label': 'View history of this post' do %>
250250
<i class="fa fa-history"></i>
@@ -533,7 +533,7 @@
533533
</h4>
534534
<% if user_signed_in? %>
535535
<% if post.followed_by?(current_user) %>
536-
<%= link_to follow_post_comments_path(post_id: post.id), method: :post,
536+
<%= link_to unfollow_post_comments_path(post_id: post.id), method: :post,
537537
class: "button is-muted is-outlined is-small",
538538
title: 'Don\'t follow new comment threads on this post',
539539
role: 'button',

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?
2525
mount Rack::Directory.new('coverage/'), at: '/coverage' if Rails.env.development?
2626
mount MaintenanceTasks::Engine, at: '/maintenance'
27-
27+
2828
scope 'admin' do
2929
root to: 'admin#index', as: :admin
3030
get 'errors', to: 'admin#error_reports', as: :admin_error_reports
@@ -244,6 +244,7 @@
244244
get 'thread/:id/followers', to: 'comments#thread_followers', as: :comment_thread_followers
245245
get 'post/:post_id', to: 'comments#post', as: :post_comments
246246
post 'post/:post_id/follow', to: 'comments#post_follow', as: :follow_post_comments
247+
post 'post/:post_id/unfollow', to: 'comments#post_unfollow', as: :unfollow_post_comments
247248
get ':id', to: 'comments#show', as: :comment
248249
get 'thread/:id', to: 'comments#thread', as: :comment_thread
249250
get 'thread/:id/content', to: 'comments#thread_content', as: :comment_thread_content

test/comments_test_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def try_post_follow(test_post)
8080
# Attempts to unfollow new threads on a given post
8181
# @param post [Post] post to unfollow
8282
def try_post_unfollow(test_post)
83-
post :post_follow, params: { post_id: test_post.id }
83+
post :post_unfollow, params: { post_id: test_post.id }
8484
end
8585

8686
# Attempts to lock a given comment thread

test/controllers/comments/post_follow_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@ class CommentsControllerTest < ActionController::TestCase
3434
# Assert user follows post
3535
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
3636
end
37+
38+
test 'follower cannot duplicate the following of a post' do
39+
user = users(:standard_user)
40+
sign_in user
41+
question = posts(:question_one)
42+
43+
# Assert user follows post
44+
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
45+
46+
try_post_follow(question)
47+
assert_response(:found)
48+
49+
# Assert user still only follows post once
50+
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
51+
end
3752
end

0 commit comments

Comments
 (0)