Commit 31b7f3229c5199572dc4981124c5088825f7b7ad

Authored by Victor Costa
1 parent 27793a54

rails3: fix spaminator plugin

plugins/spaminator/Gemfile 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +gem "progressbar"
plugins/spaminator/lib/spaminator_plugin.rb
@@ -15,7 +15,7 @@ class SpaminatorPlugin < Noosfero::Plugin @@ -15,7 +15,7 @@ class SpaminatorPlugin < Noosfero::Plugin
15 def self.schedule_scan(environment) 15 def self.schedule_scan(environment)
16 settings = Noosfero::Plugin::Settings.new(environment, self) 16 settings = Noosfero::Plugin::Settings.new(environment, self)
17 if !settings.scanning 17 if !settings.scanning
18 - job = Delayed::Job.enqueue(SpaminatorPlugin::ScanJob.new(environment.id), 0, settings.period.to_i.days.from_now) 18 + job = Delayed::Job.enqueue(SpaminatorPlugin::ScanJob.new(environment.id), :run_at => settings.period.to_i.days.from_now)
19 settings.scheduled_scan = job.id 19 settings.scheduled_scan = job.id
20 settings.save! 20 settings.save!
21 end 21 end
plugins/spaminator/lib/spaminator_plugin/mailer.rb
1 class SpaminatorPlugin::Mailer < Noosfero::Plugin::MailerBase 1 class SpaminatorPlugin::Mailer < Noosfero::Plugin::MailerBase
2 2
  3 + include Rails.application.routes.url_helpers
3 def inactive_person_notification(person) 4 def inactive_person_notification(person)
4 - recipients person.email  
5 - from "#{person.environment.name} <#{person.environment.contact_email}>"  
6 - subject _("[%s] You must reactivate your account.") % person.environment.name  
7 - content_type 'text/html'  
8 - body :person => person,  
9 - :environment => person.environment,  
10 - :url => url_for(:host => person.default_hostname, :controller => 'account', :action => 'forgot_password') 5 + mail(
  6 + :to => person.email,
  7 + :from => "#{person.environment.name} <#{person.environment.contact_email}>",
  8 + :subject => _("[%s] You must reactivate your account.") % person.environment.name,
  9 + :content_type => 'text/html',
  10 + :body => {:person => person,
  11 + :environment => person.environment,
  12 + :url => url_for(:host => person.default_hostname, :controller => 'account', :action => 'forgot_password')}
  13 + )
  14 + end
  15 +
  16 + class Job < Struct.new(:person, :method)
  17 + def perform
  18 + SpaminatorPlugin::Mailer.send(method, person).deliver
  19 + end
11 end 20 end
12 21
13 end 22 end
plugins/spaminator/lib/spaminator_plugin/report.rb
@@ -5,10 +5,12 @@ class SpaminatorPlugin::Report &lt; Noosfero::Plugin::ActiveRecord @@ -5,10 +5,12 @@ class SpaminatorPlugin::Report &lt; Noosfero::Plugin::ActiveRecord
5 5
6 validates_presence_of :environment 6 validates_presence_of :environment
7 7
8 - named_scope :from, lambda { |environment| {:conditions => {:environment_id => environment}}} 8 + attr_accessible :environment
9 9
10 - def after_initialize  
11 - self.failed ||= {:people => [], :comments => []} 10 + scope :from, lambda { |environment| {:conditions => {:environment_id => environment}}}
  11 +
  12 + after_initialize do |report|
  13 + report.failed = {:people => [], :comments => []} if report.failed.blank?
12 end 14 end
13 15
14 def spams 16 def spams
plugins/spaminator/lib/spaminator_plugin/spaminator.rb
  1 +# encoding: utf-8
1 require 'spaminator_plugin/mailer' 2 require 'spaminator_plugin/mailer'
2 3
3 class SpaminatorPlugin::Spaminator 4 class SpaminatorPlugin::Spaminator
@@ -13,8 +14,8 @@ class SpaminatorPlugin::Spaminator @@ -13,8 +14,8 @@ class SpaminatorPlugin::Spaminator
13 end 14 end
14 15
15 def initialize_logger(environment) 16 def initialize_logger(environment)
16 - logdir = File.join(RAILS_ROOT, 'log', SpaminatorPlugin.name.underscore)  
17 - File.makedirs(logdir) if !File.exist?(logdir) 17 + logdir = Rails.root.join('log', SpaminatorPlugin.name.underscore)
  18 + FileUtils.mkdir_p(logdir) if !File.exist?(logdir)
