1+ # rubocop:disable Metrics/ClassLength
12# Provides mainly web actions for using and making comments.
23class CommentsController < ApplicationController
34 before_action :authenticate_user! , except : [ :post , :show , :thread , :thread_content ]
45
56 before_action :set_comment , only : [ :update , :destroy , :undelete , :show ]
6- before_action :set_post , only : [ :create_thread ]
7+ before_action :set_post , only : [ :create_thread , :post_follow , :post_unfollow ]
78 before_action :set_thread ,
89 only : [ :create , :thread , :thread_content , :thread_rename , :thread_restrict , :thread_unrestrict ,
910 :thread_followers ]
@@ -263,25 +264,33 @@ def thread_unrestrict
263264
264265 def post
265266 @post = Post . find ( params [ :post_id ] )
266- @comment_threads = if current_user &.at_least_moderator? || current_user &.post_privilege? ( 'flag_curate' , @post )
267- CommentThread
268- else
269- CommentThread . undeleted
270- end . where ( post : @post ) . order ( deleted : :asc , archived : :asc , reply_count : :desc )
267+ @comment_threads = CommentThread . accessible_to ( current_user , @post )
268+ . where ( post : @post )
269+ . order ( deleted : :asc , archived : :asc , reply_count : :desc )
271270 respond_to do |format |
272271 format . html { render layout : false }
273272 format . json { render json : @comment_threads }
274273 end
275274 end
276275
277276 def post_follow
278- @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
277+ if ThreadFollower . where ( post : @post , user : current_user ) . none?
282278 ThreadFollower . create ( post : @post , user : current_user )
283279 end
284- redirect_to post_path ( @post )
280+
281+ respond_to do |format |
282+ format . html { redirect_to post_path ( @post ) }
283+ format . json { render json : { status : 'success' } }
284+ end
285+ end
286+
287+ def post_unfollow
288+ ThreadFollower . where ( post : @post , user : current_user ) . destroy_all
289+
290+ respond_to do |format |
291+ format . html { redirect_to post_path ( @post ) }
292+ format . json { render json : { status : 'success' } }
293+ end
285294 end
286295
287296 def pingable
@@ -297,7 +306,7 @@ def comment_params
297306 end
298307
299308 def set_comment
300- @comment = Comment . unscoped . find params [ :id ]
309+ @comment = Comment . unscoped . find ( params [ :id ] )
301310 end
302311
303312 def set_post
@@ -419,3 +428,4 @@ def audit(event_type, comment, audit_comment = '')
419428 user : current_user )
420429 end
421430end
431+ # rubocop:enable Metrics/ClassLength
0 commit comments