discourse/plugins/chat/lib/discourse_dev/thread.rb
Loïc Guitaut 8dd303826e
DEV: Refactor Chat::ListChannelThreadMessages a bit (#33380)
- Introduce a `max_page_size` option, allowing different behavior
between controllers and SDK.
- Improve the contract (validations & helper method).
- Use `model` where possible.
- Extract message existence logic to a dedicated policy, allowing easier
testing.
- Refactor specs to follow current guidelines/best practices.
2025-08-08 14:05:38 +02:00

66 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require "discourse_dev/record"
require "faker"
module DiscourseDev
class Thread < Record
def initialize(channel_id:, count: nil, ignore_current_count: false)
@channel_id = channel_id
@message_count = count&.to_i || 30
@ignore_current_count = ignore_current_count
super(::Chat::Thread, 1)
end
def data
if @channel_id
channel = ::Chat::Channel.find(@channel_id)
else
channel = ::Chat::Channel.where(chatable_type: "Category").order("RANDOM()").first
end
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::CreateMessage.call(
guardian: user.guardian,
params: {
message: Faker::Lorem.paragraph,
chat_channel_id: channel.id,
},
).message_instance
{ 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::CreateMessage.call(
{
guardian: user.guardian,
chat_channel_id: thread.channel_id,
message: Faker::Lorem.paragraph,
thread_id: thread.id,
},
)
end
end
end
end
end