Class: ChatChannel

Inherits:
ApplicationCable::Channel show all
Defined in:
app/channels/chat_channel.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#subscribedObject



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

#unsubscribedObject



44
45
46
# File 'app/channels/chat_channel.rb', line 44

def unsubscribed
  # Any cleanup needed when channel is unsubscribed
end