18 logpath = File.join(logdir, "#{environment.name.to_slug}_#{ENV['RAILS_ENV']}_#{Time.now.strftime('%F_%T')}.log") 19 logpath = File.join(logdir, "#{environment.name.to_slug}_#{ENV['RAILS_ENV']}_#{Time.now.strftime('%F_%T')}.log")
19 @logger = Logger.new(logpath) 20 @logger = Logger.new(logpath)
20 end 21 end
@@ -28,9 +29,10 @@ class SpaminatorPlugin::Spaminator @@ -28,9 +29,10 @@ class SpaminatorPlugin::Spaminator
28 def initialize(environment) 29 def initialize(environment)
29 @environment = environment 30 @environment = environment
30 @settings = Noosfero::Plugin::Settings.new(@environment, SpaminatorPlugin) 31 @settings = Noosfero::Plugin::Settings.new(@environment, SpaminatorPlugin)
31 - @report = SpaminatorPlugin::Report.new(:environment => environment, 32 + @report = SpaminatorPlugin::Report.new({:environment => environment,
32 :total_people => Person.count, 33 :total_people => Person.count,
33 - :total_comments => Comment.count) 34 + :total_comments => Comment.count},
  35 + :without_protection => true)
34 self.class.initialize_logger(environment) 36 self.class.initialize_logger(environment)
35 end 37 end
36 38
@@ -158,7 +160,7 @@ class SpaminatorPlugin::Spaminator @@ -158,7 +160,7 @@ class SpaminatorPlugin::Spaminator
158 160
159 def disable_person(person) 161 def disable_person(person)
160 if person.disable 162 if person.disable
161 - SpaminatorPlugin::Mailer.delay.deliver_inactive_person_notification(person) 163 + Delayed::Job.enqueue(SpaminatorPlugin::Mailer::Job.new(person, :inactive_person_notification))
162 end 164 end
163 end 165 end
164 166
plugins/spaminator/test/unit/spaminator_plugin/mailer_test.rb
@@ -18,7 +18,7 @@ class SpaminatorPlugin::MailerTest &lt; ActiveSupport::TestCase @@ -18,7 +18,7 @@ class SpaminatorPlugin::MailerTest &lt; ActiveSupport::TestCase
18 environment.save 18 environment.save
19 19
20 person = create_user('spammer').person 20 person = create_user('spammer').person
21 - mail = SpaminatorPlugin::Mailer.deliver_inactive_person_notification(person) 21 + mail = SpaminatorPlugin::Mailer.inactive_person_notification(person).deliver
22 22
23 assert_equal ['spammer@noosfero.org'], mail.to 23 assert_equal ['spammer@noosfero.org'], mail.to
24 assert_equal ['no-reply@noosfero.org'], mail.from 24 assert_equal ['no-reply@noosfero.org'], mail.from
plugins/spaminator/test/unit/spaminator_plugin/report_test.rb
@@ -5,11 +5,11 @@ class SpaminatorPlugin::ReportTest &lt; ActiveSupport::TestCase @@ -5,11 +5,11 @@ class SpaminatorPlugin::ReportTest &lt; ActiveSupport::TestCase
5 should 'must belong to an environment' do 5 should 'must belong to an environment' do
6 report = SpaminatorPlugin::Report.new 6 report = SpaminatorPlugin::Report.new
7 report.valid? 7 report.valid?
8 - assert report.errors.invalid?(:environment) 8 + assert report.errors.include?(:environment)
9 9
10 report.environment = Environment.default 10 report.environment = Environment.default
11 report.valid? 11 report.valid?
12 - assert !report.errors.invalid?(:environment) 12 + assert !report.errors.include?(:environment)
13 end 13 end
14 14
15 should 'have scope of all reports from an environment' do 15 should 'have scope of all reports from an environment' do
@@ -21,7 +21,7 @@ class SpaminatorPlugin::ReportTest &lt; ActiveSupport::TestCase @@ -21,7 +21,7 @@ class SpaminatorPlugin::ReportTest &lt; ActiveSupport::TestCase
21 21
22 reports = SpaminatorPlugin::Report.from(environment) 22 reports = SpaminatorPlugin::Report.from(environment)
23 23
24 - assert_equal ActiveRecord::NamedScope::Scope, reports.class 24 + assert_equal ActiveRecord::Relation, reports.class
25 assert_includes reports, r1 25 assert_includes reports, r1
26 assert_includes reports, r2 26 assert_includes reports, r2
27 assert_includes reports, r3 27 assert_includes reports, r3
plugins/spaminator/test/unit/spaminator_plugin/spaminator_test.rb
@@ -121,7 +121,7 @@ class SpaminatorPlugin::SpaminatorTest &lt; ActiveSupport::TestCase @@ -121,7 +121,7 @@ class SpaminatorPlugin::SpaminatorTest &lt; ActiveSupport::TestCase
121 121
122 should 'mark person as spammer' do 122 should 'mark person as spammer' do
123 person = create_user('spammer').person 123 person = create_user('spammer').person
124 - assert_difference AbuseComplaint.finished, :count, 1 do 124 + assert_difference 'AbuseComplaint.finished.count', 1 do
125 spaminator.send(:mark_as_spammer, person) 125 spaminator.send(:mark_as_spammer, person)
126 end 126 end
127 person.reload 127 person.reload
@@ -130,16 +130,18 @@ class SpaminatorPlugin::SpaminatorTest &lt; ActiveSupport::TestCase @@ -130,16 +130,18 @@ class SpaminatorPlugin::SpaminatorTest &lt; ActiveSupport::TestCase
130 130
131 should 'send email notification after disabling person' do 131 should 'send email notification after disabling person' do
132 person = create_user('spammer').person 132 person = create_user('spammer').person
133 - assert_difference(ActionMailer::Base.deliveries, :size, 1) do 133 + assert_difference 'ActionMailer::Base.deliveries.size', 1 do
134 spaminator.send(:disable_person, person) 134 spaminator.send(:disable_person, person)
  135 + process_delayed_job_queue
