2013-11-01 17:06:20 -04:00
|
|
|
class UserUpdater
|
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 = {})
|
|
|
|
user.website = format_url(attributes[:website]) || user.website
|
|
|
|
|
|
|
|
user.bio_raw = attributes[:bio_raw] || user.bio_raw
|
|
|
|
user.name = attributes[:name] || user.name
|
|
|
|
user.digest_after_days = attributes[:digest_after_days] || user.digest_after_days
|
|
|
|
|
2014-01-02 01:58:49 -05:00
|
|
|
if ids = attributes[:watched_category_ids]
|
|
|
|
CategoryUser.batch_set(user, :watching, ids)
|
|
|
|
end
|
|
|
|
|
2014-01-05 19:57:17 -05:00
|
|
|
if ids = attributes[:tracked_category_ids]
|
|
|
|
CategoryUser.batch_set(user, :tracking, ids)
|
|
|
|
end
|
|
|
|
|
2014-01-02 01:58:49 -05:00
|
|
|
if ids = attributes[:muted_category_ids]
|
|
|
|
CategoryUser.batch_set(user, :muted, ids)
|
|
|
|
end
|
|
|
|
|
2013-11-01 17:06:20 -04:00
|
|
|
if attributes[:auto_track_topics_after_msecs]
|
2014-01-02 01:58:49 -05:00
|
|
|
user.auto_track_topics_after_msecs = attributes[:auto_track_topics_after_msecs].to_i
|
2013-11-01 17:06:20 -04: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)
|
|
|
|
user.title = attributes[:title] || user.title
|
|
|
|
end
|
|
|
|
|
|
|
|
[
|
|
|
|
:email_digests,
|
|
|
|
:email_always,
|
|
|
|
:email_direct,
|
|
|
|
:email_private_messages,
|
|
|
|
:external_links_in_new_tab,
|
|
|
|
:enable_quoting,
|
2013-12-29 21:02:12 -05:00
|
|
|
:dynamic_favicon,
|
|
|
|
:watch_new_topics
|
2013-11-01 17:06:20 -04:00
|
|
|
].each do |attribute|
|
|
|
|
if attributes[attribute].present?
|
|
|
|
user.send("#{attribute.to_s}=", attributes[attribute] == 'true')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :guardian
|
|
|
|
|
|
|
|
def format_url(website)
|
|
|
|
if website =~ /^http/
|
|
|
|
website
|
|
|
|
else
|
|
|
|
"http://#{website}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|