discourse/app/serializers/user_serializer.rb

176 lines
4.2 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
class UserSerializer < BasicUserSerializer
2013-02-07 23:45:24 +08:00
attributes :name,
:email,
:last_posted_at,
:last_seen_at,
2013-02-06 03:16:51 +08:00
:bio_raw,
2013-02-07 23:45:24 +08:00
:bio_cooked,
:created_at,
:website,
:profile_background,
:location,
2013-02-07 23:45:24 +08:00
:can_edit,
:can_edit_username,
:can_edit_email,
:can_edit_name,
2013-02-07 23:45:24 +08:00
:stats,
2013-02-06 03:16:51 +08:00
:can_send_private_message_to_user,
:bio_excerpt,
2013-05-02 15:40:44 +08:00
:trust_level,
:moderator,
:admin,
:title,
:suspend_reason,
:suspended_till,
2014-06-02 10:59:54 +08:00
:uploaded_avatar_id,
:badge_count
2013-02-06 03:16:51 +08:00
has_one :invited_by, embed: :object, serializer: BasicUserSerializer
2014-02-07 06:34:48 +08:00
has_many :custom_groups, embed: :object, serializer: BasicGroupSerializer
has_many :featured_user_badges, embed: :ids, serializer: UserBadgeSerializer, root: :user_badges
2013-02-06 03:16:51 +08:00
def self.private_attributes(*attrs)
attributes(*attrs)
2013-02-06 03:16:51 +08:00
attrs.each do |attr|
2013-02-07 23:45:24 +08:00
define_method "include_#{attr}?" do
2013-02-06 03:16:51 +08:00
can_edit
end
end
end
def bio_excerpt
# If they have a bio return it
2014-06-10 13:19:08 +08:00
excerpt = object.user_profile.bio_excerpt
return excerpt if excerpt.present?
# Without a bio, determine what message to show
if scope.user && scope.user.id == object.id
I18n.t('user_profile.no_info_me', username_lower: object.username_lower)
else
I18n.t('user_profile.no_info_other', name: object.name)
2013-02-06 03:16:51 +08:00
end
end
2013-02-07 23:45:24 +08:00
private_attributes :email,
:locale,
:email_digests,
:email_private_messages,
:email_direct,
:email_always,
:digest_after_days,
:mailing_list_mode,
:auto_track_topics_after_msecs,
:new_topic_duration_minutes,
:external_links_in_new_tab,
:dynamic_favicon,
:enable_quoting,
:muted_category_ids,
:tracked_category_ids,
:watched_category_ids,
:private_messages_stats,
:disable_jump_reply,
:gravatar_avatar_upload_id,
:custom_avatar_upload_id,
:custom_fields
def gravatar_avatar_upload_id
object.user_avatar.try(:gravatar_upload_id)
end
def custom_avatar_upload_id
object.user_avatar.try(:custom_upload_id)
end
2013-02-06 03:16:51 +08:00
def auto_track_topics_after_msecs
object.auto_track_topics_after_msecs || SiteSetting.auto_track_topics_after
end
2013-02-26 00:42:20 +08:00
def new_topic_duration_minutes
object.new_topic_duration_minutes || SiteSetting.new_topic_duration_minutes
end
2013-02-06 03:16:51 +08:00
def can_send_private_message_to_user
scope.can_send_private_message?(object)
end
def can_edit
scope.can_edit?(object)
end
def can_edit_username
scope.can_edit_username?(object)
end
def can_edit_email
scope.can_edit_email?(object)
end
def can_edit_name
scope.can_edit_name?(object)
end
def location
object.user_profile.location
end
def include_location?
location.present?
end
2014-06-07 12:54:32 +08:00
def website
object.user_profile.website
2014-06-07 12:54:32 +08:00
end
def include_website?
website.present?
end
2014-06-10 13:19:08 +08:00
def include_bio_raw?
bio_raw.present?
end
def bio_raw
object.user_profile.bio_raw
end
def bio_cooked
object.user_profile.bio_processed
end
def profile_background
object.user_profile.profile_background
end
def include_profile_background?
profile_background.present?
end
def stats
UserAction.stats(object.id, scope)
end
def include_suspended?
object.suspended?
end
def include_suspend_reason?
object.suspended?
end
def include_suspended_till?
object.suspended?
end
def muted_category_ids
CategoryUser.lookup(object, :muted).pluck(:category_id)
end
def tracked_category_ids
CategoryUser.lookup(object, :tracking).pluck(:category_id)
end
def watched_category_ids
CategoryUser.lookup(object, :watching).pluck(:category_id)
end
def private_messages_stats
UserAction.private_messages_stats(object.id, scope)
end
2013-02-06 03:16:51 +08:00
end