FEATURE: trust level 3: likes received must be on min_likes_received/3 different days, and be from min_likes_received/4 different users

This commit is contained in:
Neil Lalonde 2014-10-07 15:52:54 -04:00
parent 056438a7f4
commit a30e70e8f1
6 changed files with 89 additions and 9 deletions

View File

@ -19,6 +19,8 @@ Discourse.TL3Requirements = Discourse.Model.extend({
flagged_by_users: this.get('num_flagged_by_users') <= this.get('max_flagged_by_users'),
likes_given: this.get('num_likes_given') >= this.get('min_likes_given'),
likes_received: this.get('num_likes_received') >= this.get('min_likes_received'),
likes_received_days: this.get('num_likes_received_days') >= this.get('min_likes_received_days'),
likes_received_users: this.get('num_likes_received_users') >= this.get('min_likes_received_users'),
level_locked: this.get('trust_level_locked')
};
}.property('days_visited', 'min_days_visited',
@ -31,5 +33,8 @@ Discourse.TL3Requirements = Discourse.Model.extend({
'num_flagged_by_users', 'max_flagged_by_users',
'num_likes_given', 'min_likes_given',
'num_likes_received', 'min_likes_received',
'num_likes_received', 'min_likes_received',
'num_likes_received_days', 'min_likes_received_days',
'num_likes_received_users', 'min_likes_received_users',
'trust_level_locked')
});

View File

@ -13,7 +13,7 @@
<p>{{i18n admin.user.tl3_requirements.table_title}}</p>
{{#with tl3Requirements}}
<table class="table" style="width: 50%;">
<table class="table" style="width: auto;">
<thead>
<tr>
<th></th>
@ -85,6 +85,18 @@
<td>{{num_likes_received}}</td>
<td>{{min_likes_received}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.likes_received_days}}</th>
<td><i {{bind-attr class=":fa met.likes_received_days:fa-check:fa-times"}}></i></td>
<td>{{num_likes_received_days}}</td>
<td>{{min_likes_received_days}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.likes_received_users}}</th>
<td><i {{bind-attr class=":fa met.likes_received_users:fa-check:fa-times"}}></i></td>
<td>{{num_likes_received_users}}</td>
<td>{{min_likes_received_users}}</td>
</tr>
</tbody>
</table>
{{/with}}

View File

@ -17,6 +17,9 @@ class TrustLevel3Requirements
:num_flagged_posts, :max_flagged_posts,
:num_likes_given, :min_likes_given,
:num_likes_received, :min_likes_received,
:num_likes_received, :min_likes_received,
:num_likes_received_days, :min_likes_received_days,
:num_likes_received_users, :min_likes_received_users,
:trust_level_locked, :on_grace_period
def initialize(user)
@ -35,7 +38,9 @@ class TrustLevel3Requirements
topics_viewed_all_time >= min_topics_viewed_all_time &&
posts_read_all_time >= min_posts_read_all_time &&
num_likes_given >= min_likes_given &&
num_likes_received >= min_likes_received
num_likes_received >= min_likes_received &&
num_likes_received_users >= min_likes_received_users &&
num_likes_received_days >= min_likes_received_days
end
def requirements_lost?
@ -50,7 +55,9 @@ class TrustLevel3Requirements
topics_viewed_all_time < min_topics_viewed_all_time ||
posts_read_all_time < min_posts_read_all_time ||
num_likes_given < min_likes_given * LOW_WATER_MARK ||
num_likes_received < min_likes_received * LOW_WATER_MARK
num_likes_received < min_likes_received * LOW_WATER_MARK ||
num_likes_received_users < min_likes_received_users * LOW_WATER_MARK ||
num_likes_received_days < min_likes_received_days * LOW_WATER_MARK
end
def trust_level_locked
@ -147,14 +154,36 @@ class TrustLevel3Requirements
SiteSetting.tl3_requires_likes_given
end
def num_likes_received_query
UserAction.where(user_id: @user.id, action_type: UserAction::WAS_LIKED).where('created_at > ?', TIME_PERIOD.days.ago)
end
def num_likes_received
UserAction.where(user_id: @user.id, action_type: UserAction::WAS_LIKED).where('created_at > ?', TIME_PERIOD.days.ago).count
num_likes_received_query.count
end
def min_likes_received
SiteSetting.tl3_requires_likes_received
end
def num_likes_received_days
# don't do a COUNT(DISTINCT date(created_at)) here!
num_likes_received_query.pluck('date(created_at)').uniq.size
end
def min_likes_received_days
(min_likes_received.to_f / 3.0).ceil
end
def num_likes_received_users
# don't do a COUNT(DISTINCT acting_user_id) here!
num_likes_received_query.pluck(:acting_user_id).uniq.size
end
def min_likes_received_users
(min_likes_received.to_f / 4.0).ceil
end
def self.clear_cache
$redis.del NUM_TOPICS_KEY

