2020-01-28 06:55:46 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserCardSerializer < BasicUserSerializer
|
2024-02-26 08:40:48 -05:00
|
|
|
include UserStatusMixin
|
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
attr_accessor :topic_post_count
|
|
|
|
|
2024-02-26 08:40:48 -05:00
|
|
|
def initialize(object, options = {})
|
|
|
|
super
|
|
|
|
options[:include_status] = true
|
|
|
|
end
|
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
def self.staff_attributes(*attrs)
|
|
|
|
attributes(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
define_method "include_#{attr}?" do
|
|
|
|
scope.is_staff?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.private_attributes(*attrs)
|
|
|
|
attributes(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
define_method "include_#{attr}?" do
|
2022-08-11 23:26:56 -04:00
|
|
|
if defined?(super)
|
|
|
|
super() && can_edit
|
|
|
|
else
|
|
|
|
can_edit
|
|
|
|
end
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# attributes that are hidden for TL0 users when seen by anonymous
|
|
|
|
def self.untrusted_attributes(*attrs)
|
|
|
|
attributes(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
method_name = "include_#{attr}?"
|
|
|
|
define_method(method_name) do
|
|
|
|
return false if scope.restrict_user_fields?(object)
|
|
|
|
public_send(attr).present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes :email,
|
2020-06-10 12:11:49 -04:00
|
|
|
:secondary_emails,
|
|
|
|
:unconfirmed_emails,
|
2020-01-28 06:55:46 -05:00
|
|
|
:last_posted_at,
|
|
|
|
:last_seen_at,
|
|
|
|
:created_at,
|
|
|
|
:ignored,
|
|
|
|
:muted,
|
|
|
|
:can_ignore_user,
|
|
|
|
:can_mute_user,
|
|
|
|
:can_send_private_messages,
|
|
|
|
:can_send_private_message_to_user,
|
|
|
|
:trust_level,
|
|
|
|
:moderator,
|
|
|
|
:admin,
|
|
|
|
:title,
|
|
|
|
:suspend_reason,
|
|
|
|
:suspended_till,
|
|
|
|
:badge_count,
|
|
|
|
:user_fields,
|
|
|
|
:custom_fields,
|
|
|
|
:topic_post_count,
|
|
|
|
:time_read,
|
|
|
|
:recent_time_read,
|
|
|
|
:primary_group_id,
|
|
|
|
:primary_group_name,
|
2021-07-20 06:06:40 -04:00
|
|
|
:flair_group_id,
|
2021-07-08 03:46:21 -04:00
|
|
|
:flair_name,
|
|
|
|
:flair_url,
|
|
|
|
:flair_bg_color,
|
|
|
|
:flair_color,
|
2020-04-27 20:13:59 -04:00
|
|
|
:featured_topic,
|
2021-08-26 12:16:00 -04:00
|
|
|
:timezone,
|
2024-02-26 08:40:48 -05:00
|
|
|
:pending_posts_count
|
2020-01-28 06:55:46 -05:00
|
|
|
|
|
|
|
untrusted_attributes :bio_excerpt, :website, :website_name, :location, :card_background_upload_url
|
|
|
|
|
|
|
|
staff_attributes :staged
|
|
|
|
|
|
|
|
has_many :featured_user_badges, embed: :ids, serializer: UserBadgeSerializer, root: :user_badges
|
|
|
|
|
2021-08-26 12:16:00 -04:00
|
|
|
delegate :user_stat, to: :object, private: true
|
|
|
|
delegate :pending_posts_count, to: :user_stat
|
|
|
|
|
|
|
|
def include_pending_posts_count?
|
|
|
|
scope.is_me?(object) || scope.is_staff?
|
|
|
|
end
|
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
def include_email?
|
|
|
|
(object.id && object.id == scope.user.try(:id)) || (scope.is_staff? && object.staged?)
|
|
|
|
end
|
|
|
|
|
2020-06-15 20:43:06 -04:00
|
|
|
alias_method :include_secondary_emails?, :include_email?
|
|
|
|
alias_method :include_unconfirmed_emails?, :include_email?
|
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
def bio_excerpt
|
|
|
|
object.user_profile.bio_excerpt(350, keep_newlines: true, keep_emoji_images: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def location
|
|
|
|
object.user_profile.location
|
|
|
|
end
|
|
|
|
|
|
|
|
def website
|
|
|
|
object.user_profile.website
|
|
|
|
end
|
|
|
|
|
|
|
|
def website_name
|
|
|
|
uri =
|
|
|
|
begin
|
|
|
|
URI(website.to_s)
|
|
|
|
rescue URI::Error
|
|
|
|
end
|
|
|
|
|
|
|
|
return if uri.nil? || uri.host.nil?
|
2023-01-20 13:52:49 -05:00
|
|
|
uri.host.sub(/\Awww\./, "") + uri.path
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ignored
|
|
|
|
scope_ignored_user_ids = scope.user&.ignored_user_ids || []
|
|
|
|
scope_ignored_user_ids.include?(object.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def muted
|
|
|
|
scope_muted_user_ids = scope.user&.muted_user_ids || []
|
|
|
|
scope_muted_user_ids.include?(object.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_mute_user
|
|
|
|
scope.can_mute_user?(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_ignore_user
|
|
|
|
scope.can_ignore_user?(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Needed because 'send_private_message_to_user' will always return false
|
|
|
|
# when the current user is being serialized
|
|
|
|
def can_send_private_messages
|
2022-11-06 18:11:18 -05:00
|
|
|
scope.can_send_private_messages?
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_send_private_message_to_user
|
2024-07-01 20:01:58 -04:00
|
|
|
scope.can_send_private_message?(object)
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_suspend_reason?
|
|
|
|
scope.can_see_suspension_reason?(object) && object.suspended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_suspended_till?
|
|
|
|
object.suspended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_fields
|
2020-03-02 14:22:49 -05:00
|
|
|
allowed_keys = scope.allowed_user_field_ids(object)
|
|
|
|
object.user_fields(allowed_keys)
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_fields?
|
|
|
|
user_fields.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_fields
|
2020-03-03 08:56:54 -05:00
|
|
|
fields = custom_field_keys
|
2020-01-28 06:55:46 -05:00
|
|
|
|
|
|
|
if fields.present?
|
2020-03-03 08:56:54 -05:00
|
|
|
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
|
2020-01-28 06:55:46 -05:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_topic_post_count?
|
|
|
|
topic_post_count.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def time_read
|
|
|
|
object.user_stat&.time_read
|
|
|
|
end
|
|
|
|
|
|
|
|
def recent_time_read
|
|
|
|
time = object.recent_time_read
|
|
|
|
end
|
|
|
|
|
|
|
|
def primary_group_name
|
2021-07-08 03:46:21 -04:00
|
|
|
object.primary_group&.name
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
2021-07-08 03:46:21 -04:00
|
|
|
def flair_name
|
|
|
|
object.flair_group&.name
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
2021-07-08 03:46:21 -04:00
|
|
|
def flair_url
|
|
|
|
object.flair_group&.flair_url
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
2021-07-08 03:46:21 -04:00
|
|
|
def flair_bg_color
|
|
|
|
object.flair_group&.flair_bg_color
|
|
|
|
end
|
|
|
|
|
|
|
|
def flair_color
|
|
|
|
object.flair_group&.flair_color
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def featured_topic
|
2024-07-04 23:00:24 -04:00
|
|
|
BasicTopicSerializer.new(object.user_profile.featured_topic, scope: scope, root: false).as_json
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|
|
|
|
|
2020-04-27 20:13:59 -04:00
|
|
|
def include_timezone?
|
|
|
|
SiteSetting.display_local_time_in_user_card?
|
|
|
|
end
|
|
|
|
|
|
|
|
def timezone
|
|
|
|
object.user_option.timezone
|
|
|
|
end
|
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
def card_background_upload_url
|
|
|
|
object.card_background_upload&.url
|
|
|
|
end
|
2020-03-03 08:56:54 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def custom_field_keys
|
|
|
|
# Can be extended by other serializers
|
2020-07-26 20:23:54 -04:00
|
|
|
User.allowed_user_custom_fields(scope)
|
2020-03-03 08:56:54 -05:00
|
|
|
end
|
2020-01-28 06:55:46 -05:00
|
|
|
end
|