闭社主体 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.

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