Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user: add method is_heavy_self_promoter? #427

Merged
merged 3 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ class User < ActiveRecord::Base
# minimum karma required to process invitation requests
MIN_KARMA_FOR_INVITATION_REQUESTS = MIN_KARMA_TO_DOWNVOTE

# proportion of posts authored by user to consider as heavy self promoter
HEAVY_SELF_PROMOTER_PROPORTION = 0.51

# minimum number of submitted stories before checking self promotion
MIN_STORIES_CHECK_SELF_PROMOTION = 2

def self.recalculate_all_karmas!
User.all.each do |u|
u.karma = u.stories.map(&:score).sum + u.comments.map(&:score).sum
Expand Down Expand Up @@ -382,6 +388,20 @@ def is_new?
Time.now - self.created_at <= NEW_USER_DAYS.days
end

def is_heavy_self_promoter?
total_count = self.stories_submitted_count

if total_count < MIN_STORIES_CHECK_SELF_PROMOTION
false
else
authored = Story.where(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails allows you to add to an association scope, so this could be self.stories.where(:user_is_author => true).count.

{ :user_id => self.id, :user_is_author => true }
).count

authored.to_f / total_count >= HEAVY_SELF_PROMOTER_PROPORTION
end
end

def linkified_about
Markdowner.to_html(self.about)
end
Expand Down
26 changes: 26 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,30 @@
u = User.make!(:banned)
expect(u.unban_by_user!(User.first)).to be true
end

it "tells if a user is a heavy self promoter" do
u = User.make!

expect(u.is_heavy_self_promoter?).to be false

Story.make!(:title => "ti1", :url => "https://a.com/1", :user_id => u.id,
:user_is_author => true)
# require at least 2 stories to be considered heavy self promoter
expect(u.is_heavy_self_promoter?).to be false

Story.make!(:title => "ti2", :url => "https://a.com/2", :user_id => u.id,
:user_is_author => true)
# 100% of 2 stories
expect(u.is_heavy_self_promoter?).to be true

Story.make!(:title => "ti3", :url => "https://a.com/3", :user_id => u.id,
:user_is_author => false)
# 66.7% of 3 stories
expect(u.is_heavy_self_promoter?).to be true

Story.make!(:title => "ti4", :url => "https://a.com/4", :user_id => u.id,
:user_is_author => false)
# 50% of 4 stories
expect(u.is_heavy_self_promoter?).to be false
end
end