mirror of
https://github.com/discourse/discourse.git
synced 2024-12-24 10:33:46 +08:00
243793ec6e
Currently, the logic for creating a new chat message is scattered between a controller and an “old” service. This patch address this issue by creating a new service (using the “new” sevice object system) encapsulating all the necessary logic. (authorization, publishing events, etc.)
35 lines
886 B
Ruby
35 lines
886 B
Ruby
# frozen_string_literal: true
|
|
|
|
require "discourse_dev/record"
|
|
require "faker"
|
|
|
|
module DiscourseDev
|
|
class Message < Record
|
|
def initialize(channel_id: nil, count: nil, ignore_current_count: false)
|
|
@channel_id = channel_id
|
|
@ignore_current_count = ignore_current_count
|
|
super(::Chat::Message, count&.to_i || 200)
|
|
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
|
|
|
|
membership =
|
|
::Chat::UserChatChannelMembership.where(chat_channel: channel).order("RANDOM()").first
|
|
user = membership.user
|
|
|
|
{ guardian: user.guardian, message: Faker::Lorem.paragraph, chat_channel_id: channel.id }
|
|
end
|
|
|
|
def create!
|
|
Chat::CreateMessage.call(data)
|
|
end
|
|
end
|
|
end
|