From d86632929fc2cc14ee3d2580fc01477791b81e9f Mon Sep 17 00:00:00 2001 From: Gaurav Sachdeva Date: Wed, 11 Jul 2018 03:30:05 +0530 Subject: [PATCH] Spam comment moderation (#2957) * change to status 1 * mark spam button for moderator and admin * ban user if comment marked as spam * test fix * danger file using message --- Dangerfile | 2 +- app/controllers/admin_controller.rb | 4 +++- app/controllers/users_controller.rb | 4 ++-- app/views/notes/_comment.html.erb | 13 ++++++++++--- app/views/notes/_comments.html.erb | 4 ++-- config/routes.rb | 2 +- test/fixtures/comments.yml | 2 +- test/functional/admin_controller_test.rb | 6 ++++-- test/functional/notes_controller_test.rb | 2 +- 9 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Dangerfile b/Dangerfile index 44deab270e..9dcb90f9cb 100644 --- a/Dangerfile +++ b/Dangerfile @@ -59,5 +59,5 @@ begin rescue => ex fail "There was an error with Danger bot's Junit parsing: #{ex.message}" - puts ex.inspect # view the entire error output in the log + message ex.inspect # view the entire error output in the log end diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 0757abed3a..f2ed2ac2b4 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -129,7 +129,9 @@ def mark_comment_spam if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') if @comment.status == 1 @comment.spam - flash[:notice] = "Comment has been marked as spam." + user = @comment.author + user.ban + flash[:notice] = "Comment has been marked as spam and comment author has been banned." else flash[:notice] = "Comment already marked as spam." end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0a7977eabd..0bdbbbe694 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -154,7 +154,7 @@ def profile .limit(20) @wikis = wikis.collect(&:parent).uniq - @comment_count = Comment.where(status: 0, uid: @user.uid).count + @comment_count = Comment.where(status: 1, uid: @user.uid).count # User's social links @github = @profile_user.social_link("github") @@ -259,7 +259,7 @@ def reset def comments @comments = Comment.limit(20) .order("timestamp DESC") - .where(status: 0, uid: params[:id]) + .where(status: 1, uid: params[:id]) .paginate(page: params[:page]) render partial: 'comments/comments' end diff --git a/app/views/notes/_comment.html.erb b/app/views/notes/_comment.html.erb index 12789ba00e..c88df03ff1 100644 --- a/app/views/notes/_comment.html.erb +++ b/app/views/notes/_comment.html.erb @@ -42,9 +42,16 @@ <% if comment.comment_via == 1 %> <% end %> - - - + <% if current_user &. can_moderate? %> + + + + <% else %> + + + + <% end %> + <% if current_user && (current_user.role == "admin" || current_user.role == "moderator" || comment.uid == current_user.uid || comment.parent.uid == current_user.uid) %> diff --git a/app/views/notes/_comments.html.erb b/app/views/notes/_comments.html.erb index b6724c78d5..ac4e14d157 100644 --- a/app/views/notes/_comments.html.erb +++ b/app/views/notes/_comments.html.erb @@ -1,10 +1,10 @@
-

<%= @node.comments.length %> <%= t('notes._comments.comments') %>

+

<%= @node.comments.where(status: 1).length %> <%= t('notes._comments.comments') %>

- <% @node.comments.order('timestamp ASC').each do |comment| %> + <% @node.comments.where(status: 1).order('timestamp ASC').each do |comment| %> <% if comment.cid == @node.comments.first.cid %><% end %> <%= render :partial => "notes/comment", :locals => {:comment => comment} %> diff --git a/config/routes.rb b/config/routes.rb index 19ecb610eb..b499b4b30c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -259,7 +259,7 @@ get 'admin/moderate/:id' => 'admin#moderate' get 'admin/unmoderate/:id' => 'admin#unmoderate' get 'admin/publish_comment/:id' => 'admin#publish_comment' - post 'admin/mark_comment_spam/:id' => 'admin#mark_comment_spam' + get 'admin/mark_comment_spam/:id' => 'admin#mark_comment_spam' get 'post' => 'editor#post' post 'post' => 'editor#post' diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index d3d3d0e031..6d88d19734 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -118,5 +118,5 @@ spam_comment: nid: 1 status: 0 comment: This thing is spam. - timestamp: <%= Time.now.to_i + 1 %> + timestamp: <%= Time.now.to_i + 14 %> thread: /01 diff --git a/test/functional/admin_controller_test.rb b/test/functional/admin_controller_test.rb index bed439b538..931be72412 100644 --- a/test/functional/admin_controller_test.rb +++ b/test/functional/admin_controller_test.rb @@ -422,7 +422,8 @@ def teardown comment = assigns(:comment) assert_equal 0, comment.status - assert_equal "Comment has been marked as spam.", flash[:notice] + + assert_equal "Comment has been marked as spam and comment author has been banned.", flash[:notice] assert_redirected_to '/dashboard' + '?_=' + Time.now.to_i.to_s end @@ -434,7 +435,8 @@ def teardown comment = assigns(:comment) assert_equal 0, comment.status - assert_equal "Comment has been marked as spam.", flash[:notice] + + assert_equal "Comment has been marked as spam and comment author has been banned.", flash[:notice] assert_redirected_to '/dashboard' + '?_=' + Time.now.to_i.to_s end diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index 9166bf1adc..58920140e6 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -459,7 +459,7 @@ def teardown id: node.title.parameterize } selector = css_select '.fa-fire' - assert_equal selector.size, 4 + assert_equal selector.size, 3 end test 'should redirect to questions show page after creating a new question' do