user_settings_decorator.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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['disable_swiping'] = disable_swiping_preference if change?('setting_disable_swiping')
  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['theme'] = theme_preference if change?('setting_theme')
  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. user.settings['always_send_emails'] = always_send_emails_preference if change?('setting_always_send_emails')
  37. end
  38. def merged_notification_emails
  39. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  40. end
  41. def merged_interactions
  42. user.settings['interactions'].merge coerced_settings('interactions').to_h
  43. end
  44. def default_privacy_preference
  45. settings['setting_default_privacy']
  46. end
  47. def default_sensitive_preference
  48. boolean_cast_setting 'setting_default_sensitive'
  49. end
  50. def unfollow_modal_preference
  51. boolean_cast_setting 'setting_unfollow_modal'
  52. end
  53. def boost_modal_preference
  54. boolean_cast_setting 'setting_boost_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 disable_swiping_preference
  75. boolean_cast_setting 'setting_disable_swiping'
  76. end
  77. def noindex_preference
  78. boolean_cast_setting 'setting_noindex'
  79. end
  80. def show_application_preference
  81. boolean_cast_setting 'setting_show_application'
  82. end
  83. def theme_preference
  84. settings['setting_theme']
  85. end
  86. def default_language_preference
  87. settings['setting_default_language']
  88. end
  89. def aggregate_reblogs_preference
  90. boolean_cast_setting 'setting_aggregate_reblogs'
  91. end
  92. def advanced_layout_preference
  93. boolean_cast_setting 'setting_advanced_layout'
  94. end
  95. def use_blurhash_preference
  96. boolean_cast_setting 'setting_use_blurhash'
  97. end
  98. def use_pending_items_preference
  99. boolean_cast_setting 'setting_use_pending_items'
  100. end
  101. def trends_preference
  102. boolean_cast_setting 'setting_trends'
  103. end
  104. def crop_images_preference
  105. boolean_cast_setting 'setting_crop_images'
  106. end
  107. def always_send_emails_preference
  108. boolean_cast_setting 'setting_always_send_emails'
  109. end
  110. def boolean_cast_setting(key)
  111. ActiveModel::Type::Boolean.new.cast(settings[key])
  112. end
  113. def coerced_settings(key)
  114. coerce_values settings.fetch(key, {})
  115. end
  116. def coerce_values(params_hash)
  117. params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
  118. end
  119. def change?(key)
  120. !settings[key].nil?
  121. end
  122. end