DEV: fix chat:*:populate rake tasks (#29740)

This commit is contained in:
Régis Hanol 2024-11-13 18:11:25 +01:00 committed by GitHub
parent b4156107df
commit fea82d04ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 26 deletions

View File

@ -5,20 +5,17 @@ require "faker"
module DiscourseDev
class CategoryChannel < Record
def initialize
super(::Chat::CategoryChannel, 5)
def initialize(ignore_current_count: true, count: 5, channel_id: nil)
super(::Chat::CategoryChannel, count)
end
def data
chatable = Category.random
description = Faker::Lorem.sentence
name = Faker::Company.name
created_at = Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now)
{
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),
}
{ chatable:, description:, user_count: 1, name:, created_at: }
end
def create!
@ -30,14 +27,19 @@ module DiscourseDev
if Faker::Boolean.boolean(true_ratio: 0.5)
admin_username =
begin
DiscourseDev::Config.new.config[:admin][:username]
DiscourseDev.config.admin[:username]
rescue StandardError
nil
end
admin_user = ::User.find_by(username: admin_username) if admin_username
admin_user = ::User.find_by_username(admin_username) if admin_username
end
user = admin_user || User.new(username: Faker::Internet.username(specifier: 10)).create!
user =
admin_user ||
::User.create!(
email: Faker::Internet.email,
username: Faker::Internet.username(specifier: 10),
)
Chat::ChannelMembershipManager.new(channel).follow(user)
users << user
end
@ -47,8 +49,10 @@ module DiscourseDev
.times do
Chat::CreateMessage.call(
guardian: users.sample.guardian,
chat_channel_id: channel.id,
message: Faker::Lorem.paragraph,
params: {
chat_channel_id: channel.id,
message: Faker::Lorem.sentence,
},
)
end
end

View File

@ -3,24 +3,16 @@
if Discourse.allow_dev_populate?
desc "Generates sample messages in channels"
task "chat:message:populate", %i[channel_id count] => ["db:load_config"] do |_, args|
DiscourseDev::Message.populate!(
ignore_current_count: true,
channel_id: args[:channel_id],
count: args[:count],
)
DiscourseDev::Message.populate!(channel_id: args[:channel_id], count: args[:count])
end
desc "Generates random channels from categories"
task "chat:category_channel:populate" => ["db:load_config"] do |_, args|
DiscourseDev::CategoryChannel.populate!(ignore_current_count: true)
DiscourseDev::CategoryChannel.populate!
end
desc "Creates a thread with sample messages in a channel"
task "chat:thread:populate", %i[channel_id message_count] => ["db:load_config"] do |_, args|
DiscourseDev::Thread.populate!(
ignore_current_count: true,
channel_id: args[:channel_id],
message_count: args[:message_count],
)
task "chat:thread:populate", %i[channel_id count] => ["db:load_config"] do |_, args|
DiscourseDev::Thread.populate!(channel_id: args[:channel_id], count: args[:count])
end
end