You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.5 KiB

7 years ago
7 years ago
  1. # frozen_string_literal: true
  2. class UserSettingsDecorator
  3. attr_reader :user, :settings
  4. def initialize(user)
  5. @user = user
  6. end
  7. def update(settings)
  8. @settings = settings
  9. process_update
  10. end
  11. private
  12. def process_update
  13. user.settings['notification_emails'] = merged_notification_emails if change?('notification_emails')
  14. user.settings['interactions'] = merged_interactions if change?('interactions')
  15. user.settings['default_privacy'] = default_privacy_preference if change?('setting_default_privacy')
  16. user.settings['default_sensitive'] = default_sensitive_preference if change?('setting_default_sensitive')
  17. user.settings['default_language'] = default_language_preference if change?('setting_default_language')
  18. user.settings['unfollow_modal'] = unfollow_modal_preference if change?('setting_unfollow_modal')
  19. user.settings['boost_modal'] = boost_modal_preference if change?('setting_boost_modal')
  20. user.settings['favourite_modal'] = favourite_modal_preference if change?('setting_favourite_modal')
  21. user.settings['delete_modal'] = delete_modal_preference if change?('setting_delete_modal')
  22. user.settings['auto_play_gif'] = auto_play_gif_preference if change?('setting_auto_play_gif')
  23. user.settings['display_media'] = display_media_preference if change?('setting_display_media')
  24. user.settings['expand_spoilers'] = expand_spoilers_preference if change?('setting_expand_spoilers')
  25. user.settings['reduce_motion'] = reduce_motion_preference if change?('setting_reduce_motion')
  26. user.settings['system_font_ui'] = system_font_ui_preference if change?('setting_system_font_ui')
  27. user.settings['noindex'] = noindex_preference if change?('setting_noindex')
  28. user.settings['hide_followers_count']= hide_followers_count_preference if change?('setting_hide_followers_count')
  29. user.settings['flavour'] = flavour_preference if change?('setting_flavour')
  30. user.settings['skin'] = skin_preference if change?('setting_skin')
  31. user.settings['hide_network'] = hide_network_preference if change?('setting_hide_network')
  32. user.settings['aggregate_reblogs'] = aggregate_reblogs_preference if change?('setting_aggregate_reblogs')
  33. user.settings['show_application'] = show_application_preference if change?('setting_show_application')
  34. end
  35. def merged_notification_emails
  36. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  37. end
  38. def merged_interactions
  39. user.settings['interactions'].merge coerced_settings('interactions').to_h
  40. end
  41. def default_privacy_preference
  42. settings['setting_default_privacy']
  43. end
  44. def default_sensitive_preference
  45. boolean_cast_setting 'setting_default_sensitive'
  46. end
  47. def unfollow_modal_preference
  48. boolean_cast_setting 'setting_unfollow_modal'
  49. end
  50. def boost_modal_preference
  51. boolean_cast_setting 'setting_boost_modal'
  52. end
  53. def favourite_modal_preference
  54. boolean_cast_setting 'setting_favourite_modal'
  55. end
  56. def delete_modal_preference
  57. boolean_cast_setting 'setting_delete_modal'
  58. end
  59. def system_font_ui_preference
  60. boolean_cast_setting 'setting_system_font_ui'
  61. end
  62. def auto_play_gif_preference
  63. boolean_cast_setting 'setting_auto_play_gif'
  64. end
  65. def display_media_preference
  66. settings['setting_display_media']
  67. end
  68. def expand_spoilers_preference
  69. boolean_cast_setting 'setting_expand_spoilers'
  70. end
  71. def reduce_motion_preference
  72. boolean_cast_setting 'setting_reduce_motion'
  73. end
  74. def noindex_preference
  75. boolean_cast_setting 'setting_noindex'
  76. end
  77. def hide_followers_count_preference
  78. boolean_cast_setting 'setting_hide_followers_count'
  79. end
  80. def flavour_preference
  81. settings['setting_flavour']
  82. end
  83. def skin_preference
  84. settings['setting_skin']
  85. end
  86. def hide_network_preference
  87. boolean_cast_setting 'setting_hide_network'
  88. end
  89. def show_application_preference
  90. boolean_cast_setting 'setting_show_application'
  91. end
  92. def default_language_preference
  93. settings['setting_default_language']
  94. end
  95. def aggregate_reblogs_preference
  96. boolean_cast_setting 'setting_aggregate_reblogs'
  97. end
  98. def boolean_cast_setting(key)
  99. ActiveModel::Type::Boolean.new.cast(settings[key])
  100. end
  101. def coerced_settings(key)
  102. coerce_values settings.fetch(key, {})
  103. end
  104. def coerce_values(params_hash)
  105. params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
  106. end
  107. def change?(key)
  108. !settings[key].nil?
  109. end
  110. end