2015-11-10 14:01:04 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Chat < ApplicationModel
|
2015-11-16 10:44:13 +00:00
|
|
|
validates :name, presence: true
|
2015-11-17 15:39:46 +00:00
|
|
|
store :preferences
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2015-11-12 09:39:14 +00:00
|
|
|
def customer_state(session_id = nil)
|
2015-11-26 10:12:01 +00:00
|
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2015-11-12 22:58:35 +00:00
|
|
|
# reconnect
|
2015-11-12 09:39:14 +00:00
|
|
|
if session_id
|
2015-11-12 22:58:35 +00:00
|
|
|
chat_session = Chat::Session.find_by(session_id: session_id, state: %w(waiting running))
|
2015-11-12 15:16:01 +00:00
|
|
|
|
2015-11-12 22:58:35 +00:00
|
|
|
if chat_session
|
2015-11-13 14:15:44 +00:00
|
|
|
if chat_session.state == 'running'
|
|
|
|
user = nil
|
|
|
|
if chat_session.user_id
|
2015-11-30 10:50:02 +00:00
|
|
|
chat_user = User.lookup(id: chat_session.user_id)
|
2015-11-13 14:15:44 +00:00
|
|
|
url = nil
|
|
|
|
if chat_user.image && chat_user.image != 'none'
|
2015-11-25 12:24:17 +00:00
|
|
|
url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/users/image/#{chat_user.image}"
|
2015-11-13 14:15:44 +00:00
|
|
|
end
|
|
|
|
user = {
|
|
|
|
name: chat_user.fullname,
|
|
|
|
avatar: url,
|
|
|
|
}
|
|
|
|
|
|
|
|
# get queue postion if needed
|
2015-11-13 16:03:58 +00:00
|
|
|
session = Chat::Session.messages_by_session_id(session_id)
|
2015-11-13 14:15:44 +00:00
|
|
|
if session
|
|
|
|
return {
|
|
|
|
state: 'reconnect',
|
|
|
|
session: session,
|
|
|
|
agent: user,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif chat_session.state == 'waiting'
|
2015-11-12 22:58:35 +00:00
|
|
|
return {
|
|
|
|
state: 'reconnect',
|
2015-11-13 14:15:44 +00:00
|
|
|
position: chat_session.position,
|
2015-11-12 22:58:35 +00:00
|
|
|
}
|
|
|
|
end
|
2015-11-12 15:16:01 +00:00
|
|
|
end
|
2015-11-12 09:39:14 +00:00
|
|
|
end
|
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
# check if agents are available
|
|
|
|
available_agents = Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - 2.minutes).count
|
|
|
|
if available_agents == 0
|
|
|
|
return { state: 'offline' }
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
# if all seads are used
|
2015-11-25 09:33:39 +00:00
|
|
|
if Chat.waiting_chat_count >= max_queue
|
2015-11-13 14:15:44 +00:00
|
|
|
return {
|
|
|
|
state: 'no_seats_available',
|
2015-11-25 09:33:39 +00:00
|
|
|
queue: Chat.waiting_chat_count,
|
2015-11-13 14:15:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# seads are available
|
|
|
|
{ state: 'online' }
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.agent_state(user_id)
|
2016-01-05 13:47:41 +00:00
|
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
|
|
|
assets = {}
|
|
|
|
Chat.where(active: true).each {|chat|
|
|
|
|
assets = chat.assets(assets)
|
|
|
|
}
|
2016-01-05 21:15:19 +00:00
|
|
|
active_agent_ids = []
|
|
|
|
active_agents.each {|user|
|
|
|
|
active_agent_ids.push user.id
|
|
|
|
assets = user.assets(assets)
|
|
|
|
}
|
|
|
|
runningchat_session_list_local = running_chat_session_list
|
|
|
|
runningchat_session_list_local.each {|session|
|
|
|
|
next if !session['user_id']
|
|
|
|
user = User.lookup(id: session['user_id'])
|
|
|
|
next if !user
|
|
|
|
assets = user.assets(assets)
|
|
|
|
}
|
2016-01-05 13:47:41 +00:00
|
|
|
{
|
|
|
|
waiting_chat_count: waiting_chat_count,
|
2016-01-05 21:15:19 +00:00
|
|
|
waiting_chat_session_list: waiting_chat_session_list,
|
2016-01-05 13:47:41 +00:00
|
|
|
running_chat_count: running_chat_count,
|
2016-01-05 21:15:19 +00:00
|
|
|
running_chat_session_list: runningchat_session_list_local,
|
|
|
|
active_agent_count: active_agent_count,
|
|
|
|
active_agent_ids: active_agent_ids,
|
2016-01-05 13:47:41 +00:00
|
|
|
seads_available: seads_available,
|
|
|
|
seads_total: seads_total,
|
|
|
|
active: Chat::Agent.state(user_id),
|
|
|
|
assets: assets,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.agent_state_with_sessions(user_id)
|
2015-11-26 10:12:01 +00:00
|
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
2016-01-05 21:15:19 +00:00
|
|
|
result = agent_state(user_id)
|
|
|
|
result[:active_sessions] = Chat::Session.active_chats_by_user_id(user_id)
|
|
|
|
result
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.waiting_chat_count
|
|
|
|
Chat::Session.where(state: ['waiting']).count
|
|
|
|
end
|
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
def self.waiting_chat_session_list
|
|
|
|
sessions = []
|
|
|
|
Chat::Session.where(state: ['waiting']).each {|session|
|
|
|
|
sessions.push session.attributes
|
|
|
|
}
|
|
|
|
sessions
|
|
|
|
end
|
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
def self.running_chat_count
|
2015-11-11 20:44:54 +00:00
|
|
|
Chat::Session.where(state: ['running']).count
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
def self.running_chat_session_list
|
|
|
|
sessions = []
|
|
|
|
Chat::Session.where(state: ['running']).each {|session|
|
|
|
|
sessions.push session.attributes
|
|
|
|
}
|
|
|
|
sessions
|
|
|
|
end
|
|
|
|
|
2015-11-13 16:03:58 +00:00
|
|
|
def self.active_chat_count
|
2015-11-10 14:01:04 +00:00
|
|
|
Chat::Session.where(state: %w(waiting running)).count
|
|
|
|
end
|
|
|
|
|
2015-11-13 16:03:58 +00:00
|
|
|
def self.available_agents(diff = 2.minutes)
|
2015-11-10 14:01:04 +00:00
|
|
|
agents = {}
|
|
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each {|record|
|
|
|
|
agents[record.updated_by_id] = record.concurrent
|
|
|
|
}
|
|
|
|
agents
|
|
|
|
end
|
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
def self.active_agent_count(diff = 2.minutes)
|
2015-11-16 10:44:13 +00:00
|
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).count
|
|
|
|
end
|
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
def self.active_agents(diff = 2.minutes)
|
|
|
|
users = []
|
|
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each {|record|
|
|
|
|
user = User.lookup(id: record.updated_by_id)
|
|
|
|
next if !user
|
|
|
|
users.push user
|
|
|
|
}
|
|
|
|
users
|
|
|
|
end
|
|
|
|
|
2015-11-13 16:03:58 +00:00
|
|
|
def self.seads_total(diff = 2.minutes)
|
2015-11-10 14:01:04 +00:00
|
|
|
total = 0
|
2015-11-13 16:03:58 +00:00
|
|
|
available_agents(diff).each {|_user_id, concurrent|
|
2015-11-10 14:01:04 +00:00
|
|
|
total += concurrent
|
|
|
|
}
|
|
|
|
total
|
|
|
|
end
|
|
|
|
|
2015-11-13 16:03:58 +00:00
|
|
|
def self.seads_available(diff = 2.minutes)
|
2015-11-10 14:01:04 +00:00
|
|
|
seads_total(diff) - active_chat_count
|
|
|
|
end
|
2015-12-10 10:35:00 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
cleanup old chat messages
|
|
|
|
|
|
|
|
Chat.cleanup
|
|
|
|
|
|
|
|
optional you can parse the max oldest chat entries
|
|
|
|
|
|
|
|
Chat.cleanup(3.months)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.cleanup(diff = 3.months)
|
|
|
|
Chat::Session.where(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each {|chat_session|
|
|
|
|
Chat::Message.where(chat_session_id: chat_session.id).delete_all
|
|
|
|
chat_session.destroy
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
close chat sessions where participients are offline
|
|
|
|
|
|
|
|
Chat.cleanup_close
|
|
|
|
|
|
|
|
optional you can parse the max oldest chat sessions
|
|
|
|
|
|
|
|
Chat.cleanup_close(5.minutes)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.cleanup_close(diff = 5.minutes)
|
|
|
|
Chat::Session.where.not(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each {|chat_session|
|
2015-12-10 10:37:02 +00:00
|
|
|
next if chat_session.recipients_active?
|
2015-12-10 10:35:00 +00:00
|
|
|
chat_session.state = 'closed'
|
|
|
|
chat_session.save
|
|
|
|
message = {
|
|
|
|
event: 'chat_session_closed',
|
|
|
|
data: {
|
|
|
|
session_id: chat_session.session_id,
|
|
|
|
realname: 'System',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
chat_session.send_to_recipients(message)
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|