From 5853fd51ddcd232938ada80c239451ee3dedffe0 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Tue, 21 Aug 2012 15:43:11 -0300 Subject: [PATCH] UI to mark comments as ham --- app/controllers/my_profile/spam_controller.rb | 40 ++++++++++++++++++++++++++++++++++++++++ app/controllers/public/content_viewer_controller.rb | 12 ++++++++++-- app/models/profile.rb | 2 ++ app/views/content_viewer/_comment.rhtml | 22 +++++++++++++++------- app/views/profile_editor/index.rhtml | 2 ++ app/views/spam/index.rhtml | 20 ++++++++++++++++++++ public/images/control-panel/mail-mark-junk.png | Bin 0 -> 4035 bytes public/images/control-panel/mail-mark-junk.svg | 504 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public/javascripts/application.js | 15 +++++++++++++++ public/stylesheets/application.css | 8 ++++++++ test/functional/content_viewer_controller_test.rb | 11 ++++++----- test/functional/spam_controller_test.rb | 35 +++++++++++++++++++++++++++++++++++ 12 files changed, 657 insertions(+), 14 deletions(-) create mode 100644 app/controllers/my_profile/spam_controller.rb create mode 100644 app/views/spam/index.rhtml create mode 100644 public/images/control-panel/mail-mark-junk.png create mode 100644 public/images/control-panel/mail-mark-junk.svg create mode 100644 test/functional/spam_controller_test.rb diff --git a/app/controllers/my_profile/spam_controller.rb b/app/controllers/my_profile/spam_controller.rb new file mode 100644 index 0000000..16818c5 --- /dev/null +++ b/app/controllers/my_profile/spam_controller.rb @@ -0,0 +1,40 @@ +class SpamController < MyProfileController + + protect :moderate_comments, :profile + + def index + if request.post? + begin + # FIXME duplicated logic + # + # This logic more or less replicates what is already in + # ContentViewerController#view_page, + # ContentViewerController#remove_comment and + # ContentViewerController#mark_comment_as_spam + if params[:remove_comment] + profile.comments_received.find(params[:remove_comment]).destroy + end + if params[:mark_comment_as_ham] + profile.comments_received.find(params[:mark_comment_as_ham]).ham! + end + if request.xhr? + json_response(true) + else + redirect_to :action => :index + end + rescue + json_response(false) + end + return + end + + @spam = profile.comments_received.spam.paginate({:page => params[:page]}) + end + + protected + + def json_response(status) + render :text => {'ok' => status }.to_json, :content_type => 'application/json' + end + +end diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index ce5f222..0569b44 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -155,7 +155,7 @@ class ContentViewerController < ApplicationController @comment.destroy session[:notice] = _('Comment succesfully deleted') end - redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view] + finish_comment_handling end def mark_comment_as_spam @@ -164,7 +164,15 @@ class ContentViewerController < ApplicationController @comment.spam! session[:notice] = _('Comment succesfully marked as SPAM') end - redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view] + finish_comment_handling + end + + def finish_comment_handling + if request.xhr? + render :text => {'ok' => true}.to_json, :content_type => 'application/json' + else + redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view] + end end def per_page diff --git a/app/models/profile.rb b/app/models/profile.rb index 4c1a6bd..336780e 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -101,6 +101,8 @@ class Profile < ActiveRecord::Base has_many :scraps_received, :class_name => 'Scrap', :foreign_key => :receiver_id, :order => "updated_at DESC", :dependent => :destroy belongs_to :template, :class_name => 'Profile', :foreign_key => 'template_id' + has_many :comments_received, :class_name => 'Comment', :through => :articles, :source => :comments + # FIXME ugly workaround def self.human_attribute_name(attrib) _(self.superclass.human_attribute_name(attrib)) diff --git a/app/views/content_viewer/_comment.rhtml b/app/views/content_viewer/_comment.rhtml index 3f1009f..d89bbbd 100644 --- a/app/views/content_viewer/_comment.rhtml +++ b/app/views/content_viewer/_comment.rhtml @@ -1,7 +1,7 @@
  • -
    +
    <% if comment.author %> <%= link_to image_tag(profile_icon(comment.author, :minor)) + @@ -53,21 +53,29 @@ <% end %> <%= report_abuse(comment.author, :comment_link, comment) if comment.author %> - <% if (logged_in? && (user == @page.profile || user.has_permission?(:moderate_comments, @page.profile))) %> - <%= link_to(_('Mark as SPAM'), { :mark_comment_as_spam => comment.id }, :method => :post, :confirm => _('Are you sure you want to mark this comment as SPAM?'), :class => 'comment-footer comment-footer-link comment-footer-hide') %> + <% if comment.spam? %>   + <%= link_to_function(_('Mark as NOT SPAM'), 'remove_comment(this, %s); return false;' % url_for(:mark_comment_as_ham => comment.id).to_json, :class => 'comment-footer comment-footer-link comment-footer-hide') %> + <% else %> + <% if (logged_in? && (user == profile || user.has_permission?(:moderate_comments, profile))) %> +   + <%= link_to_function(_('Mark as SPAM'), 'remove_comment(this, %s, %s); return false;' % [url_for(:mark_comment_as_spam => comment.id).to_json, _('Are you sure you want to mark this comment as SPAM?').to_json], :class => 'comment-footer comment-footer-link comment-footer-hide') %> + <% end %> <% end %> - <% if logged_in? && (user == @page.profile || user == comment.author || user.has_permission?(:moderate_comments, @page.profile)) %> - <%= link_to(_('Remove'), { :profile => params[:profile], :remove_comment => comment.id, :view => params[:view] }, :method => :post, :confirm => _('Are you sure you want to remove this comment and all its replies?'), :class => 'comment-footer comment-footer-link comment-footer-hide') %> + <% if logged_in? && (user == profile || user == comment.author || user.has_permission?(:moderate_comments, profile)) %>   + <%= link_to_function(_('Remove'), 'remove_comment(this, %s, %s); return false ;' % [url_for(:profile => params[:profile], :remove_comment => comment.id, :view => params[:view]).to_json, _('Are you sure you want to remove this comment and all its replies?').to_json], :class => 'comment-footer comment-footer-link comment-footer-hide') %> <% end %> - <%= link_to_function _('Reply'), + <% unless comment.spam? %> +   + <%= link_to_function _('Reply'), "var f = add_comment_reply_form(this, %s); f.find('input[name=comment[title]], textarea').val(''); return false" % comment.id, :class => 'comment-footer comment-footer-link comment-footer-hide', :id => 'comment-reply-to-' + comment.id.to_s - %> + %> + <% end %>
    <% end %> diff --git a/app/views/profile_editor/index.rhtml b/app/views/profile_editor/index.rhtml index b48c7ab..4044a64 100644 --- a/app/views/profile_editor/index.rhtml +++ b/app/views/profile_editor/index.rhtml @@ -66,6 +66,8 @@ <%= control_panel_button(_('Manage my groups'), 'groups', :controller => 'memberships') if profile.person? %> + <%= control_panel_button(_('Manage SPAM'), 'manage-spam', :controller => 'spam', :action => 'index') %> + <% @plugins.dispatch(:control_panel_buttons).each do |button| %> <%= control_panel_button(button[:title], button[:icon], button[:url]) %> <% end %> diff --git a/app/views/spam/index.rhtml b/app/views/spam/index.rhtml new file mode 100644 index 0000000..eae44dc --- /dev/null +++ b/app/views/spam/index.rhtml @@ -0,0 +1,20 @@ +

    <%= _('Manage SPAM') %>

    + +<% button_bar do %> + <%= button :back, _('Back to control panel'), :controller => :profile_editor %> +<% end %> + +<%# FIXME should not need to replicate the article structure like this to be able to use the same formatting as the comments listing %> +
    +
    +
      + <%= render :partial => 'content_viewer/comment', :collection => @spam %> +
    +
    +
    + +<%= pagination_links @spam %> + +<% button_bar do %> + <%= button :back, _('Back to control panel'), :controller => :profile_editor %> +<% end %> diff --git a/public/images/control-panel/mail-mark-junk.png b/public/images/control-panel/mail-mark-junk.png new file mode 100644 index 0000000..d14f23f Binary files /dev/null and b/public/images/control-panel/mail-mark-junk.png differ diff --git a/public/images/control-panel/mail-mark-junk.svg b/public/images/control-panel/mail-mark-junk.svg new file mode 100644 index 0000000..0163175 --- /dev/null +++ b/public/images/control-panel/mail-mark-junk.svg @@ -0,0 +1,504 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + Jakub Steiner + + + http://jimmac.musichall.cz + + Mark mail as Junk + + + mail + spam + junk + + + + + + + + + + + + + + + + + + + + + + diff --git a/public/javascripts/application.js b/public/javascripts/application.js index e8b9d41..30d66b1 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -674,6 +674,21 @@ function add_comment_reply_form(button, comment_id) { return f; } +function remove_comment(button, url, msg) { + var $ = jQuery; + var $button = $(button); + if (msg && !confirm(msg)) { + $button.removeClass('comment-button-loading'); + return; + } + $button.addClass('comment-button-loading'); + $.post(url, function(data) { + if (data.ok) { + $button.closest('.article-comment').slideUp(); + } + }); +} + function original_image_dimensions(src) { var img = new Image(); img.src = src; diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 10488f0..22d629e 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -1262,6 +1262,11 @@ a.comment-picture { } /* * * Comment Box * * */ +.comment-button-loading { + padding-left: 20px; + background: transparent url(../images/loading-small.gif) no-repeat left center; +} + .post_comment_box { text-align: center; padding: 0px 15px 5px 15px; @@ -3840,6 +3845,9 @@ h1#agenda-title { .controller-profile_editor .msie6 a.control-panel-edit-location { background-image: url(../images/control-panel/set-geolocation.gif) } +.controller-profile_editor a.control-panel-manage-spam { + background-image: url(../images/control-panel/mail-mark-junk.png) +} /* ==> public/stylesheets/controller_profile_members.css <== */ .controller-profile_members .no-boxes { margin: 30px diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 6111095..ae3fd8b 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -92,7 +92,7 @@ class ContentViewerControllerTest < ActionController::TestCase login_as 'testuser' get :view_page, :profile => 'testuser', :page => [ 'test' ] - assert_tag :tag => 'a', :attributes => { :href => '/testuser/test?remove_comment=' + comment.id.to_s } + assert_tag :tag => 'a', :attributes => { :onclick => %r(/testuser/test\?remove_comment=#{comment.id}.quot) } end should 'display remove comment button with param view when image' do @@ -106,8 +106,9 @@ class ContentViewerControllerTest < ActionController::TestCase login_as 'testuser' get :view_page, :profile => 'testuser', :page => [ image.filename ], :view => true - assert_tag :tag => 'a', :attributes => { :href => "/testuser/#{image.filename}?remove_comment=" + comment.id.to_s + '&view=true'} - end + assert_tag :tag => 'a', :attributes => { :onclick => %r(/testuser/#{image.filename}\?remove_comment=#{comment.id}.*amp;view=true.quot) } +end + should 'not add unneeded params for remove comment button' do profile = create_user('testuser').person @@ -117,8 +118,8 @@ class ContentViewerControllerTest < ActionController::TestCase comment.save! login_as 'testuser' - get :view_page, :profile => 'testuser', :page => [ 'test' ], :random_param => 'bli' # <<<<<<<<<<<<<<< - assert_tag :tag => 'a', :attributes => { :href => '/testuser/test?remove_comment=' + comment.id.to_s } + get :view_page, :profile => 'testuser', :page => [ 'test' ], :random_param => 'bli' + assert_tag :tag => 'a', :attributes => { :onclick => %r(/testuser/test\?remove_comment=#{comment.id.to_s}.quot) } end should 'be able to remove comment' do diff --git a/test/functional/spam_controller_test.rb b/test/functional/spam_controller_test.rb new file mode 100644 index 0000000..02745ff --- /dev/null +++ b/test/functional/spam_controller_test.rb @@ -0,0 +1,35 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class SpamControllerTest < ActionController::TestCase + + def setup + @profile = create_user.person + @article = fast_create(TextileArticle, :profile_id => @profile.id) + @spam = fast_create(Comment, :source_id => @article.id, :spam => true, :name => 'foo', :email => 'foo@example.com') + + login_as @profile.identifier + end + + test "should only list spammy comments" do + ham = fast_create(Comment, :source_id => @article.id) + + get :index, :profile => @profile.identifier + + assert_equivalent [@spam], assigns(:spam) + end + + test "should mark comments as ham" do + post :index, :profile => @profile.identifier, :mark_comment_as_ham => @spam.id + + @spam.reload + assert @spam.ham? + end + + test "should remove comments" do + post :index, :profile => @profile.identifier, :remove_comment => @spam.id + + assert !Comment.exists?(@spam.id) + end + + +end -- libgit2 0.21.2