Commit 52500266c38b2aa7839757cdf8a73ef10eb7875f

Authored by Rodrigo Souto
1 parent eb86a964

spammable_hot_spot: encapsulating spammable hot_spots

app/models/comment.rb
... ... @@ -106,6 +106,17 @@ class Comment < ActiveRecord::Base
106 106  
107 107 include Noosfero::Plugin::HotSpot
108 108  
  109 + include Spammable
  110 +
  111 + def after_spam!
  112 + SpammerLogger.log(ip_address, self)
  113 + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam))
  114 + end
  115 +
  116 + def after_ham!
  117 + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham))
  118 + end
  119 +
109 120 def verify_and_notify
110 121 check_for_spam
111 122 unless spam?
... ... @@ -113,10 +124,6 @@ class Comment < ActiveRecord::Base
113 124 end
114 125 end
115 126  
116   - def check_for_spam
117   - plugins.dispatch(:check_comment_for_spam, self)
118   - end
119   -
120 127 def notify_by_mail
121 128 if source.kind_of?(Article) && article.notify_comments?
122 129 if !notification_emails.empty?
... ... @@ -203,25 +210,6 @@ class Comment < ActiveRecord::Base
203 210 @rejected = true
204 211 end
205 212  
206   - include Spammable
207   -
208   - def after_spam!
209   - SpammerLogger.log(ip_address, self)
210   - Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam))
211   - end
212   -
213   - def after_ham!
214   - Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham))
215   - end
216   -
217   - def marked_as_spam
218   - plugins.dispatch(:comment_marked_as_spam, self)
219   - end
220   -
221   - def marked_as_ham
222   - plugins.dispatch(:comment_marked_as_ham, self)
223   - end
224   -
225 213 def need_moderation?
226 214 article.moderate_comments? && (author.nil? || article.author != author)
227 215 end
... ...
app/models/suggest_article.rb
... ... @@ -23,10 +23,6 @@ class SuggestArticle < Task
23 23  
24 24 include Noosfero::Plugin::HotSpot
25 25  
26   - def check_for_spam
27   - plugins.dispatch(:check_suggest_article_for_spam, self)
28   - end
29   -
30 26 def sender
31 27 "#{name} (#{email})"
32 28 end
... ... @@ -84,13 +80,4 @@ class SuggestArticle < Task
84 80 def after_ham!
85 81 self.delay.marked_as_ham
86 82 end
87   -
88   - def marked_as_spam
89   - plugins.dispatch(:suggest_article_marked_as_spam, self)
90   - end
91   -
92   - def marked_as_ham
93   - plugins.dispatch(:suggest_article_marked_as_ham, self)
94   - end
95   -
96 83 end
... ...
config/initializers/plugins.rb
... ... @@ -4,4 +4,5 @@ require 'noosfero/plugin/manager'
4 4 require 'noosfero/plugin/active_record'
5 5 require 'noosfero/plugin/mailer_base'
6 6 require 'noosfero/plugin/settings'
  7 +require 'noosfero/plugin/spammable'
7 8 Noosfero::Plugin.init_system if $NOOSFERO_LOAD_PLUGINS
... ...
lib/noosfero/plugin.rb
... ... @@ -308,45 +308,16 @@ class Noosfero::Plugin
308 308 scope
309 309 end
310 310  
311   - # This method is called by the CommentHandler background job before sending
312   - # the notification email. If the comment is marked as spam (i.e. by calling
313   - # <tt>comment.spam!</tt>), then the notification email will *not* be sent.
314   - #
315   - # example:
316   - #
317   - # def check_comment_for_spam(comment)
318   - # if anti_spam_service.is_spam?(comment)
319   - # comment.spam!
320   - # end
321   - # end
322   - #
323   - def check_comment_for_spam(comment)
  311 + # -> Allows plugins to check weather object is a spam
  312 + def check_for_spam(object)
