2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-01 17:06:20 -04:00
|
|
|
class UserUpdater
|
2014-01-20 17:58:02 -05:00
|
|
|
|
|
|
|
CATEGORY_IDS = {
|
2016-07-08 00:08:10 -04:00
|
|
|
watched_first_post_category_ids: :watching_first_post,
|
2014-01-20 17:58:02 -05:00
|
|
|
watched_category_ids: :watching,
|
|
|
|
tracked_category_ids: :tracking,
|
2020-08-19 15:05:04 -04:00
|
|
|
regular_category_ids: :regular,
|
2014-01-20 17:58:02 -05:00
|
|
|
muted_category_ids: :muted
|
|
|
|
}
|
|
|
|
|
2016-07-07 22:58:18 -04:00
|
|
|
TAG_NAMES = {
|
2016-07-22 16:16:45 -04:00
|
|
|
watching_first_post_tags: :watching_first_post,
|
2016-07-07 22:58:18 -04:00
|
|
|
watched_tags: :watching,
|
|
|
|
tracked_tags: :tracking,
|
|
|
|
muted_tags: :muted
|
|
|
|
}
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
OPTION_ATTR = [
|
|
|
|
:mailing_list_mode,
|
2016-05-21 09:17:54 -04:00
|
|
|
:mailing_list_mode_frequency,
|
2016-02-16 23:46:19 -05:00
|
|
|
:email_digests,
|
2019-03-15 10:55:11 -04:00
|
|
|
:email_level,
|
|
|
|
:email_messages_level,
|
2015-02-26 10:50:01 -05:00
|
|
|
:external_links_in_new_tab,
|
|
|
|
:enable_quoting,
|
2019-05-31 01:43:17 -04:00
|
|
|
:enable_defer,
|
2020-08-28 10:36:52 -04:00
|
|
|
:color_scheme_id,
|
2020-08-06 09:45:37 -04:00
|
|
|
:dark_scheme_id,
|
2015-02-26 10:50:01 -05:00
|
|
|
:dynamic_favicon,
|
2015-11-17 12:21:40 -05:00
|
|
|
:automatically_unpin_topics,
|
2016-03-02 15:26:27 -05:00
|
|
|
:digest_after_minutes,
|
2016-02-18 00:57:22 -05:00
|
|
|
:new_topic_duration_minutes,
|
2016-02-18 21:56:52 -05:00
|
|
|
:auto_track_topics_after_msecs,
|
2016-09-30 12:36:43 -04:00
|
|
|
:notification_level_when_replying,
|
2016-02-25 08:05:40 -05:00
|
|
|
:email_previous_replies,
|
2016-03-02 07:16:52 -05:00
|
|
|
:email_in_reply_to,
|
2016-03-17 17:35:23 -04:00
|
|
|
:like_notification_frequency,
|
2017-05-12 12:41:26 -04:00
|
|
|
:include_tl0_in_digests,
|
2018-07-12 00:18:21 -04:00
|
|
|
:theme_ids,
|
2017-10-06 03:56:58 -04:00
|
|
|
:allow_private_messages,
|
2020-07-20 17:23:49 -04:00
|
|
|
:enable_allowed_pm_users,
|
2017-11-09 14:45:19 -05:00
|
|
|
:homepage_id,
|
2019-01-14 08:21:46 -05:00
|
|
|
:hide_profile_and_presence,
|
2019-04-11 19:02:18 -04:00
|
|
|
:text_size,
|
2019-11-24 19:49:27 -05:00
|
|
|
:title_count_mode,
|
2020-08-14 09:40:56 -04:00
|
|
|
:timezone,
|
2021-10-05 23:11:52 -04:00
|
|
|
:skip_new_user_tips,
|
2022-10-12 11:38:45 -04:00
|
|
|
:seen_popups,
|
2022-10-17 22:21:52 -04:00
|
|
|
:default_calendar,
|
|
|
|
:sidebar_list_destination
|
2014-01-20 17:58:02 -05:00
|
|
|
]
|
|
|
|
|
2021-01-20 11:31:52 -05:00
|
|
|
NOTIFICATION_SCHEDULE_ATTRS = -> {
|
|
|
|
attrs = [:enabled]
|
|
|
|
7.times do |n|
|
|
|
|
attrs.push("day_#{n}_start_time".to_sym)
|
|
|
|
attrs.push("day_#{n}_end_time".to_sym)
|
|
|
|
end
|
|
|
|
{ user_notification_schedule: attrs }
|
|
|
|
}.call
|
|
|
|
|
2013-12-10 12:46:35 -05:00
|
|
|
def initialize(actor, user)
|
2013-11-01 17:06:20 -04:00
|
|
|
@user = user
|
2013-12-10 12:46:35 -05:00
|
|
|
@guardian = Guardian.new(actor)
|
2017-02-23 00:48:57 -05:00
|
|
|
@actor = actor
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(attributes = {})
|
2014-06-07 00:54:32 -04:00
|
|
|
user_profile = user.user_profile
|
2015-05-19 19:39:58 -04:00
|
|
|
user_profile.dismissed_banner_key = attributes[:dismissed_banner_key] if attributes[:dismissed_banner_key].present?
|
2021-02-08 05:04:33 -05:00
|
|
|
unless SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_bio
|
2016-08-01 01:29:28 -04:00
|
|
|
user_profile.bio_raw = attributes.fetch(:bio_raw) { user_profile.bio_raw }
|
|
|
|
end
|
2019-04-28 23:58:52 -04:00
|
|
|
|
2021-02-08 05:04:33 -05:00
|
|
|
unless SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_location
|
2020-04-28 02:06:35 -04:00
|
|
|
user_profile.location = attributes.fetch(:location) { user_profile.location }
|
|
|
|
end
|
|
|
|
|
2021-02-08 05:04:33 -05:00
|
|
|
unless SiteSetting.enable_discourse_connect && SiteSetting.discourse_connect_overrides_website
|
2020-04-28 02:06:35 -04:00
|
|
|
user_profile.website = format_url(attributes.fetch(:website) { user_profile.website })
|
|
|
|
end
|
|
|
|
|
2020-08-17 12:37:45 -04:00
|
|
|
if attributes[:profile_background_upload_url] == "" || !guardian.can_upload_profile_header?(user)
|
2019-05-02 04:53:17 -04:00
|
|
|
user_profile.profile_background_upload_id = nil
|
|
|
|
elsif upload = Upload.get_from_url(attributes[:profile_background_upload_url])
|
|
|
|
user_profile.profile_background_upload_id = upload.id
|
2019-04-28 23:58:52 -04:00
|
|
|
end
|
|
|
|
|
2020-08-17 12:37:45 -04:00
|
|
|
if attributes[:card_background_upload_url] == "" || !guardian.can_upload_user_card_background?(user)
|
2019-05-02 04:53:17 -04:00
|
|
|
user_profile.card_background_upload_id = nil
|
|
|
|
elsif upload = Upload.get_from_url(attributes[:card_background_upload_url])
|
|
|
|
user_profile.card_background_upload_id = upload.id
|
2019-04-28 23:58:52 -04:00
|
|
|
end
|
2014-01-02 01:58:49 -05:00
|
|
|
|
2021-01-20 11:31:52 -05:00
|
|
|
if attributes[:user_notification_schedule]
|
|
|
|
user_notification_schedule = user.user_notification_schedule || UserNotificationSchedule.new(user: user)
|
|
|
|
user_notification_schedule.assign_attributes(attributes[:user_notification_schedule])
|
|
|
|
end
|
|
|
|
|
2017-03-01 03:12:17 -05:00
|
|
|
old_user_name = user.name.present? ? user.name : ""
|
2021-07-28 02:07:18 -04:00
|
|
|
|
|
|
|
if guardian.can_edit_name?(user)
|
|
|
|
user.name = attributes.fetch(:name) { user.name }
|
|
|
|
end
|
2017-03-01 03:12:17 -05:00
|
|
|
|
2014-02-07 22:24:10 -05:00
|
|
|
user.locale = attributes.fetch(:locale) { user.locale }
|
2016-11-15 03:10:20 -05:00
|
|
|
user.date_of_birth = attributes.fetch(:date_of_birth) { user.date_of_birth }
|
2014-01-02 01:58:49 -05:00
|
|
|
|
2018-04-26 16:50:50 -04:00
|
|
|
if attributes[:title] &&
|
|
|
|
attributes[:title] != user.title &&
|
|
|
|
guardian.can_grant_title?(user, attributes[:title])
|
|
|
|
user.title = attributes[:title]
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
|
2019-10-28 13:46:27 -04:00
|
|
|
if SiteSetting.user_selected_primary_groups &&
|
|
|
|
attributes[:primary_group_id] &&
|
|
|
|
attributes[:primary_group_id] != user.primary_group_id &&
|
|
|
|
guardian.can_use_primary_group?(user, attributes[:primary_group_id])
|
|
|
|
|
|
|
|
user.primary_group_id = attributes[:primary_group_id]
|
2020-01-17 17:43:54 -05:00
|
|
|
elsif SiteSetting.user_selected_primary_groups &&
|
2020-02-07 18:26:33 -05:00
|
|
|
attributes[:primary_group_id] &&
|
2020-01-18 09:30:16 -05:00
|
|
|
attributes[:primary_group_id].blank?
|
2020-01-17 17:43:54 -05:00
|
|
|
|
|
|
|
user.primary_group_id = nil
|
2019-10-28 13:46:27 -04:00
|
|
|
end
|
|
|
|
|
2021-07-08 03:46:21 -04:00
|
|
|
if attributes[:flair_group_id] &&
|
|
|
|
attributes[:flair_group_id] != user.flair_group_id &&
|
2021-07-09 00:41:26 -04:00
|
|
|
(attributes[:flair_group_id].blank? ||
|
2021-07-21 07:41:04 -04:00
|
|
|
guardian.can_use_flair_group?(user, attributes[:flair_group_id]))
|
2021-07-08 03:46:21 -04:00
|
|
|
|
|
|
|
user.flair_group_id = attributes[:flair_group_id]
|
|
|
|
end
|
|
|
|
|
2021-07-16 14:50:40 -04:00
|
|
|
if @guardian.can_change_tracking_preferences?(user)
|
|
|
|
CATEGORY_IDS.each do |attribute, level|
|
|
|
|
if ids = attributes[attribute]
|
|
|
|
CategoryUser.batch_set(user, level, ids)
|
|
|
|
end
|
2014-01-20 17:58:02 -05:00
|
|
|
end
|
|
|
|
|
2021-07-16 14:50:40 -04:00
|
|
|
TAG_NAMES.each do |attribute, level|
|
|
|
|
if attributes.has_key?(attribute)
|
|
|
|
TagUser.batch_set(user, level, attributes[attribute]&.split(',') || [])
|
|
|
|
end
|
2018-03-29 15:08:22 -04:00
|
|
|
end
|
2016-07-07 22:58:18 -04:00
|
|
|
end
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
save_options = false
|
2016-02-25 08:05:40 -05:00
|
|
|
|
2018-07-12 00:18:21 -04:00
|
|
|
# special handling for theme_id cause we need to bump a sequence number
|
|
|
|
if attributes.key?(:theme_ids)
|
|
|
|
user_guardian = Guardian.new(user)
|
2018-08-10 07:12:02 -04:00
|
|
|
attributes[:theme_ids].reject!(&:blank?)
|
2018-07-12 00:18:21 -04:00
|
|
|
attributes[:theme_ids].map!(&:to_i)
|
|
|
|
if user_guardian.allow_themes?(attributes[:theme_ids])
|
|
|
|
user.user_option.theme_key_seq += 1 if user.user_option.theme_ids != attributes[:theme_ids]
|
|
|
|
else
|
|
|
|
attributes.delete(:theme_ids)
|
|
|
|
end
|
2017-05-15 12:48:08 -04:00
|
|
|
end
|
|
|
|
|
2019-01-28 06:19:50 -05:00
|
|
|
if attributes.key?(:text_size)
|
|
|
|
user.user_option.text_size_seq += 1 if user.user_option.text_size.to_s != attributes[:text_size]
|
|
|
|
end
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
OPTION_ATTR.each do |attribute|
|
2016-02-25 08:05:40 -05:00
|
|
|
if attributes.key?(attribute)
|
2016-02-16 23:46:19 -05:00
|
|
|
save_options = true
|
|
|
|
|
2019-05-06 21:27:05 -04:00
|
|
|
if [true, false].include?(user.user_option.public_send(attribute))
|
2016-02-16 23:46:19 -05:00
|
|
|
val = attributes[attribute].to_s == 'true'
|
2019-05-06 21:27:05 -04:00
|
|
|
user.user_option.public_send("#{attribute}=", val)
|
2016-02-16 23:46:19 -05:00
|
|
|
else
|
2019-05-06 21:27:05 -04:00
|
|
|
user.user_option.public_send("#{attribute}=", attributes[attribute])
|
2016-02-16 23:46:19 -05:00
|
|
|
end
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-12 11:38:45 -04:00
|
|
|
if attributes.key?(:skip_new_user_tips)
|
2022-11-09 13:20:34 -05:00
|
|
|
user.user_option.seen_popups = user.user_option.skip_new_user_tips ? [-1] : nil
|
2022-10-12 11:38:45 -04:00
|
|
|
end
|
|
|
|
|
2016-11-28 09:52:35 -05:00
|
|
|
# automatically disable digests when mailing_list_mode is enabled
|
|
|
|
user.user_option.email_digests = false if user.user_option.mailing_list_mode
|
|
|
|
|
2014-09-22 13:23:15 -04:00
|
|
|
fields = attributes[:custom_fields]
|
|
|
|
if fields.present?
|
2014-09-26 14:48:34 -04:00
|
|
|
user.custom_fields = user.custom_fields.merge(fields)
|
2014-06-11 01:50:37 -04:00
|
|
|
end
|
|
|
|
|
2017-03-16 02:44:09 -04:00
|
|
|
saved = nil
|
|
|
|
|
2020-04-23 18:26:29 -04:00
|
|
|
User.transaction do
|
|
|
|
if attributes.key?(:muted_usernames)
|
|
|
|
update_muted_users(attributes[:muted_usernames])
|
|
|
|
end
|
2015-03-23 20:55:22 -04:00
|
|
|
|
2020-07-20 17:23:49 -04:00
|
|
|
if attributes.key?(:allowed_pm_usernames)
|
|
|
|
update_allowed_pm_users(attributes[:allowed_pm_usernames])
|
|
|
|
end
|
|
|
|
|
2022-03-03 11:17:02 -05:00
|
|
|
if attributes.key?(:user_associated_accounts)
|
|
|
|
updated_associated_accounts(attributes[:user_associated_accounts])
|
|
|
|
end
|
|
|
|
|
2022-06-30 02:54:20 -04:00
|
|
|
if attributes.key?(:sidebar_category_ids)
|
|
|
|
update_sidebar_category_section_links(attributes[:sidebar_category_ids])
|
|
|
|
end
|
|
|
|
|
|
|
|
if attributes.key?(:sidebar_tag_names) && SiteSetting.tagging_enabled
|
|
|
|
update_sidebar_tag_section_links(attributes[:sidebar_tag_names])
|
|
|
|
end
|
|
|
|
|
2022-10-12 15:35:25 -04:00
|
|
|
if SiteSetting.enable_user_status?
|
|
|
|
update_user_status(attributes[:status])
|
|
|
|
end
|
|
|
|
|
2020-04-23 18:26:29 -04:00
|
|
|
name_changed = user.name_changed?
|
2022-10-12 15:35:25 -04:00
|
|
|
saved = (!save_options || user.user_option.save) &&
|
|
|
|
(user_notification_schedule.nil? || user_notification_schedule.save) &&
|
|
|
|
user_profile.save &&
|
|
|
|
user.save
|
|
|
|
|
|
|
|
if saved && (name_changed && old_user_name.casecmp(attributes.fetch(:name)) != 0)
|
2017-10-22 23:16:14 -04:00
|
|
|
|
2020-04-23 18:26:29 -04:00
|
|
|
StaffActionLogger.new(@actor).log_name_change(
|
|
|
|
user.id,
|
|
|
|
old_user_name,
|
|
|
|
attributes.fetch(:name) { '' }
|
|
|
|
)
|
2017-03-01 03:12:17 -05:00
|
|
|
end
|
2020-04-22 15:50:45 -04:00
|
|
|
rescue Addressable::URI::InvalidURIError => e
|
|
|
|
# Prevent 500 for crazy url input
|
|
|
|
return saved
|
2014-05-27 13:54:04 -04:00
|
|
|
end
|
2017-03-16 02:44:09 -04:00
|
|
|
|
2021-01-20 11:31:52 -05:00
|
|
|
if saved
|
|
|
|
if user_notification_schedule
|
2021-01-22 14:02:11 -05:00
|
|
|
user_notification_schedule.enabled ?
|
|
|
|
user_notification_schedule.create_do_not_disturb_timings(delete_existing: true) :
|
|
|
|
user_notification_schedule.destroy_scheduled_timings
|
2021-01-20 11:31:52 -05:00
|
|
|
end
|
|
|
|
DiscourseEvent.trigger(:user_updated, user)
|
|
|
|
end
|
|
|
|
|
2017-03-16 02:44:09 -04:00
|
|
|
saved
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
|
2015-03-23 20:55:22 -04:00
|
|
|
def update_muted_users(usernames)
|
|
|
|
usernames ||= ""
|
2019-03-20 06:18:46 -04:00
|
|
|
desired_usernames = usernames.split(",").reject { |username| user.username == username }
|
|
|
|
desired_ids = User.where(username: desired_usernames).pluck(:id)
|
2015-03-23 20:55:22 -04:00
|
|
|
if desired_ids.empty?
|
|
|
|
MutedUser.where(user_id: user.id).destroy_all
|
|
|
|
else
|
2015-03-30 19:16:11 -04:00
|
|
|
MutedUser.where('user_id = ? AND muted_user_id not in (?)', user.id, desired_ids).destroy_all
|
2015-03-23 20:55:22 -04:00
|
|
|
|
|
|
|
# SQL is easier here than figuring out how to do the same in AR
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(<<~SQL, now: Time.now, user_id: user.id, desired_ids: desired_ids)
|
|
|
|
INSERT into muted_users(user_id, muted_user_id, created_at, updated_at)
|
|
|
|
SELECT :user_id, id, :now, :now
|
|
|
|
FROM users
|
2019-03-06 06:21:58 -05:00
|
|
|
WHERE id in (:desired_ids)
|
|
|
|
ON CONFLICT DO NOTHING
|
2018-06-19 02:13:14 -04:00
|
|
|
SQL
|
2015-03-23 20:55:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-20 17:23:49 -04:00
|
|
|
def update_allowed_pm_users(usernames)
|
|
|
|
usernames ||= ""
|
|
|
|
desired_usernames = usernames.split(",").reject { |username| user.username == username }
|
2020-07-31 11:52:19 -04:00
|
|
|
desired_ids = User.where(username: desired_usernames).pluck(:id)
|
|
|
|
|
2020-07-20 17:23:49 -04:00
|
|
|
if desired_ids.empty?
|
|
|
|
AllowedPmUser.where(user_id: user.id).destroy_all
|
|
|
|
else
|
|
|
|
AllowedPmUser.where('user_id = ? AND allowed_pm_user_id not in (?)', user.id, desired_ids).destroy_all
|
|
|
|
|
|
|
|
# SQL is easier here than figuring out how to do the same in AR
|
|
|
|
DB.exec(<<~SQL, now: Time.zone.now, user_id: user.id, desired_ids: desired_ids)
|
|
|
|
INSERT into allowed_pm_users(user_id, allowed_pm_user_id, created_at, updated_at)
|
|
|
|
SELECT :user_id, id, :now, :now
|
|
|
|
FROM users
|
|
|
|
WHERE id in (:desired_ids)
|
|
|
|
ON CONFLICT DO NOTHING
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 11:17:02 -05:00
|
|
|
def updated_associated_accounts(associations)
|
|
|
|
associations.each do |association|
|
|
|
|
user_associated_account = UserAssociatedAccount.find_or_initialize_by(user_id: user.id, provider_name: association[:provider_name])
|
|
|
|
if association[:provider_uid].present?
|
|
|
|
user_associated_account.update!(provider_uid: association[:provider_uid])
|
|
|
|
else
|
|
|
|
user_associated_account.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-30 19:16:11 -04:00
|
|
|
private
|
|
|
|
|
2022-06-30 02:54:20 -04:00
|
|
|
def delete_all_sidebar_section_links(linkable_type)
|
|
|
|
SidebarSectionLink.where(user: user, linkable_type: linkable_type).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_sidebar_section_links(linkable_type, new_linkable_ids)
|
|
|
|
if new_linkable_ids.blank?
|
|
|
|
SidebarSectionLink.where(user: user, linkable_type: linkable_type).delete_all
|
|
|
|
else
|
|
|
|
existing_linkable_ids = SidebarSectionLink.where(user: user, linkable_type: linkable_type).pluck(:linkable_id)
|
|
|
|
|
|
|
|
to_delete = existing_linkable_ids - new_linkable_ids
|
|
|
|
to_insert = new_linkable_ids - existing_linkable_ids
|
|
|
|
|
|
|
|
to_insert_attributes = to_insert.map do |linkable_id|
|
|
|
|
{
|
|
|
|
linkable_type: linkable_type,
|
|
|
|
linkable_id: linkable_id,
|
|
|
|
user_id: user.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
SidebarSectionLink.where(user: user, linkable_type: linkable_type, linkable_id: to_delete).delete_all if to_delete.present?
|
|
|
|
SidebarSectionLink.insert_all(to_insert_attributes) if to_insert_attributes.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_sidebar_tag_section_links(tag_names)
|
|
|
|
if tag_names.blank?
|
|
|
|
delete_all_sidebar_section_links('Tag')
|
|
|
|
else
|
|
|
|
update_sidebar_section_links('Tag', Tag.where(name: tag_names).pluck(:id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_sidebar_category_section_links(category_ids)
|
|
|
|
if category_ids.blank?
|
|
|
|
delete_all_sidebar_section_links('Category')
|
|
|
|
else
|
|
|
|
update_sidebar_section_links('Category', Category.secured(guardian).where(id: category_ids).pluck(:id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-12 15:35:25 -04:00
|
|
|
def update_user_status(status)
|
|
|
|
if status.blank?
|
|
|
|
@user.clear_status!
|
|
|
|
else
|
|
|
|
@user.set_status!(status[:description], status[:emoji], status[:ends_at])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-30 19:16:11 -04:00
|
|
|
attr_reader :user, :guardian
|
|
|
|
|
2013-11-01 17:06:20 -04:00
|
|
|
def format_url(website)
|
2016-02-05 15:49:48 -05:00
|
|
|
return nil if website.blank?
|
2015-05-19 19:39:58 -04:00
|
|
|
website =~ /^http/ ? website : "http://#{website}"
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
end
|