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.

125 lines
3.9 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['flavour'] = flavour_preference if change?('setting_flavour')
  29. user.settings['skin'] = skin_preference if change?('setting_skin')
  30. user.settings['hide_network'] = hide_network_preference if change?('setting_hide_network')
  31. end
  32. def merged_notification_emails
  33. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  34. end
  35. def merged_interactions
  36. user.settings['interactions'].merge coerced_settings('interactions').to_h
  37. end
  38. def default_privacy_preference
  39. settings['setting_default_privacy']
  40. end
  41. def default_sensitive_preference
  42. boolean_cast_setting 'setting_default_sensitive'
  43. end
  44. def unfollow_modal_preference
  45. boolean_cast_setting 'setting_unfollow_modal'
  46. end
  47. def boost_modal_preference
  48. boolean_cast_setting 'setting_boost_modal'
  49. end
  50. def favourite_modal_preference
  51. boolean_cast_setting 'setting_favourite_modal'
  52. end
  53. def delete_modal_preference
  54. boolean_cast_setting 'setting_delete_modal'
  55. end
  56. def system_font_ui_preference
  57. boolean_cast_setting 'setting_system_font_ui'
  58. end
  59. def auto_play_gif_preference
  60. boolean_cast_setting 'setting_auto_play_gif'
  61. end
  62. def display_media_preference
  63. settings['setting_display_media']
  64. end
  65. def expand_spoilers_preference
  66. boolean_cast_setting 'setting_expand_spoilers'
  67. end
  68. def reduce_motion_preference
  69. boolean_cast_setting 'setting_reduce_motion'
  70. end
  71. def noindex_preference
  72. boolean_cast_setting 'setting_noindex'
  73. end
  74. def flavour_preference
  75. settings['setting_flavour']
  76. end
  77. def skin_preference
  78. settings['setting_skin']
  79. end
  80. def hide_network_preference
  81. boolean_cast_setting 'setting_hide_network'
  82. end
  83. def default_language_preference
  84. settings['setting_default_language']
  85. end
  86. def boolean_cast_setting(key)
  87. ActiveModel::Type::Boolean.new.cast(settings[key])
  88. end
  89. def coerced_settings(key)
  90. coerce_values settings.fetch(key, {})
  91. end
  92. def coerce_values(params_hash)
  93. params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
  94. end
  95. def change?(key)
  96. !settings[key].nil?
  97. end
  98. end