2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'app/channels/chat_channel.rb', line 2
def subscribed
authorize_user!
chat_id = params[:chat_id]
stream_from "chat_channel_#{chat_id}"
chat = if @current_user.system_record
Chat.find_by!(chat_id: chat_id)
else
Chat.find_by!(chat_id: chat_id, company_id: @current_company_id)
end
chat_messages = chat.chat_messages
.select(
:content,
:sender,
:status,
:chat_message_id,
:chat_id,
:created_at,
:updated_at,
:completion_model_name,
:completion_cost,
:completion_time,
:completion_latency,
:speed
)
.order(chat_message_id: :desc)
.limit(100)
.reverse
data = chat.as_json
data[:chat_messages] = chat_messages
ActionCable.server.broadcast(
"chat_channel_#{chat_id}",
{
message: 'all_messages',
data: data
}
)
end
|