Add likes given and likes received requirement for being TL3. Configure with site settings leader_requires_likes_given and leader_requires_likes_received.

This commit is contained in:
Neil Lalonde 2014-08-22 17:33:05 -04:00
parent 94ca45b20b
commit bbb358b356
8 changed files with 126 additions and 6 deletions

View File

@ -16,7 +16,9 @@ Discourse.LeaderRequirements = Discourse.Model.extend({
topics_viewed_all_time: this.get('topics_viewed_all_time') >= this.get('min_topics_viewed_all_time'),
posts_read_all_time: this.get('posts_read_all_time') >= this.get('min_posts_read_all_time'),
flagged_posts: this.get('num_flagged_posts') <= this.get('max_flagged_posts'),
flagged_by_users: this.get('num_flagged_by_users') <= this.get('max_flagged_by_users')
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')
};
}.property('days_visited', 'min_days_visited',
'num_topics_replied_to', 'min_topics_replied_to',
@ -25,5 +27,7 @@ Discourse.LeaderRequirements = Discourse.Model.extend({
'num_flagged_posts', 'max_flagged_posts',
'topics_viewed_all_time', 'min_topics_viewed_all_time',
'posts_read_all_time', 'min_posts_read_all_time',
'num_flagged_by_users', 'max_flagged_by_users')
'num_flagged_by_users', 'max_flagged_by_users',
'num_likes_given', 'min_likes_given',
'num_likes_received', 'min_likes_received')
});

View File

@ -73,6 +73,18 @@
<td>{{num_flagged_by_users}}</td>
<td>{{max_flagged_by_users}} {{i18n max}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.likes_given}}</th>
<td><i {{bind-attr class=":fa met.likes_given:fa-check:fa-times"}}></i></td>
<td>{{num_likes_given}}</td>
<td>{{min_likes_given}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.likes_received}}</th>
<td><i {{bind-attr class=":fa met.likes_received:fa-check:fa-times"}}></i></td>
<td>{{num_likes_received}}</td>
<td>{{min_likes_received}}</td>
</tr>
</tbody>
</table>
{{/with}}

View File

@ -14,7 +14,9 @@ class LeaderRequirements
:posts_read, :min_posts_read,
:topics_viewed_all_time, :min_topics_viewed_all_time,
:posts_read_all_time, :min_posts_read_all_time,
:num_flagged_posts, :max_flagged_posts
:num_flagged_posts, :max_flagged_posts,
:num_likes_given, :min_likes_given,
:num_likes_received, :min_likes_received
def initialize(user)
@user = user
@ -28,7 +30,9 @@ class LeaderRequirements
num_flagged_posts <= max_flagged_posts &&
num_flagged_by_users <= max_flagged_by_users &&
topics_viewed_all_time >= min_topics_viewed_all_time &&
posts_read_all_time >= min_posts_read_all_time
posts_read_all_time >= min_posts_read_all_time &&
num_likes_given >= min_likes_given &&
num_likes_received >= min_likes_received
end
def requirements_lost?
@ -39,7 +43,9 @@ class LeaderRequirements
num_flagged_posts > max_flagged_posts ||
num_flagged_by_users > max_flagged_by_users ||
topics_viewed_all_time < min_topics_viewed_all_time ||
posts_read_all_time < min_posts_read_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
end
def days_visited
@ -120,6 +126,23 @@ class LeaderRequirements
SiteSetting.leader_requires_max_flagged
end
def num_likes_given
UserAction.where(user_id: @user.id, action_type: UserAction::LIKE).where('created_at > ?', TIME_PERIOD.days.ago).count
end
def min_likes_given
SiteSetting.leader_requires_likes_given
end
def num_likes_received
UserAction.where(user_id: @user.id, action_type: UserAction::WAS_LIKED).where('created_at > ?', TIME_PERIOD.days.ago).count
end
def min_likes_received
SiteSetting.leader_requires_likes_received
end
def self.clear_cache
$redis.del NUM_TOPICS_KEY
$redis.del NUM_POSTS_KEY

View File

@ -8,7 +8,9 @@ class LeaderRequirementsSerializer < ApplicationSerializer
:topics_viewed_all_time, :min_topics_viewed_all_time,
:posts_read_all_time, :min_posts_read_all_time,
:num_flagged_posts, :max_flagged_posts,
:num_flagged_by_users, :max_flagged_by_users
:num_flagged_by_users, :max_flagged_by_users,
:num_likes_given, :min_likes_given,
:num_likes_received, :min_likes_received
def time_period
LeaderRequirements::TIME_PERIOD

View File

@ -1939,6 +1939,8 @@ en:
posts_read_all_time: "Posts Read (all time)"
flagged_posts: "Flagged Posts"
flagged_by_users: "Users Who Flagged"
likes_given: "Likes Given"
likes_received: "Likes Received"
qualifies: "Qualifies for trust level 3."
will_be_promoted: "Will be promoted within 24 hours."
does_not_qualify: "Doesn't qualify for trust level 3."

