2013-11-01 16:06:20 -05:00
|
|
|
class UserUpdater
|
2014-01-20 16:58:02 -06:00
|
|
|
|
|
|
|
CATEGORY_IDS = {
|
|
|
|
watched_category_ids: :watching,
|
|
|
|
tracked_category_ids: :tracking,
|
|
|
|
muted_category_ids: :muted
|
|
|
|
}
|
|
|
|
|
|
|
|
USER_ATTR = [
|
|
|
|
:email_digests,
|
|
|
|
:email_always,
|
|
|
|
:email_direct,
|
|
|
|
:email_private_messages,
|
|
|
|
:external_links_in_new_tab,
|
|
|
|
:enable_quoting,
|
|
|
|
:dynamic_favicon,
|
2014-05-26 15:39:03 -05:00
|
|
|
:mailing_list_mode,
|
2014-07-26 23:04:46 -05:00
|
|
|
:disable_jump_reply,
|
|
|
|
:edit_history_public
|
2014-01-20 16:58:02 -06:00
|
|
|
]
|
|
|
|
|
2014-05-27 12:54:04 -05:00
|
|
|
PROFILE_ATTR = [
|
2014-06-18 13:04:10 -05:00
|
|
|
:location,
|
|
|
|
:dismissed_banner_key
|
2014-05-27 12:54:04 -05: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)
|
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
|
|
|
|
user_profile.website = format_url(attributes.fetch(:website) { user_profile.website })
|
2014-06-10 00:19:08 -05:00
|
|
|
user_profile.bio_raw = attributes.fetch(:bio_raw) { user_profile.bio_raw }
|
2014-01-02 00:58:49 -06:00
|
|
|
|
2014-01-20 16:58:02 -06:00
|
|
|
user.name = attributes.fetch(:name) { user.name }
|
2014-02-07 21:24:10 -06:00
|
|
|
user.locale = attributes.fetch(:locale) { user.locale }
|
2014-01-20 16:58:02 -06:00
|
|
|
user.digest_after_days = attributes.fetch(:digest_after_days) { user.digest_after_days }
|
2014-01-02 00:58:49 -06:00
|
|
|
|
2013-11-01 16:06:20 -05:00
|
|
|
if attributes[:auto_track_topics_after_msecs]
|
2014-01-02 00:58:49 -06:00
|
|
|
user.auto_track_topics_after_msecs = attributes[:auto_track_topics_after_msecs].to_i
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if attributes[:new_topic_duration_minutes]
|
|
|
|
user.new_topic_duration_minutes = attributes[:new_topic_duration_minutes].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
if guardian.can_grant_title?(user)
|
2014-01-20 16:58:02 -06:00
|
|
|
user.title = attributes.fetch(:title) { user.title }
|
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
|
|
|
|
|
|
|
|
USER_ATTR.each do |attribute|
|
2013-11-01 16:06:20 -05:00
|
|
|
if attributes[attribute].present?
|
2014-08-14 13:20:52 -05:00
|
|
|
user.send("#{attribute}=", attributes[attribute] == 'true')
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-27 12:54:04 -05:00
|
|
|
PROFILE_ATTR.each do |attribute|
|
2014-08-14 13:20:52 -05:00
|
|
|
user_profile.send("#{attribute}=", attributes[attribute])
|
2014-05-27 12:54:04 -05:00
|
|
|
end
|
|
|
|
|
2014-09-22 12:23:15 -05:00
|
|
|
fields = attributes[:custom_fields]
|
|
|
|
if fields.present?
|
2014-06-11 00:50:37 -05:00
|
|
|
user.custom_fields = fields
|
|
|
|
end
|
|
|
|
|
2014-05-27 12:54:04 -05:00
|
|
|
User.transaction do
|
2014-09-08 14:17:31 -05:00
|
|
|
user_profile.save && user.save
|
2014-05-27 12:54:04 -05:00
|
|
|
end
|
2013-11-01 16:06:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :guardian
|
|
|
|
|
|
|
|
def format_url(website)
|
|
|
|
if website =~ /^http/
|
|
|
|
website
|
|
|
|
else
|
|
|
|
"http://#{website}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|