FEATURE: Display the reason for many reviewable items

FEATURE: Display the reason for many reviewable items

Queued Posts and Users will now display a reason why they are in the review queue.

diff --git a/app/assets/javascripts/discourse/templates/components/reviewable-score.hbs b/app/assets/javascripts/discourse/templates/components/reviewable-score.hbs
index e19c9ce..6454ffb 100644
--- a/app/assets/javascripts/discourse/templates/components/reviewable-score.hbs
+++ b/app/assets/javascripts/discourse/templates/components/reviewable-score.hbs
@@ -19,7 +19,7 @@
   </td>
 
   <td class="reviewable-score-spacer">
-{{d-icon "angle-double-right"}}
+    {{d-icon "angle-double-right"}}
   </td>
 
   <td class='reviewed-by'>
@@ -38,12 +38,19 @@
   </td>
   <td>
     {{#if rs.reviewed_by}}
-
-    {{format-date rs.reviewed_at format="tiny"}}
+      {{format-date rs.reviewed_at format="tiny"}}
     {{/if}}
-
   </td>
 </tr>
+
+{{#if rs.reason}}
+  <tr>
+    <td colspan='7'>
+      <div class='reviewable-score-reason'>{{{rs.reason}}}</div>
+    </td>
+  </tr>
+{{/if}}
+
 {{#if rs.reviewable_conversation}}
   <tr>
     <td colspan='7'>
diff --git a/app/assets/stylesheets/common/base/reviewables.scss b/app/assets/stylesheets/common/base/reviewables.scss
index 88cf15b..0dbd182 100644
--- a/app/assets/stylesheets/common/base/reviewables.scss
+++ b/app/assets/stylesheets/common/base/reviewables.scss
@@ -296,6 +296,12 @@
   }
 }
 
+.reviewable-score-reason {
+  margin: 0.5em 0;
+  max-width: $topic-body-width;
+  margin-bottom: 0.5em;
+}
+
 .reviewable-conversation {
   margin: 0.5em 0;
 
diff --git a/app/jobs/regular/create_user_reviewable.rb b/app/jobs/regular/create_user_reviewable.rb
index 2546f7f..fb1824c 100644
--- a/app/jobs/regular/create_user_reviewable.rb
+++ b/app/jobs/regular/create_user_reviewable.rb
@@ -2,6 +2,12 @@ class Jobs::CreateUserReviewable < Jobs::Base
   def execute(args)
     raise Discourse::InvalidParameters unless args[:user_id].present?
 
+    reason = nil
+    reason ||= :must_approve_users if SiteSetting.must_approve_users?
+    reason ||= :invite_only if SiteSetting.invite_only?
+
+    return unless reason
+
     if user = User.find_by(id: args[:user_id])
       return if user.approved?
 
@@ -18,6 +24,7 @@ class Jobs::CreateUserReviewable < Jobs::Base
       reviewable.add_score(
         Discourse.system_user,
         ReviewableScore.types[:needs_approval],
+        reason: reason,
         force_review: true
       )
     end
diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index dfd73b8..5debd5b 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -115,6 +115,7 @@ class Reviewable < ActiveRecord::Base
   def add_score(
     user,
     reviewable_score_type,
+    reason: nil,
     created_at: nil,
     take_action: false,
     meta_topic_id: nil,
@@ -130,7 +131,7 @@ class Reviewable < ActiveRecord::Base
       sub_total = SiteSetting.min_score_default_visibility
     end
 
-    rs = reviewable_scores.create!(
+    rs = reviewable_scores.new(
       user: user,
       status: ReviewableScore.statuses[:pending],
       reviewable_score_type: reviewable_score_type,
@@ -139,6 +140,8 @@ class Reviewable < ActiveRecord::Base
       take_action_bonus: take_action_bonus,
       created_at: created_at || Time.zone.now
     )
+    rs.reason = reason.to_s if reason
+    rs.save!
 
     update(score: self.score + rs.score, latest_score: rs.created_at)
     topic.update(reviewable_score: topic.reviewable_score + rs.score) if topic
diff --git a/app/serializers/reviewable_score_serializer.rb b/app/serializers/reviewable_score_serializer.rb
index c585689..f5180d8 100644
--- a/app/serializers/reviewable_score_serializer.rb
+++ b/app/serializers/reviewable_score_serializer.rb
@@ -2,7 +2,7 @@ require_dependency 'reviewable_score_type_serializer'
 
 class ReviewableScoreSerializer < ApplicationSerializer
 
-  attributes :id, :score, :agree_stats, :status, :created_at, :reviewed_at
+  attributes :id, :score, :agree_stats, :status, :reason, :created_at, :reviewed_at
   has_one :user, serializer: BasicUserSerializer, root: 'users'
   has_one :score_type, serializer: ReviewableScoreTypeSerializer
   has_one :reviewable_conversation, serializer: ReviewableConversationSerializer
@@ -16,4 +16,28 @@ class ReviewableScoreSerializer < ApplicationSerializer
     }
   end
 
+  def reason
+    return unless object.reason
+
+    if text = I18n.t("reviewables.reasons.#{object.reason}", default: nil)
+      # Create a convenient link to any site settings if the user is staff
+      settings_url = "#{Discourse.base_uri}/admin/site_settings/category/all_results?filter="
+
+      text.gsub!(/`[a-z_]+`/) do |m|
+        if scope.is_staff?
+          setting = m[1..-2]
+          "<a href=\"#{settings_url}#{setting}\">#{setting.gsub('_', ' ')}</a>"
+        else
+          m.gsub('_', ' ')
+        end
+      end
+    end
+
+    text
+  end
+
+  def include_reason?
+    reason.present?
+  end
+
 end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 4d2f668..7d7aefd 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -429,6 +429,7 @@ en:
       conversation:
         view_full: "view full conversation"
       scores:
+        score: "Score"
         date: "Date"
         type: "Type"
         status: "Status"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index f156782..a859b85 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -4385,6 +4385,18 @@ en:
   reviewables:
     missing_version: "You must supply a version parameter"
     conflict: "There was an update conflict preventing you from doing that."
+    reasons:
+      post_count: "The poster has not met the `approve_post_count` requirements."
+      trust_level: "The poster has not met the `approve_unless_trust_level` requirement."
+      new_topics_unless_trust_level: "The poster has not met the `approve_new_topics_unless_trust_level` requirement."
+      fast_typer: "The poster typed in their post suspiciously fast. See: `min_first_post_typing_time`."
+      auto_silence_regexp: "The post matched the `auto_silence_first_post_regex` setting."
+      watched_word: "The post included a Watched Word."
+      staged: "The poster was staged and `approve_unless_staged` was set."
+      category: "The category of the post requires approval."
+      must_approve_users: "The user must be approved because `must_approve_users` is enabled."
+      invite_only: "The user must be approved because `invite_only` is enabled."
+
     actions:
       agree:
         title: "Agree..."
diff --git a/db/migrate/20190411144545_add_reason_to_reviewable_scores.rb b/db/migrate/20190411144545_add_reason_to_reviewable_scores.rb
new file mode 100644
index 0000000..a6105c6
--- /dev/null
+++ b/db/migrate/20190411144545_add_reason_to_reviewable_scores.rb
@@ -0,0 +1,5 @@
+class AddReasonToReviewableScores < ActiveRecord::Migration[5.2]
+  def change
+    add_column :reviewable_scores, :reason, :string
+  end
+end
diff --git a/lib/new_post_manager.rb b/lib/new_post_manager.rb
index d0eca26..6269d97 100644
--- a/lib/new_post_manager.rb
+++ b/lib/new_post_manager.rb
@@ -198,12 +198,9 @@ class NewPostManager
   def enqueue(reason = nil)
     result = NewPostResult.new(:enqueued)
 
-    payload = { raw: @args[:raw], tags: @args[:tags] }
-    payload[:reason] = reason.to_s if reason
-
     reviewable = ReviewableQueuedPost.new(
       created_by: @user,
-      payload: payload,
+      payload: { raw: @args[:raw], tags: @args[:tags] },
       topic_id: @args[:topic_id],
       reviewable_by_moderator: true
     )
@@ -225,6 +222,7 @@ class NewPostManager
         reviewable.add_score(
           Discourse.system_user,
           ReviewableScore.types[:needs_approval],
+          reason: reason,
           force_review: true
         )
       else
diff --git a/spec/jobs/create_user_reviewable_spec.rb b/spec/jobs/create_user_reviewable_spec.rb
index ffb2c3b..acab20b 100644
--- a/spec/jobs/create_user_reviewable_spec.rb
+++ b/spec/jobs/create_user_reviewable_spec.rb
@@ -6,14 +6,40 @@ describe Jobs::CreateUserReviewable do

[... diff too long, it was truncated ...]

GitHub sha: 331a8097

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: