discourse/plugins/chat/lib/discourse_dev/thread.rb
Martin Brennan 6442bbf46c
DEV: Reintroduce chat rake dev generate tasks (#21164)
This is to help generate random channels and chat
messages for local dev. This was removed in 12a18d4d55
presumably because it was not worth refactoring at the
time.

I've only added these tasks:

- `rake chat:message:populate\[113,20\]` (channel_id, count)
  - Generates the count of messages for a channel ID provided,
    otherwise uses a random channel and 200 count.
- `rake chat:category_channel:populate`
  - Creates a chat channel for a random category.
- `rake chat🧵populate\[132,5\]` (channel_id, message_count)
  - Creates a thread with N messages in the specified channel,
    and enables threading in that channel if necessary
2023-04-20 10:53:10 +10:00

65 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require "discourse_dev/record"
require "faker"
module DiscourseDev
class Thread < Record
def initialize(channel_id:, message_count: nil, ignore_current_count: false)
@channel_id = channel_id
@message_count = message_count&.to_i || 30
@ignore_current_count = ignore_current_count
super(::Chat::Thread, 1)
end
def data
if !SiteSetting.enable_experimental_chat_threaded_discussions
raise "You need to enable_experimental_chat_threaded_discussions to run this task"
end
channel = ::Chat::Channel.find(@channel_id)
return if !channel
if !channel.threading_enabled
puts "Enabling threads in channel #{channel.id}"
channel.update!(threading_enabled: true)
end
membership =
::Chat::UserChatChannelMembership.where(chat_channel: channel).order("RANDOM()").first
user = membership.user
om =
Chat::MessageCreator.create(
user: user,
content: Faker::Lorem.paragraph,
chat_channel: channel,
).chat_message
{ original_message_user: user, original_message: om, channel: channel }
end
def create!
super do |thread|
thread.original_message.update!(thread: thread)
user =
::Chat::UserChatChannelMembership
.where(chat_channel: thread.channel)
.order("RANDOM()")
.first
.user
@message_count.times do
Chat::MessageCreator.create(
{
user: user,
chat_channel: thread.channel,
content: Faker::Lorem.paragraph,
thread_id: thread.id,
},
)
end
end
end
end
end