Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 17 files changed
  • 0 commit comments
  • 1 contributor
View
3 Gemfile
@@ -64,6 +64,9 @@ gem 'htmlentities'
gem "json"
gem "nokogiri"
+# spam protection
+gem "defender"
+
group :deployment do
gem 'capistrano'
gem 'capistrano-ext'
View
6 Gemfile.lock
@@ -99,6 +99,11 @@ GEM
cucumber (>= 0.8.0)
culerity (0.2.15)
daemons (1.1.4)
+ defender (2.0.2)
+ activemodel (>= 3.0.0)
+ defensio (~> 0.9.1)
+ defensio (0.9.1)
+ httparty (>= 0.5.0)
delayed_job (2.1.4)
activesupport (~> 3.0)
daemons
@@ -297,6 +302,7 @@ DEPENDENCIES
closure-compiler
cucumber (= 0.8.5)
cucumber-rails
+ defender
delayed_job (~> 2.1)
facebooker2
fog
View
8 app/controllers/admin/comments_controller.rb
@@ -5,11 +5,14 @@ class Admin::CommentsController < Admin::IndexController
def index
if params[:flagged_only]
- @teh_comments = Comment.paginate_by_flagged_and_censored(true,false, :order => "created_at ASC", :page => params[:page], :per_page => 100)
+ @teh_comments = Comment.where("flagged=? AND censored=?", true, false).order("created_at ASC").paginate(:page => params[:page], :per_page => 100)
@page_title = "Flagged Comments"
+ elsif params[:spam]
+ @page_title = "Spam Comments"
+ @teh_comments = Comment.spam.paginate(:page => params[:page], :per_page => 100)
else
@page_title = "Comment Moderation"
- @teh_comments = Comment.paginate_by_ok_and_censored(nil,false, :order => "created_at ASC", :page => params[:page], :per_page => 100)
+ @teh_comments = Comment.where("censored=?", false).order("created_at ASC").paginate(:page => params[:page], :per_page => 100)
end
end
@@ -62,6 +65,7 @@ def bulk_operation
unless params[:ok].nil? || params[:ok].empty?
Comment.update_all("ok = true", ["id in (?)", params[:ok]])
Comment.update_all("flagged = false", ["id in (?)", params[:ok]])
+ Comment.update_all("spam = false", ["id in (?)", params[:ok]])
end
View
23 app/controllers/comments_controller.rb
@@ -178,17 +178,30 @@ def filter_by_rating
def censor
comment = Comment.find_by_id(params[:id])
- if params[:commit] == "Censor+BanIP"
+ if params[:commit] == "BanIP+Destroy User"
+ flash[:notice] = ""
if comment && !comment.ip_address.blank? && comment.ip_address !=~ /^127./
noob = ApacheBan.create_by_ip(comment.ip_address)
- flash[:notice] = "IP #{comment.ip_address} banned"
+ flash[:notice] = "IP #{comment.ip_address} banned, "
+ end
+ userlogin = comment.user.login
+ comment.user.destroy
+
+ flash[:notice] += "#{userlogin} and comments destroyed"
+ else
+ if params[:commit] == "Censor+BanIP"
+ if comment && !comment.ip_address.blank? && comment.ip_address !=~ /^127./
+ noob = ApacheBan.create_by_ip(comment.ip_address)
+ flash[:notice] = "IP #{comment.ip_address} banned"
- else
- flash[:notice] = "No IP associated with that comment."
+ else
+ flash[:notice] = "No IP associated with that comment."
+ end
end
+
+ comment.update_attribute(:censored, true)
end
- comment.update_attribute(:censored, true)
redirect_back_or_default('/')
end
View
4 app/models/bill.rb
@@ -758,11 +758,11 @@ def unique_referrers(since = 2.days)
end
def related_articles
- Article.tagged_with(subject_terms, :order => 'created_at DESC', :limit => 5)
+ Article.tagged_with(subject_terms, :any => true).order('created_at DESC').limit(5)
end
def subject_terms
- subjects.collect{|s| s.term}.join(',')
+ subjects.collect{|s| s.term }
end
def subject
View
23 app/models/comment.rb
@@ -1,4 +1,6 @@
class Comment < ActiveRecord::Base
+ include Defender::Spammable
+
belongs_to :user
belongs_to :commentable, :polymorphic => true
@@ -22,6 +24,7 @@ class Comment < ActiveRecord::Base
scope :useless, :conditions => ["comments.plus_score_count - comments.minus_score_count DESC < 0"]
scope :most_useful, :order => ["comments.plus_score_count - comments.minus_score_count DESC"], :limit => 3
scope :uncensored, :conditions => ["censored != ?", true]
+ scope :spam, where("comments.spam = ? AND comments.defensio_sig <> ''", true).order("comments.created_at ASC")
apply_simple_captcha
validates_presence_of :comment, :message => " : You must enter a comment."
@@ -29,6 +32,26 @@ class Comment < ActiveRecord::Base
acts_as_nested_set :scope => :root
+ configure_defender :keys => {
+ 'content' => :comment,
+ 'author-ip' => :ip_address,
+ 'author-name' => :author_name,
+ 'author-email' => :author_email
+ }
+
+ # these methods are for defender to help with spam detection
+ def author_name
+ user.nil? ? nil : user.login
+ end
+
+ def author_email
+ user.nil? ? nil : user.email
+ end
+
+ def is_spam?
+ spam? and !defensio_sig.blank?
+ end
+
def score_count_sum
plus_score_count.to_i - minus_score_count.to_i
end
View
2 app/models/user.rb
@@ -60,7 +60,7 @@ class User < ActiveRecord::Base
has_many :groups, :through => :group_members
has_many :api_hits
- has_many :comments
+ has_many :comments, :dependent => :destroy
has_one :privacy_option
has_one :user_mailing_list
has_one :twitter_config
View
2 app/views/admin/comments/index.html.erb
@@ -18,7 +18,7 @@
<% end %>
<td><%= c.user ? c.user.login : "Anonymous" %></td>
<td><%= Object.const_get(c.commentable_type).find_by_id(c.commentable_id).ident %></td>
- <td><%= link_to c.created_at.to_s(:short), url_for(c.page_link_admin).gsub('admin/', '') %></td>
+ <td><%= link_to c.created_at, url_for(c.page_link_admin).gsub('admin/', '') %></td>
<td>
<%= check_box_tag "ban_ip[]", c.id %> Ban IP <br/>
<% if c.user %>
View
1 app/views/admin/index/index.html.erb
@@ -36,6 +36,7 @@
<li><%= link_to 'Approve/Reject Questionable Blog/News Articles', :controller => 'commentary', :action => 'pending' %></li>
<li><%= link_to 'Approve/Reject User Comments', :controller => 'admin/comments', :action => 'index' %></li>
<li><%= link_to 'Moderate Flagged Comments', :controller => 'admin/comments', :action => 'index', :flagged_only => true %></li>
+ <li><%= link_to 'Moderate Spam Comments', :controller => 'admin/comments', :action => 'index', :spam => true %></li>
<li><%= link_to 'Clear Cache', :controller => 'admin/commentary', :action => 'clear_cache' %></li>
<% end %>
<% if current_user.user_role.can_see_stats %>
View
8 app/views/shared/_comments_comment.html.haml
@@ -3,7 +3,9 @@
- if comment.censored
.commentfl{ :style => "clear:both;float:none;border-top:1px solid #D0D6E2;" }
%p Moderated Comment
-
+- elsif comment.is_spam?
+ .commentfl{ :style => "clear:both;float:none;border-top:1px solid #D0D6E2;" }
+ %p Spam Comment
- else
- if comment.parent_id && !flat
.replytri= image_tag 'comm_reply.png'
@@ -129,4 +131,6 @@
%div{ :style => "clear:left;" }
%form.button-to{ :action => "/comments/censor/#{h comment.id }", :method => "get"}
= submit_tag "Censor", :value => "Censor"
- = submit_tag "Censor+BanIP", :value => "Censor+BanIP"
+ = submit_tag "Censor+BanIP", :value => "Censor+BanIP"
+ = submit_tag "BanIP+Destroy User", :value => "BanIP+Destroy User"
+
View
1 config/api_keys.yml
@@ -22,6 +22,7 @@ defaults: &defaults
formageddon_password: xxx
formageddon_get_replies_key: xxx
bing: xxx
+ defensio: xxx
development:
<<: *defaults
View
3 config/application_settings.yml
@@ -54,6 +54,9 @@ development:
base_url: http://localhost:3000/
api_host: api.127localhost.com:3000
api_base_url: http://api.127localhost.com:3000/
+
+ ban_file: /tmp/banned_users.txt
+
drm:
<<: *defaults
View
2 config/environments/production.rb
@@ -33,4 +33,6 @@
config.active_support.deprecation = :notify
Paperclip.options[:command_path] = "/usr/local/bin"
+
+ GC.enable_stats if defined?(GC) && GC.respond_to?(:enable_stats)
end
View
1 config/initializers/defender.rb
@@ -0,0 +1 @@
+Defender.api_key = ApiKeys.defensio
View
13 db/migrate/20120221205815_defender_columns.rb
@@ -0,0 +1,13 @@
+class DefenderColumns < ActiveRecord::Migration
+ def self.up
+ add_column :comments, :spam, :boolean
+ add_column :comments, :defensio_sig, :string
+ add_column :comments, :spaminess, :float
+ end
+
+ def self.down
+ remove_column :comments, :spam
+ remove_column :comments, :defensio_sig
+ remove_column :comments, :spaminess
+ end
+end
View
6 spec/models/bill_spec.rb
@@ -30,9 +30,9 @@
bill.related_articles.should == [@article]
end
- it "must match all of a bill's subjects" do
- bill.stub(:subject_terms).and_return("foo,bar,other")
- bill.related_articles.should be_empty
+ it "can match any of a bill's subjects" do
+ bill.stub(:subject_terms).and_return("foo,bar,other,another,yet another")
+ bill.related_articles.should == [@article]
end
it "won't match if there are no matching tags" do
View
49 spec/models/comment_spec.rb
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+describe Comment do
+ describe "spam detection" do
+ let(:comment) { Comment.new }
+
+ before(:each) do
+ @article = Article.create!
+
+ @user = User.new(
+ :login => 'commenttest',
+ :password => 'generic',
+ :password_confirmation => 'generic',
+ :email => "commenttest@opencongress.org",
+ :zipcode => '90039',
+ :enabled => true,
+ :is_banned => false,
+ :accept_tos => true
+ )
+ @user.accepted_tos = true
+ @user.accepted_tos_at = Time.now
+
+ @user.save
+
+ @user.activate
+
+ comment.commentable = @article
+ comment.user = @user
+
+ # this api key is not the same as the one used in production
+ Defender.api_key = '7381e638d4d9163d409266b313dee312'
+ end
+
+ it "does not identify good comments as spam" do
+ comment.comment = "But behind the public pronouncements, American officials described a growing concern, even at the highest levels of the Obama administration and Pentagon, about the challenges of pulling off a troop withdrawal in Afghanistan that hinges on the close mentoring and training of army and police forces."
+ comment.save
+
+ comment.is_spam?.should == false
+ comment.defensio_sig.blank?.should == false
+ end
+
+ it "does identify spammy comments as spam" do
+ comment.comment = '<a href="http://www.kigtropin-shop.com/Wholesale-hgh_c6">HGH</a> <a href="http://www.kigtropin-shop.com/Wholesale-jintropin_c1">Jintropin</a> <a href="http://www.kigtropin-shop.com/Wholesale-hygetropin_c3">Hygetropin</a> <a href="http://www.kigtropin-shop.com/Wholesale-kigtropin_c4">Kigtropin</a> <a href="http://www.kigtropin-shop.com/Wholesale-jintropin-aq_c2">Jintropin AQ</a> <a href="http://www.kigtropin-shop.com/Wholesale-hcg_c7">HCG</a>'
+ comment.save
+
+ comment.is_spam?.should == true
+ end
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.