mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 10:52:45 +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
|
||||
end
|
||||
|
||||
def custom_fields_preloaded?
|
||||
!!@preloaded_custom_fields
|
||||
end
|
||||
|
||||
def custom_field_preloaded?(name)
|
||||
@preloaded_custom_fields && @preloaded_custom_fields.key?(name)
|
||||
end
|
||||
|
|
|
@ -149,14 +149,14 @@ class UserCardSerializer < BasicUserSerializer
|
|||
end
|
||||
|
||||
def custom_fields
|
||||
fields = User.whitelisted_user_custom_fields(scope)
|
||||
|
||||
if scope.can_edit?(object)
|
||||
fields += DiscoursePluginRegistry.serialized_current_user_fields.to_a
|
||||
end
|
||||
fields = custom_field_keys
|
||||
|
||||
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
|
||||
{}
|
||||
end
|
||||
|
@ -197,4 +197,11 @@ class UserCardSerializer < BasicUserSerializer
|
|||
def card_background_upload_url
|
||||
object.card_background_upload&.url
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def custom_field_keys
|
||||
# Can be extended by other serializers
|
||||
User.whitelisted_user_custom_fields(scope)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -270,4 +270,16 @@ class UserSerializer < UserCardSerializer
|
|||
object.profile_background_upload&.url
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user