2013-11-01 17:06:20 -04:00
|
|
|
class UserUpdater
|
2014-01-20 17:58:02 -05:00
|
|
|
|
|
|
|
CATEGORY_IDS = {
|
|
|
|
watched_category_ids: :watching,
|
|
|
|
tracked_category_ids: :tracking,
|
|
|
|
muted_category_ids: :muted
|
|
|
|
}
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
OPTION_ATTR = [
|
2015-02-26 10:50:01 -05:00
|
|
|
:email_always,
|
2016-02-16 23:46:19 -05:00
|
|
|
:mailing_list_mode,
|
|
|
|
:email_digests,
|
2015-02-26 10:50:01 -05:00
|
|
|
:email_direct,
|
|
|
|
:email_private_messages,
|
|
|
|
:external_links_in_new_tab,
|
|
|
|
:enable_quoting,
|
|
|
|
:dynamic_favicon,
|
|
|
|
:disable_jump_reply,
|
2015-11-17 12:21:40 -05:00
|
|
|
:edit_history_public,
|
|
|
|
:automatically_unpin_topics,
|
2016-02-18 00:57:22 -05:00
|
|
|
:digest_after_days,
|
|
|
|
:new_topic_duration_minutes,
|
2016-02-18 21:56:52 -05:00
|
|
|
:auto_track_topics_after_msecs,
|
2016-02-25 08:05:40 -05:00
|
|
|
:email_previous_replies,
|
2016-03-02 07:16:52 -05:00
|
|
|
:email_in_reply_to,
|
|
|
|
:like_notification_frequency
|
2014-01-20 17:58:02 -05:00
|
|
|
]
|
|
|
|
|
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)
|
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.location = attributes[:location]
|
|
|
|
user_profile.dismissed_banner_key = attributes[:dismissed_banner_key] if attributes[:dismissed_banner_key].present?
|
2014-06-07 00:54:32 -04:00
|
|
|
user_profile.website = format_url(attributes.fetch(:website) { user_profile.website })
|
2014-06-10 01:19:08 -04:00
|
|
|
user_profile.bio_raw = attributes.fetch(:bio_raw) { user_profile.bio_raw }
|
2015-05-19 19:39:58 -04: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 01:58:49 -05:00
|
|
|
|
2014-01-20 17:58:02 -05:00
|
|
|
user.name = attributes.fetch(:name) { user.name }
|
2014-02-07 22:24:10 -05:00
|
|
|
user.locale = attributes.fetch(:locale) { user.locale }
|
2014-01-02 01:58:49 -05:00
|
|
|
|
2013-11-01 17:06:20 -04:00
|
|
|
if guardian.can_grant_title?(user)
|
2014-01-20 17:58:02 -05:00
|
|
|
user.title = attributes.fetch(:title) { user.title }
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
|
2014-01-20 17:58:02 -05:00
|
|
|
CATEGORY_IDS.each do |attribute, level|
|
|
|
|
if ids = attributes[attribute]
|
|
|
|
CategoryUser.batch_set(user, level, ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
|
|
|
|
save_options = false
|
2016-02-25 08:05:40 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
|
if [true,false].include?(user.user_option.send(attribute))
|
|
|
|
val = attributes[attribute].to_s == 'true'
|
|
|
|
user.user_option.send("#{attribute}=", val)
|
|
|
|
else
|
|
|
|
user.user_option.send("#{attribute}=", attributes[attribute])
|
|
|
|
end
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2014-05-27 13:54:04 -04:00
|
|
|
User.transaction do
|
2015-03-23 20:55:22 -04:00
|
|
|
if attributes.key?(:muted_usernames)
|
|
|
|
update_muted_users(attributes[:muted_usernames])
|
|
|
|
end
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
(!save_options || user.user_option.save) && user_profile.save && user.save
|
2014-05-27 13:54:04 -04:00
|
|
|
end
|
2013-11-01 17:06:20 -04:00
|
|
|
end
|
|
|
|
|
2015-03-23 20:55:22 -04: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 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
|
|
|
|
MutedUser.exec_sql("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
|
|
|
|
)",
|
|
|
|
now: Time.now, user_id: user.id, desired_ids: desired_ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-30 19:16:11 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
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
|