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