user_settings.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. class UserSettings
  3. class Error < StandardError; end
  4. class KeyError < Error; end
  5. include UserSettings::DSL
  6. include UserSettings::Glue
  7. setting :always_send_emails, default: false
  8. setting :aggregate_reblogs, default: true
  9. setting :theme, default: -> { ::Setting.theme }
  10. setting :noindex, default: -> { ::Setting.noindex }
  11. setting :show_application, default: true
  12. setting :default_language, default: nil
  13. setting :default_sensitive, default: false
  14. setting :default_privacy, default: nil, in: %w(public unlisted private)
  15. setting_inverse_alias :indexable, :noindex
  16. namespace :web do
  17. setting :advanced_layout, default: false
  18. setting :trends, default: true
  19. setting :use_blurhash, default: true
  20. setting :use_pending_items, default: false
  21. setting :use_system_font, default: false
  22. setting :disable_swiping, default: false
  23. setting :delete_modal, default: true
  24. setting :reblog_modal, default: false
  25. setting :unfollow_modal, default: true
  26. setting :reduce_motion, default: false
  27. setting :expand_content_warnings, default: false
  28. setting :display_media, default: 'default', in: %w(default show_all hide_all)
  29. setting :auto_play, default: false
  30. end
  31. namespace :notification_emails do
  32. setting :follow, default: true
  33. setting :reblog, default: false
  34. setting :favourite, default: false
  35. setting :mention, default: true
  36. setting :follow_request, default: true
  37. setting :report, default: true
  38. setting :pending_account, default: true
  39. setting :trends, default: true
  40. setting :appeal, default: true
  41. setting :software_updates, default: 'critical', in: %w(none critical patch all)
  42. end
  43. namespace :interactions do
  44. setting :must_be_follower, default: false
  45. setting :must_be_following, default: false
  46. setting :must_be_following_dm, default: false
  47. end
  48. def initialize(original_hash)
  49. @original_hash = original_hash || {}
  50. end
  51. def [](key)
  52. definition = self.class.definition_for(key)
  53. raise KeyError, "Undefined setting: #{key}" if definition.nil?
  54. definition.value_for(key, @original_hash[definition.key])
  55. end
  56. def []=(key, value)
  57. definition = self.class.definition_for(key)
  58. raise KeyError, "Undefined setting: #{key}" if definition.nil?
  59. typecast_value = definition.type_cast(value)
  60. raise ArgumentError, "Invalid value for setting #{definition.key}: #{typecast_value}" if definition.in.present? && definition.in.exclude?(typecast_value)
  61. if typecast_value.nil?
  62. @original_hash.delete(definition.key)
  63. else
  64. @original_hash[definition.key] = definition.value_for(key, typecast_value)
  65. end
  66. end
  67. def update(params)
  68. params.each do |k, v|
  69. self[k] = v unless v.nil?
  70. end
  71. end
  72. keys.each do |key|
  73. define_method(key) do
  74. self[key]
  75. end
  76. end
  77. def as_json
  78. @original_hash
  79. end
  80. end