2013-11-02 05:06:20 +08:00
|
|
|
class UserUpdater
|
2014-01-21 06:58:02 +08: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-27 04:39:03 +08:00
|
|
|
:mailing_list_mode,
|
|
|
|
:disable_jump_reply
|
2014-01-21 06:58:02 +08:00
|
|
|
]
|
|
|
|
|
2014-05-28 01:54:04 +08:00
|
|
|
PROFILE_ATTR = [
|
|
|
|
:location
|
|
|
|
]
|
|
|
|
|
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)
|
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
|
|
|
|
user_profile.website = format_url(attributes.fetch(:website) { user_profile.website })
|
2014-01-02 14:58:49 +08:00
|
|
|
|
2014-01-21 06:58:02 +08:00
|
|
|
user.bio_raw = attributes.fetch(:bio_raw) { user.bio_raw }
|
|
|
|
user.name = attributes.fetch(:name) { user.name }
|
2014-02-08 11:24:10 +08:00
|
|
|
user.locale = attributes.fetch(:locale) { user.locale }
|
2014-01-21 06:58:02 +08:00
|
|
|
user.digest_after_days = attributes.fetch(:digest_after_days) { user.digest_after_days }
|
2014-01-02 14:58:49 +08:00
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
if attributes[:auto_track_topics_after_msecs]
|
2014-01-02 14:58:49 +08:00
|
|
|
user.auto_track_topics_after_msecs = attributes[:auto_track_topics_after_msecs].to_i
|
2013-11-02 05:06:20 +08: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-21 06:58:02 +08:00
|
|
|
user.title = attributes.fetch(:title) { user.title }
|
2013-11-02 05:06:20 +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
|
|
|
|
|
|
|
|
USER_ATTR.each do |attribute|
|
2013-11-02 05:06:20 +08:00
|
|
|
if attributes[attribute].present?
|
|
|
|
user.send("#{attribute.to_s}=", attributes[attribute] == 'true')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-28 01:54:04 +08:00
|
|
|
PROFILE_ATTR.each do |attribute|
|
|
|
|
user_profile.send("#{attribute.to_s}=", attributes[attribute])
|
|
|
|
end
|
|
|
|
|
2014-06-11 13:50:37 +08:00
|
|
|
if fields = attributes[:custom_fields]
|
|
|
|
user.custom_fields = fields
|
|
|
|
end
|
|
|
|
|
2014-05-28 01:54:04 +08:00
|
|
|
User.transaction do
|
|
|
|
user_profile.save
|
|
|
|
user.save
|
|
|
|
end
|
2013-11-02 05:06:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :guardian
|
|
|
|
|
|
|
|
def format_url(website)
|
|
|
|
if website =~ /^http/
|
|
|
|
website
|
|
|
|
else
|
|
|
|
"http://#{website}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|