2013-11-01 16:06:20 -05:00
|
|
|
class UserUpdater
|
2014-01-20 16:58:02 -06:00
|
|
|
|
|
|
|
CATEGORY_IDS = {
|
2016-07-07 23:08:10 -05:00
|
|
|
watched_first_post_category_ids: :watching_first_post,
|
2014-01-20 16:58:02 -06:00
|
|
|
watched_category_ids: :watching,
|
|
|
|
tracked_category_ids: :tracking,
|
|
|
|
muted_category_ids: :muted
|
|
|
|
}
|
|
|
|
|
2016-07-07 21:58:18 -05:00
|
|
|
TAG_NAMES = {
|
2016-07-22 15:16:45 -05:00
|
|
|
watching_first_post_tags: :watching_first_post,
|
2016-07-07 21:58:18 -05:00
|
|
|
watched_tags: :watching,
|
|
|
|
tracked_tags: :tracking,
|
|
|
|
muted_tags: :muted
|
|
|
|
}
|
|
|
|
|
2016-02-16 22:46:19 -06:00
|
|
|
OPTION_ATTR = [
|
2015-02-26 09:50:01 -06:00
|
|
|
:email_always,
|
2016-02-16 22:46:19 -06:00
|
|
|
:mailing_list_mode,
|
2016-05-21 08:17:54 -05:00
|
|
|
:mailing_list_mode_frequency,
|
2016-02-16 22:46:19 -06:00
|
|
|
:email_digests,
|
2015-02-26 09:50:01 -06:00
|
|
|
:email_direct,
|
|
|
|
:email_private_messages,
|
|
|
|
:external_links_in_new_tab,
|
|
|
|
:enable_quoting,
|
|
|
|
:dynamic_favicon,
|
|
|
|
:disable_jump_reply,
|
2015-11-17 11:21:40 -06:00
|
|
|
:automatically_unpin_topics,
|
2016-03-02 14:26:27 -06:00
|
|
|
:digest_after_minutes,
|
2016-02-17 23:57:22 -06:00
|
|
|
:new_topic_duration_minutes,
|
2016-02-18 20:56:52 -06:00
|
|
|
:auto_track_topics_after_msecs,
|
2016-09-30 11:36:43 -05:00
|
|
|
:notification_level_when_replying,
|
2016-02-25 07:05:40 -06:00
|
|
|
:email_previous_replies,
|
2016-03-02 06:16:52 -06:00
|
|
|
:email_in_reply_to,
|
2016-03-17 16:35:23 -05:00
|
|
|
:like_notification_frequency,
|
2017-05-12 11:41:26 -05:00
|
|
|
:include_tl0_in_digests,
|
2018-07-11 23:18:21 -05:00
|
|
|
:theme_ids,
|
2017-10-06 02:56:58 -05:00
|
|
|
:allow_private_messages,
|
2017-11-09 13:45:19 -06:00
|
|
|
:homepage_id,
|
2014-01-20 16:58:02 -06:00
|
|
|
]
|
|
|
|
|
2013-12-10 11:46:35 -06:00
|
|
|
def initialize(actor, user)
|
2013-11-01 16:06:20 -05:00
|
|
|
@user = user
|
2013-12-10 11:46:35 -06:00
|
|
|
@guardian = Guardian.new(actor)
|
2017-02-22 23:48:57 -06:00
|
|
|
@actor = actor
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(attributes = {})
|
2014-06-06 23:54:32 -05:00
|
|
|
user_profile = user.user_profile
|
2016-05-03 11:49:30 -05:00
|
|
|
user_profile.location = attributes.fetch(:location) { user_profile.location }
|
2015-05-19 18:39:58 -05:00
|
|
|
user_profile.dismissed_banner_key = attributes[:dismissed_banner_key] if attributes[:dismissed_banner_key].present?
|
2014-06-06 23:54:32 -05:00
|
|
|
user_profile.website = format_url(attributes.fetch(:website) { user_profile.website })
|
2016-08-01 00:29:28 -05:00
|
|
|
unless SiteSetting.enable_sso && SiteSetting.sso_overrides_bio
|
|
|
|
user_profile.bio_raw = attributes.fetch(:bio_raw) { user_profile.bio_raw }
|
|
|
|
end
|
2015-05-19 18:39:58 -05:00
|
|
|
user_profile.profile_background = attributes.fetch(:profile_background) { user_profile.profile_background }
|
|
|
|
user_profile.card_background = attributes.fetch(:card_background) { user_profile.card_background }
|
2014-01-02 00:58:49 -06:00
|
|
|
|
2017-03-01 02:12:17 -06:00
|
|
|
old_user_name = user.name.present? ? user.name : ""
|
|
|
|
user.name = attributes.fetch(:name) { user.name }
|
|
|
|
|
2014-02-07 21:24:10 -06:00
|
|
|
user.locale = attributes.fetch(:locale) { user.locale }
|
2016-11-15 02:10:20 -06:00
|
|
|
user.date_of_birth = attributes.fetch(:date_of_birth) { user.date_of_birth }
|
2014-01-02 00:58:49 -06:00
|
|
|
|
2018-04-26 15:50:50 -05:00
|
|
|
if attributes[:title] &&
|
|
|
|
attributes[:title] != user.title &&
|
|
|
|
guardian.can_grant_title?(user, attributes[:title])
|
|
|
|
user.title = attributes[:title]
|
|
|
|
if user.badges.where(name: user.title).exists?
|
|
|
|
user_profile.badge_granted_title = true
|
|
|
|
end
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
|
2014-01-20 16:58:02 -06:00
|
|
|
CATEGORY_IDS.each do |attribute, level|
|
|
|
|
if ids = attributes[attribute]
|
|
|
|
CategoryUser.batch_set(user, level, ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-07 21:58:18 -05:00
|
|
|
TAG_NAMES.each do |attribute, level|
|
2018-03-29 14:08:22 -05:00
|
|
|
if attributes.has_key?(attribute)
|
|
|
|
TagUser.batch_set(user, level, attributes[attribute]&.split(',') || [])
|
|
|
|
end
|
2016-07-07 21:58:18 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 22:46:19 -06:00
|
|
|
save_options = false
|
2016-02-25 07:05:40 -06:00
|
|
|
|
2018-07-11 23:18:21 -05: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 06:12:02 -05:00
|
|
|
attributes[:theme_ids].reject!(&:blank?)
|
2018-07-11 23:18:21 -05: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 11:48:08 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 22:46:19 -06:00
|
|
|
OPTION_ATTR.each do |attribute|
|
2016-02-25 07:05:40 -06:00
|
|
|
if attributes.key?(attribute)
|
2016-02-16 22:46:19 -06:00
|
|
|
save_options = true
|
|
|
|
|
2016-11-28 08:52:35 -06:00
|
|
|
if [true, false].include?(user.user_option.send(attribute))
|
2016-02-16 22:46:19 -06:00
|
|
|
val = attributes[attribute].to_s == 'true'
|
|
|
|
user.user_option.send("#{attribute}=", val)
|
|
|
|
else
|
|
|
|
user.user_option.send("#{attribute}=", attributes[attribute])
|
|
|
|
end
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-28 08:52:35 -06: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 12:23:15 -05:00
|
|
|
fields = attributes[:custom_fields]
|
|
|
|
if fields.present?
|
2014-09-26 13:48:34 -05:00
|
|
|
user.custom_fields = user.custom_fields.merge(fields)
|
2014-06-11 00:50:37 -05:00
|
|
|
end
|
|
|
|
|
2017-03-16 01:44:09 -05:00
|
|
|
saved = nil
|
|
|
|
|
2014-05-27 12:54:04 -05:00
|
|
|
User.transaction do
|
2015-03-23 19:55:22 -05:00
|
|
|
if attributes.key?(:muted_usernames)
|
|
|
|
update_muted_users(attributes[:muted_usernames])
|
|
|
|
end
|
|
|
|
|
2017-10-22 22:16:14 -05:00
|
|
|
if (saved = (!save_options || user.user_option.save) && user_profile.save && user.save) &&
|
|
|
|
(attributes[:name].present? && old_user_name.casecmp(attributes.fetch(:name)) != 0) ||
|
|
|
|
(attributes[:name].blank? && old_user_name.present?)
|
|
|
|
|
|
|
|
StaffActionLogger.new(@actor).log_name_change(
|
|
|
|
user.id,
|
|
|
|
old_user_name,
|
|
|
|
attributes.fetch(:name) { '' }
|
|
|
|
)
|
2017-03-01 02:12:17 -06:00
|
|
|
end
|
2014-05-27 12:54:04 -05:00
|
|
|
end
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2017-10-22 22:31:04 -05:00
|
|
|
DiscourseEvent.trigger(:user_updated, user) if saved
|
2017-03-16 01:44:09 -05:00
|
|
|
saved
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
|
2015-03-23 19:55:22 -05:00
|
|
|
def update_muted_users(usernames)
|
|
|
|
usernames ||= ""
|
|
|
|
desired_ids = User.where(username: usernames.split(",")).pluck(:id)
|
|
|
|
if desired_ids.empty?
|
|
|
|
MutedUser.where(user_id: user.id).destroy_all
|
|
|
|
else
|
2015-03-30 18:16:11 -05:00
|
|
|
MutedUser.where('user_id = ? AND muted_user_id not in (?)', user.id, desired_ids).destroy_all
|
2015-03-23 19:55:22 -05:00
|
|
|
|
|
|
|
# SQL is easier here than figuring out how to do the same in AR
|
2018-06-19 01:13:14 -05: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
|
|
|
|
WHERE
|
|
|
|
id in (:desired_ids) AND
|
|
|
|
id NOT IN (
|
|
|
|
SELECT muted_user_id
|
|
|
|
FROM muted_users
|
|
|
|
WHERE user_id = :user_id
|
|
|
|
)
|
|
|
|
SQL
|
2015-03-23 19:55:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-30 18:16:11 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :guardian
|
|
|
|
|
2013-11-01 16:06:20 -05:00
|
|
|
def format_url(website)
|
2016-02-05 14:49:48 -06:00
|
|
|
return nil if website.blank?
|
2015-05-19 18:39:58 -05:00
|
|
|
website =~ /^http/ ? website : "http://#{website}"
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
end
|