2013-02-05 14:16:51 -05:00
|
|
|
class UserSerializer < BasicUserSerializer
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2014-06-30 16:46:47 -04: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
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
attributes :name,
|
|
|
|
:email,
|
|
|
|
:last_posted_at,
|
|
|
|
:last_seen_at,
|
2013-02-05 14:16:51 -05:00
|
|
|
:bio_raw,
|
2013-02-07 10:45:24 -05:00
|
|
|
:bio_cooked,
|
|
|
|
:created_at,
|
|
|
|
:website,
|
2014-02-28 15:12:51 -05:00
|
|
|
:profile_background,
|
2014-05-27 13:54:04 -04:00
|
|
|
:location,
|
2013-02-07 10:45:24 -05:00
|
|
|
:can_edit,
|
2013-08-12 14:54:52 -04:00
|
|
|
:can_edit_username,
|
2013-09-07 22:42:41 -04:00
|
|
|
:can_edit_email,
|
2014-03-13 16:26:40 -04:00
|
|
|
:can_edit_name,
|
2013-02-07 10:45:24 -05:00
|
|
|
:stats,
|
2013-02-05 14:16:51 -05:00
|
|
|
:can_send_private_message_to_user,
|
|
|
|
:bio_excerpt,
|
2013-05-02 03:40:44 -04:00
|
|
|
:trust_level,
|
|
|
|
:moderator,
|
2013-06-25 18:39:20 -04:00
|
|
|
:admin,
|
2013-11-07 16:34:18 -05:00
|
|
|
:title,
|
|
|
|
:suspend_reason,
|
2014-03-28 04:49:30 -04:00
|
|
|
:suspended_till,
|
2014-06-01 22:59:54 -04:00
|
|
|
:uploaded_avatar_id,
|
2014-07-09 01:31:49 -04:00
|
|
|
:badge_count,
|
2014-09-09 23:13:36 -04:00
|
|
|
:notification_count,
|
2014-07-27 13:12:36 -04:00
|
|
|
:has_title_badges,
|
2014-08-19 11:05:35 -04:00
|
|
|
:edit_history_public,
|
|
|
|
:custom_fields
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-21 12:03:51 -04:00
|
|
|
has_one :invited_by, embed: :object, serializer: BasicUserSerializer
|
2014-02-06 17:34:48 -05:00
|
|
|
has_many :custom_groups, embed: :object, serializer: BasicGroupSerializer
|
2014-03-28 04:49:30 -04:00
|
|
|
has_many :featured_user_badges, embed: :ids, serializer: UserBadgeSerializer, root: :user_badges
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
staff_attributes :number_of_deleted_posts,
|
|
|
|
:number_of_flagged_posts,
|
|
|
|
:number_of_flags_given,
|
2014-09-08 11:11:56 -04:00
|
|
|
:number_of_suspensions,
|
|
|
|
:number_of_warnings
|
2013-06-04 12:05:36 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
private_attributes :email,
|
2014-02-07 22:24:10 -05:00
|
|
|
:locale,
|
2013-05-20 16:52:37 -04:00
|
|
|
:email_digests,
|
|
|
|
:email_private_messages,
|
|
|
|
:email_direct,
|
2013-10-01 03:04:02 -04:00
|
|
|
:email_always,
|
2013-05-20 16:52:37 -04:00
|
|
|
:digest_after_days,
|
2014-02-06 19:06:35 -05:00
|
|
|
:mailing_list_mode,
|
2013-05-20 16:52:37 -04:00
|
|
|
:auto_track_topics_after_msecs,
|
|
|
|
:new_topic_duration_minutes,
|
|
|
|
:external_links_in_new_tab,
|
2013-06-15 02:58:24 -04:00
|
|
|
:dynamic_favicon,
|
2013-08-25 11:26:44 -04:00
|
|
|
:enable_quoting,
|
2014-01-02 01:58:49 -05:00
|
|
|
:muted_category_ids,
|
2014-01-05 19:57:17 -05:00
|
|
|
:tracked_category_ids,
|
2014-05-02 16:36:52 -04:00
|
|
|
:watched_category_ids,
|
2014-05-26 16:39:03 -04:00
|
|
|
:private_messages_stats,
|
2014-09-09 23:13:36 -04:00
|
|
|
:notification_count,
|
2014-05-22 03:37:02 -04:00
|
|
|
:disable_jump_reply,
|
|
|
|
:gravatar_avatar_upload_id,
|
2014-06-11 01:50:37 -04:00
|
|
|
:custom_avatar_upload_id,
|
2014-07-27 13:12:36 -04:00
|
|
|
:has_title_badges
|
2013-05-20 16:52:37 -04:00
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
###
|
|
|
|
### ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
|
|
|
def bio_raw
|
|
|
|
object.user_profile.bio_raw
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def include_bio_raw?
|
|
|
|
bio_raw.present?
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def bio_cooked
|
|
|
|
object.user_profile.bio_processed
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def website
|
|
|
|
object.user_profile.website
|
2013-02-14 01:32:58 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def include_website?
|
|
|
|
website.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def profile_background
|
|
|
|
object.user_profile.profile_background
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_profile_background?
|
|
|
|
profile_background.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def location
|
|
|
|
object.user_profile.location
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_location?
|
|
|
|
location.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit
|
|
|
|
scope.can_edit?(object)
|
|
|
|
end
|
|
|
|
|
2013-08-12 14:54:52 -04:00
|
|
|
def can_edit_username
|
|
|
|
scope.can_edit_username?(object)
|
|
|
|
end
|
|
|
|
|
2013-09-07 22:42:41 -04:00
|
|
|
def can_edit_email
|
|
|
|
scope.can_edit_email?(object)
|
|
|
|
end
|
|
|
|
|
2014-03-13 16:26:40 -04:00
|
|
|
def can_edit_name
|
|
|
|
scope.can_edit_name?(object)
|
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def stats
|
|
|
|
UserAction.stats(object.id, scope)
|
2014-05-27 13:54:04 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def can_send_private_message_to_user
|
|
|
|
scope.can_send_private_message?(object)
|
2014-06-07 00:54:32 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def bio_excerpt
|
|
|
|
# If they have a bio return it
|
|
|
|
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)
|
|
|
|
end
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
2014-06-30 16:46:47 -04:00
|
|
|
|
|
|
|
def include_suspend_reason?
|
|
|
|
object.suspended?
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def include_suspended_till?
|
|
|
|
object.suspended?
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
###
|
|
|
|
### STAFF ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
|
|
|
def number_of_deleted_posts
|
|
|
|
Post.with_deleted
|
|
|
|
.where(user_id: object.id)
|
|
|
|
.where(user_deleted: false)
|
2014-07-02 16:33:18 -04:00
|
|
|
.where.not(deleted_by_id: object.id)
|
2014-06-30 16:46:47 -04:00
|
|
|
.count
|
2014-06-11 21:52:50 -04:00
|
|
|
end
|
2014-06-30 16:46:47 -04:00
|
|
|
|
|
|
|
def number_of_flagged_posts
|
|
|
|
Post.with_deleted
|
|
|
|
.where(user_id: object.id)
|
2014-07-02 16:33:18 -04:00
|
|
|
.where(id: PostAction.with_deleted
|
|
|
|
.where(post_action_type_id: PostActionType.notify_flag_type_ids)
|
|
|
|
.select(:post_id))
|
2014-06-30 16:46:47 -04:00
|
|
|
.count
|
2014-06-11 21:52:50 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def number_of_flags_given
|
2014-07-02 16:33:18 -04:00
|
|
|
PostAction.with_deleted
|
2014-06-30 16:46:47 -04:00
|
|
|
.where(user_id: object.id)
|
2014-07-02 16:33:18 -04:00
|
|
|
.where(post_action_type_id: PostActionType.notify_flag_type_ids)
|
2014-06-30 16:46:47 -04:00
|
|
|
.count
|
2013-05-22 11:20:16 -04:00
|
|
|
end
|
|
|
|
|
2014-09-08 11:11:56 -04:00
|
|
|
def number_of_warnings
|
|
|
|
object.warnings.count
|
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def number_of_suspensions
|
|
|
|
UserHistory.for(object, :suspend_user).count
|
2013-11-07 16:34:18 -05:00
|
|
|
end
|
2014-06-30 16:46:47 -04:00
|
|
|
|
|
|
|
###
|
|
|
|
### PRIVATE ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
|
|
|
def auto_track_topics_after_msecs
|
|
|
|
object.auto_track_topics_after_msecs || SiteSetting.auto_track_topics_after
|
2013-11-07 16:34:18 -05:00
|
|
|
end
|
2014-01-02 01:58:49 -05:00
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def new_topic_duration_minutes
|
|
|
|
object.new_topic_duration_minutes || SiteSetting.new_topic_duration_minutes
|
2013-11-07 16:34:18 -05:00
|
|
|
end
|
|
|
|
|
2014-01-02 01:58:49 -05:00
|
|
|
def muted_category_ids
|
|
|
|
CategoryUser.lookup(object, :muted).pluck(:category_id)
|
|
|
|
end
|
|
|
|
|
2014-01-05 19:57:17 -05:00
|
|
|
def tracked_category_ids
|
|
|
|
CategoryUser.lookup(object, :tracking).pluck(:category_id)
|
|
|
|
end
|
|
|
|
|
2014-01-02 01:58:49 -05:00
|
|
|
def watched_category_ids
|
|
|
|
CategoryUser.lookup(object, :watching).pluck(:category_id)
|
|
|
|
end
|
2014-03-28 04:49:30 -04:00
|
|
|
|
2014-05-02 16:36:52 -04:00
|
|
|
def private_messages_stats
|
|
|
|
UserAction.private_messages_stats(object.id, scope)
|
|
|
|
end
|
2014-06-30 16:46:47 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-07-09 01:31:49 -04:00
|
|
|
def has_title_badges
|
|
|
|
object.badges.where(allow_title: true).count > 0
|
|
|
|
end
|
|
|
|
|
2014-09-09 23:13:36 -04:00
|
|
|
def notification_count
|
|
|
|
Notification.where(user_id: object.id).count
|
2014-09-02 21:32:27 -04:00
|
|
|
end
|
|
|
|
|
2014-07-27 13:12:36 -04:00
|
|
|
def include_edit_history_public?
|
|
|
|
can_edit && !SiteSetting.edit_history_visible_to_public
|
|
|
|
end
|
2014-08-19 11:05:35 -04:00
|
|
|
|
|
|
|
def custom_fields
|
|
|
|
fields = nil
|
|
|
|
|
|
|
|
if SiteSetting.public_user_custom_fields.present?
|
|
|
|
fields = SiteSetting.public_user_custom_fields.split('|')
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.present?
|
|
|
|
User.custom_fields_for_ids([object.id], fields)[object.id]
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|