FEATURE: Allow expanded posts to return user custom fields
This commit is contained in:
parent
1570b01184
commit
467be59d75
app
assets/javascripts/discourse
controllers
serializers
spec/requests
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -275,7 +275,18 @@ class PostsController < ApplicationController
|
|||
|
||||
def reply_history
|
||||
post = 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
|
||||
)
|
||||
end
|
||||
|
||||
def reply_ids
|
||||
|
@ -351,7 +362,13 @@ class PostsController < ApplicationController
|
|||
def replies
|
||||
post = find_post_from_params
|
||||
replies = 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)
|
||||
end
|
||||
|
||||
def revisions
|
||||
|
|
|
@ -318,11 +318,11 @@ class PostSerializer < BasicPostSerializer
|
|||
end
|
||||
|
||||
def user_custom_fields
|
||||
@topic_view.user_custom_fields[object.user_id]
|
||||
user_custom_fields_object[object.user_id]
|
||||
end
|
||||
|
||||
def include_user_custom_fields?
|
||||
(@topic_view&.user_custom_fields || {})[object.user_id]
|
||||
user_custom_fields_object[object.user_id]
|
||||
end
|
||||
|
||||
def static_doc
|
||||
|
@ -388,6 +388,10 @@ class PostSerializer < BasicPostSerializer
|
|||
|
||||
private
|
||||
|
||||
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?
|
||||
|
|
|
@ -121,10 +121,20 @@ describe PostsController do
|
|||
let(: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_blank
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -134,9 +144,20 @@ describe PostsController do
|
|||
end
|
||||
|
||||
it '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_blank
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue