TL3: Add requirements for min all-time topics viewed, and min all-time posts read

This commit is contained in:
Neil Lalonde 2014-07-01 13:34:48 -04:00
parent d9e583af1a
commit 292e9bb611
8 changed files with 92 additions and 11 deletions

View file

@ -13,6 +13,8 @@ Discourse.LeaderRequirements = Discourse.Model.extend({
topics_replied_to: this.get('num_topics_replied_to') >= this.get('min_topics_replied_to'),
topics_viewed: this.get('topics_viewed') >= this.get('min_topics_viewed'),
posts_read: this.get('posts_read') >= this.get('min_posts_read'),
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')
};
@ -21,5 +23,7 @@ Discourse.LeaderRequirements = Discourse.Model.extend({
'topics_viewed', 'min_topics_viewed',
'posts_read', 'min_posts_read',
'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')
});

View file

@ -25,7 +25,7 @@
<tbody>
<tr>
<th>{{i18n admin.user.tl3_requirements.visits}}</th>
<td><i {{bindAttr class=":fa met.days_visited:fa-check:fa-times"}}></i></td>
<td><i {{bind-attr class=":fa met.days_visited:fa-check:fa-times"}}></i></td>
<td>
{{days_visited_percent}}% ({{days_visited}} / {{time_period}} {{i18n admin.user.tl3_requirements.days}})
</td>
@ -33,31 +33,43 @@
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.topics_replied_to}}</th>
<td><i {{bindAttr class=":fa met.topics_replied_to:fa-check:fa-times"}}></i></td>
<td><i {{bind-attr class=":fa met.topics_replied_to:fa-check:fa-times"}}></i></td>
<td>{{num_topics_replied_to}}</td>
<td>{{min_topics_replied_to}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.topics_viewed}}</th>
<td><i {{bindAttr class=":fa met.topics_viewed:fa-check:fa-times"}}></i></td>
<td><i {{bind-attr class=":fa met.topics_viewed:fa-check:fa-times"}}></i></td>
<td>{{topics_viewed}}</td>
<td>{{min_topics_viewed}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.topics_viewed_all_time}}</th>
<td><i {{bind-attr class=":fa met.topics_viewed_all_time:fa-check:fa-times"}}></i></td>
<td>{{topics_viewed_all_time}}</td>
<td>{{min_topics_viewed_all_time}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.posts_read}}</th>
<td><i {{bindAttr class=":fa met.posts_read:fa-check:fa-times"}}></i></td>
<td><i {{bind-attr class=":fa met.posts_read:fa-check:fa-times"}}></i></td>
<td>{{posts_read}}</td>
<td>{{min_posts_read}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.posts_read_all_time}}</th>
<td><i {{bind-attr class=":fa met.posts_read_all_time:fa-check:fa-times"}}></i></td>
<td>{{posts_read_all_time}}</td>
<td>{{min_posts_read_all_time}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.flagged_posts}}</th>
<td><i {{bindAttr class=":fa met.flagged_posts:fa-check:fa-times"}}></i></td>
<td><i {{bind-attr class=":fa met.flagged_posts:fa-check:fa-times"}}></i></td>
<td>{{num_flagged_posts}}</td>
<td>{{max_flagged_posts}} {{i18n max}}</td>
</tr>
<tr>
<th>{{i18n admin.user.tl3_requirements.flagged_by_users}}</th>
<td><i {{bindAttr class=":fa met.flagged_by_users:fa-check:fa-times"}}></i></td>
<td><i {{bind-attr class=":fa met.flagged_by_users:fa-check:fa-times"}}></i></td>
<td>{{num_flagged_by_users}}</td>
<td>{{max_flagged_by_users}} {{i18n max}}</td>
</tr>

View file

@ -10,6 +10,8 @@ class LeaderRequirements
:num_topics_replied_to, :min_topics_replied_to,
:topics_viewed, :min_topics_viewed,
: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
def initialize(user)
@ -22,6 +24,8 @@ class LeaderRequirements
topics_viewed >= min_topics_viewed &&
posts_read >= min_posts_read &&
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
end
@ -41,8 +45,12 @@ class LeaderRequirements
SiteSetting.leader_requires_topics_replied_to
end
def topics_viewed_query
View.where(user_id: @user.id, parent_type: 'Topic').select('distinct(parent_id)')
end
def topics_viewed
View.where('viewed_at > ?', TIME_PERIOD.days.ago).where(user_id: @user.id, parent_type: 'Topic').select('distinct(parent_id)').count
topics_viewed_query.where('viewed_at > ?', TIME_PERIOD.days.ago).count
end
def min_topics_viewed
@ -57,6 +65,22 @@ class LeaderRequirements
(LeaderRequirements.num_posts_in_time_period.to_i * (SiteSetting.leader_requires_posts_read.to_f / 100.0)).round
end
def topics_viewed_all_time
topics_viewed_query.count
end
def min_topics_viewed_all_time
SiteSetting.leader_requires_topics_viewed_all_time
end
def posts_read_all_time
@user.user_visits.pluck(:posts_read).sum
end
def min_posts_read_all_time
SiteSetting.leader_requires_posts_read_all_time
end
def num_flagged_posts
PostAction.with_deleted.where(post_id: flagged_post_ids).where.not(user_id: @user.id).pluck(:post_id).uniq.count
end

