Skip to content

Commit 83771bd

Browse files
committed
Merge branch 'nicolasiensen-bounty_84'
2 parents d5decad + 1cfb252 commit 83771bd

15 files changed

+98
-2
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ TWITTER_REDIRECT_URL=http://localhost:3000/auth/twitter/callback
4343

4444
SESSION_SECRET=session_secret
4545

46+
AKISMET_KEY=your_akismet_key
47+
AKISMET_URL=http://localhost:3000/
4648

4749
WEB_ROOT=/home/vagrant/web/
4850
WEB_MIN_CONCURRENCY=0
4951
WEB_MAX_CONCURRENCY=16
5052
WEB_WORKERS=8
51-
WEB_PORT=tcp://0.0.0.0:3000
53+
WEB_PORT=tcp://0.0.0.0:3000

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ gem 'octokit', '~> 1.23.0'
122122
gem 'pubnub', '0.1.9'
123123
gem 'querystring'
124124
gem 'rails_autolink'
125+
gem 'rakismet'
125126
gem 'ruby-progressbar'
126127
gem 'sanitize'
127128
gem 'simple_form'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ GEM
432432
rdoc (~> 3.4)
433433
thor (>= 0.14.6, < 2.0)
434434
rake (10.3.2)
435+
rakismet (1.5.0)
435436
rb-fsevent (0.9.4)
436437
rb-inotify (0.9.3)
437438
ffi (>= 0.5.0)
@@ -659,6 +660,7 @@ DEPENDENCIES
659660
rails-erd
660661
rails_12factor
661662
rails_autolink
663+
rakismet
662664
redcarpet
663665
redis
664666
resque

app/jobs/analyze_spam.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AnalyzeSpam < Struct.new(:spammable)
2+
extend ResqueSupport::Basic
3+
4+
@queue = 'MEDIUM'
5+
6+
def perform
7+
spammable.create_spam_report if spammable.spam?
8+
end
9+
end

app/models/comment.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
class Comment < ActiveRecord::Base
3232
include ResqueSupport::Basic
3333
include ActsAsCommentable::Comment
34+
include Rakismet::Model
3435

3536
belongs_to :commentable, polymorphic: true
3637
has_many :likes, as: :likable, dependent: :destroy, after_add: :update_likes_cache, after_remove: :update_likes_cache
38+
has_one :spam_report, as: :spammable
3739
after_create :generate_event
40+
after_create :analyze_spam
3841
after_save :commented_callback
3942

4043
default_scope order: 'likes_cache DESC, created_at ASC'
@@ -44,6 +47,14 @@ class Comment < ActiveRecord::Base
4447
alias_method :author, :user
4548
alias_attribute :body, :comment
4649

50+
rakismet_attrs author: proc { self.user.name },
51+
author_email: proc { self.user.email },
52+
content: :comment,
53+
blog: ENV['AKISMET_URL']
54+
# TODO: add columns ip and http_user_agent into the users table
55+
# user_ip: proc { self.user.ip }
56+
# user_agent: proc { self.user.http_user_agent }
57+
4758
validates :comment, length: { minimum: 2 }
4859

4960
def self.latest_comments_as_strings(count=5)
@@ -172,4 +183,8 @@ def event_type(options={})
172183
:new_comment
173184
end
174185
end
186+
187+
def analyze_spam
188+
Resque.enqueue(AnalyzeSpam, self)
189+
end
175190
end

app/models/protip.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Protip < ActiveRecord::Base
4747
include ResqueSupport::Basic
4848
include Scoring::HotStream
4949
include SearchModule
50+
include Rakismet::Model
5051

5152
acts_as_commentable
5253

@@ -116,8 +117,17 @@ class Protip < ActiveRecord::Base
116117

117118
has_many :likes, as: :likable, dependent: :destroy, after_add: :reset_likes_cache, after_remove: :reset_likes_cache
118119
has_many :protip_links, autosave: true, dependent: :destroy, after_add: :reset_links_cache, after_remove: :reset_links_cache
120+
has_one :spam_report, as: :spammable
119121
belongs_to :user
120122

