Commit 884e842c59b2088a2d0fe98e65fad3611899c0d3
1 parent
71066d56
Exists in
master
and in
29 other branches
Comment-related hotspots
(ActionItem2316)
Showing
5 changed files
with
83 additions
and
0 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
... | ... | @@ -112,7 +112,10 @@ class ContentViewerController < ApplicationController |
112 | 112 | @comment.author = user if logged_in? |
113 | 113 | @comment.article = @page |
114 | 114 | @comment.ip_address = request.remote_ip |
115 | + plugins_filter_comment(@comment) | |
116 | + return if @comment.rejected? | |
115 | 117 | if (pass_without_comment_captcha? || verify_recaptcha(:model => @comment, :message => _('Please type the words correctly'))) && @comment.save |
118 | + plugins_comment_saved(@comment) | |
116 | 119 | @page.touch |
117 | 120 | @comment = nil # clear the comment form |
118 | 121 | redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view] |
... | ... | @@ -121,6 +124,18 @@ class ContentViewerController < ApplicationController |
121 | 124 | end |
122 | 125 | end |
123 | 126 | |
127 | + def plugins_filter_comment(comment) | |
128 | + @plugins.each do |plugin| | |
129 | + plugin.filter_comment(comment) | |
130 | + end | |
131 | + end | |
132 | + | |
133 | + def plugins_comment_saved(comment) | |
134 | + @plugins.each do |plugin| | |
135 | + plugin.comment_saved(comment) | |
136 | + end | |
137 | + end | |
138 | + | |
124 | 139 | def pass_without_comment_captcha? |
125 | 140 | logged_in? && !environment.enabled?('captcha_for_logged_users') |
126 | 141 | end | ... | ... |
app/models/comment.rb
lib/noosfero/plugin.rb
... | ... | @@ -218,4 +218,24 @@ class Noosfero::Plugin |
218 | 218 | end |
219 | 219 | end |
220 | 220 | |
221 | + # This method will be called just before a comment in saved to the database. | |
222 | + # | |
223 | + # It can modify the comment in several ways. In special, a plugin can call | |
224 | + # reject! on the comment and that will cause the comment to not be saved. | |
225 | + # | |
226 | + # example: | |
227 | + # | |
228 | + # def filter_comment(comment) | |
229 | + # comment.reject! if anti_spam_service.is_spam?(comment) | |
230 | + # end | |
231 | + # | |
232 | + def filter_comment(comment) | |
233 | + end | |
234 | + | |
235 | + # This method will be called just after a comment has been saved to the | |
236 | + # database, so that a plugin can perform some action on it. | |
237 | + # | |
238 | + def comment_saved(comment) | |
239 | + end | |
240 | + | |
221 | 241 | end | ... | ... |
test/functional/content_viewer_controller_test.rb
... | ... | @@ -1379,4 +1379,36 @@ class ContentViewerControllerTest < ActionController::TestCase |
1379 | 1379 | assert_equal '33.44.55.66', comment.ip_address |
1380 | 1380 | end |
1381 | 1381 | |
1382 | + should 'not save a comment if a plugin rejects it' do | |
1383 | + class TestFilterPlugin < Noosfero::Plugin | |
1384 | + def filter_comment(c) | |
1385 | + c.reject! | |
1386 | + end | |
1387 | + end | |
1388 | + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestFilterPlugin.new]) | |
1389 | + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') | |
1390 | + assert_no_difference Comment, :count do | |
1391 | + post :view_page, :profile => profile.identifier, :page => [ 'myarticle' ], :comment => { :title => 'title', :body => 'body', :name => "Spammer", :email => 'damn@spammer.com' }, :confirm => 'true' | |
1392 | + end | |
1393 | + end | |
1394 | + | |
1395 | + should 'notify plugins after a comment is saved' do | |
1396 | + class TestNotifyCommentPlugin < Noosfero::Plugin | |
1397 | + def comment_saved(c) | |
1398 | + @__saved = c.id | |
1399 | + @__title = c.title | |
1400 | + end | |
1401 | + attr_reader :__title | |
1402 | + attr_reader :__saved | |
1403 | + end | |
1404 | + plugin = TestNotifyCommentPlugin.new | |
1405 | + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([plugin]) | |
1406 | + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') | |
1407 | + 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' | |
1408 | + | |
1409 | + assert_equal 'the title of the comment', plugin.__title | |
1410 | + assert plugin.__saved | |
1411 | + | |
1412 | + end | |
1413 | + | |
1382 | 1414 | end | ... | ... |
test/unit/comment_test.rb
... | ... | @@ -330,4 +330,12 @@ class CommentTest < ActiveSupport::TestCase |
330 | 330 | assert_nil Comment.new(:email => 'my@email.com').author_url |
331 | 331 | end |
332 | 332 | |
333 | + should 'be able to reject a comment' do | |
334 | + c = Comment.new | |
335 | + assert !c.rejected? | |
336 | + | |
337 | + c.reject! | |
338 | + assert c.rejected? | |
339 | + end | |
340 | + | |
333 | 341 | end | ... | ... |