View file

@ -5,6 +5,8 @@ class LeaderRequirementsSerializer < ApplicationSerializer
:num_topics_replied_to, :min_topics_replied_to,
:topics_viewed, :min_topics_viewed,
: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_by_users, :max_flagged_by_users

View file

@ -1807,7 +1807,9 @@ en:
days: "days"
topics_replied_to: "Topics Replied To"
topics_viewed: "Topics Viewed"
topics_viewed_all_time: "Topics Viewed (all time)"
posts_read: "Posts Read"
posts_read_all_time: "Posts Read (all time)"
flagged_posts: "Flagged Posts"
flagged_by_users: "Users Who Flagged"
qualifies: "Qualifies for trust level 3."

View file

@ -807,6 +807,8 @@ en:
leader_requires_topics_replied_to: "Minimum number of topics a user needs to have replied to in the last 100 days to qualify for promotion to leader (3) trust level. (0 or higher)"
leader_requires_topics_viewed: "The percentage of topics created in the last 100 days that a user needs to have viewed to qualify for promotion to leader (3) trust level. (0 to 100)"
leader_requires_posts_read: "The percentage of posts created in the last 100 days that a user needs to have viewed to qualify for promotion to leader (3) trust level. (0 to 100)"
leader_requires_topics_viewed_all_time: "The minimum total number of topics a user must have viewed to qualify for leader (3) trust level."
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)"
min_trust_to_create_topic: "The minimum trust level required to create a new topic."

View file

@ -468,6 +468,12 @@ trust:
default: 25
min: 0
max: 100
leader_requires_topics_viewed_all_time:
default: 200
min: 0
leader_requires_posts_read_all_time:
default: 500
min: 0
leader_requires_max_flagged:
default: 5
min: 0

View file

@ -9,6 +9,10 @@ describe LeaderRequirements do
described_class.clear_cache
end
def make_view(id, at, user_id)
View.create!(parent_id: id, parent_type: 'Topic', ip_address: '11.22.33.44', viewed_at: at, user_id: user_id)
end
describe "requirements" do
it "min_days_visited uses site setting" do
SiteSetting.stubs(:leader_requires_days_visited).returns(66)
@ -32,6 +36,16 @@ describe LeaderRequirements do
leader_requirements.min_posts_read.should == 814
end
it "min_topics_viewed_all_time depends on site setting" do
SiteSetting.stubs(:leader_requires_topics_viewed_all_time).returns(75)
leader_requirements.min_topics_viewed_all_time.should == 75
end
it "min_posts_read_all_time depends on site setting" do
SiteSetting.stubs(:leader_requires_posts_read_all_time).returns(1001)
leader_requirements.min_posts_read_all_time.should == 1001
end
it "max_flagged_posts depends on site setting" do
SiteSetting.stubs(:leader_requires_max_flagged).returns(3)
leader_requirements.max_flagged_posts.should == 3
@ -67,10 +81,6 @@ describe LeaderRequirements do
end
describe "topics_viewed" do
def make_view(id, at, user_id)
View.create!(parent_id: id, parent_type: 'Topic', ip_address: '11.22.33.44', viewed_at: at, user_id: user_id)
end
it "counts topics views within last 100 days, not counting a topic more than once" do
user.save
make_view(9, 1.day.ago, user.id)
@ -92,6 +102,25 @@ describe LeaderRequirements do
end
end
describe "topics_viewed_all_time" do
it "counts topics viewed at any time" do
user.save
make_view(10, 1.day.ago, user.id)
make_view(9, 100.days.ago, user.id)
make_view(8, 101.days.ago, user.id)
leader_requirements.topics_viewed_all_time.should == 3
end
end
describe "posts_read_all_time" do
it "counts posts read at any time" do
user.save
user.update_posts_read!(3, 2.days.ago)
user.update_posts_read!(1, 101.days.ago)
leader_requirements.posts_read_all_time.should == 4
end
end
context "with flagged posts" do
before do
user.save