View File

@ -12,7 +12,9 @@ class TrustLevel3RequirementsSerializer < ApplicationSerializer
:num_flagged_posts, :max_flagged_posts,
:num_flagged_by_users, :max_flagged_by_users,
:num_likes_given, :min_likes_given,
:num_likes_received, :min_likes_received
:num_likes_received, :min_likes_received,
:num_likes_received_days, :min_likes_received_days,
:num_likes_received_users, :min_likes_received_users
def time_period
TrustLevel3Requirements::TIME_PERIOD

View File

@ -1989,6 +1989,8 @@ en:
flagged_by_users: "Users Who Flagged"
likes_given: "Likes Given"
likes_received: "Likes Received"
likes_received_days: "Likes Received: unique days"
likes_received_users: "Likes Received: unique users"
qualifies: "Qualifies for trust level 3."
does_not_qualify: "Doesn't qualify for trust level 3."
will_be_promoted: "Will be promoted soon."

View File

@ -59,6 +59,8 @@ describe TrustLevel3Requirements do
it "min_likes_received depends on site setting" do
SiteSetting.stubs(:tl3_requires_likes_received).returns(20)
tl3_requirements.min_likes_received.should == 20
tl3_requirements.min_likes_received_days.should == 7
tl3_requirements.min_likes_received_users.should == 5
end
end
@ -188,11 +190,15 @@ describe TrustLevel3Requirements do
recent_post1 = create_post(topic: t, user: user, created_at: 1.hour.ago)
liker = Fabricate(:user)
Fabricate(:like, user: liker, post: recent_post1, created_at: 2.hours.ago)
Fabricate(:like, user: liker, post: recent_post2, created_at: 5.days.ago)
Fabricate(:like, user: liker, post: old_post, created_at: 101.days.ago)
liker2 = Fabricate(:user)
Fabricate(:like, user: liker, post: recent_post1, created_at: 2.hours.ago)
Fabricate(:like, user: liker2, post: recent_post1, created_at: 2.hours.ago)
Fabricate(:like, user: liker, post: recent_post2, created_at: 5.days.ago)
Fabricate(:like, user: liker, post: old_post, created_at: 101.days.ago)
tl3_requirements.num_likes_received.should == 2
tl3_requirements.num_likes_received.should == 3
tl3_requirements.num_likes_received_days.should == 2
tl3_requirements.num_likes_received_users.should == 2
end
end
@ -209,6 +215,8 @@ describe TrustLevel3Requirements do
tl3_requirements.stubs(:max_flagged_by_users).returns(5)
tl3_requirements.stubs(:min_likes_given).returns(30)
tl3_requirements.stubs(:min_likes_received).returns(20)
tl3_requirements.stubs(:min_likes_received_days).returns(7)
tl3_requirements.stubs(:min_likes_received_users).returns(5)
tl3_requirements.stubs(:days_visited).returns(50)
tl3_requirements.stubs(:num_topics_replied_to).returns(10)
@ -220,6 +228,8 @@ describe TrustLevel3Requirements do
tl3_requirements.stubs(:num_flagged_by_users).returns(0)
tl3_requirements.stubs(:num_likes_given).returns(30)
tl3_requirements.stubs(:num_likes_received).returns(20)
tl3_requirements.stubs(:num_likes_received_days).returns(7)
tl3_requirements.stubs(:num_likes_received_users).returns(5)
end
it "are met when all requirements are met" do
@ -271,6 +281,16 @@ describe TrustLevel3Requirements do
tl3_requirements.requirements_met?.should == false
end
it "are not met if not enough likes received on different days" do
tl3_requirements.stubs(:num_likes_received_days).returns(6)
tl3_requirements.requirements_met?.should == false
end
it "are not met if not enough likes received by different users" do
tl3_requirements.stubs(:num_likes_received_users).returns(4)
tl3_requirements.requirements_met?.should == false
end
it "are lost if not enough likes given" do
tl3_requirements.stubs(:num_likes_given).returns(26)
tl3_requirements.requirements_lost?.should == true
@ -286,6 +306,16 @@ describe TrustLevel3Requirements do
tl3_requirements.requirements_met?.should == false
end
it "are lost if not enough likes received on different days" do
tl3_requirements.stubs(:num_likes_received_days).returns(4)
tl3_requirements.requirements_lost?.should == true
end
it "are lost if not enough likes received by different users" do
tl3_requirements.stubs(:num_likes_received_users).returns(3)
tl3_requirements.requirements_lost?.should == true
end
it "are lost if suspended" do
user.stubs(:suspended?).returns(true)
tl3_requirements.requirements_lost?.should == true