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

@ -44,7 +44,7 @@ describe "API keys scoped to chat#create_message" do
end
it "can create chat messages" do
UserChatChannelMembership.create(user: admin, chat_channel: chat_channel, following: true)
Chat::UserChatChannelMembership.create(user: admin, chat_channel: chat_channel, following: true)
expect {
post "/chat/#{chat_channel.id}.json",
headers: {
@ -54,12 +54,12 @@ describe "API keys scoped to chat#create_message" do
params: {
message: "asdfasdf asdfasdf",
}
}.to change { ChatMessage.where(chat_channel: chat_channel).count }.by(1)
}.to change { Chat::Message.where(chat_channel: chat_channel).count }.by(1)
expect(response.status).to eq(200)
end
it "cannot post in a channel it is not scoped for" do
UserChatChannelMembership.create(user: admin, chat_channel: chat_channel, following: true)
Chat::UserChatChannelMembership.create(user: admin, chat_channel: chat_channel, following: true)
expect {
post "/chat/#{chat_channel.id}.json",
headers: {
@ -69,12 +69,16 @@ describe "API keys scoped to chat#create_message" do
params: {
message: "asdfasdf asdfasdf",
}
}.not_to change { ChatMessage.where(chat_channel: chat_channel).count }
}.not_to change { Chat::Message.where(chat_channel: chat_channel).count }
expect(response.status).to eq(403)
end
it "can only post in scoped channels" do
UserChatChannelMembership.create(user: admin, chat_channel: chat_channel_2, following: true)
Chat::UserChatChannelMembership.create(
user: admin,
chat_channel: chat_channel_2,
following: true,
)
expect {
post "/chat/#{chat_channel_2.id}.json",
headers: {
@ -84,7 +88,7 @@ describe "API keys scoped to chat#create_message" do
params: {
message: "asdfasdf asdfasdf",
}
}.to change { ChatMessage.where(chat_channel: chat_channel_2).count }.by(1)
}.to change { Chat::Message.where(chat_channel: chat_channel_2).count }.by(1)
expect(response.status).to eq(200)
end
end

View File

@ -220,14 +220,14 @@ martin</div>
message1 = Fabricate(:chat_message, chat_channel: channel, user: post.user)
message2 = Fabricate(:chat_message, chat_channel: channel, user: post.user)
md =
ChatTranscriptService.new(
Chat::TranscriptService.new(
channel,
message2.user,
messages_or_ids: [message2.id],
).generate_markdown
message1.update!(message: md)
md_for_post =
ChatTranscriptService.new(
Chat::TranscriptService.new(
channel,
message1.user,
messages_or_ids: [message1.id],