闭社主体 forked from https://github.com/tootsuite/mastodon
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.

56 lines
1.3 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
  14. user.settings['interactions'] = merged_interactions
  15. user.settings['default_privacy'] = default_privacy_preference
  16. user.settings['boost_modal'] = boost_modal_preference
  17. user.settings['auto_play_gif'] = auto_play_gif_preference
  18. end
  19. def merged_notification_emails
  20. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  21. end
  22. def merged_interactions
  23. user.settings['interactions'].merge coerced_settings('interactions').to_h
  24. end
  25. def default_privacy_preference
  26. settings['setting_default_privacy']
  27. end
  28. def boost_modal_preference
  29. boolean_cast_setting 'setting_boost_modal'
  30. end
  31. def auto_play_gif_preference
  32. boolean_cast_setting 'setting_auto_play_gif'
  33. end
  34. def boolean_cast_setting(key)
  35. settings[key] == '1'
  36. end
  37. def coerced_settings(key)
  38. coerce_values settings.fetch(key, {})
  39. end
  40. def coerce_values(params_hash)
  41. params_hash.transform_values { |x| x == '1' }
  42. end
  43. end