From b69bc5a5f5f61d5daa1121ada355ba07312f2190 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 26 Jun 2014 13:48:07 -0400 Subject: [PATCH] Trust level 3 measurements redone --- .../admin/models/leader_requirements.js | 6 ++- .../user_leader_requirements.js.handlebars | 43 +++++++---------- app/models/leader_requirements.rb | 46 +++++++++++++------ app/models/post.rb | 1 + .../leader_requirements_serializer.rb | 8 +++- config/locales/client.en.yml | 6 +-- 6 files changed, 62 insertions(+), 48 deletions(-) diff --git a/app/assets/javascripts/admin/models/leader_requirements.js b/app/assets/javascripts/admin/models/leader_requirements.js index f4f3d402c02..2076386b384 100644 --- a/app/assets/javascripts/admin/models/leader_requirements.js +++ b/app/assets/javascripts/admin/models/leader_requirements.js @@ -10,12 +10,14 @@ Discourse.LeaderRequirements = Discourse.Model.extend({ met: function() { return { days_visited: this.get('days_visited') >= this.get('min_days_visited'), - topics_with_replies: this.get('num_topics_with_replies') >= this.get('min_topics_with_replies'), 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'), flagged_posts: this.get('num_flagged_posts') < this.get('max_flagged_posts') }; }.property('days_visited', 'min_days_visited', - 'num_topics_with_replies', 'min_topics_with_replies', 'num_topics_replied_to', 'min_topics_replied_to', + 'topics_viewed', 'min_topics_viewed', + 'posts_read', 'min_posts_read', 'num_flagged_posts', 'max_flagged_posts') }); diff --git a/app/assets/javascripts/admin/templates/user_leader_requirements.js.handlebars b/app/assets/javascripts/admin/templates/user_leader_requirements.js.handlebars index 9fe0d23c7f9..21ea6926e82 100644 --- a/app/assets/javascripts/admin/templates/user_leader_requirements.js.handlebars +++ b/app/assets/javascripts/admin/templates/user_leader_requirements.js.handlebars @@ -31,12 +31,6 @@ {{min_days_visited_percent}}% - - {{i18n admin.user.tl3_requirements.topics_with_replies}} - - {{num_topics_with_replies}} - {{min_topics_with_replies}} - {{i18n admin.user.tl3_requirements.topics_replied_to}} @@ -44,28 +38,16 @@ {{min_topics_replied_to}} - {{i18n admin.user.tl3_requirements.quality_content}} - - - TODO - - + {{i18n admin.user.tl3_requirements.topics_viewed}} + + {{topics_viewed}} + {{min_topics_viewed}} - {{i18n admin.user.tl3_requirements.reading}} - - - TODO - - - - - {{i18n admin.user.tl3_requirements.site_promotion}} - - - TODO - - + {{i18n admin.user.tl3_requirements.posts_read}} + + {{posts_read}} + {{min_posts_read}} {{i18n admin.user.tl3_requirements.flagged_posts}} @@ -75,6 +57,15 @@ + +
+

+ {{#if requirements_met}} + Qualifies for trust level 3 + {{else}} + Doesn't qualify for trust level 3 + {{/if}} +

{{/with}} diff --git a/app/models/leader_requirements.rb b/app/models/leader_requirements.rb index d8a2c504d20..2e5d2997f97 100644 --- a/app/models/leader_requirements.rb +++ b/app/models/leader_requirements.rb @@ -6,18 +6,22 @@ class LeaderRequirements attr_accessor :time_period, :days_visited, :min_days_visited, - :num_topics_with_replies, :min_topics_with_replies, :num_topics_replied_to, :min_topics_replied_to, + :topics_viewed, :min_topics_viewed, + :posts_read, :min_posts_read, :num_flagged_posts, :max_flagged_posts def initialize(user) @user = user end - # TODO - # def requirements_met? - # false - # end + def requirements_met? + days_visited >= min_days_visited && + num_topics_replied_to >= min_topics_replied_to && + topics_viewed >= min_topics_viewed && + posts_read >= min_posts_read && + num_flagged_posts <= max_flagged_posts + end def time_period 100 # days @@ -31,14 +35,6 @@ class LeaderRequirements time_period * 0.5 end - def num_topics_with_replies - @user.topics.where('posts_count > 1 AND participant_count > 1 AND created_at > ?', time_period.days.ago).count - end - - def min_topics_with_replies - 5 - end - def num_topics_replied_to @user.posts.select('distinct topic_id').where('created_at > ? AND post_number > 1', time_period.days.ago).count end @@ -47,11 +43,31 @@ class LeaderRequirements 10 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 + end + + def min_topics_viewed + (Topic.listable_topics.visible.created_since(time_period.days.ago).count * 0.25).round + end + + def posts_read + @user.user_visits.where('visited_at > ?', time_period.days.ago).pluck(:posts_read).sum + end + + def min_posts_read + (Post.public_posts.visible.created_since(time_period.days.ago).count * 0.25).round + end + def num_flagged_posts - @user.posts.where('created_at > ? AND (off_topic_count > 0 OR spam_count > 0 OR illegal_count > 0 OR inappropriate_count > 0 OR notify_moderators_count > 0)', time_period.days.ago).count + # Count the number of posts that were flagged, and moderators explicitly agreed with the flags + # by clicking the "Agree (hide post + send PM)" or "Defer" (on an automatically hidden post) buttons. + # In both cases, the defer flag is set to true. + post_ids = @user.posts.with_deleted.where('created_at > ? AND (spam_count > 0 OR inappropriate_count > 0)', time_period.days.ago).pluck(:id) + PostAction.with_deleted.where(post_id: post_ids).where(defer: true).pluck(:post_id).uniq.count end def max_flagged_posts - 5 # TODO what should it be? + 5 end end diff --git a/app/models/post.rb b/app/models/post.rb index 0ff7415392f..649168173d8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -54,6 +54,7 @@ class Post < ActiveRecord::Base scope :public_posts, -> { joins(:topic).where('topics.archetype <> ?', Archetype.private_message) } scope :private_posts, -> { joins(:topic).where('topics.archetype = ?', Archetype.private_message) } scope :with_topic_subtype, ->(subtype) { joins(:topic).where('topics.subtype = ?', subtype) } + scope :visible, -> { joins(:topic).where('topics.visible = true').where(hidden: false) } delegate :username, to: :user diff --git a/app/serializers/leader_requirements_serializer.rb b/app/serializers/leader_requirements_serializer.rb index 88cb0847702..aa32159f975 100644 --- a/app/serializers/leader_requirements_serializer.rb +++ b/app/serializers/leader_requirements_serializer.rb @@ -1,7 +1,13 @@ class LeaderRequirementsSerializer < ApplicationSerializer attributes :time_period, + :requirements_met, :days_visited, :min_days_visited, - :num_topics_with_replies, :min_topics_with_replies, :num_topics_replied_to, :min_topics_replied_to, + :topics_viewed, :min_topics_viewed, + :posts_read, :min_posts_read, :num_flagged_posts, :max_flagged_posts + + def requirements_met + object.requirements_met? + end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index e284226a53d..24274a88b6c 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1796,11 +1796,9 @@ en: requirement_heading: "Requirement" visits: "Visits" days: "days" - topics_with_replies: "Topics with Replies" topics_replied_to: "Topics Replied To" - quality_content: "Quality Content" - reading: "Reading" - site_promotion: "Site Promotion" + topics_viewed: "Topics Viewed" + posts_read: "Posts Read" flagged_posts: "Flagged Posts" site_content: