Commit 2e047f5c05a15644c7caa843e9c5efb44e465b58
1 parent
1934fc88
Exists in
master
and in
29 other branches
Implemented the antispam backend with Rakismet
ActionItem2306
Showing
5 changed files
with
122 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1 @@ |
1 | +require 'rakismet' | ... | ... |
plugins/anti_spam/lib/anti_spam_plugin.rb
... | ... | @@ -8,4 +8,32 @@ class AntiSpamPlugin < Noosfero::Plugin |
8 | 8 | _("Checks comments against a spam checking service compatible with the Akismet API") |
9 | 9 | end |
10 | 10 | |
11 | + def check_comment_for_spam(comment) | |
12 | + if rakismet_call(comment, :spam?) | |
13 | + comment.spam = true | |
14 | + comment.save! | |
15 | + end | |
16 | + end | |
17 | + | |
18 | + def comment_marked_as_spam(comment) | |
19 | + rakismet_call(comment, :spam!) | |
20 | + end | |
21 | + | |
22 | + def comment_marked_as_ham(comment) | |
23 | + rakismet_call(comment, :ham!) | |
24 | + end | |
25 | + | |
26 | + protected | |
27 | + | |
28 | + def rakismet_call(comment, op) | |
29 | + settings = AntiSpamPlugin::Settings.new(comment.environment) | |
30 | + | |
31 | + Rakismet.host = settings.host | |
32 | + Rakismet.key = settings.api_key | |
33 | + Rakismet.url = comment.environment.top_url | |
34 | + | |
35 | + submission = AntiSpamPlugin::CommentWrapper.new(comment) | |
36 | + submission.send(op) | |
37 | + end | |
38 | + | |
11 | 39 | end | ... | ... |
plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb
0 → 100644
... | ... | @@ -0,0 +1,11 @@ |
1 | +class AntiSpamPlugin::CommentWrapper < Struct.new(:comment) | |
2 | + | |
3 | + delegate :author_name, :author_email, :title, :body, :ip_address, :user_agent, :referrer, :to => :comment | |
4 | + | |
5 | + include Rakismet::Model | |
6 | + | |
7 | + alias :author :author_name | |
8 | + alias :user_ip :ip_address | |
9 | + alias :content :body | |
10 | + | |
11 | +end | ... | ... |
plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb
0 → 100644
... | ... | @@ -0,0 +1,46 @@ |
1 | +require 'test_helper' | |
2 | + | |
3 | +class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase | |
4 | + | |
5 | + def setup | |
6 | + @comment = Comment.new( | |
7 | + :title => 'comment title', | |
8 | + :body => 'comment body', | |
9 | + :name => 'foo', | |
10 | + :email => 'foo@example.com', | |
11 | + :ip_address => '1.2.3.4', | |
12 | + :user_agent => 'Some Good Browser (I hope)', | |
13 | + :referrer => 'http://noosfero.org/' | |
14 | + ) | |
15 | + @wrapper = AntiSpamPlugin::CommentWrapper.new(@comment) | |
16 | + end | |
17 | + | |
18 | + should 'use Rakismet::Model' do | |
19 | + assert_includes @wrapper.class.included_modules, Rakismet::Model | |
20 | + end | |
21 | + | |
22 | + should 'get contents' do | |
23 | + assert_equal @comment.body, @wrapper.content | |
24 | + end | |
25 | + | |
26 | + should 'get author name' do | |
27 | + assert_equal @comment.author_name, @wrapper.author | |
28 | + end | |
29 | + | |
30 | + should 'get author email' do | |
31 | + assert_equal @comment.author_email, @wrapper.author_email | |
32 | + end | |
33 | + | |
34 | + should 'get IP address' do | |
35 | + assert_equal @comment.ip_address, @wrapper.user_ip | |
36 | + end | |
37 | + | |
38 | + should 'get User-Agent' do | |
39 | + assert_equal @comment.user_agent, @wrapper.user_agent | |
40 | + end | |
41 | + | |
42 | + should 'get get Referrer' do | |
43 | + assert_equal @comment.referrer, @wrapper.referrer | |
44 | + end | |
45 | + | |
46 | +end | ... | ... |
... | ... | @@ -0,0 +1,36 @@ |
1 | +require 'test_helper' | |
2 | + | |
3 | +class AntiSpamPluginTest < ActiveSupport::TestCase | |
4 | + | |
5 | + def setup | |
6 | + profile = fast_create(Profile) | |
7 | + article = fast_create(TextileArticle, :profile_id => profile.id) | |
8 | + @comment = fast_create(Comment, :source_id => article.id, :source_type => 'Article') | |
9 | + | |
10 | + @settings = AntiSpamPlugin::Settings.new(@comment.environment) | |
11 | + @settings.api_key = 'b8b80ddb8084062d0c9119c945ce3bc3' | |
12 | + @settings.save! | |
13 | + | |
14 | + @plugin = AntiSpamPlugin.new | |
15 | + @plugin.context = @comment | |
16 | + end | |
17 | + | |
18 | + should 'check for spam and mark comment as spam if server says it is spam' do | |
19 | + AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam?).returns(true) | |
20 | + @comment.expects(:save!) | |
21 | + | |
22 | + @plugin.check_comment_for_spam(@comment) | |
23 | + assert @comment.spam | |
24 | + end | |
25 | + | |
26 | + should 'report spam' do | |
27 | + AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam!) | |
28 | + @plugin.comment_marked_as_spam(@comment) | |
29 | + end | |
30 | + | |
31 | + should 'report ham' do | |
32 | + AntiSpamPlugin::CommentWrapper.any_instance.expects(:ham!) | |
33 | + @plugin.comment_marked_as_ham(@comment) | |
34 | + end | |
35 | + | |
36 | +end | ... | ... |