mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 04:31:43 +08:00
PERF: Allow user serializer to make use of preloaded custom fields (#9074)
This commit is contained in:
parent
0df72a51b8
commit
d23f7af3cb
|
@ -142,6 +142,10 @@ module HasCustomFields
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def custom_fields_preloaded?
|
||||||
|
!!@preloaded_custom_fields
|
||||||
|
end
|
||||||
|
|
||||||
def custom_field_preloaded?(name)
|
def custom_field_preloaded?(name)
|
||||||
@preloaded_custom_fields && @preloaded_custom_fields.key?(name)
|
@preloaded_custom_fields && @preloaded_custom_fields.key?(name)
|
||||||
end
|
end
|
||||||
|
|
|
@ -149,14 +149,14 @@ class UserCardSerializer < BasicUserSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def custom_fields
|
def custom_fields
|
||||||
fields = User.whitelisted_user_custom_fields(scope)
|
fields = custom_field_keys
|
||||||
|
|
||||||
if scope.can_edit?(object)
|
|
||||||
fields += DiscoursePluginRegistry.serialized_current_user_fields.to_a
|
|
||||||
end
|
|
||||||
|
|
||||||
if fields.present?
|
if fields.present?
|
||||||
User.custom_fields_for_ids([object.id], fields)[object.id] || {}
|
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
|
else
|
||||||
{}
|
{}
|
||||||
end
|
end
|
||||||
|
@ -197,4 +197,11 @@ class UserCardSerializer < BasicUserSerializer
|
||||||
def card_background_upload_url
|
def card_background_upload_url
|
||||||
object.card_background_upload&.url
|
object.card_background_upload&.url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def custom_field_keys
|
||||||
|
# Can be extended by other serializers
|
||||||
|
User.whitelisted_user_custom_fields(scope)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -270,4 +270,16 @@ class UserSerializer < UserCardSerializer
|
||||||
object.profile_background_upload&.url
|
object.profile_background_upload&.url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def custom_field_keys
|
||||||
|
fields = super
|
||||||
|
|
||||||
|
if scope.can_edit?(object)
|
||||||
|
fields += DiscoursePluginRegistry.serialized_current_user_fields.to_a
|
||||||
|
end
|
||||||
|
|
||||||
|
fields
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user