about summary refs log tree commit diff
path: root/app/models/user_settings.rb
blob: 0be8c5fbcefe209c2bc6378532ef87f27edd0b05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

class UserSettings
  class Error < StandardError; end
  class KeyError < Error; end

  include UserSettings::DSL
  include UserSettings::Glue

  setting :always_send_emails, default: false
  setting :aggregate_reblogs, default: true
  setting :flavour, default: -> { ::Setting.flavour }
  setting :skin, default: -> { ::Setting.skin }
  setting :noindex, default: -> { ::Setting.noindex }
  setting :show_application, default: true
  setting :default_language, default: nil
  setting :default_sensitive, default: false
  setting :default_privacy, default: nil
  setting :default_content_type, default: 'text/plain'
  setting :hide_followers_count, default: false

  namespace :web do
    setting :crop_images, default: true
    setting :advanced_layout, default: false
    setting :trends, default: true
    setting :use_blurhash, default: true
    setting :use_pending_items, default: false
    setting :use_system_font, default: false
    setting :disable_swiping, default: false
    setting :delete_modal, default: true
    setting :reblog_modal, default: false
    setting :unfollow_modal, default: true
    setting :favourite_modal, default: false
    setting :reduce_motion, default: false
    setting :expand_content_warnings, default: false
    setting :display_media, default: 'default', in: %w(default show_all hide_all)
    setting :auto_play, default: false
    setting :use_system_emoji_font, default: false
  end

  namespace :notification_emails do
    setting :follow, default: true
    setting :reblog, default: false
    setting :favourite, default: false
    setting :mention, default: true
    setting :follow_request, default: true
    setting :report, default: true
    setting :pending_account, default: true
    setting :trends, default: true
    setting :link_trends, default: false
    setting :status_trends, default: false
    setting :appeal, default: true
  end

  namespace :interactions do
    setting :must_be_follower, default: false
    setting :must_be_following, default: false
    setting :must_be_following_dm, default: false
  end

  def initialize(original_hash)
    @original_hash = original_hash || {}
  end

  def [](key)
    key = key.to_sym

    raise KeyError, "Undefined setting: #{key}" unless self.class.definition_for?(key)

    if @original_hash.key?(key)
      @original_hash[key]
    else
      self.class.definition_for(key).default_value
    end
  end

  def []=(key, value)
    key = key.to_sym

    raise KeyError, "Undefined setting: #{key}" unless self.class.definition_for?(key)

    typecast_value = self.class.definition_for(key).type_cast(value)

    if typecast_value.nil?
      @original_hash.delete(key)
    else
      @original_hash[key] = typecast_value
    end
  end

  def update(params)
    params.each do |k, v|
      self[k] = v unless v.nil?
    end
  end

  keys.each do |key|
    define_method(key) do
      self[key]
    end
  end

  def as_json
    @original_hash
  end
end