discourse/plugins/chat/lib/chat/seeder.rb
Joffrey JAFFEUX 12a18d4d55
DEV: properly namespace chat (#20690)
This commit main goal was to comply with Zeitwerk and properly rely on autoloading. To achieve this, most resources have been namespaced under the `Chat` module.

- Given all models are now namespaced with `Chat::` and would change the stored types in DB when using polymorphism or STI (single table inheritance), this commit uses various Rails methods to ensure proper class is loaded and the stored name in DB is unchanged, eg: `Chat::Message` model will be stored as `"ChatMessage"`, and `"ChatMessage"` will correctly load `Chat::Message` model.
- Jobs are now using constants only, eg: `Jobs::Chat::Foo` and should only be enqueued this way

Notes:
- This commit also used this opportunity to limit the number of registered css files in plugin.rb
- `discourse_dev` support has been removed within this commit and will be reintroduced later

<!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2023-03-17 14:24:38 +01:00

31 lines
898 B
Ruby

# frozen_string_literal: true
module Chat
class Seeder
def execute(args = {})
return if !SiteSetting.needs_chat_seeded
begin
create_category_channel_from(SiteSetting.staff_category_id)
create_category_channel_from(SiteSetting.general_category_id)
rescue => error
Rails.logger.warn("Error seeding chat category - #{error.inspect}")
ensure
SiteSetting.needs_chat_seeded = false
end
end
def create_category_channel_from(category_id)
category = Category.find_by(id: category_id)
return if category.nil?
chat_channel = category.create_chat_channel!(auto_join_users: true, name: category.name)
category.custom_fields[Chat::HAS_CHAT_ENABLED] = true
category.save!
Chat::ChannelMembershipManager.new(chat_channel).enforce_automatic_channel_memberships
chat_channel
end
end
end