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.

145 lines
4.7 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. user.settings['default_content_type']= default_content_type_preference if change?('setting_default_content_type')
  35. end
  36. def merged_notification_emails
  37. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  38. end
  39. def merged_interactions
  40. user.settings['interactions'].merge coerced_settings('interactions').to_h
  41. end
  42. def default_privacy_preference
  43. settings['setting_default_privacy']
  44. end
  45. def default_sensitive_preference
  46. boolean_cast_setting 'setting_default_sensitive'
  47. end
  48. def unfollow_modal_preference
  49. boolean_cast_setting 'setting_unfollow_modal'
  50. end
  51. def boost_modal_preference
  52. boolean_cast_setting 'setting_boost_modal'
  53. end
  54. def favourite_modal_preference
  55. boolean_cast_setting 'setting_favourite_modal'
  56. end
  57. def delete_modal_preference
  58. boolean_cast_setting 'setting_delete_modal'
  59. end
  60. def system_font_ui_preference
  61. boolean_cast_setting 'setting_system_font_ui'
  62. end
  63. def auto_play_gif_preference
  64. boolean_cast_setting 'setting_auto_play_gif'
  65. end
  66. def display_media_preference
  67. settings['setting_display_media']
  68. end
  69. def expand_spoilers_preference
  70. boolean_cast_setting 'setting_expand_spoilers'
  71. end
  72. def reduce_motion_preference
  73. boolean_cast_setting 'setting_reduce_motion'
  74. end
  75. def noindex_preference
  76. boolean_cast_setting 'setting_noindex'
  77. end
  78. def hide_followers_count_preference
  79. boolean_cast_setting 'setting_hide_followers_count'
  80. end
  81. def flavour_preference
  82. settings['setting_flavour']
  83. end
  84. def skin_preference
  85. settings['setting_skin']
  86. end
  87. def hide_network_preference
  88. boolean_cast_setting 'setting_hide_network'
  89. end
  90. def show_application_preference
  91. boolean_cast_setting 'setting_show_application'
  92. end
  93. def default_language_preference
  94. settings['setting_default_language']
  95. end
  96. def aggregate_reblogs_preference
  97. boolean_cast_setting 'setting_aggregate_reblogs'
  98. end
  99. def default_content_type_preference
  100. settings['setting_default_content_type']
  101. end
  102. def boolean_cast_setting(key)
  103. ActiveModel::Type::Boolean.new.cast(settings[key])
  104. end
  105. def coerced_settings(key)
  106. coerce_values settings.fetch(key, {})
  107. end
  108. def coerce_values(params_hash)
  109. params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
  110. end
  111. def change?(key)
  112. !settings[key].nil?
  113. end
  114. end