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 all commits
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
17 changes: 17 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,17 @@ 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 = self.stories.where(: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
21 changes: 21 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@
</div>
<% end %>

<% if @user && @user.is_moderator? && !@showing_user.is_moderator? %>
<div style="clear: both;"></div>
<br>

<p>
<div class="legend">
Moderation Information
</div>
</p>

<label class="required">Self Promoter:</label>
<span class="d">
<% if @showing_user.is_heavy_self_promoter? %>
<strong>Yes</strong>
<% else %>
No
<% end %>
</span>
<br>
<% end %>

<% if @user && @user.is_admin? && !@showing_user.is_moderator? %>
<div style="clear: both;"></div>
<br>
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