diff --git a/plugins/anti_spam/dependencies.rb b/plugins/anti_spam/dependencies.rb new file mode 100644 index 0000000..3478c48 --- /dev/null +++ b/plugins/anti_spam/dependencies.rb @@ -0,0 +1 @@ +require 'rakismet' diff --git a/plugins/anti_spam/lib/anti_spam_plugin.rb b/plugins/anti_spam/lib/anti_spam_plugin.rb index b03e935..3aeacdc 100644 --- a/plugins/anti_spam/lib/anti_spam_plugin.rb +++ b/plugins/anti_spam/lib/anti_spam_plugin.rb @@ -8,4 +8,32 @@ class AntiSpamPlugin < Noosfero::Plugin _("Checks comments against a spam checking service compatible with the Akismet API") end + def check_comment_for_spam(comment) + if rakismet_call(comment, :spam?) + comment.spam = true + comment.save! + end + end + + def comment_marked_as_spam(comment) + rakismet_call(comment, :spam!) + end + + def comment_marked_as_ham(comment) + rakismet_call(comment, :ham!) + end + + protected + + def rakismet_call(comment, op) + settings = AntiSpamPlugin::Settings.new(comment.environment) + + Rakismet.host = settings.host + Rakismet.key = settings.api_key + Rakismet.url = comment.environment.top_url + + submission = AntiSpamPlugin::CommentWrapper.new(comment) + submission.send(op) + end + end diff --git a/plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb b/plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb new file mode 100644 index 0000000..e4833dc --- /dev/null +++ b/plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb @@ -0,0 +1,11 @@ +class AntiSpamPlugin::CommentWrapper < Struct.new(:comment) + + delegate :author_name, :author_email, :title, :body, :ip_address, :user_agent, :referrer, :to => :comment + + include Rakismet::Model + + alias :author :author_name + alias :user_ip :ip_address + alias :content :body + +end diff --git a/plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb b/plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb new file mode 100644 index 0000000..1da0295 --- /dev/null +++ b/plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb @@ -0,0 +1,46 @@ +require 'test_helper' + +class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase + + def setup + @comment = Comment.new( + :title => 'comment title', + :body => 'comment body', + :name => 'foo', + :email => 'foo@example.com', + :ip_address => '1.2.3.4', + :user_agent => 'Some Good Browser (I hope)', + :referrer => 'http://noosfero.org/' + ) + @wrapper = AntiSpamPlugin::CommentWrapper.new(@comment) + end + + should 'use Rakismet::Model' do + assert_includes @wrapper.class.included_modules, Rakismet::Model + end + + should 'get contents' do + assert_equal @comment.body, @wrapper.content + end + + should 'get author name' do + assert_equal @comment.author_name, @wrapper.author + end + + should 'get author email' do + assert_equal @comment.author_email, @wrapper.author_email + end + + should 'get IP address' do + assert_equal @comment.ip_address, @wrapper.user_ip + end + + should 'get User-Agent' do + assert_equal @comment.user_agent, @wrapper.user_agent + end + + should 'get get Referrer' do + assert_equal @comment.referrer, @wrapper.referrer + end + +end diff --git a/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb b/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb new file mode 100644 index 0000000..99dd85b --- /dev/null +++ b/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb @@ -0,0 +1,36 @@ +require 'test_helper' + +class AntiSpamPluginTest < ActiveSupport::TestCase + + def setup + profile = fast_create(Profile) + article = fast_create(TextileArticle, :profile_id => profile.id) + @comment = fast_create(Comment, :source_id => article.id, :source_type => 'Article') + + @settings = AntiSpamPlugin::Settings.new(@comment.environment) + @settings.api_key = 'b8b80ddb8084062d0c9119c945ce3bc3' + @settings.save! + + @plugin = AntiSpamPlugin.new + @plugin.context = @comment + end + + should 'check for spam and mark comment as spam if server says it is spam' do + AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam?).returns(true) + @comment.expects(:save!) + + @plugin.check_comment_for_spam(@comment) + assert @comment.spam + end + + should 'report spam' do + AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam!) + @plugin.comment_marked_as_spam(@comment) + end + + should 'report ham' do + AntiSpamPlugin::CommentWrapper.any_instance.expects(:ham!) + @plugin.comment_marked_as_ham(@comment) + end + +end -- libgit2 0.21.2