2015-11-11 13:10:26 +00:00
|
|
|
class Sessions::Event::ChatSessionInit < Sessions::Event::ChatBase
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
def run
|
2015-11-10 14:01:04 +00:00
|
|
|
|
|
|
|
chat_id = 1
|
|
|
|
chat = Chat.find_by(id: chat_id)
|
|
|
|
if !chat
|
|
|
|
return {
|
|
|
|
event: 'chat_session_init',
|
|
|
|
data: {
|
|
|
|
state: 'no_such_chat',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# create chat session
|
|
|
|
chat_session = Chat::Session.create(
|
|
|
|
chat_id: chat_id,
|
|
|
|
name: '',
|
|
|
|
state: 'waiting',
|
|
|
|
preferences: {
|
2015-11-11 13:10:26 +00:00
|
|
|
participants: [@client_id],
|
2015-11-10 14:01:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
# send broadcast to agents
|
|
|
|
Chat::Agent.where(active: true).each {|item|
|
2015-11-10 14:01:04 +00:00
|
|
|
data = {
|
|
|
|
event: 'chat_status_agent',
|
2015-11-11 13:10:26 +00:00
|
|
|
data: Chat.agent_state(item.updated_by_id),
|
2015-11-10 14:01:04 +00:00
|
|
|
}
|
2015-11-11 13:10:26 +00:00
|
|
|
Sessions.send_to(item.updated_by_id, data)
|
2015-11-10 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# return new session
|
|
|
|
{
|
2015-11-12 13:19:41 +00:00
|
|
|
event: 'chat_session_queue',
|
2015-11-10 14:01:04 +00:00
|
|
|
data: {
|
|
|
|
state: 'queue',
|
|
|
|
position: Chat.waiting_chat_count,
|
|
|
|
session_id: chat_session.session_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|