2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-28 19:55:46 +08:00
|
|
|
class UserSerializer < UserCardSerializer
|
2022-06-14 15:39:56 +08:00
|
|
|
include UserTagNotificationsMixin
|
2022-12-23 11:45:29 +08:00
|
|
|
include UserSidebarMixin
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2020-01-28 19:55:46 +08:00
|
|
|
attributes :bio_raw,
|
2013-02-07 23:45:24 +08:00
|
|
|
:bio_cooked,
|
|
|
|
: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,
|
2014-06-02 10:59:54 +08:00
|
|
|
:uploaded_avatar_id,
|
2014-07-28 01:12:36 +08:00
|
|
|
:has_title_badges,
|
2015-09-14 15:51:17 +08:00
|
|
|
:pending_count,
|
2016-09-16 04:15:08 +08:00
|
|
|
:profile_view_count,
|
2018-06-28 16:12:32 +08:00
|
|
|
:second_factor_enabled,
|
2018-07-04 16:45:42 +08:00
|
|
|
:second_factor_backup_enabled,
|
2018-07-23 23:51:57 +08:00
|
|
|
:second_factor_remaining_backup_codes,
|
2019-04-29 11:58:52 +08:00
|
|
|
:associated_accounts,
|
2020-08-18 00:37:45 +08:00
|
|
|
:profile_background_upload_url,
|
|
|
|
:can_upload_profile_header,
|
|
|
|
:can_upload_user_card_background
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-22 00:03:51 +08:00
|
|
|
has_one :invited_by, embed: :object, serializer: BasicUserSerializer
|
2015-12-17 15:06:04 +08:00
|
|
|
has_many :groups, embed: :object, serializer: BasicGroupSerializer
|
2016-11-25 15:26:49 +08:00
|
|
|
has_many :group_users, embed: :object, serializer: BasicGroupUserSerializer
|
2016-02-17 12:46:19 +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, :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,
|
2020-08-20 03:05:04 +08:00
|
|
|
:regular_category_ids,
|
2016-07-08 10:58:18 +08:00
|
|
|
:watched_tags,
|
2016-07-23 04:16:45 +08:00
|
|
|
:watching_first_post_tags,
|
2016-07-08 10:58:18 +08:00
|
|
|
:tracked_tags,
|
|
|
|
:muted_tags,
|
2014-01-06 08:57:17 +08:00
|
|
|
:tracked_category_ids,
|
2014-05-03 04:36:52 +08:00
|
|
|
:watched_category_ids,
|
2016-07-08 12:08:10 +08:00
|
|
|
:watched_first_post_category_ids,
|
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,
|
2016-03-29 15:50:17 +08:00
|
|
|
:muted_usernames,
|
2019-03-05 22:47:51 +08:00
|
|
|
:ignored_usernames,
|
2020-07-21 05:23:49 +08:00
|
|
|
:allowed_pm_usernames,
|
2016-08-01 13:29:28 +08:00
|
|
|
:mailing_list_posts_per_day,
|
2016-08-16 15:06:33 +08:00
|
|
|
:can_change_bio,
|
2020-04-28 14:06:35 +08:00
|
|
|
:can_change_location,
|
|
|
|
:can_change_website,
|
2021-07-17 02:50:40 +08:00
|
|
|
:can_change_tracking_preferences,
|
2018-08-31 16:18:06 +08:00
|
|
|
:user_api_keys,
|
2023-10-12 02:36:54 +08:00
|
|
|
:user_passkeys,
|
2021-01-21 00:31:52 +08:00
|
|
|
:user_auth_tokens,
|
2021-04-28 04:28:15 +08:00
|
|
|
:user_notification_schedule,
|
2022-08-12 11:26:56 +08:00
|
|
|
:use_logo_small_as_avatar,
|
2022-12-23 11:45:29 +08:00
|
|
|
:sidebar_tags,
|
|
|
|
:sidebar_category_ids,
|
|
|
|
:display_sidebar_tags
|
2013-05-21 04:52:37 +08:00
|
|
|
|
2023-01-09 21:59:43 +08:00
|
|
|
untrusted_attributes :bio_raw, :bio_cooked, :profile_background_upload_url
|
|
|
|
|
|
|
|
###
|
|
|
|
### ATTRIBUTES
|
|
|
|
###
|
|
|
|
#
|
|
|
|
def user_notification_schedule
|
|
|
|
object.user_notification_schedule || UserNotificationSchedule::DEFAULT
|
|
|
|
end
|
2014-07-01 04:46:47 +08:00
|
|
|
|
2016-03-29 15:50:17 +08: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 15:06:04 +08:00
|
|
|
def groups
|
2019-08-14 21:30:04 +08:00
|
|
|
object.groups.order(:id).visible_groups(scope.user).members_visible_groups(scope.user)
|
2015-12-17 15:06:04 +08:00
|
|
|
end
|
|
|
|
|
2016-11-25 15:26:49 +08:00
|
|
|
def group_users
|
|
|
|
object.group_users.order(:group_id)
|
|
|
|
end
|
|
|
|
|
2020-06-16 08:43:06 +08:00
|
|
|
def include_group_users?
|
2021-05-14 07:45:14 +08:00
|
|
|
user_is_current_user || scope.is_admin?
|
2020-06-16 08:43:06 +08:00
|
|
|
end
|
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
def include_associated_accounts?
|
2021-05-14 07:45:14 +08:00
|
|
|
user_is_current_user
|
2018-07-23 23:51:57 +08:00
|
|
|
end
|
|
|
|
|
2017-12-22 09:18:12 +08:00
|
|
|
def include_second_factor_enabled?
|
2021-05-14 07:45:14 +08:00
|
|
|
user_is_current_user || scope.is_admin?
|
2017-12-22 09:18:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_enabled
|
2022-12-08 06:41:22 +08:00
|
|
|
object.totp_enabled? || object.security_keys_enabled?
|
2017-12-22 09:18:12 +08:00
|
|
|
end
|
|
|
|
|
2018-06-28 16:12:32 +08:00
|
|
|
def include_second_factor_backup_enabled?
|
2021-05-14 07:45:14 +08:00
|
|
|
user_is_current_user
|
2018-06-28 16:12:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_backup_enabled
|
|
|
|
object.backup_codes_enabled?
|
|
|
|
end
|
|
|
|
|
2018-07-04 16:45:42 +08:00
|
|
|
def include_second_factor_remaining_backup_codes?
|
2021-05-14 07:45:14 +08:00
|
|
|
user_is_current_user && object.backup_codes_enabled?
|
2018-07-04 16:45:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_remaining_backup_codes
|
|
|
|
object.remaining_backup_codes
|
|
|
|
end
|
|
|
|
|
2016-08-01 13:29:28 +08:00
|
|
|
def can_change_bio
|
2021-02-08 18:04:33 +08:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_bio)
|
2016-08-01 13:29:28 +08:00
|
|
|
end
|
|
|
|
|
2020-04-28 14:06:35 +08:00
|
|
|
def can_change_location
|
2021-02-08 18:04:33 +08:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_location)
|
2020-04-28 14:06:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_change_website
|
2021-02-08 18:04:33 +08:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_website)
|
2020-04-28 14:06:35 +08:00
|
|
|
end
|
|
|
|
|
2021-07-17 02:50:40 +08:00
|
|
|
def can_change_tracking_preferences
|
|
|
|
scope.can_change_tracking_preferences?(object)
|
|
|
|
end
|
|
|
|
|
2016-08-16 15:06:33 +08: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,
|
2020-10-09 01:12:24 +08:00
|
|
|
scopes: k.scopes.map { |s| I18n.t("user_api_key.scopes.#{s.name}") },
|
2018-08-20 23:36:14 +08:00
|
|
|
created_at: k.created_at,
|
|
|
|
last_used_at: k.last_used_at,
|
2016-08-16 15:06:33 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-21 00:56:21 +08:00
|
|
|
keys.sort! { |a, b| a[:last_used_at].to_time <=> b[:last_used_at].to_time }
|
2016-08-16 15:06:33 +08:00
|
|
|
keys.length > 0 ? keys : nil
|
|
|
|
end
|
|
|
|
|
2018-08-31 16:18:06 +08:00
|
|
|
def user_auth_tokens
|
2018-09-03 10:44:22 +08:00
|
|
|
ActiveModel::ArraySerializer.new(
|
2018-10-09 22:21:41 +08:00
|
|
|
object.user_auth_tokens,
|
|
|
|
each_serializer: UserAuthTokenSerializer,
|
|
|
|
scope: scope,
|
2018-09-03 10:44:22 +08:00
|
|
|
)
|
2018-08-31 16:18:06 +08:00
|
|
|
end
|
|
|
|
|
2023-10-12 02:36:54 +08:00
|
|
|
def user_passkeys
|
|
|
|
UserSecurityKey
|
|
|
|
.where(user_id: object.id, factor_type: UserSecurityKey.factor_types[:first_factor])
|
|
|
|
.map do |usk|
|
|
|
|
{ id: usk.id, name: usk.name, last_used: usk.last_used, created_at: usk.created_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_passkeys?
|
|
|
|
SiteSetting.experimental_passkeys?
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def bio_raw
|
2019-06-04 09:28:52 +08:00
|
|
|
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
|
2019-06-04 09:28:52 +08:00
|
|
|
object.user_profile.bio_processed
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
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
|
|
|
|
|
2020-08-18 00:37:45 +08:00
|
|
|
def can_upload_profile_header
|
|
|
|
scope.can_upload_profile_header?(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_upload_user_card_background
|
|
|
|
scope.can_upload_user_card_background?(object)
|
|
|
|
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
|
2021-05-06 07:14:07 +08:00
|
|
|
categories_with_notification_level(:muted)
|
2014-01-02 14:58:49 +08:00
|
|
|
end
|
|
|
|
|
2020-08-20 03:05:04 +08:00
|
|
|
def regular_category_ids
|
2021-05-06 07:14:07 +08:00
|
|
|
categories_with_notification_level(:regular)
|
2020-08-20 03:05:04 +08:00
|
|
|
end
|
|
|
|
|
2014-01-06 08:57:17 +08:00
|
|
|
def tracked_category_ids
|
2021-05-06 07:14:07 +08:00
|
|
|
categories_with_notification_level(:tracking)
|
2014-01-06 08:57:17 +08:00
|
|
|
end
|
|
|
|
|
2014-01-02 14:58:49 +08:00
|
|
|
def watched_category_ids
|
2021-05-06 07:14:07 +08:00
|
|
|
categories_with_notification_level(:watching)
|
2014-01-02 14:58:49 +08:00
|
|
|
end
|
2014-03-28 16:49:30 +08:00
|
|
|
|
2016-07-08 12:08:10 +08:00
|
|
|
def watched_first_post_category_ids
|
2021-05-06 07:14:07 +08:00
|
|
|
categories_with_notification_level(:watching_first_post)
|
2016-07-08 12:08:10 +08:00
|
|
|
end
|
|
|
|
|
2015-03-24 08:55:22 +08:00
|
|
|
def muted_usernames
|
|
|
|
MutedUser.where(user_id: object.id).joins(:muted_user).pluck(:username)
|
2019-03-05 22:47:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def ignored_usernames
|
|
|
|
IgnoredUser.where(user_id: object.id).joins(:ignored_user).pluck(:username)
|
2015-03-24 08:55:22 +08:00
|
|
|
end
|
|
|
|
|
2020-07-21 05:23:49 +08:00
|
|
|
def allowed_pm_usernames
|
|
|
|
AllowedPmUser.where(user_id: object.id).joins(:allowed_pm_user).pluck(:username)
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2018-05-21 11:25:01 +08:00
|
|
|
def include_gravatar_avatar_upload_id?
|
|
|
|
object.user_avatar&.gravatar_upload_id
|
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def gravatar_avatar_upload_id
|
2018-05-21 11:25:01 +08:00
|
|
|
object.user_avatar.gravatar_upload_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_gravatar_avatar_template?
|
|
|
|
include_gravatar_avatar_upload_id?
|
2014-07-01 04:46:47 +08:00
|
|
|
end
|
|
|
|
|
2015-09-11 18:56:34 +08:00
|
|
|
def gravatar_avatar_template
|
2018-05-21 11:25:01 +08: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 18:56:34 +08:00
|
|
|
end
|
|
|
|
|
2014-07-01 04:46:47 +08:00
|
|
|
def custom_avatar_upload_id
|
2018-05-21 11:25:01 +08:00
|
|
|
object.user_avatar.custom_upload_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_custom_avatar_template?
|
|
|
|
include_custom_avatar_upload_id?
|
2014-07-01 04:46:47 +08:00
|
|
|
end
|
|
|
|
|
2015-09-11 18:56:34 +08:00
|
|
|
def custom_avatar_template
|
2018-05-21 11:25:01 +08:00
|
|
|
User.avatar_template(object.username, object.user_avatar.custom_upload_id)
|
2015-09-11 18:56:34 +08:00
|
|
|
end
|
|
|
|
|
2014-07-09 13:31:49 +08:00
|
|
|
def has_title_badges
|
2017-08-24 06:54:51 +08:00
|
|
|
object.badges.where(allow_title: true).exists?
|
2014-07-09 13:31:49 +08:00
|
|
|
end
|
|
|
|
|
2015-04-22 02:36:46 +08:00
|
|
|
def pending_count
|
|
|
|
0
|
|
|
|
end
|
2015-09-11 08:12:40 +08:00
|
|
|
|
2015-09-14 15:51:17 +08:00
|
|
|
def profile_view_count
|
2019-06-04 09:28:52 +08:00
|
|
|
object.user_profile.views
|
2015-09-14 15:51:17 +08:00
|
|
|
end
|
|
|
|
|
2019-04-29 11:58:52 +08:00
|
|
|
def profile_background_upload_url
|
|
|
|
object.profile_background_upload&.url
|
|
|
|
end
|
|
|
|
|
2021-04-28 04:28:15 +08:00
|
|
|
def use_logo_small_as_avatar
|
|
|
|
object.is_system_user? && SiteSetting.logo_small &&
|
|
|
|
SiteSetting.use_site_small_logo_as_system_avatar
|
|
|
|
end
|
|
|
|
|
2020-03-03 21:56:54 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def custom_field_keys
|
|
|
|
fields = super
|
|
|
|
|
|
|
|
fields += DiscoursePluginRegistry.serialized_current_user_fields.to_a if scope.can_edit?(object)
|
|
|
|
|
|
|
|
fields
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|