discourse/config/initializers/014-track-setting-changes.rb
Robin Ward ba6d4b2a8d FIX: Better handling for toggling must_approve_users
If you turn it on now, default all users to approved since they were
previously. Also support approving a user that doesn't have a reviewable
record (it will be created first.)

This also includes a refactor to move class method calls to
`DiscourseEvent` into an initializer. Otherwise the load order of
classes makes a difference in the test environment and some settings
might be triggered and others not, randomly.
2019-04-16 15:56:35 -04:00

39 lines
1.2 KiB
Ruby

# Enabling `must_approve_users` on an existing site is odd, so we assume that the
# existing users are approved.
DiscourseEvent.on(:site_setting_saved) do |site_setting|
name = site_setting.name.to_sym
next unless site_setting.value_changed?
if name == :must_approve_users && site_setting.value == 't'
User.where(approved: false).update_all(approved: true)
end
if name == :emoji_set
Emoji.clear_cache
previous_value = site_setting.attribute_in_database(:value) || SiteSetting.defaults[:emoji_set]
before = "/images/emoji/#{previous_value}/"
after = "/images/emoji/#{site_setting.value}/"
Scheduler::Defer.later("Fix Emoji Links") do
DB.exec("UPDATE posts SET cooked = REPLACE(cooked, :before, :after) WHERE cooked LIKE :like",
before: before,
after: after,
like: "%#{before}%"
)
end
end
Report.clear_cache(:storage_stats) if [:backup_location, :s3_backup_bucket].include?(name)
if name == :slug_generation_method
Scheduler::Defer.later("Null topic slug") do
Topic.update_all(slug: nil)
end
end
Jobs.enqueue(:update_s3_inventory) if [:s3_inventory, :s3_upload_bucket].include?(name)
SvgSprite.expire_cache if name.to_s.include?("_icon")
end