A platform for community discussion. Free, open, simple.
FEATURE: Allow expanded posts to return user custom fields
Robin Ward467be59d758a
diff --git a/app/assets/javascripts/discourse/lib/transform-post.js.es6 b/app/assets/javascripts/discourse/lib/transform-post.js.es6index 80106ecc0b3..a560d1ba702 100644--- a/app/assets/javascripts/discourse/lib/transform-post.js.es6+++ b/app/assets/javascripts/discourse/lib/transform-post.js.es6@@ -79,7 +79,8 @@ export function transformBasicPost(post) {cooked_hidden: !!post.cooked_hidden,expandablePost: false,replyCount: post.reply_count,- locked: post.locked+ locked: post.locked,+ userCustomFields: post.user_custom_fields};_additionalAttributes.forEach(a => (postAtts[a] = post[a]));@@ -128,7 +129,6 @@ export default function transformPost(postAtts.linkCounts = post.link_counts;postAtts.actionCode = post.action_code;postAtts.actionCodeWho = post.action_code_who;- postAtts.userCustomFields = post.user_custom_fields;postAtts.topicUrl = topic.get("url");postAtts.isSaving = post.isSaving;diff --git a/app/assets/javascripts/discourse/widgets/post-stream.js.es6 b/app/assets/javascripts/discourse/widgets/post-stream.js.es6index 92b5c17dc67..0515d8e3fe3 100644--- a/app/assets/javascripts/discourse/widgets/post-stream.js.es6+++ b/app/assets/javascripts/discourse/widgets/post-stream.js.es6@@ -4,7 +4,7 @@ import { Placeholder } from "discourse/lib/posts-with-placeholders";import { addWidgetCleanCallback } from "discourse/components/mount-widget";let transformCallbacks = null;-function postTransformCallbacks(transformed) {+export function postTransformCallbacks(transformed) {if (transformCallbacks === null) {return;}diff --git a/app/assets/javascripts/discourse/widgets/post.js.es6 b/app/assets/javascripts/discourse/widgets/post.js.es6index 2f20c049003..abcdd5a20e2 100644--- a/app/assets/javascripts/discourse/widgets/post.js.es6+++ b/app/assets/javascripts/discourse/widgets/post.js.es6@@ -3,6 +3,7 @@ import DecoratorHelper from "discourse/widgets/decorator-helper";import { createWidget, applyDecorators } from "discourse/widgets/widget";import { iconNode } from "discourse-common/lib/icon-library";import { transformBasicPost } from "discourse/lib/transform-post";+import { postTransformCallbacks } from "discourse/widgets/post-stream";import { h } from "virtual-dom";import DiscourseURL from "discourse/lib/url";import { dateNode } from "discourse/helpers/node";@@ -13,6 +14,15 @@ import {} from "discourse/lib/utilities";import hbs from "discourse/widgets/hbs-compiler";+function transformWithCallbacks(post) {+ let transformed = transformBasicPost(post);+ console.log("transforming!");+ console.log(transformed);+ postTransformCallbacks(transformed);+ console.log("transforming! done");+ return transformed;+}+export function avatarImg(wanted, attrs) {const size = translateSize(wanted);const url = avatarUrl(attrs.template, size);@@ -402,7 +412,7 @@ createWidget("post-contents", {.then(posts => {this.state.repliesBelow = posts.map(p => {p.shareUrl = `${topicUrl}/${p.post_number}`;- return transformBasicPost(p);+ return transformWithCallbacks(p);});});},@@ -528,7 +538,7 @@ createWidget("post-article", {.then(posts => {this.state.repliesAbove = posts.map(p => {p.shareUrl = `${topicUrl}/${p.post_number}`;- return transformBasicPost(p);+ return transformWithCallbacks(p);});});}diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rbindex 948bab91017..f35ae00e690 100644--- a/app/controllers/posts_controller.rb+++ b/app/controllers/posts_controller.rb@@ -275,7 +275,18 @@ class PostsController < ApplicationControllerdef reply_historypost = find_post_from_params- render_serialized(post.reply_history(params[:max_replies].to_i, guardian), PostSerializer)++ reply_history = post.reply_history(params[:max_replies].to_i, guardian)+ user_custom_fields = {}+ if (added_fields = User.whitelisted_user_custom_fields(guardian)).present?+ user_custom_fields = User.custom_fields_for_ids(reply_history.pluck(:user_id), added_fields)+ end++ render_serialized(+ reply_history,+ PostSerializer,+ user_custom_fields: user_custom_fields+ )enddef reply_ids@@ -351,7 +362,13 @@ class PostsController < ApplicationControllerdef repliespost = find_post_from_paramsreplies = post.replies.secured(guardian)- render_serialized(replies, PostSerializer)++ user_custom_fields = {}+ if (added_fields = User.whitelisted_user_custom_fields(guardian)).present?+ user_custom_fields = User.custom_fields_for_ids(replies.pluck(:user_id), added_fields)+ end++ render_serialized(replies, PostSerializer, user_custom_fields: user_custom_fields)enddef revisionsdiff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rbindex 887374e0f9f..f778c80ba64 100644--- a/app/serializers/post_serializer.rb+++ b/app/serializers/post_serializer.rb@@ -318,11 +318,11 @@ class PostSerializer < BasicPostSerializerenddef user_custom_fields- @topic_view.user_custom_fields[object.user_id]+ user_custom_fields_object[object.user_id]enddef include_user_custom_fields?- (@topic_view&.user_custom_fields || {})[object.user_id]+ user_custom_fields_object[object.user_id]enddef static_doc@@ -388,6 +388,10 @@ class PostSerializer < BasicPostSerializerprivate+ def user_custom_fields_object+ (@topic_view&.user_custom_fields || @options[:user_custom_fields] || {})+ end+def topic@topic = object.topic@topic ||= Topic.with_deleted.find(object.topic_id) if scope.is_staff?diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex aba72e8de17..dac7ad784d5 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -121,10 +121,20 @@ describe PostsController dolet(:url) { "/posts/#{post.id}/reply-history.json" }end- it 'asks post for reply history' do- post = Fabricate(:post)- get "/posts/#{post.id}/reply-history.json"+ it "returns the replies with whitelisted user custom fields" do+ parent = Fabricate(:post)+ child = Fabricate(:post, topic: parent.topic, reply_to_post_number: parent.post_number)++ parent.user.upsert_custom_fields(hello: 'world', hidden: 'dontshow')+ SiteSetting.public_user_custom_fields = 'hello'++ get "/posts/#{child.id}/reply-history.json"expect(response.status).to eq(200)++ json = JSON.parse(response.body)+ expect(json[0]['id']).to eq(parent.id)+ expect(json[0]['user_custom_fields']['hello']).to eq('world')+ expect(json[0]['user_custom_fields']['hidden']).to be_blankendend@@ -134,9 +144,20 @@ describe PostsController doendit 'asks post for replies' do- p1 = Fabricate(:post)- get "/posts/#{p1.id}/replies.json"+ parent = Fabricate(:post)+ child = Fabricate(:post, topic: parent.topic, reply_to_post_number: parent.post_number)+ PostReply.create!(post: parent, reply: child)++ child.user.upsert_custom_fields(hello: 'world', hidden: 'dontshow')+ SiteSetting.public_user_custom_fields = 'hello'++ get "/posts/#{parent.id}/replies.json"expect(response.status).to eq(200)++ json = JSON.parse(response.body)+ expect(json[0]['id']).to eq(child.id)+ expect(json[0]['user_custom_fields']['hello']).to eq('world')+ expect(json[0]['user_custom_fields']['hidden']).to be_blankendend