135 end 136 end
136 end 137 end
137 138
138 should 'not send email notification if person was not disabled' do 139 should 'not send email notification if person was not disabled' do
139 person = create_user('spammer').person 140 person = create_user('spammer').person
140 person.expects(:disable).returns(false) 141 person.expects(:disable).returns(false)
141 - assert_no_difference(ActionMailer::Base.deliveries, :size) do 142 + assert_no_difference 'ActionMailer::Base.deliveries.size' do
142 spaminator.send(:disable_person, person) 143 spaminator.send(:disable_person, person)
  144 + process_delayed_job_queue
143 end 145 end
144 end 146 end
145 147
plugins/spaminator/test/unit/spaminator_plugin_test.rb
@@ -12,13 +12,13 @@ class SpaminatorPluginTest &lt; ActiveSupport::TestCase @@ -12,13 +12,13 @@ class SpaminatorPluginTest &lt; ActiveSupport::TestCase
12 should 'schedule a scan if not already scanning' do 12 should 'schedule a scan if not already scanning' do
13 settings.scanning = true 13 settings.scanning = true
14 settings.save! 14 settings.save!
15 - assert_no_difference Delayed::Job, :count do 15 + assert_no_difference 'Delayed::Job.count' do
16 SpaminatorPlugin.schedule_scan(environment) 16 SpaminatorPlugin.schedule_scan(environment)
17 end 17 end
18 18
19 settings.scanning = false 19 settings.scanning = false
20 settings.save! 20 settings.save!
21 - assert_difference Delayed::Job, :count, 1 do 21 + assert_difference 'Delayed::Job.count', 1 do
22 SpaminatorPlugin.schedule_scan(environment) 22 SpaminatorPlugin.schedule_scan(environment)
23 end 23 end
24 end 24 end
plugins/spaminator/views/spaminator_plugin_admin/index.html.erb 0 → 100644
@@ -0,0 +1,60 @@ @@ -0,0 +1,60 @@
  1 +<h1><%= _('Spaminator settings')%></h1>
  2 +
  3 +<%= form_for(:settings) do |f| %>
  4 +
  5 + <div id="spaminator-config-fields">
  6 + <%= labelled_form_field _('Period (days) for scanning spammers'), f.text_field(:period, :size => 4) %>
  7 +
  8 + <p id="deploy-status">
  9 + <% if @settings.deployed.nil? %>
  10 + <%= _("Spaminator was never deployed. While deployed, Spaminator will periodically scan your social network for spams. Do it now!") %>
  11 + <% elsif @settings.deployed %>
  12 + <%= _("Spaminator is deployed.") %>
  13 + <% else %>
  14 + <%= _("Spaminator is withhold.") %>
  15 + <% end %>
  16 + </p>
  17 + <p id="next-run-status">
  18 + <% if @settings.scanning %>
  19 + <%= _("Spaminator is scanning...") %>
  20 + <% elsif @settings.deployed %>
  21 + <%= _("Next scan on %s days.") % @next_run %>
  22 + <% end %>
  23 + </p>
  24 +
  25 + <% content = @settings.scanning ? _('Scanning...') : _('Scan now!') %>
  26 + <% klass = @settings.scanning ? 'disabled' : '' %>
  27 + <%= button(:search, content, {:action => 'scan'}, :class => klass, :disabled => @settings.scanning) %>
  28 + <% if !@settings.scanning %>
  29 + <%= button(:right, _('Deploy'), {:action => 'deploy'}, :title => _('Schedule next scan for the period defined')) if !@settings.deployed %>
  30 + <%= button(:cancel, _('Withhold'), {:action => 'withhold'}, :title => _('Cancel next scheduled scans')) if @settings.deployed %>
  31 + <% end %>
  32 +
  33 + <% button_bar do %>
  34 + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
  35 + <% end %>
  36 + </div>
  37 +
  38 + <table id="spaminator-report-table">
  39 + <tr>
  40 + <th><%= _('Date') %></th>
  41 + <th><%= _('Spams') %></th>
  42 + <th><%= _('Spammers')%></th>
  43 + </tr>
  44 + <% @reports.each do |report| %>
  45 + <tr>
  46 + <td><%= report.formated_date %></td>
  47 + <td title='<%= _('Spam comments found / number of comments verified') %>'><%= "#{report.spams} / #{report.total_comments}" %></td>
  48 + <td title='<%= _('Spammers found / number of users verified') %>'><%= "#{report.spammers} / #{report.total_people}" %></td>
  49 + </tr>
  50 + <% end %>
  51 + <tr id="spaminator-reports-see-all">
  52 + <% content = @reports_count > 0 ? link_to(_('SEE FULL REPORTS (%s)') % @reports_count, :action => 'reports') : _('No Report') %>
  53 + <td colspan='3'><%= content %></td>
  54 + </tr>
  55 + </table>
  56 +
  57 + <br style='clear: both'/>
  58 +
  59 +<% end %>
  60 +
