discourse/app/serializers/user_with_custom_fields_serializer.rb
Andrei Prigorshnev b3a1199493
FEATURE: Hide user status when user is hiding public profile and presence (#24300)
Users can hide their public profile and presence information by checking 
“Hide my public profile and presence features” on the 
`u/{username}/preferences/interface` page. In that case, we also don't 
want to return user status from the server.

This work has been started in https://github.com/discourse/discourse/pull/23946. 
The current PR fixes all the remaining places in Core.

Note that the actual fix is quite simple – a5802f484d. 
But we had a fair amount of duplication in the code responsible for 
the user status serialization, so I had to dry that up first. The refactoring 
as well as adding some additional tests is the main part of this PR.
2024-02-26 17:40:48 +04:00

29 lines
638 B
Ruby

# frozen_string_literal: true
# A basic user serializer, with custom fields
class UserWithCustomFieldsSerializer < BasicUserSerializer
include UserStatusMixin
attribute :custom_fields
def custom_fields
fields = custom_field_keys
if fields.present?
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
end
private
def custom_field_keys
# Can be extended by other serializers
User.allowed_user_custom_fields(scope)
end
end