2017-04-21 01:26:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserSettingsDecorator
|
|
|
|
attr_reader :user, :settings
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(settings)
|
|
|
|
@settings = settings
|
|
|
|
process_update
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_update
|
2018-02-08 23:26:57 +00:00
|
|
|
user.settings['notification_emails'] = merged_notification_emails if change?('notification_emails')
|
|
|
|
user.settings['interactions'] = merged_interactions if change?('interactions')
|
|
|
|
user.settings['default_privacy'] = default_privacy_preference if change?('setting_default_privacy')
|
|
|
|
user.settings['default_sensitive'] = default_sensitive_preference if change?('setting_default_sensitive')
|
|
|
|
user.settings['unfollow_modal'] = unfollow_modal_preference if change?('setting_unfollow_modal')
|
|
|
|
user.settings['boost_modal'] = boost_modal_preference if change?('setting_boost_modal')
|
|
|
|
user.settings['delete_modal'] = delete_modal_preference if change?('setting_delete_modal')
|
|
|
|
user.settings['auto_play_gif'] = auto_play_gif_preference if change?('setting_auto_play_gif')
|
|
|
|
user.settings['display_sensitive_media'] = display_sensitive_media_preference if change?('setting_display_sensitive_media')
|
|
|
|
user.settings['reduce_motion'] = reduce_motion_preference if change?('setting_reduce_motion')
|
|
|
|
user.settings['system_font_ui'] = system_font_ui_preference if change?('setting_system_font_ui')
|
|
|
|
user.settings['noindex'] = noindex_preference if change?('setting_noindex')
|
|
|
|
user.settings['theme'] = theme_preference if change?('setting_theme')
|
2017-04-21 01:26:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def merged_notification_emails
|
|
|
|
user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def merged_interactions
|
|
|
|
user.settings['interactions'].merge coerced_settings('interactions').to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_privacy_preference
|
|
|
|
settings['setting_default_privacy']
|
|
|
|
end
|
|
|
|
|
2017-07-10 12:00:32 +00:00
|
|
|
def default_sensitive_preference
|
|
|
|
boolean_cast_setting 'setting_default_sensitive'
|
|
|
|
end
|
|
|
|
|
2017-07-18 15:14:43 +00:00
|
|
|
def unfollow_modal_preference
|
|
|
|
boolean_cast_setting 'setting_unfollow_modal'
|
|
|
|
end
|
|
|
|
|
2017-04-21 01:26:52 +00:00
|
|
|
def boost_modal_preference
|
|
|
|
boolean_cast_setting 'setting_boost_modal'
|
|
|
|
end
|
|
|
|
|
2017-05-29 15:56:13 +00:00
|
|
|
def delete_modal_preference
|
|
|
|
boolean_cast_setting 'setting_delete_modal'
|
|
|
|
end
|
|
|
|
|
2017-07-06 20:39:56 +00:00
|
|
|
def system_font_ui_preference
|
|
|
|
boolean_cast_setting 'setting_system_font_ui'
|
|
|
|
end
|
|
|
|
|
2017-04-21 01:26:52 +00:00
|
|
|
def auto_play_gif_preference
|
|
|
|
boolean_cast_setting 'setting_auto_play_gif'
|
|
|
|
end
|
|
|
|
|
2018-02-08 23:26:57 +00:00
|
|
|
def display_sensitive_media_preference
|
|
|
|
boolean_cast_setting 'setting_display_sensitive_media'
|
|
|
|
end
|
|
|
|
|
2017-10-16 07:36:15 +00:00
|
|
|
def reduce_motion_preference
|
|
|
|
boolean_cast_setting 'setting_reduce_motion'
|
|
|
|
end
|
|
|
|
|
2017-07-14 14:41:02 +00:00
|
|
|
def noindex_preference
|
|
|
|
boolean_cast_setting 'setting_noindex'
|
|
|
|
end
|
|
|
|
|
2017-09-19 14:36:23 +00:00
|
|
|
def theme_preference
|
|
|
|
settings['setting_theme']
|
|
|
|
end
|
|
|
|
|
2017-04-21 01:26:52 +00:00
|
|
|
def boolean_cast_setting(key)
|
2018-04-08 11:43:10 +00:00
|
|
|
ActiveModel::Type::Boolean.new.cast(settings[key])
|
2017-04-21 01:26:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def coerced_settings(key)
|
|
|
|
coerce_values settings.fetch(key, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
def coerce_values(params_hash)
|
2018-04-08 11:43:10 +00:00
|
|
|
params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
|
2017-04-21 01:26:52 +00:00
|
|
|
end
|
2017-10-04 08:22:52 +00:00
|
|
|
|
|
|
|
def change?(key)
|
|
|
|
!settings[key].nil?
|
|
|
|
end
|
2017-04-21 01:26:52 +00:00
|
|
|
end
|