From 884e842c59b2088a2d0fe98e65fad3611899c0d3 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Thu, 8 Mar 2012 22:26:41 -0300 Subject: [PATCH] Comment-related hotspots --- app/controllers/public/content_viewer_controller.rb | 15 +++++++++++++++ app/models/comment.rb | 8 ++++++++ lib/noosfero/plugin.rb | 20 ++++++++++++++++++++ test/functional/content_viewer_controller_test.rb | 32 ++++++++++++++++++++++++++++++++ test/unit/comment_test.rb | 8 ++++++++ 5 files changed, 83 insertions(+), 0 deletions(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index ff7e2e2..4679dae 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -112,7 +112,10 @@ class ContentViewerController < ApplicationController @comment.author = user if logged_in? @comment.article = @page @comment.ip_address = request.remote_ip + plugins_filter_comment(@comment) + return if @comment.rejected? if (pass_without_comment_captcha? || verify_recaptcha(:model => @comment, :message => _('Please type the words correctly'))) && @comment.save + plugins_comment_saved(@comment) @page.touch @comment = nil # clear the comment form redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view] @@ -121,6 +124,18 @@ class ContentViewerController < ApplicationController end end + def plugins_filter_comment(comment) + @plugins.each do |plugin| + plugin.filter_comment(comment) + end + end + + def plugins_comment_saved(comment) + @plugins.each do |plugin| + plugin.comment_saved(comment) + end + end + def pass_without_comment_captcha? logged_in? && !environment.enabled?('captcha_for_logged_users') end diff --git a/app/models/comment.rb b/app/models/comment.rb index f83be6e..244ed25 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -126,4 +126,12 @@ class Comment < ActiveRecord::Base end end + def rejected? + @rejected + end + + def reject! + @rejected = true + end + end diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index 67adbe7..0951044 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -218,4 +218,24 @@ class Noosfero::Plugin end end + # This method will be called just before a comment in saved to the database. + # + # It can modify the comment in several ways. In special, a plugin can call + # reject! on the comment and that will cause the comment to not be saved. + # + # example: + # + # def filter_comment(comment) + # comment.reject! if anti_spam_service.is_spam?(comment) + # end + # + def filter_comment(comment) + end + + # This method will be called just after a comment has been saved to the + # database, so that a plugin can perform some action on it. + # + def comment_saved(comment) + end + end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 1579e43..1d7e4c7 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1379,4 +1379,36 @@ class ContentViewerControllerTest < ActionController::TestCase assert_equal '33.44.55.66', comment.ip_address end + should 'not save a comment if a plugin rejects it' do + class TestFilterPlugin < Noosfero::Plugin + def filter_comment(c) + c.reject! + end + end + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestFilterPlugin.new]) + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + assert_no_difference Comment, :count do + post :view_page, :profile => profile.identifier, :page => [ 'myarticle' ], :comment => { :title => 'title', :body => 'body', :name => "Spammer", :email => 'damn@spammer.com' }, :confirm => 'true' + end + end + + should 'notify plugins after a comment is saved' do + class TestNotifyCommentPlugin < Noosfero::Plugin + def comment_saved(c) + @__saved = c.id + @__title = c.title + end + attr_reader :__title + attr_reader :__saved + end + plugin = TestNotifyCommentPlugin.new + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([plugin]) + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + post :view_page, :profile => profile.identifier, :page => [ 'myarticle' ], :comment => { :title => 'the title of the comment', :body => 'body', :name => "Spammer", :email => 'damn@spammer.com' }, :confirm => 'true' + + assert_equal 'the title of the comment', plugin.__title + assert plugin.__saved + + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 82ab6de..32a348f 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -330,4 +330,12 @@ class CommentTest < ActiveSupport::TestCase assert_nil Comment.new(:email => 'my@email.com').author_url end + should 'be able to reject a comment' do + c = Comment.new + assert !c.rejected? + + c.reject! + assert c.rejected? + end + end -- libgit2 0.21.2