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. -->
This commit is contained in:
Joffrey JAFFEUX
2023-03-17 14:24:38 +01:00
committed by GitHub
parent 74349e17c9
commit 12a18d4d55
343 changed files with 9077 additions and 8745 deletions

View File

@ -1,23 +0,0 @@
# frozen_string_literal: true
if Discourse.allow_dev_populate?
chat_task = Rake::Task["dev:populate"]
chat_task.enhance do
SiteSetting.chat_enabled = true
DiscourseDev::PublicChannel.populate!
DiscourseDev::DirectChannel.populate!
DiscourseDev::Message.populate!
end
desc "Generates sample content for chat"
task "chat:populate" => ["db:load_config"] do |_, args|
DiscourseDev::PublicChannel.new.populate!(ignore_current_count: true)
DiscourseDev::DirectChannel.new.populate!(ignore_current_count: true)
DiscourseDev::Message.new.populate!(ignore_current_count: true)
end
desc "Generates sample messages in channels"
task "chat:message:populate" => ["db:load_config"] do |_, args|
DiscourseDev::Message.new.populate!(ignore_current_count: true)
end
end

View File

@ -15,7 +15,7 @@ end
def rebake_uncooked_chat_messages
puts "Rebaking uncooked chat messages on #{RailsMultisite::ConnectionManagement.current_db}"
uncooked = ChatMessage.uncooked
uncooked = Chat::Message.uncooked
rebaked = 0
total = uncooked.count
@ -100,7 +100,7 @@ task "chat:make_channel_to_test_archiving", [:user_for_membership] => :environme
raw: "This is some cool first post for archive stuff",
)
chat_channel =
ChatChannel.create(
Chat::Channel.create(
chatable: topic,
chatable_type: "Topic",
name: "testing channel for archiving #{SecureRandom.hex(4)}",
@ -112,12 +112,13 @@ task "chat:make_channel_to_test_archiving", [:user_for_membership] => :environme
users = [make_test_user, make_test_user, make_test_user]
ChatChannel.transaction do
Chat::Channel.transaction do
start_time = Time.now
puts "creating 1039 messages for the channel"
1039.times do
cm = ChatMessage.new(message: messages.sample, user: users.sample, chat_channel: chat_channel)
cm =
Chat::Message.new(message: messages.sample, user: users.sample, chat_channel: chat_channel)
cm.cook
cm.save!
end
@ -125,7 +126,7 @@ task "chat:make_channel_to_test_archiving", [:user_for_membership] => :environme
puts "message creation done"
puts "took #{Time.now - start_time} seconds"
UserChatChannelMembership.create(
Chat::UserChatChannelMembership.create(
chat_channel: chat_channel,
last_read_message_id: 0,
user: User.find_by(username: user_for_membership),