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

View File

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