2013-02-06 03:16:51 +08:00
|
|
|
class UserSerializer < BasicUserSerializer
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2015-03-25 00:33:17 +08:00
|
|
|
attr_accessor :omit_stats,
|
|
|
|
:topic_post_count
|
2015-02-24 10:31:23 +08:00
|
|
|
|
2014-07-01 04:46:47 +08: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
|
|
|
|
can_edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-27 02:20:03 +08:00
|
|
|
# attributes that are hidden for TL0 users when seen by anonymous
|
|
|
|
def self.untrusted_attributes(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
method_name = "include_#{attr}?"
|
|
|
|
define_method(method_name) do
|
2014-11-28 02:51:13 +08:00
|
|
|
return false if scope.restrict_user_fields?(object)
|
2014-11-27 02:20:03 +08:00
|
|
|
send(attr).present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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,
|
2015-08-10 16:07:53 +08:00
|
|
|
:website_name,
|
2014-03-01 04:12:51 +08:00
|
|
|
:profile_background,
|
2014-10-21 00:11:36 +08:00
|
|
|
:card_background,
|
2014-05-28 01:54:04 +08:00
|
|
|
:location,
|
2013-02-07 23:45:24 +08:00
|
|
|
:can_edit,
|
2013-08-13 02:54:52 +08:00
|
|
|
:can_edit_username,
|
2013-09-08 10:42:41 +08:00
|
|
|
:can_edit_email,
|
2014-03-14 04:26:40 +08:00
|
|
|
:can_edit_name,
|
2013-02-07 23:45:24 +08:00
|
|
|
:stats,
|
2014-11-30 10:09:34 +08:00
|
|
|
:can_send_private_messages,
|
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,
|
2013-06-26 06:39:20 +08:00
|
|
|
:admin,
|
2013-11-08 05:34:18 +08:00
|
|
|
:title,
|
|
|
|
:suspend_reason,
|
2014-03-28 16:49:30 +08:00
|
|
|
:suspended_till,
|
2014-06-02 10:59:54 +08:00
|
|
|
:uploaded_avatar_id,
|
2014-07-09 13:31:49 +08:00
|
|
|
:badge_count,
|
2014-07-28 01:12:36 +08:00
|
|
|
:has_title_badges,
|
2014-09-27 02:48:34 +08:00
|
|
|
:custom_fields,
|
2015-03-25 00:33:17 +08:00
|
|
|
:user_fields,
|
2015-04-22 02:36:46 +08:00
|
|
|
:topic_post_count,
|
2016-04-01 05:33:25 +08:00
|
|
|
:pending_count,
|
|
|
|
:profile_view_count
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-22 00:03:51 +08:00
|
|
|
has_one :invited_by, embed: :object, serializer: BasicUserSerializer
|
2016-04-01 05:33:25 +08:00
|
|
|
has_many :groups, embed: :object, serializer: BasicGroupSerializer
|
2014-03-28 16:49:30 +08:00
|
|
|
has_many :featured_user_badges, embed: :ids, serializer: UserBadgeSerializer, root: :user_badges
|
2014-10-21 01:15:58 +08:00
|
|
|
has_one :card_badge, embed: :object, serializer: BadgeSerializer
|
2016-04-01 05:33:25 +08:00
|
|
|
has_one :user_option, embed: :object, serializer: UserOptionSerializer
|
|
|
|
|
|
|
|
def include_user_option?
|
|
|
|
can_edit
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-03 01:52:56 +08:00
|
|
|
staff_attributes :post_count,
|
2014-11-15 04:23:09 +08:00
|
|
|
:can_be_deleted,
|
|
|
|
:can_delete_all_posts
|
2013-06-05 00:05:36 +08:00
|
|
|
|
2014-09-30 04:31:05 +08:00
|
|
|
private_attributes :locale,
|
2014-01-02 14:58:49 +08:00
|
|
|
:muted_category_ids,
|
2014-01-06 08:57:17 +08:00
|
|
|
:tracked_category_ids,
|
2014-05-03 04:36:52 +08:00
|
|
|
:watched_category_ids,
|
2014-05-27 04:39:03 +08:00
|
|
|
:private_messages_stats,
|
2015-09-11 18:56:34 +08:00
|
|
|
:system_avatar_upload_id,
|
|
|
|
:system_avatar_template,
|
2014-05-22 15:37:02 +08:00
|
|
|
:gravatar_avatar_upload_id,
|
2015-09-11 18:56:34 +08:00
|
|
|
:gravatar_avatar_template,
|
2014-06-11 13:50:37 +08:00
|
|
|
:custom_avatar_upload_id,
|
2015-09-11 18:56:34 +08:00
|
|
|
:custom_avatar_template,
|
2014-10-21 01:15:58 +08:00
|
|
|
:has_title_badges,
|
|
|
|
:card_image_badge,
|
2015-03-24 08:55:22 +08:00
|
|
|
:card_image_badge_id,
|
2016-04-01 05:33:25 +08:00
|
|
|
:muted_usernames,
|
|
|
|
:mailing_list_posts_per_day
|
2013-05-21 04:52:37 +08:00
|
|
|
|
2014-11-27 02:20:03 +08:00
|
|
|
untrusted_attributes :bio_raw,
|
|
|
|
:bio_cooked,
|
|
|
|
:bio_excerpt,
|
|
|
|
:location,
|
|
|
|
:website,
|
|
|
|
:profile_background,
|
|
|
|
:card_background
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
###
|
|
|
|
### ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def mailing_list_posts_per_day
|
|
|
|
val = Post.estimate_posts_per_day
|
|
|
|
[val,SiteSetting.max_emails_per_day_per_user].min
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups
|
|
|
|
if scope.is_admin? || object.id == scope.user.try(:id)
|
|
|
|
object.groups
|
|
|
|
else
|
|
|
|
object.groups.where(visible: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-30 04:31:05 +08:00
|
|
|
def include_email?
|
|
|
|
object.id && object.id == scope.user.try(:id)
|
|
|
|
end
|
|
|
|
|
2014-10-21 01:15:58 +08:00
|
|
|
def card_badge
|
|
|
|
object.user_profile.card_image_badge
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def bio_raw
|
|
|
|
object.user_profile.bio_raw
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def bio_cooked
|
|
|
|
object.user_profile.bio_processed
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def website
|
|
|
|
object.user_profile.website
|
2013-02-14 14:32:58 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-08-10 16:07:53 +08:00
|
|
|
def website_name
|
|
|
|
website_host = URI(website.to_s).host rescue nil
|
|
|
|
discourse_host = Discourse.current_hostname
|
|
|
|
return if website_host.nil?
|
|
|
|
if website_host == discourse_host
|
|
|
|
# example.com == example.com
|
|
|
|
website_host + URI(website.to_s).path
|
|
|
|
elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2
|
|
|
|
# www.example.com == forum.example.com
|
|
|
|
website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host
|
|
|
|
else
|
|
|
|
# example.com == forum.example.com
|
|
|
|
discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_website_name
|
|
|
|
website.present?
|
|
|
|
end
|
|
|
|
|
2014-10-21 01:15:58 +08:00
|
|
|
def card_image_badge_id
|
|
|
|
object.user_profile.card_image_badge.try(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_card_image_badge_id?
|
|
|
|
card_image_badge_id.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def card_image_badge
|
|
|
|
object.user_profile.card_image_badge.try(:image)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_card_image_badge?
|
|
|
|
card_image_badge.present?
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def profile_background
|
|
|
|
object.user_profile.profile_background
|
|
|
|
end
|
|
|
|
|
2014-10-21 00:11:36 +08:00
|
|
|
def card_background
|
|
|
|
object.user_profile.card_background
|
2014-10-17 03:05:36 +08:00
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def location
|
|
|
|
object.user_profile.location
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def can_edit
|
|
|
|
scope.can_edit?(object)
|
|
|
|
end
|
|
|
|
|
2013-08-13 02:54:52 +08:00
|
|
|
def can_edit_username
|
|
|
|
scope.can_edit_username?(object)
|
|
|
|
end
|
|
|
|
|
2013-09-08 10:42:41 +08:00
|
|
|
def can_edit_email
|
|
|
|
scope.can_edit_email?(object)
|
|
|
|
end
|
|
|
|
|
2014-03-14 04:26:40 +08:00
|
|
|
def can_edit_name
|
|
|
|
scope.can_edit_name?(object)
|
|
|
|
end
|
|
|
|
|
2015-02-24 10:31:23 +08:00
|
|
|
def include_stats?
|
|
|
|
!omit_stats == true
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def stats
|
|
|
|
UserAction.stats(object.id, scope)
|
2014-05-28 01:54:04 +08:00
|
|
|
end
|
|
|
|
|
2014-11-30 10:09:34 +08:00
|
|
|
# Needed because 'send_private_message_to_user' will always return false
|
|
|
|
# when the current user is being serialized
|
|
|
|
def can_send_private_messages
|
|
|
|
scope.can_send_private_message?(Discourse.system_user)
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def can_send_private_message_to_user
|
|
|
|
scope.can_send_private_message?(object)
|
2014-06-07 12:54:32 +08:00
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def bio_excerpt
|
2016-04-01 05:33:25 +08:00
|
|
|
object.user_profile.bio_excerpt(350 , { keep_newlines: true, keep_emoji_images: true })
|
2014-06-10 13:19:08 +08:00
|
|
|
end
|
2014-07-01 04:46:47 +08:00
|
|
|
|
|
|
|
def include_suspend_reason?
|
|
|
|
object.suspended?
|
2014-06-10 13:19:08 +08:00
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def include_suspended_till?
|
|
|
|
object.suspended?
|
2014-06-10 13:19:08 +08:00
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
###
|
|
|
|
### STAFF ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
2014-12-03 01:52:56 +08:00
|
|
|
def post_count
|
|
|
|
object.user_stat.try(:post_count)
|
|
|
|
end
|
|
|
|
|
2014-11-15 04:23:09 +08:00
|
|
|
def can_be_deleted
|
|
|
|
scope.can_delete_user?(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_all_posts
|
|
|
|
scope.can_delete_all_posts?(object)
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
###
|
|
|
|
### PRIVATE ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
2014-01-02 14:58:49 +08:00
|
|
|
def muted_category_ids
|
|
|
|
CategoryUser.lookup(object, :muted).pluck(:category_id)
|
|
|
|
end
|
|
|
|
|
2014-01-06 08:57:17 +08:00
|
|
|
def tracked_category_ids
|
|
|
|
CategoryUser.lookup(object, :tracking).pluck(:category_id)
|
|
|
|
end
|
|
|
|
|
2014-01-02 14:58:49 +08:00
|
|
|
def watched_category_ids
|
|
|
|
CategoryUser.lookup(object, :watching).pluck(:category_id)
|
|
|
|
end
|
2014-03-28 16:49:30 +08:00
|
|
|
|
2015-03-24 08:55:22 +08:00
|
|
|
def muted_usernames
|
|
|
|
MutedUser.where(user_id: object.id).joins(:muted_user).pluck(:username)
|
|
|
|
end
|
|
|
|
|
2015-09-10 15:18:43 +08:00
|
|
|
def include_private_messages_stats?
|
2015-02-24 10:39:31 +08:00
|
|
|
can_edit && !(omit_stats == true)
|
|
|
|
end
|
|
|
|
|
2014-05-03 04:36:52 +08:00
|
|
|
def private_messages_stats
|
|
|
|
UserAction.private_messages_stats(object.id, scope)
|
|
|
|
end
|
2014-07-01 04:46:47 +08:00
|
|
|
|
2015-09-11 18:56:34 +08:00
|
|
|
def system_avatar_upload_id
|
|
|
|
# should be left blank
|
|
|
|
end
|
|
|
|
|
|
|
|
def system_avatar_template
|
|
|
|
User.system_avatar_template(object.username)
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def gravatar_avatar_upload_id
|
|
|
|
object.user_avatar.try(:gravatar_upload_id)
|
|
|
|
end
|
|
|
|
|
2015-09-11 18:56:34 +08:00
|
|
|
def gravatar_avatar_template
|
|
|
|
return unless gravatar_upload_id = object.user_avatar.try(:gravatar_upload_id)
|
|
|
|
User.avatar_template(object.username, gravatar_upload_id)
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def custom_avatar_upload_id
|
|
|
|
object.user_avatar.try(:custom_upload_id)
|
|
|
|
end
|
|
|
|
|
2015-09-11 18:56:34 +08:00
|
|
|
def custom_avatar_template
|
|
|
|
return unless custom_upload_id = object.user_avatar.try(:custom_upload_id)
|
|
|
|
User.avatar_template(object.username, custom_upload_id)
|
|
|
|
end
|
|
|
|
|
2014-07-09 13:31:49 +08:00
|
|
|
def has_title_badges
|
|
|
|
object.badges.where(allow_title: true).count > 0
|
|
|
|
end
|
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
def user_fields
|
|
|
|
object.user_fields
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_fields?
|
|
|
|
user_fields.present?
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:33:17 +08:00
|
|
|
def include_topic_post_count?
|
|
|
|
topic_post_count.present?
|
|
|
|
end
|
|
|
|
|
2014-08-19 23:05:35 +08:00
|
|
|
def custom_fields
|
2016-04-01 05:33:25 +08:00
|
|
|
fields = User.whitelisted_user_custom_fields(scope)
|
2014-08-19 23:05:35 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
if scope.can_edit?(object)
|
|
|
|
fields += DiscoursePluginRegistry.serialized_current_user_fields.to_a
|
2014-08-19 23:05:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if fields.present?
|
|
|
|
User.custom_fields_for_ids([object.id], fields)[object.id]
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
2015-04-22 02:36:46 +08:00
|
|
|
|
|
|
|
def pending_count
|
|
|
|
0
|
|
|
|
end
|
2015-09-11 08:12:40 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def profile_view_count
|
|
|
|
object.user_profile.views
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|