2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-28 06:55:46 -05:00
|
|
|
class UserSerializer < UserCardSerializer
|
2022-06-14 03:39:56 -04:00
|
|
|
include UserTagNotificationsMixin
|
2022-08-11 23:26:56 -04:00
|
|
|
include UserSidebarTagsMixin
|
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-08-17 12:37:45 -04:00
|
|
|
:profile_background_upload_url,
|
|
|
|
:can_upload_profile_header,
|
|
|
|
:can_upload_user_card_background
|
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,
|
2020-08-19 15:05:04 -04:00
|
|
|
:regular_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,
|
2021-07-16 14:50:40 -04:00
|
|
|
:can_change_tracking_preferences,
|
2018-08-31 04:18:06 -04:00
|
|
|
:user_api_keys,
|
2021-01-20 11:31:52 -05:00
|
|
|
:user_auth_tokens,
|
2021-04-27 16:28:15 -04:00
|
|
|
:user_notification_schedule,
|
2022-08-11 23:26:56 -04:00
|
|
|
:use_logo_small_as_avatar,
|
|
|
|
:sidebar_tags
|
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
|
|
|
|
###
|
2021-01-20 11:31:52 -05:00
|
|
|
#
|
|
|
|
def user_notification_schedule
|
|
|
|
object.user_notification_schedule || UserNotificationSchedule::DEFAULT
|
|
|
|
end
|
2014-06-30 16:46:47 -04:00
|
|
|
|
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?
|
2021-05-13 19:45:14 -04:00
|
|
|
user_is_current_user || scope.is_admin?
|
2020-06-15 20:43:06 -04:00
|
|
|
end
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
def include_associated_accounts?
|
2021-05-13 19:45:14 -04:00
|
|
|
user_is_current_user
|
2018-07-23 11:51:57 -04:00
|
|
|
end
|
|
|
|
|
2017-12-21 20:18:12 -05:00
|
|
|
def include_second_factor_enabled?
|
2021-05-13 19:45:14 -04:00
|
|
|
user_is_current_user || scope.is_admin?
|
2017-12-21 20:18:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_enabled
|
2022-11-10 21:00:06 -05:00
|
|
|
object.totp_enabled? || object.security_keys_enabled? || object.backup_codes_enabled?
|
2017-12-21 20:18:12 -05:00
|
|
|
end
|
|
|
|
|
2018-06-28 04:12:32 -04:00
|
|
|
def include_second_factor_backup_enabled?
|
2021-05-13 19:45:14 -04:00
|
|
|
user_is_current_user
|
2018-06-28 04:12:32 -04:00
|
|
|
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?
|
2021-05-13 19:45:14 -04:00
|
|
|
user_is_current_user && object.backup_codes_enabled?
|
2018-07-04 04:45:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_remaining_backup_codes
|
|
|
|
object.remaining_backup_codes
|
|
|
|
end
|
|
|
|
|
2016-08-01 01:29:28 -04:00
|
|
|
def can_change_bio
|
2021-02-08 05:04:33 -05:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_bio)
|
2016-08-01 01:29:28 -04:00
|
|
|
end
|
|
|
|
|
2020-04-28 02:06:35 -04:00
|
|
|
def can_change_location
|
2021-02-08 05:04:33 -05:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_location)
|
2020-04-28 02:06:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_change_website
|
2021-02-08 05:04:33 -05:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_website)
|
2020-04-28 02:06:35 -04:00
|
|
|
end
|
|
|
|
|
2021-07-16 14:50:40 -04:00
|
|
|
def can_change_tracking_preferences
|
|
|
|
scope.can_change_tracking_preferences?(object)
|
|
|
|
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,
|
2020-10-08 13:12:24 -04:00
|
|
|
scopes: k.scopes.map { |s| I18n.t("user_api_key.scopes.#{s.name}") },
|
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
|
|
|
|
|
2020-08-17 12:37:45 -04: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 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
|
|
|
|
###
|
2014-01-02 01:58:49 -05:00
|
|
|
def muted_category_ids
|
2021-05-05 19:14:07 -04:00
|
|
|
categories_with_notification_level(:muted)
|
2014-01-02 01:58:49 -05:00
|
|
|
end
|
|
|
|
|
2020-08-19 15:05:04 -04:00
|
|
|
def regular_category_ids
|
2021-05-05 19:14:07 -04:00
|
|
|
categories_with_notification_level(:regular)
|
2020-08-19 15:05:04 -04:00
|
|
|
end
|
|
|
|
|
2014-01-05 19:57:17 -05:00
|
|
|
def tracked_category_ids
|
2021-05-05 19:14:07 -04:00
|
|
|
categories_with_notification_level(:tracking)
|
2014-01-05 19:57:17 -05:00
|
|
|
end
|
|
|
|
|
2014-01-02 01:58:49 -05:00
|
|
|
def watched_category_ids
|
2021-05-05 19:14:07 -04:00
|
|
|
categories_with_notification_level(:watching)
|
2014-01-02 01:58:49 -05:00
|
|
|
end
|
2014-03-28 04:49:30 -04:00
|
|
|
|
2016-07-08 00:08:10 -04:00
|
|
|
def watched_first_post_category_ids
|
2021-05-05 19:14:07 -04:00
|
|
|
categories_with_notification_level(:watching_first_post)
|
2016-07-08 00:08:10 -04:00
|
|
|
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
|
|
|
|
|
2021-04-27 16:28:15 -04: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 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
|