324 313 end
325 314  
326   - # This method is called when the user manually marks a comment as SPAM. A
327   - # plugin implementing this method should train its spam detection mechanism
328   - # by submitting this comment as a confirmed spam.
329   - #
330   - # example:
331   - #
332   - # def comment_marked_as_spam(comment)
333   - # anti_spam_service.train_with_spam(comment)
334   - # end
335   - #
336   - def comment_marked_as_spam(comment)
  315 + # -> Allows plugins to know when an object is marked as a spam
  316 + def marked_as_spam(object)
337 317 end
338 318  
339   - # This method is called when the user manually marks a comment a NOT SPAM. A
340   - # plugin implementing this method should train its spam detection mechanism
341   - # by submitting this coimment as a confirmed ham.
342   - #
343   - # example:
344   - #
345   - # def comment_marked_as_ham(comment)
346   - # anti_spam_service.train_with_ham(comment)
347   - # end
348   - #
349   - def comment_marked_as_ham(comment)
  319 + # -> Allows plugins to know when an object is marked as a ham
  320 + def marked_as_ham(object)
350 321 end
351 322  
352 323 # Adds extra actions for comments
... ...
lib/noosfero/plugin/spammable.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +Spammable.module_eval do
  2 + def marked_as_spam
  3 + plugins.dispatch(:marked_as_spam, self)
  4 + end
  5 +
  6 + def marked_as_ham
  7 + plugins.dispatch(:marked_as_ham, self)
  8 + end
  9 +
  10 + def check_for_spam
  11 + plugins.dispatch(:check_for_spam, self)
  12 + end
  13 +end
... ...
plugins/anti_spam/test/unit/anti_spam_plugin_test.rb
... ... @@ -4,6 +4,8 @@ class AntiSpamPluginTest &lt; ActiveSupport::TestCase
4 4  
5 5 class SpammableContent
6 6 attr_accessor :spam
  7 + def self.named_scope(*args); end
  8 +
7 9 include Spammable
8 10  
9 11 def save!; end
... ...
test/unit/comment_test.rb
... ... @@ -447,7 +447,7 @@ class CommentTest &lt; ActiveSupport::TestCase
447 447 end
448 448  
449 449 class EverythingIsSpam < Noosfero::Plugin
450   - def check_comment_for_spam(comment)
  450 + def check_for_spam(comment)
451 451 comment.spam!
452 452 end
453 453 end
... ... @@ -469,11 +469,11 @@ class CommentTest &lt; ActiveSupport::TestCase
469 469 attr_accessor :marked_as_ham
470 470 end
471 471  
472   - def comment_marked_as_spam(c)
  472 + def marked_as_spam(c)
473 473 self.class.marked_as_spam = c
474 474 end
475 475  
476   - def comment_marked_as_ham(c)
  476 + def marked_as_ham(c)
477 477 self.class.marked_as_ham = c
478 478 end
479 479 end
... ...
test/unit/suggest_article_test.rb
... ... @@ -151,8 +151,8 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
151 151 end
152 152  
153 153 class EverythingIsSpam < Noosfero::Plugin
154   - def check_suggest_article_for_spam(suggest_article)
155   - suggest_article.spam!
  154 + def check_for_spam(object)
  155 + object.spam!
156 156 end
157 157 end
158 158  
... ... @@ -161,7 +161,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
161 161  
162 162 t1 = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com')
163 163  
164   - EverythingIsSpam.any_instance.expects(:check_suggest_article_for_spam)
  164 + EverythingIsSpam.any_instance.expects(:check_for_spam)
165 165  
166 166 t1.check_for_spam
167 167 end
... ... @@ -172,15 +172,15 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
172 172 attr_accessor :marked_as_ham
173 173 end
174 174  
175   - def check_suggest_article_for_spam(c)
  175 + def check_for_spam(c)
176 176 # do nothing
177 177 end
178 178  
179   - def suggest_article_marked_as_spam(c)
  179 + def marked_as_spam(c)
180 180 self.class.marked_as_spam = c
181 181 end
182 182  
183   - def suggest_article_marked_as_ham(c)
  183 + def marked_as_ham(c)
184 184 self.class.marked_as_ham = c
185 185 end
186 186 end
... ...