View File

@ -853,6 +853,8 @@ en:
leader_requires_posts_read_all_time: "The minimum total number of posts a user must have read to qualify for leader (3) trust level."
leader_requires_max_flagged: "User must not have had more than x posts flagged by x different users in the last 100 days to qualify for promotion to leader (3) trust level, where x is this setting's value. (0 or higher)"
leader_promotion_min_duration: "The minimum number of days that a promotion to leader lasts before a user can be demoted."
leader_requires_likes_given: "The minimum number of likes that must be given in the last 100 days to qualify for promotion to leader (3) trust level."
leader_requires_likes_received: "The minimum number of likes that must be received in the last 100 days to qualify for promotion to leader (3) trust level."
leader_links_no_follow: "Whether links from leaders have the nofollow attribute."
min_trust_to_create_topic: "The minimum trust level required to create a new topic."

View File

@ -503,6 +503,12 @@ trust:
leader_promotion_min_duration:
default: 14
min: 0
leader_requires_likes_given:
default: 30
min: 0
leader_requires_likes_received:
default: 20
min: 0
leader_links_no_follow: false
security:

View File

@ -50,6 +50,16 @@ describe LeaderRequirements do
SiteSetting.stubs(:leader_requires_max_flagged).returns(3)
leader_requirements.max_flagged_posts.should == 3
end
it "min_likes_given depends on site setting" do
SiteSetting.stubs(:leader_requires_likes_given).returns(30)
leader_requirements.min_likes_given.should == 30
end
it "min_likes_received depends on site setting" do
SiteSetting.stubs(:leader_requires_likes_received).returns(20)
leader_requirements.min_likes_received.should == 20
end
end
describe "days_visited" do
@ -152,6 +162,40 @@ describe LeaderRequirements do
end
end
describe "num_likes_given" do
it "counts likes given in the last 100 days" do
ActiveRecord::Base.observers.enable :user_action_observer
recent_post1 = create_post(created_at: 1.hour.ago)
recent_post2 = create_post(created_at: 10.days.ago)
old_post = create_post(created_at: 102.days.ago)
Fabricate(:like, user: user, post: recent_post1, created_at: 2.hours.ago)
Fabricate(:like, user: user, post: recent_post2, created_at: 5.days.ago)
Fabricate(:like, user: user, post: old_post, created_at: 101.days.ago)
leader_requirements.num_likes_given.should == 2
end
end
describe "num_likes_received" do
it "counts likes received in the last 100 days" do
ActiveRecord::Base.observers.enable :user_action_observer
t = Fabricate(:topic, user: user, created_at: 102.days.ago)
old_post = create_post(topic: t, user: user, created_at: 102.days.ago)
recent_post2 = create_post(topic: t, user: user, created_at: 10.days.ago)
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)
leader_requirements.num_likes_received.should == 2
end
end
describe "requirements" do
before do
@ -163,6 +207,8 @@ describe LeaderRequirements do
leader_requirements.stubs(:min_posts_read_all_time).returns(500)
leader_requirements.stubs(:max_flagged_posts).returns(5)
leader_requirements.stubs(:max_flagged_by_users).returns(5)
leader_requirements.stubs(:min_likes_given).returns(30)
leader_requirements.stubs(:min_likes_received).returns(20)
leader_requirements.stubs(:days_visited).returns(50)
leader_requirements.stubs(:num_topics_replied_to).returns(10)
@ -172,6 +218,8 @@ describe LeaderRequirements do
leader_requirements.stubs(:posts_read_all_time).returns(500)
leader_requirements.stubs(:num_flagged_posts).returns(0)
leader_requirements.stubs(:num_flagged_by_users).returns(0)
leader_requirements.stubs(:num_likes_given).returns(30)
leader_requirements.stubs(:num_likes_received).returns(20)
end
it "are met when all requirements are met" do
@ -188,6 +236,8 @@ describe LeaderRequirements do
leader_requirements.stubs(:num_topics_replied_to).returns(9)
leader_requirements.stubs(:topics_viewed).returns(23)
leader_requirements.stubs(:posts_read).returns(23)
leader_requirements.stubs(:num_likes_given).returns(29)
leader_requirements.stubs(:num_likes_received).returns(19)
leader_requirements.requirements_lost?.should == false
end
@ -211,6 +261,25 @@ describe LeaderRequirements do
leader_requirements.requirements_lost?.should == true
end
it "are not met if not enough likes given" do
leader_requirements.stubs(:num_likes_given).returns(29)
leader_requirements.requirements_met?.should == false
end
it "are not met if not enough likes received" do
leader_requirements.stubs(:num_likes_received).returns(19)
leader_requirements.requirements_met?.should == false
end
it "are lost if not enough likes given" do
leader_requirements.stubs(:num_likes_given).returns(26)
leader_requirements.requirements_lost?.should == true
end
it "are lost if not enough likes received" do
leader_requirements.stubs(:num_likes_received).returns(17)
leader_requirements.requirements_lost?.should == true
end
end
end