plugins/spaminator/views/spaminator_plugin_admin/index.rhtml
@@ -1,60 +0,0 @@ @@ -1,60 +0,0 @@
1 -<h1><%= _('Spaminator settings')%></h1>  
2 -  
3 -<%= form_for(:settings) do |f| %>  
4 -  
5 - <div id="spaminator-config-fields">  
6 - <%= labelled_form_field _('Period (days) for scanning spammers'), f.text_field(:period, :size => 4) %>  
7 -  
8 - <p id="deploy-status">  
9 - <% if @settings.deployed.nil? %>  
10 - <%= _("Spaminator was never deployed. While deployed, Spaminator will periodically scan your social network for spams. Do it now!") %>  
11 - <% elsif @settings.deployed %>  
12 - <%= _("Spaminator is deployed.") %>  
13 - <% else %>  
14 - <%= _("Spaminator is withhold.") %>  
15 - <% end %>  
16 - </p>  
17 - <p id="next-run-status">  
18 - <% if @settings.scanning %>  
19 - <%= _("Spaminator is scanning...") %>  
20 - <% elsif @settings.deployed %>  
21 - <%= _("Next scan on %s days.") % @next_run %>  
22 - <% end %>  
23 - </p>  
24 -  
25 - <% content = @settings.scanning ? _('Scanning...') : _('Scan now!') %>  
26 - <% klass = @settings.scanning ? 'disabled' : '' %>  
27 - <%= button(:search, content, {:action => 'scan'}, :class => klass, :disabled => @settings.scanning) %>  
28 - <% if !@settings.scanning %>  
29 - <%= button(:right, _('Deploy'), {:action => 'deploy'}, :title => _('Schedule next scan for the period defined')) if !@settings.deployed %>  
30 - <%= button(:cancel, _('Withhold'), {:action => 'withhold'}, :title => _('Cancel next scheduled scans')) if @settings.deployed %>  
31 - <% end %>  
32 -  
33 - <% button_bar do %>  
34 - <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>  
35 - <% end %>  
36 - </div>  
37 -  
38 - <table id="spaminator-report-table">  
39 - <tr>  
40 - <th><%= _('Date') %></th>  
41 - <th><%= _('Spams') %></th>  
42 - <th><%= _('Spammers')%></th>  
43 - </tr>  
44 - <% @reports.each do |report| %>  
45 - <tr>  
46 - <td><%= report.formated_date %></td>  
47 - <td title='<%= _('Spam comments found / number of comments verified') %>'><%= "#{report.spams} / #{report.total_comments}" %></td>  
48 - <td title='<%= _('Spammers found / number of users verified') %>'><%= "#{report.spammers} / #{report.total_people}" %></td>  
49 - </tr>  
50 - <% end %>  
51 - <tr id="spaminator-reports-see-all">  
52 - <% content = @reports_count > 0 ? link_to(_('SEE FULL REPORTS (%s)') % @reports_count, :action => 'reports') : _('No Report') %>  
53 - <td colspan='3'><%= content %></td>  
54 - </tr>  
55 - </table>  
56 -  
57 - <br style='clear: both'/>  
58 -  
59 -<% end %>  
60 -