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,31 +0,0 @@
# frozen_string_literal: true
require "discourse_dev/record"
require "faker"
module DiscourseDev
class DirectChannel < Record
def initialize
super(::DirectMessage, 5)
end
def data
if Faker::Boolean.boolean(true_ratio: 0.5)
admin_username =
begin
DiscourseDev::Config.new.config[:admin][:username]
rescue StandardError
nil
end
admin_user = ::User.find_by(username: admin_username) if admin_username
end
[User.new.create!, admin_user || User.new.create!]
end
def create!
users = data
Chat::DirectMessageChannelCreator.create!(acting_user: users[0], target_users: users)
end
end
end

View File

@ -1,30 +0,0 @@
# frozen_string_literal: true
require "discourse_dev/record"
require "faker"
module DiscourseDev
class Message < Record
def initialize
super(::ChatMessage, 200)
end
def data
if Faker::Boolean.boolean(true_ratio: 0.5)
channel = ::ChatChannel.where(chatable_type: "DirectMessage").order("RANDOM()").first
channel.user_chat_channel_memberships.update_all(following: true)
user = channel.chatable.users.order("RANDOM()").first
else
membership = ::UserChatChannelMembership.order("RANDOM()").first
channel = membership.chat_channel
user = membership.user
end
{ user: user, content: Faker::Lorem.paragraph, chat_channel: channel }
end
def create!
Chat::ChatMessageCreator.create(data)
end
end
end

View File

@ -1,44 +0,0 @@
# frozen_string_literal: true
require "discourse_dev/record"
require "faker"
module DiscourseDev
class PublicChannel < Record
def initialize
super(::CategoryChannel, 5)
end
def data
chatable = Category.random
{
chatable: chatable,
description: Faker::Lorem.paragraph,
user_count: 1,
name: Faker::Company.name,
created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
}
end
def create!
super do |channel|
Faker::Number
.between(from: 5, to: 10)
.times do
if Faker::Boolean.boolean(true_ratio: 0.5)
admin_username =
begin
DiscourseDev::Config.new.config[:admin][:username]
rescue StandardError
nil
end
admin_user = ::User.find_by(username: admin_username) if admin_username
end
Chat::ChatChannelMembershipManager.new(channel).follow(admin_user || User.new.create!)
end
end
end
end
end