2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
class UserSerializer < UserCardSerializer
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
attributes :bio_raw,
|
2013-02-07 10:45:24 -05:00
|
|
|
:bio_cooked,
|
|
|
|
: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,
|
2014-06-01 22:59:54 -04:00
|
|
|
:uploaded_avatar_id,
|
2014-07-27 13:12:36 -04:00
|
|
|
:has_title_badges,
|
2015-09-14 03:51:17 -04:00
|
|
|
:pending_count,
|
2016-09-15 16:15:08 -04:00
|
|
|
:profile_view_count,
|
2018-06-28 04:12:32 -04:00
|
|
|
:second_factor_enabled,
|
2018-07-04 04:45:42 -04:00
|
|
|
:second_factor_backup_enabled,
|
2018-07-23 11:51:57 -04:00
|
|
|
:second_factor_remaining_backup_codes,
|
2019-04-28 23:58:52 -04:00
|
|
|
:associated_accounts,
|
2020-01-28 06:55:46 -05:00
|
|
|
:profile_background_upload_url
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-21 12:03:51 -04:00
|
|
|
has_one :invited_by, embed: :object, serializer: BasicUserSerializer
|
2015-12-17 02:06:04 -05:00
|
|
|
has_many :groups, embed: :object, serializer: BasicGroupSerializer
|
2016-11-25 02:26:49 -05:00
|
|
|
has_many :group_users, embed: :object, serializer: BasicGroupUserSerializer
|
2016-02-16 23:46:19 -05:00
|
|
|
has_one :user_option, embed: :object, serializer: UserOptionSerializer
|
|
|
|
|
|
|
|
def include_user_option?
|
|
|
|
can_edit
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-12-02 12:52:56 -05:00
|
|
|
staff_attributes :post_count,
|
2014-11-14 15:23:09 -05:00
|
|
|
:can_be_deleted,
|
|
|
|
:can_delete_all_posts
|
2013-06-04 12:05:36 -04:00
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
private_attributes :locale,
|
2014-01-02 01:58:49 -05:00
|
|
|
:muted_category_ids,
|
2016-07-07 22:58:18 -04:00
|
|
|
:watched_tags,
|
2016-07-22 16:16:45 -04:00
|
|
|
:watching_first_post_tags,
|
2016-07-07 22:58:18 -04:00
|
|
|
:tracked_tags,
|
|
|
|
:muted_tags,
|
2014-01-05 19:57:17 -05:00
|
|
|
:tracked_category_ids,
|
2014-05-02 16:36:52 -04:00
|
|
|
:watched_category_ids,
|
2016-07-08 00:08:10 -04:00
|
|
|
:watched_first_post_category_ids,
|
2015-09-11 06:56:34 -04:00
|
|
|
:system_avatar_upload_id,
|
|
|
|
:system_avatar_template,
|
2014-05-22 03:37:02 -04:00
|
|
|
:gravatar_avatar_upload_id,
|
2015-09-11 06:56:34 -04:00
|
|
|
:gravatar_avatar_template,
|
2014-06-11 01:50:37 -04:00
|
|
|
:custom_avatar_upload_id,
|
2015-09-11 06:56:34 -04:00
|
|
|
:custom_avatar_template,
|
2014-10-20 13:15:58 -04:00
|
|
|
:has_title_badges,
|
2016-03-29 03:50:17 -04:00
|
|
|
:muted_usernames,
|
2019-03-05 09:47:51 -05:00
|
|
|
:ignored_usernames,
|
2020-07-20 17:23:49 -04:00
|
|
|
:allowed_pm_usernames,
|
2016-08-01 01:29:28 -04:00
|
|
|
:mailing_list_posts_per_day,
|
2016-08-16 03:06:33 -04:00
|
|
|
:can_change_bio,
|
2020-04-28 02:06:35 -04:00
|
|
|
:can_change_location,
|
|
|
|
:can_change_website,
|
2018-08-31 04:18:06 -04:00
|
|
|
:user_api_keys,
|
2019-08-27 08:07:03 -04:00
|
|
|
:user_auth_tokens
|
2013-05-20 16:52:37 -04:00
|
|
|
|
2014-11-26 13:20:03 -05:00
|
|
|
untrusted_attributes :bio_raw,
|
|
|
|
:bio_cooked,
|
2019-04-28 23:58:52 -04:00
|
|
|
:profile_background_upload_url,
|
2014-11-26 13:20:03 -05:00
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
###
|
|
|
|
### ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
2016-03-29 03:50:17 -04:00
|
|
|
def mailing_list_posts_per_day
|
|
|
|
val = Post.estimate_posts_per_day
|
|
|
|
[val, SiteSetting.max_emails_per_day_per_user].min
|
|
|
|
end
|
|
|
|
|
2015-12-17 02:06:04 -05:00
|
|
|
def groups
|
2017-07-03 15:26:46 -04:00
|
|
|
object.groups.order(:id)
|
2019-08-14 09:30:04 -04:00
|
|
|
.visible_groups(scope.user).members_visible_groups(scope.user)
|
2015-12-17 02:06:04 -05:00
|
|
|
end
|
|
|
|
|
2016-11-25 02:26:49 -05:00
|
|
|
def group_users
|
|
|
|
object.group_users.order(:group_id)
|
|
|
|
end
|
|
|
|
|
2020-06-15 20:43:06 -04:00
|
|
|
def include_group_users?
|
|
|
|
(object.id && object.id == scope.user.try(:id)) || scope.is_staff?
|
|
|
|
end
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
def include_associated_accounts?
|
|
|
|
(object.id && object.id == scope.user.try(:id))
|
|
|
|
end
|
|
|
|
|
2017-12-21 20:18:12 -05:00
|
|
|
def include_second_factor_enabled?
|
2018-02-20 01:44:51 -05:00
|
|
|
(object&.id == scope.user&.id) || scope.is_staff?
|
2017-12-21 20:18:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_enabled
|
2019-11-08 00:11:53 -05:00
|
|
|
object.totp_enabled? || object.security_keys_enabled?
|
2017-12-21 20:18:12 -05:00
|
|
|
end
|
|
|
|
|
2018-06-28 04:12:32 -04:00
|
|
|
def include_second_factor_backup_enabled?
|
|
|
|
object&.id == scope.user&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_backup_enabled
|
|
|
|
object.backup_codes_enabled?
|
|
|
|
end
|
|
|
|
|
2018-07-04 04:45:42 -04:00
|
|
|
def include_second_factor_remaining_backup_codes?
|
|
|
|
(object&.id == scope.user&.id) && object.backup_codes_enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_remaining_backup_codes
|
|
|
|
object.remaining_backup_codes
|
|
|
|
end
|
|
|
|
|
2016-08-01 01:29:28 -04:00
|
|
|
def can_change_bio
|
|
|
|
!(SiteSetting.enable_sso && SiteSetting.sso_overrides_bio)
|
|
|
|
end
|
|
|
|
|
2020-04-28 02:06:35 -04:00
|
|
|
def can_change_location
|
|
|
|
!(SiteSetting.enable_sso && SiteSetting.sso_overrides_location)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_change_website
|
|
|
|
!(SiteSetting.enable_sso && SiteSetting.sso_overrides_website)
|
|
|
|
end
|
|
|
|
|
2016-08-16 03:06:33 -04:00
|
|
|
def user_api_keys
|
|
|
|
keys = object.user_api_keys.where(revoked_at: nil).map do |k|
|
|
|
|
{
|
|
|
|
id: k.id,
|
|
|
|
application_name: k.application_name,
|
2016-10-14 01:05:27 -04:00
|
|
|
scopes: k.scopes.map { |s| I18n.t("user_api_key.scopes.#{s}") },
|
2018-08-20 11:36:14 -04:00
|
|
|
created_at: k.created_at,
|
|
|
|
last_used_at: k.last_used_at,
|
2016-08-16 03:06:33 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-20 12:56:21 -04:00
|
|
|
keys.sort! { |a, b| a[:last_used_at].to_time <=> b[:last_used_at].to_time }
|
2016-08-16 03:06:33 -04:00
|
|
|
keys.length > 0 ? keys : nil
|
|
|
|
end
|
|
|
|
|
2018-08-31 04:18:06 -04:00
|
|
|
def user_auth_tokens
|
2018-09-02 22:44:22 -04:00
|
|
|
ActiveModel::ArraySerializer.new(
|
2018-10-09 10:21:41 -04:00
|
|
|
object.user_auth_tokens,
|
|
|
|
each_serializer: UserAuthTokenSerializer,
|
|
|
|
scope: scope
|
2018-09-02 22:44:22 -04:00
|
|
|
)
|
2018-08-31 04:18:06 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def bio_raw
|
2019-06-03 21:28:52 -04:00
|
|
|
object.user_profile.bio_raw
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def bio_cooked
|
2019-06-03 21:28:52 -04:00
|
|
|
object.user_profile.bio_processed
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
|
|
###
|
|
|
|
### STAFF ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
2014-12-02 12:52:56 -05:00
|
|
|
def post_count
|
|
|
|
object.user_stat.try(:post_count)
|
|
|
|
end
|
|
|
|
|
2014-11-14 15:23:09 -05:00
|
|
|
def can_be_deleted
|
|
|
|
scope.can_delete_user?(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_all_posts
|
|
|
|
scope.can_delete_all_posts?(object)
|
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
###
|
|
|
|
### PRIVATE ATTRIBUTES
|
|
|
|
###
|
2016-07-07 22:58:18 -04:00
|
|
|
def muted_tags
|
|
|
|
TagUser.lookup(object, :muted).joins(:tag).pluck('tags.name')
|
|
|
|
end
|
|
|
|
|
|
|
|
def tracked_tags
|
|
|
|
TagUser.lookup(object, :tracking).joins(:tag).pluck('tags.name')
|
|
|
|
end
|
|
|
|
|
2016-07-22 16:16:45 -04:00
|
|
|
def watching_first_post_tags
|
|
|
|
TagUser.lookup(object, :watching_first_post).joins(:tag).pluck('tags.name')
|
|
|
|
end
|
|
|
|
|
2016-07-07 22:58:18 -04:00
|
|
|
def watched_tags
|
|
|
|
TagUser.lookup(object, :watching).joins(:tag).pluck('tags.name')
|
|
|
|
end
|
2014-06-30 16:46:47 -04:00
|
|
|
|
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
|
|
|
|
2016-07-08 00:08:10 -04:00
|
|
|
def watched_first_post_category_ids
|
|
|
|
CategoryUser.lookup(object, :watching_first_post).pluck(:category_id)
|
|
|
|
end
|
|
|
|
|
2015-03-23 20:55:22 -04:00
|
|
|
def muted_usernames
|
|
|
|
MutedUser.where(user_id: object.id).joins(:muted_user).pluck(:username)
|
2019-03-05 09:47:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ignored_usernames
|
|
|
|
IgnoredUser.where(user_id: object.id).joins(:ignored_user).pluck(:username)
|
2015-03-23 20:55:22 -04:00
|
|
|
end
|
|
|
|
|
2020-07-20 17:23:49 -04:00
|
|
|
def allowed_pm_usernames
|
|
|
|
AllowedPmUser.where(user_id: object.id).joins(:allowed_pm_user).pluck(:username)
|
|
|
|
end
|
|
|
|
|
2015-09-11 06:56:34 -04:00
|
|
|
def system_avatar_upload_id
|
|
|
|
# should be left blank
|
|
|
|
end
|
|
|
|
|
|
|
|
def system_avatar_template
|
|
|
|
User.system_avatar_template(object.username)
|
|
|
|
end
|
|
|
|
|
2018-05-20 23:25:01 -04:00
|
|
|
def include_gravatar_avatar_upload_id?
|
|
|
|
object.user_avatar&.gravatar_upload_id
|
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def gravatar_avatar_upload_id
|
2018-05-20 23:25:01 -04:00
|
|
|
object.user_avatar.gravatar_upload_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_gravatar_avatar_template?
|
|
|
|
include_gravatar_avatar_upload_id?
|
2014-06-30 16:46:47 -04:00
|
|
|
end
|
|
|
|
|
2015-09-11 06:56:34 -04:00
|
|
|
def gravatar_avatar_template
|
2018-05-20 23:25:01 -04:00
|
|
|
User.avatar_template(object.username, object.user_avatar.gravatar_upload_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_custom_avatar_upload_id?
|
|
|
|
object.user_avatar&.custom_upload_id
|
2015-09-11 06:56:34 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:46:47 -04:00
|
|
|
def custom_avatar_upload_id
|
2018-05-20 23:25:01 -04:00
|
|
|
object.user_avatar.custom_upload_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_custom_avatar_template?
|
|
|
|
include_custom_avatar_upload_id?
|
2014-06-30 16:46:47 -04:00
|
|
|
end
|
|
|
|
|
2015-09-11 06:56:34 -04:00
|
|
|
def custom_avatar_template
|
2018-05-20 23:25:01 -04:00
|
|
|
User.avatar_template(object.username, object.user_avatar.custom_upload_id)
|
2015-09-11 06:56:34 -04:00
|
|
|
end
|
|
|
|
|
2014-07-09 01:31:49 -04:00
|
|
|
def has_title_badges
|
2017-08-23 18:54:51 -04:00
|
|
|
object.badges.where(allow_title: true).exists?
|
2014-07-09 01:31:49 -04:00
|
|
|
end
|
|
|
|
|
2015-04-21 14:36:46 -04:00
|
|
|
def pending_count
|
|
|
|
0
|
|
|
|
end
|
2015-09-10 20:12:40 -04:00
|
|
|
|
2015-09-14 03:51:17 -04:00
|
|
|
def profile_view_count
|
2019-06-03 21:28:52 -04:00
|
|
|
object.user_profile.views
|
2015-09-14 03:51:17 -04:00
|
|
|
end
|
|
|
|
|
2019-04-28 23:58:52 -04:00
|
|
|
def profile_background_upload_url
|
|
|
|
object.profile_background_upload&.url
|
|
|
|
end
|
|
|
|
|
2020-03-03 08:56:54 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def custom_field_keys
|
|
|
|
fields = super
|
|
|
|
|
|
|
|
if scope.can_edit?(object)
|
|
|
|
fields += DiscoursePluginRegistry.serialized_current_user_fields.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|