123+
rakismet_attrs author: proc { self.user.name },
124+
author_email: proc { self.user.email },
125+
content: :body,
126+
blog: ENV['AKISMET_URL']
127+
# TODO: add columns ip and http_user_agent into the users table
128+
# user_ip: proc { self.user.ip }
129+
# user_agent: proc { self.user.http_user_agent }
130+
121131
attr_taggable :topics, :users
122132
attr_accessor :upvotes
123133

@@ -164,6 +174,7 @@ class Protip < ActiveRecord::Base
164174
after_save :unqueue_flagged, if: :flagged?
165175
after_destroy :index_search_after_destroy
166176
after_create :update_network
177+
after_create :analyze_spam
167178
# End of test failing lines
168179

169180
attr_accessor :upvotes_value
@@ -1101,6 +1112,10 @@ def adjust_like_value(user, like_value)
11011112
user.is_a?(User) && self.author.team_member_of?(user) ? [like_value/2, 1].max : like_value
11021113
end
11031114

1115+
def analyze_spam
1116+
Resque.enqueue(AnalyzeSpam, self)
1117+
end
1118+
11041119
class SearchWrapper
11051120
attr_reader :item
11061121

app/models/spam_report.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SpamReport < ActiveRecord::Base
2+
belongs_to :spammable, polymorphic: true
3+
end

config/application.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class Application < Rails::Application
4444
Hirb.enable
4545
end
4646
end
47+
48+
config.rakismet.key = ENV['AKISMET_KEY']
49+
config.rakismet.url = ENV['AKISMET_URL']
4750
end
4851
end
4952

db/schema.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@
330330
add_index "skills", ["deleted", "user_id"], :name => "index_skills_on_deleted_and_user_id"
331331
add_index "skills", ["user_id"], :name => "index_skills_on_user_id"
332332

333+
create_table "spam_reports", :force => true do |t|
334+
t.integer "spammable_id", :null => false
335+
t.string "spammable_type", :null => false
336+
t.datetime "created_at", :null => false
337+
t.datetime "updated_at", :null => false
338+
end
339+
333340
create_table "taggings", :force => true do |t|
334341
t.integer "tag_id"
335342
t.integer "taggable_id"

spec/controllers/users_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
post :create, user: {}
237237

238238
assigns[:user].username.should == 'mdeiters'
239-
assigns[:user].thumbnail_url.should == 'http://a1.twimg.com/profile_images/1672080012/instagram_profile_normal.jpg'
239+
assigns[:user].thumbnail_url.should == 'https://si0.twimg.com/profile_images/1672080012/instagram_profile_normal.jpg'
240240
assigns[:user].twitter.should == 'mdeiters'
241241
assigns[:user].twitter_token.should == '6271932-8erxrXfJykBNMrvsdCEq5WqKd6FIcO97L9BzvPq7'
242242
assigns[:user].twitter_secret.should == '8fRS1ZARd6Wm53wvvDwHNrBmZcW0H2aSwmQjuOTHl'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fabricator(:comment) do
2+
comment { 'Lorem Ipsum is simply dummy text...' }
3+
commentable! { Fabricate(:protip) }
4+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fabricator(:spam_report) do
2+
end

spec/jobs/analyze_spam_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
describe AnalyzeSpam do
2+
describe '#perform' do
3+
context 'when it is a spam' do
4+
it 'should create a spam report' do
5+
Protip.any_instance.stub(:index_search)
6+
spammable = Fabricate(:comment)
7+
spammable.stub(:spam?).and_return(true)
8+
AnalyzeSpam.new(spammable).perform
9+
spammable.spam_report.should_not be_nil
10+
end
11+
end
12+
13+
context 'when it is not a spam' do
14+
15+
it 'should not create a spam report' do
16+
Protip.any_instance.stub(:index_search)
17+
spammable = Fabricate(:comment)
18+
spammable.stub(:spam?).and_return(false)
19+
spammable.spam_report.should be_nil
20+
end
21+
end
22+
end
23+
end

spec/models/comment_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'spec_helper'
2+
3+
describe Comment do
4+
its(:spam_report) { should be_nil }
5+
end

spec/models/spam_report_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'spec_helper'
2+
3+
describe SpamReport do
4+
its(:spammable) { should be_nil }
5+
end

0 commit comments

Comments
 (0)