2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
class Sessions::Event::ChatSessionStart < Sessions::Event::ChatBase
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2019-11-21 07:47:05 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
a agent start`s a new chat session
|
|
|
|
|
|
|
|
payload
|
|
|
|
|
|
|
|
{
|
|
|
|
event: 'chat_session_start',
|
|
|
|
data: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
return is sent as message back to peer
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
def run
|
2015-12-09 13:09:37 +00:00
|
|
|
return super if super
|
2016-08-12 16:39:09 +00:00
|
|
|
return if !permission_check('chat.agent', 'chat')
|
2015-11-10 14:01:04 +00:00
|
|
|
|
|
|
|
# find first in waiting list
|
2019-11-12 16:06:42 +00:00
|
|
|
chat_user = User.lookup(id: @session['id'])
|
|
|
|
chat_ids = Chat.agent_active_chat_ids(chat_user)
|
2019-12-03 06:29:02 +00:00
|
|
|
chat_session = if @payload['chat_id']
|
|
|
|
Chat::Session.where(state: 'waiting', chat_id: @payload['chat_id']).order(created_at: :asc).first
|
|
|
|
else
|
|
|
|
Chat::Session.where(state: 'waiting', chat_id: chat_ids).order(created_at: :asc).first
|
|
|
|
end
|
2015-11-10 14:01:04 +00:00
|
|
|
if !chat_session
|
|
|
|
return {
|
|
|
|
event: 'chat_session_start',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
|
|
|
state: 'failed',
|
2015-11-10 14:01:04 +00:00
|
|
|
message: 'No session available.',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2019-11-12 16:06:42 +00:00
|
|
|
chat_session.user_id = chat_user.id
|
2015-11-10 14:01:04 +00:00
|
|
|
chat_session.state = 'running'
|
2015-11-11 13:10:26 +00:00
|
|
|
chat_session.preferences[:participants] = chat_session.add_recipient(@client_id)
|
2015-11-10 14:01:04 +00:00
|
|
|
chat_session.save
|
|
|
|
|
2019-12-03 06:29:02 +00:00
|
|
|
session_attributes = chat_session.attributes
|
|
|
|
session_attributes['messages'] = []
|
|
|
|
Chat::Message.where(chat_session_id: chat_session.id).order(created_at: :asc).each do |message|
|
|
|
|
session_attributes['messages'].push message.attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
# send chat_session_init to customer client
|
|
|
|
if session_attributes['messages'].blank?
|
|
|
|
user = chat_session.agent_user
|
|
|
|
data = {
|
|
|
|
event: 'chat_session_start',
|
|
|
|
data: {
|
|
|
|
state: 'ok',
|
|
|
|
agent: user,
|
|
|
|
session_id: chat_session.session_id,
|
|
|
|
chat_id: chat_session.chat_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
chat_session.send_to_recipients(data, @client_id)
|
|
|
|
end
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
# send to agent
|
|
|
|
data = {
|
|
|
|
event: 'chat_session_start',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2019-12-03 06:29:02 +00:00
|
|
|
session: session_attributes,
|
2016-01-05 13:47:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, data)
|
2015-11-12 13:19:41 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
# send state update with sessions to agents
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_agent_state_update([chat_session.chat_id])
|
2015-11-12 22:56:56 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
# send position update to other waiting sessions
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_customer_state_update(chat_session.chat_id)
|
2016-01-05 13:47:41 +00:00
|
|
|
|
2015-11-12 13:19:41 +00:00
|
|
|
nil
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|