FEATURE: add after-reviewable-post-user plugin outlet (#9397)

FEATURE: add after-reviewable-post-user plugin outlet (#9397)

FEATURE: add after-reviewable-post-user plugin outlet

Add a plugin outlet after reviewable post user

Add a basic user serializer that includes custom fields. Allows review queue serializer to include custom fields for its users

diff --git a/app/assets/javascripts/discourse/templates/components/reviewable-created-by-name.hbs b/app/assets/javascripts/discourse/templates/components/reviewable-created-by-name.hbs
index e2f2afd..f455065 100644
--- a/app/assets/javascripts/discourse/templates/components/reviewable-created-by-name.hbs
+++ b/app/assets/javascripts/discourse/templates/components/reviewable-created-by-name.hbs
@@ -9,4 +9,7 @@
       {{i18n "review.deleted_user"}}
     {{/if}}
   </span>
+  {{plugin-outlet name="after-reviewable-post-user"
+      tagName=""
+      args=(hash user=user)}}
 </div>
diff --git a/app/serializers/reviewable_serializer.rb b/app/serializers/reviewable_serializer.rb
index af03419..bd7f69d 100644
--- a/app/serializers/reviewable_serializer.rb
+++ b/app/serializers/reviewable_serializer.rb
@@ -19,13 +19,13 @@ class ReviewableSerializer < ApplicationSerializer
     :version,
   )
 
-  has_one :created_by, serializer: BasicUserSerializer, root: 'users'
-  has_one :target_created_by, serializer: BasicUserSerializer, root: 'users'
+  has_one :created_by, serializer: UserWithCustomFieldsSerializer, root: 'users'
+  has_one :target_created_by, serializer: UserWithCustomFieldsSerializer, root: 'users'
   has_one :topic, serializer: ListableTopicSerializer
   has_many :editable_fields, serializer: ReviewableEditableFieldSerializer, embed: :objects
   has_many :reviewable_scores, serializer: ReviewableScoreSerializer
   has_many :bundled_actions, serializer: ReviewableBundledActionSerializer
-  has_one :claimed_by, serializer: BasicUserSerializer, root: 'users'
+  has_one :claimed_by, serializer: UserWithCustomFieldsSerializer, root: 'users'
 
   # Used to keep track of our payload attributes
   class_attribute :_payload_for_serialization
diff --git a/app/serializers/user_with_custom_fields_serializer.rb b/app/serializers/user_with_custom_fields_serializer.rb
new file mode 100644
index 0000000..6492d2f
--- /dev/null
+++ b/app/serializers/user_with_custom_fields_serializer.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+# A basic user serializer, with custom fields
+class UserWithCustomFieldsSerializer < BasicUserSerializer
+  attributes :custom_fields
+
+  def custom_fields
+    fields = custom_field_keys
+
+    if fields.present?
+      if object.custom_fields_preloaded?
+        {}.tap { |h| fields.each { |f| h[f] = object.custom_fields[f] } }
+      else
+        User.custom_fields_for_ids([object.id], fields)[object.id] || {}
+      end
+    else
+      {}
+    end
+  end
+
+  private
+
+  def custom_field_keys
+    # Can be extended by other serializers
+    User.whitelisted_user_custom_fields(scope)
+  end
+end
diff --git a/spec/requests/reviewables_controller_spec.rb b/spec/requests/reviewables_controller_spec.rb
index 6ae6dfa..e42d67a 100644
--- a/spec/requests/reviewables_controller_spec.rb
+++ b/spec/requests/reviewables_controller_spec.rb
@@ -192,6 +192,32 @@ describe ReviewablesController do
           expect(json_review['id']).to eq(reviewable.id)
         end
       end
+
+      context "with user custom field" do
+        before do
+          plugin = Plugin::Instance.new
+          plugin.whitelist_public_user_custom_field :public_field
+        end
+
+        after do
+          User.plugin_public_user_custom_fields.clear
+        end
+
+        it "returns user data with custom fields" do
+          user = Fabricate(:user)
+          user.custom_fields["public_field"] = "public"
+          user.custom_fields["private_field"] = "private"
+          user.save!
+
+          reviewable = Fabricate(:reviewable, target_created_by: user)
+
+          get "/review.json"
+          json = response.parsed_body
+          expect(json['users']).to be_present
+          expect(json['users'].any? { |u| u['id'] == reviewable.target_created_by_id && u['custom_fields']['public_field'] == 'public' }).to eq(true)
+          expect(json['users'].any? { |u| u['id'] == reviewable.target_created_by_id && u['custom_fields']['private_field'] == 'private' }).to eq(false)
+        end
+      end
     end
 
     context "#show" do

GitHub sha: 5ccbc5f3

This commit appears in #9397 which was approved by eviltrout. It was merged by featheredtoast.