2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
class UserUpdater
|
2014-01-21 06:58:02 +08:00
|
|
|
|
|
|
|
CATEGORY_IDS = {
|
2016-07-08 12:08:10 +08:00
|
|
|
watched_first_post_category_ids: :watching_first_post,
|
2014-01-21 06:58:02 +08:00
|
|
|
watched_category_ids: :watching,
|
|
|
|
tracked_category_ids: :tracking,
|
|
|
|
muted_category_ids: :muted
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:58:18 +08:00
|
|
|
TAG_NAMES = {
|
2016-07-23 04:16:45 +08:00
|
|
|
watching_first_post_tags: :watching_first_post,
|
2016-07-08 10:58:18 +08:00
|
|
|
watched_tags: :watching,
|
|
|
|
tracked_tags: :tracking,
|
|
|
|
muted_tags: :muted
|
|
|
|
}
|
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
OPTION_ATTR = [
|
|
|
|
:mailing_list_mode,
|
2016-05-21 21:17:54 +08:00
|
|
|
:mailing_list_mode_frequency,
|
2016-02-17 12:46:19 +08:00
|
|
|
:email_digests,
|
2019-03-15 22:55:11 +08:00
|
|
|
:email_level,
|
|
|
|
:email_messages_level,
|
2015-02-26 23:50:01 +08:00
|
|
|
:external_links_in_new_tab,
|
|
|
|
:enable_quoting,
|
2019-05-31 13:43:17 +08:00
|
|
|
:enable_defer,
|
2015-02-26 23:50:01 +08:00
|
|
|
:dynamic_favicon,
|
2015-11-18 01:21:40 +08:00
|
|
|
:automatically_unpin_topics,
|
2016-03-03 04:26:27 +08:00
|
|
|
:digest_after_minutes,
|
2016-02-18 13:57:22 +08:00
|
|
|
:new_topic_duration_minutes,
|
2016-02-19 10:56:52 +08:00
|
|
|
:auto_track_topics_after_msecs,
|
2016-10-01 00:36:43 +08:00
|
|
|
:notification_level_when_replying,
|
2016-02-25 21:05:40 +08:00
|
|
|
:email_previous_replies,
|
2016-03-02 20:16:52 +08:00
|
|
|
:email_in_reply_to,
|
2016-03-18 05:35:23 +08:00
|
|
|
:like_notification_frequency,
|
2017-05-13 00:41:26 +08:00
|
|
|
:include_tl0_in_digests,
|
2018-07-12 12:18:21 +08:00
|
|
|
:theme_ids,
|
2017-10-06 15:56:58 +08:00
|
|
|
:allow_private_messages,
|
2017-11-10 03:45:19 +08:00
|
|
|
:homepage_id,
|
2019-01-14 21:21:46 +08:00
|
|
|
:hide_profile_and_presence,
|
2019-04-12 07:02:18 +08:00
|
|
|
:text_size,
|
2019-11-25 08:49:27 +08:00
|
|
|
:title_count_mode,
|
|
|
|
:timezone
|
2014-01-21 06:58:02 +08:00
|
|
|
]
|
|
|
|
|
2013-12-11 01:46:35 +08:00
|
|
|
def initialize(actor, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
@user = user
|
2013-12-11 01:46:35 +08:00
|
|
|
@guardian = Guardian.new(actor)
|
2017-02-23 13:48:57 +08:00
|
|
|
@actor = actor
|
2013-11-02 05:06:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(attributes = {})
|
2014-06-07 12:54:32 +08:00
|
|
|
user_profile = user.user_profile
|
2016-05-04 00:49:30 +08:00
|
|
|
user_profile.location = attributes.fetch(:location) { user_profile.location }
|
2015-05-20 07:39:58 +08:00
|
|
|
user_profile.dismissed_banner_key = attributes[:dismissed_banner_key] if attributes[:dismissed_banner_key].present?
|
2014-06-07 12:54:32 +08:00
|
|
|
user_profile.website = format_url(attributes.fetch(:website) { user_profile.website })
|
2016-08-01 13:29:28 +08:00
|
|
|
unless SiteSetting.enable_sso && SiteSetting.sso_overrides_bio
|
|
|
|
user_profile.bio_raw = attributes.fetch(:bio_raw) { user_profile.bio_raw }
|
|
|
|
end
|
2019-04-29 11:58:52 +08:00
|
|
|
|
2019-05-02 16:53:17 +08:00
|
|
|
if attributes[:profile_background_upload_url] == ""
|
|
|
|
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-29 11:58:52 +08:00
|
|
|
end
|
|
|
|
|
2019-05-02 16:53:17 +08:00
|
|
|
if attributes[:card_background_upload_url] == ""
|
|
|
|
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-29 11:58:52 +08:00
|
|
|
end
|
2014-01-02 14:58:49 +08:00
|
|
|
|
2017-03-01 16:12:17 +08:00
|
|
|
old_user_name = user.name.present? ? user.name : ""
|
|
|
|
user.name = attributes.fetch(:name) { user.name }
|
|
|
|
|
2014-02-08 11:24:10 +08:00
|
|
|
user.locale = attributes.fetch(:locale) { user.locale }
|
2016-11-15 16:10:20 +08:00
|
|
|
user.date_of_birth = attributes.fetch(:date_of_birth) { user.date_of_birth }
|
2014-01-02 14:58:49 +08:00
|
|
|
|
2018-04-27 04:50:50 +08:00
|
|
|
if attributes[:title] &&
|
|
|
|
attributes[:title] != user.title &&
|
|
|
|
guardian.can_grant_title?(user, attributes[:title])
|
|
|
|
user.title = attributes[:title]
|
2013-11-02 05:06:20 +08:00
|
|
|
end
|
|
|
|
|
2019-10-29 01:46:27 +08: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-18 06:43:54 +08:00
|
|
|
elsif SiteSetting.user_selected_primary_groups &&
|
2020-02-08 07:26:33 +08:00
|
|
|
attributes[:primary_group_id] &&
|
2020-01-18 22:30:16 +08:00
|
|
|
attributes[:primary_group_id].blank?
|
2020-01-18 06:43:54 +08:00
|
|
|
|
|
|
|
user.primary_group_id = nil
|
2019-10-29 01:46:27 +08:00
|
|
|
end
|
|
|
|
|
2014-01-21 06:58:02 +08:00
|
|
|
CATEGORY_IDS.each do |attribute, level|
|
|
|
|
if ids = attributes[attribute]
|
|
|
|
CategoryUser.batch_set(user, level, ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-08 10:58:18 +08:00
|
|
|
TAG_NAMES.each do |attribute, level|
|
2018-03-30 03:08:22 +08:00
|
|
|
if attributes.has_key?(attribute)
|
|
|
|
TagUser.batch_set(user, level, attributes[attribute]&.split(',') || [])
|
|
|
|
end
|
2016-07-08 10:58:18 +08:00
|
|
|
end
|
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
save_options = false
|
2016-02-25 21:05:40 +08:00
|
|
|
|
2018-07-12 12:18:21 +08: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 19:12:02 +08:00
|
|
|
attributes[:theme_ids].reject!(&:blank?)
|
2018-07-12 12:18:21 +08: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-16 00:48:08 +08:00
|
|
|
end
|
|
|
|
|
2019-01-28 19:19:50 +08: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-17 12:46:19 +08:00
|
|
|
OPTION_ATTR.each do |attribute|
|
2016-02-25 21:05:40 +08:00
|
|
|
if attributes.key?(attribute)
|
2016-02-17 12:46:19 +08:00
|
|
|
save_options = true
|
|
|
|
|
2019-05-07 09:27:05 +08:00
|
|
|
if [true, false].include?(user.user_option.public_send(attribute))
|
2016-02-17 12:46:19 +08:00
|
|
|
val = attributes[attribute].to_s == 'true'
|
2019-05-07 09:27:05 +08:00
|
|
|
user.user_option.public_send("#{attribute}=", val)
|
2016-02-17 12:46:19 +08:00
|
|
|
else
|
2019-05-07 09:27:05 +08:00
|
|
|
user.user_option.public_send("#{attribute}=", attributes[attribute])
|
2016-02-17 12:46:19 +08:00
|
|
|
end
|
2013-11-02 05:06:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-28 22:52:35 +08: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-23 01:23:15 +08:00
|
|
|
fields = attributes[:custom_fields]
|
|
|
|
if fields.present?
|
2014-09-27 02:48:34 +08:00
|
|
|
user.custom_fields = user.custom_fields.merge(fields)
|
2014-06-11 13:50:37 +08:00
|
|
|
end
|
|
|
|
|
2017-03-16 14:44:09 +08:00
|
|
|
saved = nil
|
|
|
|
|
2020-04-24 06:26:29 +08:00
|
|
|
User.transaction do
|
|
|
|
if attributes.key?(:muted_usernames)
|
|
|
|
update_muted_users(attributes[:muted_usernames])
|
|
|
|
end
|
2015-03-24 08:55:22 +08:00
|
|
|
|
2020-04-24 06:26:29 +08:00
|
|
|
if attributes.key?(:ignored_usernames)
|
|
|
|
update_ignored_users(attributes[:ignored_usernames])
|
|
|
|
end
|
2019-03-06 19:21:58 +08:00
|
|
|
|
2020-04-24 06:26:29 +08:00
|
|
|
name_changed = user.name_changed?
|
|
|
|
if (saved = (!save_options || user.user_option.save) && user_profile.save && user.save) &&
|
|
|
|
(name_changed && old_user_name.casecmp(attributes.fetch(:name)) != 0)
|
2017-10-23 11:16:14 +08:00
|
|
|
|
2020-04-24 06:26:29 +08:00
|
|
|
StaffActionLogger.new(@actor).log_name_change(
|
|
|
|
user.id,
|
|
|
|
old_user_name,
|
|
|
|
attributes.fetch(:name) { '' }
|
|
|
|
)
|
2017-03-01 16:12:17 +08:00
|
|
|
end
|
2020-04-23 03:50:45 +08:00
|
|
|
rescue Addressable::URI::InvalidURIError => e
|
|
|
|
# Prevent 500 for crazy url input
|
|
|
|
return saved
|
2014-05-28 01:54:04 +08:00
|
|
|
end
|
2017-03-16 14:44:09 +08:00
|
|
|
|
2017-10-23 11:31:04 +08:00
|
|
|
DiscourseEvent.trigger(:user_updated, user) if saved
|
2017-03-16 14:44:09 +08:00
|
|
|
saved
|
2013-11-02 05:06:20 +08:00
|
|
|
end
|
|
|
|
|
2015-03-24 08:55:22 +08:00
|
|
|
def update_muted_users(usernames)
|
|
|
|
usernames ||= ""
|
2019-03-20 18:18:46 +08:00
|
|
|
desired_usernames = usernames.split(",").reject { |username| user.username == username }
|
|
|
|
desired_ids = User.where(username: desired_usernames).pluck(:id)
|
2015-03-24 08:55:22 +08:00
|
|
|
if desired_ids.empty?
|
|
|
|
MutedUser.where(user_id: user.id).destroy_all
|
|
|
|
else
|
2015-03-31 07:16:11 +08:00
|
|
|
MutedUser.where('user_id = ? AND muted_user_id not in (?)', user.id, desired_ids).destroy_all
|
2015-03-24 08:55:22 +08:00
|
|
|
|
|
|
|
# SQL is easier here than figuring out how to do the same in AR
|
2018-06-19 14:13:14 +08: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 19:21:58 +08:00
|
|
|
WHERE id in (:desired_ids)
|
|
|
|
ON CONFLICT DO NOTHING
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_ignored_users(usernames)
|
2019-03-21 03:55:46 +08:00
|
|
|
return unless guardian.can_ignore_users?
|
2019-03-20 22:02:34 +08:00
|
|
|
|
2019-03-06 19:21:58 +08:00
|
|
|
usernames ||= ""
|
2019-03-20 18:18:46 +08:00
|
|
|
desired_usernames = usernames.split(",").reject { |username| user.username == username }
|
|
|
|
desired_ids = User.where(username: desired_usernames).where(admin: false, moderator: false).pluck(:id)
|
2019-03-06 19:21:58 +08:00
|
|
|
if desired_ids.empty?
|
|
|
|
IgnoredUser.where(user_id: user.id).destroy_all
|
|
|
|
else
|
|
|
|
IgnoredUser.where('user_id = ? AND ignored_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.now, user_id: user.id, desired_ids: desired_ids)
|
|
|
|
INSERT into ignored_users(user_id, ignored_user_id, created_at, updated_at)
|
|
|
|
SELECT :user_id, id, :now, :now
|
|
|
|
FROM users
|
|
|
|
WHERE id in (:desired_ids)
|
|
|
|
ON CONFLICT DO NOTHING
|
2018-06-19 14:13:14 +08:00
|
|
|
SQL
|
2015-03-24 08:55:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-31 07:16:11 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :guardian
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
def format_url(website)
|
2016-02-06 04:49:48 +08:00
|
|
|
return nil if website.blank?
|
2015-05-20 07:39:58 +08:00
|
|
|
website =~ /^http/ ? website : "http://#{website}"
|
2013-11-02 05:06:20 +08:00
|
|
|
end
|
|
|
|
end
|