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.

150 lines
4.9 KiB

  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['delete_modal'] = delete_modal_preference if change?('setting_delete_modal')
  21. user.settings['auto_play_gif'] = auto_play_gif_preference if change?('setting_auto_play_gif')
  22. user.settings['display_media'] = display_media_preference if change?('setting_display_media')
  23. user.settings['expand_spoilers'] = expand_spoilers_preference if change?('setting_expand_spoilers')
  24. user.settings['reduce_motion'] = reduce_motion_preference if change?('setting_reduce_motion')
  25. user.settings['system_font_ui'] = system_font_ui_preference if change?('setting_system_font_ui')
  26. user.settings['noindex'] = noindex_preference if change?('setting_noindex')
  27. user.settings['theme'] = theme_preference if change?('setting_theme')
  28. user.settings['hide_network'] = hide_network_preference if change?('setting_hide_network')
  29. user.settings['aggregate_reblogs'] = aggregate_reblogs_preference if change?('setting_aggregate_reblogs')
  30. user.settings['show_application'] = show_application_preference if change?('setting_show_application')
  31. user.settings['advanced_layout'] = advanced_layout_preference if change?('setting_advanced_layout')
  32. user.settings['use_blurhash'] = use_blurhash_preference if change?('setting_use_blurhash')
  33. user.settings['use_pending_items'] = use_pending_items_preference if change?('setting_use_pending_items')
  34. user.settings['trends'] = trends_preference if change?('setting_trends')
  35. user.settings['crop_images'] = crop_images_preference if change?('setting_crop_images')
  36. end
  37. def merged_notification_emails
  38. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  39. end
  40. def merged_interactions
  41. user.settings['interactions'].merge coerced_settings('interactions').to_h
  42. end
  43. def default_privacy_preference
  44. settings['setting_default_privacy']
  45. end
  46. def default_sensitive_preference
  47. boolean_cast_setting 'setting_default_sensitive'
  48. end
  49. def unfollow_modal_preference
  50. boolean_cast_setting 'setting_unfollow_modal'
  51. end
  52. def boost_modal_preference
  53. boolean_cast_setting 'setting_boost_modal'
  54. end
  55. def delete_modal_preference
  56. boolean_cast_setting 'setting_delete_modal'
  57. end
  58. def system_font_ui_preference
  59. boolean_cast_setting 'setting_system_font_ui'
  60. end
  61. def auto_play_gif_preference
  62. boolean_cast_setting 'setting_auto_play_gif'
  63. end
  64. def display_media_preference
  65. settings['setting_display_media']
  66. end
  67. def expand_spoilers_preference
  68. boolean_cast_setting 'setting_expand_spoilers'
  69. end
  70. def reduce_motion_preference
  71. boolean_cast_setting 'setting_reduce_motion'
  72. end
  73. def noindex_preference
  74. boolean_cast_setting 'setting_noindex'
  75. end
  76. def hide_network_preference
  77. boolean_cast_setting 'setting_hide_network'
  78. end
  79. def show_application_preference
  80. boolean_cast_setting 'setting_show_application'
  81. end
  82. def theme_preference
  83. settings['setting_theme']
  84. end
  85. def default_language_preference
  86. settings['setting_default_language']
  87. end
  88. def aggregate_reblogs_preference
  89. boolean_cast_setting 'setting_aggregate_reblogs'
  90. end
  91. def advanced_layout_preference
  92. boolean_cast_setting 'setting_advanced_layout'
  93. end
  94. def use_blurhash_preference
  95. boolean_cast_setting 'setting_use_blurhash'
  96. end
  97. def use_pending_items_preference
  98. boolean_cast_setting 'setting_use_pending_items'
  99. end
  100. def trends_preference
  101. boolean_cast_setting 'setting_trends'
  102. end
  103. def crop_images_preference
  104. boolean_cast_setting 'setting_crop_images'
  105. end
  106. def boolean_cast_setting(key)
  107. ActiveModel::Type::Boolean.new.cast(settings[key])
  108. end
  109. def coerced_settings(key)
  110. coerce_values settings.fetch(key, {})
  111. end
  112. def coerce_values(params_hash)
  113. params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
  114. end
  115. def change?(key)
  116. !settings[key].nil?
  117. end
  118. end