2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-28 11:55:46 +00: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 16:45:24 +01:00
|
|
|
|
2020-01-28 11:55:46 +00:00
|
|
|
attributes :bio_raw,
|
2013-02-07 16:45:24 +01:00
|
|
|
:bio_cooked,
|
|
|
|
:can_edit,
|
2013-08-12 14:54:52 -04:00
|
|
|
:can_edit_username,
|
2013-09-08 11:42:41 +09:00
|
|
|
:can_edit_email,
|
2014-03-13 13:26:40 -07:00
|
|
|
:can_edit_name,
|
2014-06-02 12:59:54 +10:00
|
|
|
:uploaded_avatar_id,
|
2014-07-27 12:12:36 -05:00
|
|
|
:has_title_badges,
|
2015-09-14 15:51:17 +08:00
|
|
|
:pending_count,
|
2016-09-15 16:15:08 -04:00
|
|
|
:profile_view_count,
|
2018-06-28 10:12:32 +02:00
|
|
|
:second_factor_enabled,
|
2018-07-04 10:45:42 +02:00
|
|
|
:second_factor_backup_enabled,
|
2018-07-23 16:51:57 +01:00
|
|
|
:second_factor_remaining_backup_codes,
|
2019-04-29 11:58:52 +08:00
|
|
|
:associated_accounts,
|
2020-08-17 22:07:45 +05:30
|
|
|
: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 18:06:04 +11: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 15:46:19 +11: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 18:52:56 +01:00
|
|
|
staff_attributes :post_count, :can_be_deleted, :can_delete_all_posts
|
2013-06-04 12:05:36 -04:00
|
|
|
|
2014-09-29 22:31:05 +02:00
|
|
|
private_attributes :locale,
|
2014-01-02 17:58:49 +11:00
|
|
|
:muted_category_ids,
|
2020-08-20 00:35:04 +05:30
|
|
|
:regular_category_ids,
|
2016-07-08 12:58:18 +10:00
|
|
|
:watched_tags,
|
2016-07-22 16:16:45 -04:00
|
|
|
:watching_first_post_tags,
|
2016-07-08 12:58:18 +10:00
|
|
|
:tracked_tags,
|
|
|
|
:muted_tags,
|
2014-01-06 11:57:17 +11:00
|
|
|
:tracked_category_ids,
|
2014-05-02 22:36:52 +02:00
|
|
|
:watched_category_ids,
|
2016-07-08 14:08:10 +10:00
|
|
|
:watched_first_post_category_ids,
|
2015-09-11 12:56:34 +02:00
|
|
|
:system_avatar_upload_id,
|
|
|
|
:system_avatar_template,
|
2014-05-22 17:37:02 +10:00
|
|
|
:gravatar_avatar_upload_id,
|
2015-09-11 12:56:34 +02:00
|
|
|
:gravatar_avatar_template,
|
2014-06-11 15:50:37 +10:00
|
|
|
:custom_avatar_upload_id,
|
2015-09-11 12:56:34 +02:00
|
|
|
:custom_avatar_template,
|
2014-10-20 13:15:58 -04:00
|
|
|
:has_title_badges,
|
2016-03-29 18:50:17 +11:00
|
|
|
:muted_usernames,
|
2019-03-05 14:47:51 +00:00
|
|
|
:ignored_usernames,
|
2020-07-20 15:23:49 -06:00
|
|
|
:allowed_pm_usernames,
|
2016-08-01 15:29:28 +10:00
|
|
|
:mailing_list_posts_per_day,
|
2016-08-16 17:06:33 +10:00
|
|
|
:can_change_bio,
|
2020-04-28 07:06:35 +01:00
|
|
|
:can_change_location,
|
|
|
|
:can_change_website,
|
2021-07-16 14:50:40 -04:00
|
|
|
:can_change_tracking_preferences,
|
2018-08-31 10:18:06 +02:00
|
|
|
:user_api_keys,
|
2021-01-20 10:31:52 -06:00
|
|
|
:user_auth_tokens,
|
2021-04-27 17:28:15 -03: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-20 16:52:37 -04:00
|
|
|
|
2023-01-09 13:59:43 +00: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 22:46:47 +02:00
|
|
|
|
2016-03-29 18:50:17 +11: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 18:06:04 +11:00
|
|
|
def groups
|
2019-08-14 19:00:04 +05:30
|
|
|
object.groups.order(:id).visible_groups(scope.user).members_visible_groups(scope.user)
|
2015-12-17 18:06:04 +11:00
|
|
|
end
|
|
|
|
|
2016-11-25 15:26:49 +08:00
|
|
|
def group_users
|
|
|
|
object.group_users.order(:group_id)
|
|
|
|
end
|
|
|
|
|
2020-06-16 10:43:06 +10:00
|
|
|
def include_group_users?
|
2021-05-14 09:45:14 +10:00
|
|
|
user_is_current_user || scope.is_admin?
|
2020-06-16 10:43:06 +10:00
|
|
|
end
|
|
|
|
|
2018-07-23 16:51:57 +01:00
|
|
|
def include_associated_accounts?
|
2021-05-14 09:45:14 +10:00
|
|
|
user_is_current_user
|
2018-07-23 16:51:57 +01:00
|
|
|
end
|
|
|
|
|
2017-12-21 17:18:12 -08:00
|
|
|
def include_second_factor_enabled?
|
2021-05-14 09:45:14 +10:00
|
|
|
user_is_current_user || scope.is_admin?
|
2017-12-21 17:18:12 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_enabled
|
2022-12-08 09:41:22 +11:00
|
|
|
object.totp_enabled? || object.security_keys_enabled?
|
2017-12-21 17:18:12 -08:00
|
|
|
end
|
|
|
|
|
2018-06-28 10:12:32 +02:00
|
|
|
def include_second_factor_backup_enabled?
|
2021-05-14 09:45:14 +10:00
|
|
|
user_is_current_user
|
2018-06-28 10:12:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_backup_enabled
|
|
|
|
object.backup_codes_enabled?
|
|
|
|
end
|
|
|
|
|
2018-07-04 10:45:42 +02:00
|
|
|
def include_second_factor_remaining_backup_codes?
|
2021-05-14 09:45:14 +10:00
|
|
|
user_is_current_user && object.backup_codes_enabled?
|
2018-07-04 10:45:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def second_factor_remaining_backup_codes
|
|
|
|
object.remaining_backup_codes
|
|
|
|
end
|
|
|
|
|
2016-08-01 15:29:28 +10:00
|
|
|
def can_change_bio
|
2021-02-08 10:04:33 +00:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_bio)
|
2016-08-01 15:29:28 +10:00
|
|
|
end
|
|
|
|
|
2020-04-28 07:06:35 +01:00
|
|
|
def can_change_location
|
2021-02-08 10:04:33 +00:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_location)
|
2020-04-28 07:06:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_change_website
|
2021-02-08 10:04:33 +00:00
|
|
|
!(SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_website)
|
2020-04-28 07:06:35 +01: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 17:06:33 +10: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 18:12:24 +01:00
|
|
|
scopes: k.scopes.map { |s| I18n.t("user_api_key.scopes.#{s.name}") },
|
2018-08-20 17:36:14 +02:00
|
|
|
created_at: k.created_at,
|
|
|
|
last_used_at: k.last_used_at,
|
2016-08-16 17:06:33 +10:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-20 18:56:21 +02:00
|
|
|
keys.sort! { |a, b| a[:last_used_at].to_time <=> b[:last_used_at].to_time }
|
2016-08-16 17:06:33 +10:00
|
|
|
keys.length > 0 ? keys : nil
|
|
|
|
end
|
|
|
|
|
2018-08-31 10:18:06 +02:00
|
|
|
def user_auth_tokens
|
2018-09-03 10:44:22 +08:00
|
|
|
ActiveModel::ArraySerializer.new(
|
2018-10-09 17:21:41 +03:00
|
|
|
object.user_auth_tokens,
|
|
|
|
each_serializer: UserAuthTokenSerializer,
|
|
|
|
scope: scope,
|
2018-09-03 10:44:22 +08:00
|
|
|
)
|
2018-08-31 10:18:06 +02:00
|
|
|
end
|
|
|
|
|
2014-06-30 22:46:47 +02:00
|
|
|
def bio_raw
|
2019-06-04 09:28:52 +08:00
|
|
|
object.user_profile.bio_raw
|
2014-05-22 17:37:02 +10:00
|
|
|
end
|
|
|
|
|
2014-06-30 22:46:47 +02:00
|
|
|
def bio_cooked
|
2019-06-04 09:28:52 +08:00
|
|
|
object.user_profile.bio_processed
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 19:42:20 +03: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-08 11:42:41 +09:00
|
|
|
def can_edit_email
|
|
|
|
scope.can_edit_email?(object)
|
|
|
|
end
|
|
|
|
|
2014-03-13 13:26:40 -07:00
|
|
|
def can_edit_name
|
|
|
|
scope.can_edit_name?(object)
|
|
|
|
end
|
|
|
|
|
2020-08-17 22:07:45 +05:30
|
|
|
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 22:46:47 +02:00
|
|
|
###
|
|
|
|
### STAFF ATTRIBUTES
|
|
|
|
###
|
|
|
|
|
2014-12-02 18:52:56 +01: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 22:46:47 +02:00
|
|
|
###
|
|
|
|
### PRIVATE ATTRIBUTES
|
|
|
|
###
|
2014-01-02 17:58:49 +11:00
|
|
|
def muted_category_ids
|
2021-05-06 09:14:07 +10:00
|
|
|
categories_with_notification_level(:muted)
|
2014-01-02 17:58:49 +11:00
|
|
|
end
|
|
|
|
|
2020-08-20 00:35:04 +05:30
|
|
|
def regular_category_ids
|
2021-05-06 09:14:07 +10:00
|
|
|
categories_with_notification_level(:regular)
|
2020-08-20 00:35:04 +05:30
|
|
|
end
|
|
|
|
|
2014-01-06 11:57:17 +11:00
|
|
|
def tracked_category_ids
|
2021-05-06 09:14:07 +10:00
|
|
|
categories_with_notification_level(:tracking)
|
2014-01-06 11:57:17 +11:00
|
|
|
end
|
|
|
|
|
2014-01-02 17:58:49 +11:00
|
|
|
def watched_category_ids
|
2021-05-06 09:14:07 +10:00
|
|
|
categories_with_notification_level(:watching)
|
2014-01-02 17:58:49 +11:00
|
|
|
end
|
2014-03-28 14:19:30 +05:30
|
|
|
|
2016-07-08 14:08:10 +10:00
|
|
|
def watched_first_post_category_ids
|
2021-05-06 09:14:07 +10:00
|
|
|
categories_with_notification_level(:watching_first_post)
|
2016-07-08 14:08:10 +10:00
|
|
|
end
|
|
|
|
|
2015-03-24 11:55:22 +11:00
|
|
|
def muted_usernames
|
|
|
|
MutedUser.where(user_id: object.id).joins(:muted_user).pluck(:username)
|
2019-03-05 14:47:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ignored_usernames
|
|
|
|
IgnoredUser.where(user_id: object.id).joins(:ignored_user).pluck(:username)
|
2015-03-24 11:55:22 +11:00
|
|
|
end
|
|
|
|
|
2020-07-20 15:23:49 -06:00
|
|
|
def allowed_pm_usernames
|
|
|
|
AllowedPmUser.where(user_id: object.id).joins(:allowed_pm_user).pluck(:username)
|
|
|
|
end
|
|
|
|
|
2015-09-11 12:56:34 +02: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-06-30 22:46:47 +02: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-06-30 22:46:47 +02:00
|
|
|
end
|
|
|
|
|
2015-09-11 12:56:34 +02: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 12:56:34 +02:00
|
|
|
end
|
|
|
|
|
2014-06-30 22:46:47 +02: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-06-30 22:46:47 +02:00
|
|
|
end
|
|
|
|
|
2015-09-11 12:56:34 +02: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 12:56:34 +02:00
|
|
|
end
|
|
|
|
|
2014-07-09 15:31:49 +10:00
|
|
|
def has_title_badges
|
2017-08-23 22:54:51 +00:00
|
|
|
object.badges.where(allow_title: true).exists?
|
2014-07-09 15:31:49 +10:00
|
|
|
end
|
|
|
|
|
2015-04-21 14:36:46 -04:00
|
|
|
def pending_count
|
|
|
|
0
|
|
|
|
end
|
2015-09-11 02:12:40 +02: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-27 17:28:15 -03: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 13:56:54 +00: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-05 14:16:51 -05:00
|
|
|
end
|