@@ -32,7 +32,12 @@ def create_thread
3232
3333 pings = check_for_pings @comment_thread , body
3434
35- return if comment_rate_limited
35+ rate_limited , limit_message = helpers . comment_rate_limited? ( current_user , @post )
36+ if rate_limited
37+ flash [ :danger ] = limit_message
38+ redirect_to helpers . generic_share_link ( @post )
39+ return
40+ end
3641
3742 success = ActiveRecord ::Base . transaction do
3843 @comment_thread . save!
@@ -76,7 +81,12 @@ def create
7681 @comment = Comment . new ( post : @post , content : body , user : current_user ,
7782 comment_thread : @comment_thread , has_reference : false )
7883
79- return if comment_rate_limited
84+ rate_limited , limit_message = helpers . comment_rate_limited? ( current_user , @post )
85+ if rate_limited
86+ flash [ :danger ] = limit_message
87+ redirect_to helpers . generic_share_link ( @post )
88+ return
89+ end
8090
8191 if @comment . save
8292 apply_pings pings
@@ -324,29 +334,4 @@ def apply_pings(pings)
324334 helpers . comment_link ( @comment ) )
325335 end
326336 end
327-
328- def comment_rate_limited
329- recent_comments = Comment . where ( created_at : 24 . hours . ago ..DateTime . now , user : current_user ) . where \
330- . not ( post : Post . includes ( :parent ) . where ( parents_posts : { user_id : current_user . id } ) ) \
331- . where . not ( post : Post . where ( user_id : current_user . id ) ) . count
332- max_comments_per_day = SiteSetting [ current_user . privilege? ( 'unrestricted' ) ? 'RL_Comments' : 'RL_NewUserComments' ]
333-
334- if ( !@post . user_id == current_user . id || @post &.parent &.user_id == current_user . id ) \
335- && recent_comments >= max_comments_per_day
336- comment_limit_msg = "You have used your daily comment limit of #{ recent_comments } comments. " \
337- 'Come back tomorrow to continue commenting. Comments on own posts and on answers ' \
338- 'to own posts are exempt.'
339-
340- if recent_comments . zero? && !current_user . privilege? ( 'unrestricted' )
341- comment_limit_msg = 'New users can only comment on their own posts and on answers to them.'
342- end
343-
344- AuditLog . rate_limit_log ( event_type : 'comment' , related : @comment , user : current_user ,
345- comment : "limit: #{ max_comments_per_day } \n \c omment:\n #{ @comment . attributes_print } " )
346-
347- render json : { status : 'failed' , message : comment_limit_msg } , status : :forbidden
348- return true
349- end
350- false
351- end
352337end
0 commit comments