PERF: Allow user serializer to make use of preloaded custom fields (#9074)

This commit is contained in:
David Taylor 2020-03-03 13:56:54 +00:00 committed by GitHub
parent 0df72a51b8
commit d23f7af3cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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