From 52500266c38b2aa7839757cdf8a73ef10eb7875f Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 24 Oct 2013 19:14:53 -0300 Subject: [PATCH] spammable_hot_spot: encapsulating spammable hot_spots --- app/models/comment.rb | 34 +++++++++++----------------------- app/models/suggest_article.rb | 13 ------------- config/initializers/plugins.rb | 1 + lib/noosfero/plugin.rb | 41 ++++++----------------------------------- lib/noosfero/plugin/spammable.rb | 13 +++++++++++++ plugins/anti_spam/test/unit/anti_spam_plugin_test.rb | 2 ++ test/unit/comment_test.rb | 6 +++--- test/unit/suggest_article_test.rb | 12 ++++++------ 8 files changed, 42 insertions(+), 80 deletions(-) create mode 100644 lib/noosfero/plugin/spammable.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index fe55f8f..652938b 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -106,6 +106,17 @@ class Comment < ActiveRecord::Base include Noosfero::Plugin::HotSpot + include Spammable + + def after_spam! + SpammerLogger.log(ip_address, self) + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam)) + end + + def after_ham! + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham)) + end + def verify_and_notify check_for_spam unless spam? @@ -113,10 +124,6 @@ class Comment < ActiveRecord::Base end end - def check_for_spam - plugins.dispatch(:check_comment_for_spam, self) - end - def notify_by_mail if source.kind_of?(Article) && article.notify_comments? if !notification_emails.empty? @@ -203,25 +210,6 @@ class Comment < ActiveRecord::Base @rejected = true end - include Spammable - - def after_spam! - SpammerLogger.log(ip_address, self) - Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam)) - end - - def after_ham! - Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham)) - end - - def marked_as_spam - plugins.dispatch(:comment_marked_as_spam, self) - end - - def marked_as_ham - plugins.dispatch(:comment_marked_as_ham, self) - end - def need_moderation? article.moderate_comments? && (author.nil? || article.author != author) end diff --git a/app/models/suggest_article.rb b/app/models/suggest_article.rb index c2b8deb..0ed3654 100644 --- a/app/models/suggest_article.rb +++ b/app/models/suggest_article.rb @@ -23,10 +23,6 @@ class SuggestArticle < Task include Noosfero::Plugin::HotSpot - def check_for_spam - plugins.dispatch(:check_suggest_article_for_spam, self) - end - def sender "#{name} (#{email})" end @@ -84,13 +80,4 @@ class SuggestArticle < Task def after_ham! self.delay.marked_as_ham end - - def marked_as_spam - plugins.dispatch(:suggest_article_marked_as_spam, self) - end - - def marked_as_ham - plugins.dispatch(:suggest_article_marked_as_ham, self) - end - end diff --git a/config/initializers/plugins.rb b/config/initializers/plugins.rb index 7c95396..df05799 100644 --- a/config/initializers/plugins.rb +++ b/config/initializers/plugins.rb @@ -4,4 +4,5 @@ require 'noosfero/plugin/manager' require 'noosfero/plugin/active_record' require 'noosfero/plugin/mailer_base' require 'noosfero/plugin/settings' +require 'noosfero/plugin/spammable' Noosfero::Plugin.init_system if $NOOSFERO_LOAD_PLUGINS diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index 034f444..765a260 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -308,45 +308,16 @@ class Noosfero::Plugin scope end - # This method is called by the CommentHandler background job before sending - # the notification email. If the comment is marked as spam (i.e. by calling - # comment.spam!), then the notification email will *not* be sent. - # - # example: - # - # def check_comment_for_spam(comment) - # if anti_spam_service.is_spam?(comment) - # comment.spam! - # end - # end - # - def check_comment_for_spam(comment) + # -> Allows plugins to check weather object is a spam + def check_for_spam(object) end - # This method is called when the user manually marks a comment as SPAM. A - # plugin implementing this method should train its spam detection mechanism - # by submitting this comment as a confirmed spam. - # - # example: - # - # def comment_marked_as_spam(comment) - # anti_spam_service.train_with_spam(comment) - # end - # - def comment_marked_as_spam(comment) + # -> Allows plugins to know when an object is marked as a spam + def marked_as_spam(object) end - # This method is called when the user manually marks a comment a NOT SPAM. A - # plugin implementing this method should train its spam detection mechanism - # by submitting this coimment as a confirmed ham. - # - # example: - # - # def comment_marked_as_ham(comment) - # anti_spam_service.train_with_ham(comment) - # end - # - def comment_marked_as_ham(comment) + # -> Allows plugins to know when an object is marked as a ham + def marked_as_ham(object) end # Adds extra actions for comments diff --git a/lib/noosfero/plugin/spammable.rb b/lib/noosfero/plugin/spammable.rb new file mode 100644 index 0000000..78d38a6 --- /dev/null +++ b/lib/noosfero/plugin/spammable.rb @@ -0,0 +1,13 @@ +Spammable.module_eval do + def marked_as_spam + plugins.dispatch(:marked_as_spam, self) + end + + def marked_as_ham + plugins.dispatch(:marked_as_ham, self) + end + + def check_for_spam + plugins.dispatch(:check_for_spam, self) + 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 index 0101578..6cb88e4 100644 --- a/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb +++ b/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb @@ -4,6 +4,8 @@ class AntiSpamPluginTest < ActiveSupport::TestCase class SpammableContent attr_accessor :spam + def self.named_scope(*args); end + include Spammable def save!; end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index fdd48f0..60055cb 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -447,7 +447,7 @@ class CommentTest < ActiveSupport::TestCase end class EverythingIsSpam < Noosfero::Plugin - def check_comment_for_spam(comment) + def check_for_spam(comment) comment.spam! end end @@ -469,11 +469,11 @@ class CommentTest < ActiveSupport::TestCase attr_accessor :marked_as_ham end - def comment_marked_as_spam(c) + def marked_as_spam(c) self.class.marked_as_spam = c end - def comment_marked_as_ham(c) + def marked_as_ham(c) self.class.marked_as_ham = c end end diff --git a/test/unit/suggest_article_test.rb b/test/unit/suggest_article_test.rb index bddee59..ce55acf 100644 --- a/test/unit/suggest_article_test.rb +++ b/test/unit/suggest_article_test.rb @@ -151,8 +151,8 @@ class SuggestArticleTest < ActiveSupport::TestCase end class EverythingIsSpam < Noosfero::Plugin - def check_suggest_article_for_spam(suggest_article) - suggest_article.spam! + def check_for_spam(object) + object.spam! end end @@ -161,7 +161,7 @@ class SuggestArticleTest < ActiveSupport::TestCase t1 = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com') - EverythingIsSpam.any_instance.expects(:check_suggest_article_for_spam) + EverythingIsSpam.any_instance.expects(:check_for_spam) t1.check_for_spam end @@ -172,15 +172,15 @@ class SuggestArticleTest < ActiveSupport::TestCase attr_accessor :marked_as_ham end - def check_suggest_article_for_spam(c) + def check_for_spam(c) # do nothing end - def suggest_article_marked_as_spam(c) + def marked_as_spam(c) self.class.marked_as_spam = c end - def suggest_article_marked_as_ham(c) + def marked_as_ham(c) self.class.marked_as_ham = c end end -- libgit2 0.21.2