logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

user_settings_decorator.rb (3167B)


  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['unfollow_modal'] = unfollow_modal_preference if change?('setting_unfollow_modal')
  18. user.settings['boost_modal'] = boost_modal_preference if change?('setting_boost_modal')
  19. user.settings['delete_modal'] = delete_modal_preference if change?('setting_delete_modal')
  20. user.settings['auto_play_gif'] = auto_play_gif_preference if change?('setting_auto_play_gif')
  21. user.settings['display_sensitive_media'] = display_sensitive_media_preference if change?('setting_display_sensitive_media')
  22. user.settings['reduce_motion'] = reduce_motion_preference if change?('setting_reduce_motion')
  23. user.settings['system_font_ui'] = system_font_ui_preference if change?('setting_system_font_ui')
  24. user.settings['noindex'] = noindex_preference if change?('setting_noindex')
  25. user.settings['theme'] = theme_preference if change?('setting_theme')
  26. end
  27. def merged_notification_emails
  28. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  29. end
  30. def merged_interactions
  31. user.settings['interactions'].merge coerced_settings('interactions').to_h
  32. end
  33. def default_privacy_preference
  34. settings['setting_default_privacy']
  35. end
  36. def default_sensitive_preference
  37. boolean_cast_setting 'setting_default_sensitive'
  38. end
  39. def unfollow_modal_preference
  40. boolean_cast_setting 'setting_unfollow_modal'
  41. end
  42. def boost_modal_preference
  43. boolean_cast_setting 'setting_boost_modal'
  44. end
  45. def delete_modal_preference
  46. boolean_cast_setting 'setting_delete_modal'
  47. end
  48. def system_font_ui_preference
  49. boolean_cast_setting 'setting_system_font_ui'
  50. end
  51. def auto_play_gif_preference
  52. boolean_cast_setting 'setting_auto_play_gif'
  53. end
  54. def display_sensitive_media_preference
  55. boolean_cast_setting 'setting_display_sensitive_media'
  56. end
  57. def reduce_motion_preference
  58. boolean_cast_setting 'setting_reduce_motion'
  59. end
  60. def noindex_preference
  61. boolean_cast_setting 'setting_noindex'
  62. end
  63. def theme_preference
  64. settings['setting_theme']
  65. end
  66. def boolean_cast_setting(key)
  67. ActiveModel::Type::Boolean.new.cast(settings[key])
  68. end
  69. def coerced_settings(key)
  70. coerce_values settings.fetch(key, {})
  71. end
  72. def coerce_values(params_hash)
  73. params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
  74. end
  75. def change?(key)
  76. !settings[key].nil?
  77. end
  78. end