Class: Admin::SiteSettingsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/site_settings_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



8
9
10
# File 'app/controllers/admin/site_settings_controller.rb', line 8

def index
  render_json_dump(site_settings: SiteSetting.all_settings)
end

#updateObject



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/admin/site_settings_controller.rb', line 12

def update
  params.require(:id)
  id = params[:id]
  value = params[id]
  value.strip! if value.is_a?(String)

  new_setting_name =
    SiteSettings::DeprecatedSettings::SETTINGS.find do |old_name, new_name, override, _|
      if old_name == id
        if !override
          raise Discourse::InvalidParameters,
                "You cannot change this site setting because it is deprecated, use #{new_name} instead."
        end

        break new_name
      end
    end

  id = new_setting_name if new_setting_name

  raise_access_hidden_setting(id)

  if SiteSetting.type_supervisor.get_type(id) == :uploaded_image_list
    value = Upload.get_from_urls(value.split("|")).to_a
  end

  value = Upload.get_from_url(value) || "" if SiteSetting.type_supervisor.get_type(id) == :upload

  update_existing_users = params[:update_existing_user].present?
  previous_value = value_or_default(SiteSetting.get(id)) if update_existing_users

  SiteSetting.set_and_log(id, value, current_user)

  if update_existing_users
    new_value = value_or_default(value)

    if (user_option = user_options[id.to_sym]).present?
      if user_option == "text_size_key"
        previous_value = UserOption.text_sizes[previous_value.to_sym]
        new_value = UserOption.text_sizes[new_value.to_sym]
      elsif user_option == "title_count_mode_key"
        previous_value = UserOption.title_count_modes[previous_value.to_sym]
        new_value = UserOption.title_count_modes[new_value.to_sym]
      end

      attrs = { user_option => new_value }
      attrs[:email_digests] = (new_value.to_i != 0) if id == "default_email_digest_frequency"

      UserOption.human_users.where(user_option => previous_value).update_all(attrs)
    elsif id.start_with?("default_categories_")
      previous_category_ids = previous_value.split("|")
      new_category_ids = new_value.split("|")

      notification_level = category_notification_level(id)

      categories_to_unwatch = previous_category_ids - new_category_ids
      CategoryUser.where(
        category_id: categories_to_unwatch,
        notification_level: notification_level,
      ).delete_all
      TopicUser
        .joins(:topic)
        .where(
          notification_level: TopicUser.notification_levels[:watching],
          notifications_reason_id: TopicUser.notification_reasons[:auto_watch_category],
          topics: {
            category_id: categories_to_unwatch,
          },
        )
        .update_all(notification_level: TopicUser.notification_levels[:regular])

      (new_category_ids - previous_category_ids).each do |category_id|
        skip_user_ids = CategoryUser.where(category_id: category_id).pluck(:user_id)

        User
          .real
          .where(staged: false)
          .where.not(id: skip_user_ids)
          .select(:id)
          .find_in_batches do |users|
            category_users = []
            users.each do |user|
              category_users << {
                category_id: category_id,
                user_id: user.id,
                notification_level: notification_level,
              }
            end
            CategoryUser.insert_all!(category_users)
          end
      end
    elsif id.start_with?("default_tags_")
      previous_tag_ids = Tag.where(name: previous_value.split("|")).pluck(:id)
      new_tag_ids = Tag.where(name: new_value.split("|")).pluck(:id)
      now = Time.zone.now

      notification_level = tag_notification_level(id)

      TagUser.where(
        tag_id: (previous_tag_ids - new_tag_ids),
        notification_level: notification_level,
      ).delete_all

      (new_tag_ids - previous_tag_ids).each do |tag_id|
        skip_user_ids = TagUser.where(tag_id: tag_id).pluck(:user_id)

        User
          .real
          .where(staged: false)
          .where.not(id: skip_user_ids)
          .select(:id)
          .find_in_batches do |users|
            tag_users = []
            users.each do |user|
              tag_users << {
                tag_id: tag_id,
                user_id: user.id,
                notification_level: notification_level,
                created_at: now,
                updated_at: now,
              }
            end
            TagUser.insert_all!(tag_users)
          end
      end
    elsif is_sidebar_default_setting?(id)
      Jobs.enqueue(
        :backfill_sidebar_site_settings,
        setting_name: id,
        previous_value: previous_value,
        new_value: new_value,
      )
    end
  end

  render body: nil
end

#user_countObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/controllers/admin/site_settings_controller.rb', line 150

def user_count
  params.require(:site_setting_id)
  id = params[:site_setting_id]
  raise Discourse::NotFound unless id.start_with?("default_")
  new_value = value_or_default(params[id])

  raise_access_hidden_setting(id)
  previous_value = value_or_default(SiteSetting.public_send(id))
  json = {}

  if (user_option = user_options[id.to_sym]).present?
    if user_option == "text_size_key"
      previous_value = UserOption.text_sizes[previous_value.to_sym]
    elsif user_option == "title_count_mode_key"
      previous_value = UserOption.title_count_modes[previous_value.to_sym]
    end

    json[:user_count] = UserOption.human_users.where(user_option => previous_value).count
  elsif id.start_with?("default_categories_")
    previous_category_ids = previous_value.split("|")
    new_category_ids = new_value.split("|")

    notification_level = category_notification_level(id)

    user_ids =
      CategoryUser
        .where(
          category_id: previous_category_ids - new_category_ids,
          notification_level: notification_level,
        )
        .distinct
        .pluck(:user_id)
    user_ids +=
      User
        .real
        .joins("CROSS JOIN categories c")
        .joins("LEFT JOIN category_users cu ON users.id = cu.user_id AND c.id = cu.category_id")
        .where(staged: false)
        .where(
          "c.id IN (?) AND cu.notification_level IS NULL",
          new_category_ids - previous_category_ids,
        )
        .distinct
        .pluck("users.id")

    json[:user_count] = user_ids.uniq.count
  elsif id.start_with?("default_tags_")
    previous_tag_ids = Tag.where(name: previous_value.split("|")).pluck(:id)
    new_tag_ids = Tag.where(name: new_value.split("|")).pluck(:id)

    notification_level = tag_notification_level(id)

    user_ids =
      TagUser
        .where(tag_id: previous_tag_ids - new_tag_ids, notification_level: notification_level)
        .distinct
        .pluck(:user_id)
    user_ids +=
      User
        .real
        .joins("CROSS JOIN tags t")
        .joins("LEFT JOIN tag_users tu ON users.id = tu.user_id AND t.id = tu.tag_id")
        .where(staged: false)
        .where("t.id IN (?) AND tu.notification_level IS NULL", new_tag_ids - previous_tag_ids)
        .distinct
        .pluck("users.id")

    json[:user_count] = user_ids.uniq.count
  elsif is_sidebar_default_setting?(id)
    json[:user_count] = SidebarSiteSettingsBackfiller.new(
      id,
      previous_value: previous_value,
      new_value: new_value,
    ).number_of_users_to_backfill
  end

  render json: json
end