2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-11-10 14:01:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get the customer state of a chat
|
|
|
|
|
|
|
|
chat = Chat.find(123)
|
|
|
|
chat.customer_state(session_id = nil)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
chat_disabled - chat is disabled
|
|
|
|
|
|
|
|
{
|
|
|
|
state: 'chat_disabled'
|
|
|
|
}
|
|
|
|
|
|
|
|
returns (without session_id)
|
|
|
|
|
|
|
|
offline - no agent is online
|
|
|
|
|
|
|
|
{
|
|
|
|
state: 'offline'
|
|
|
|
}
|
|
|
|
|
|
|
|
no_seats_available - no agent is available (all slots are used, max_queue is full)
|
|
|
|
|
|
|
|
{
|
|
|
|
state: 'no_seats_available'
|
|
|
|
}
|
|
|
|
|
|
|
|
online - ready for chats
|
|
|
|
|
|
|
|
{
|
|
|
|
state: 'online'
|
|
|
|
}
|
|
|
|
|
|
|
|
returns (session_id)
|
|
|
|
|
|
|
|
reconnect - position of waiting list for new chat
|
|
|
|
|
|
|
|
{
|
|
|
|
state: 'reconnect',
|
|
|
|
position: chat_session.position,
|
|
|
|
}
|
|
|
|
|
|
|
|
reconnect - chat session already exists, serve agent and session chat messages (to redraw chat history)
|
|
|
|
|
|
|
|
{
|
|
|
|
state: 'reconnect',
|
|
|
|
session: session,
|
|
|
|
agent: user,
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
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
|
2017-11-23 08:09:44 +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'
|
2019-11-21 07:47:05 +00:00
|
|
|
user = chat_session.agent_user
|
|
|
|
if user
|
2015-11-13 14:15:44 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# get queue position 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 {
|
2018-12-19 17:31:51 +00:00
|
|
|
state: 'reconnect',
|
2015-11-13 14:15:44 +00:00
|
|
|
session: session,
|
2018-12-19 17:31:51 +00:00
|
|
|
agent: user,
|
2015-11-13 14:15:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif chat_session.state == 'waiting'
|
2015-11-12 22:58:35 +00:00
|
|
|
return {
|
2018-12-19 17:31:51 +00:00
|
|
|
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
|
2019-11-12 16:06:42 +00:00
|
|
|
if Chat.active_agent_count([id]).zero?
|
2015-11-13 14:15:44 +00:00
|
|
|
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
|
2019-11-12 16:06:42 +00:00
|
|
|
waiting_count = Chat.waiting_chat_count(id)
|
|
|
|
if waiting_count >= max_queue
|
2015-11-13 14:15:44 +00:00
|
|
|
return {
|
|
|
|
state: 'no_seats_available',
|
2019-11-12 16:06:42 +00:00
|
|
|
queue: waiting_count,
|
2015-11-13 14:15:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# seads are available
|
|
|
|
{ state: 'online' }
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get available chat_ids for agent
|
|
|
|
|
|
|
|
chat_ids = Chat.agent_active_chat_ids(User.find(123))
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
[1, 2, 3]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.agent_active_chat_ids(user)
|
|
|
|
return [] if user.preferences[:chat].blank?
|
|
|
|
return [] if user.preferences[:chat][:active].blank?
|
|
|
|
|
|
|
|
chat_ids = []
|
|
|
|
user.preferences[:chat][:active].each do |chat_id, state|
|
|
|
|
next if state != 'on'
|
|
|
|
|
|
|
|
chat_ids.push chat_id.to_i
|
|
|
|
end
|
|
|
|
return [] if chat_ids.blank?
|
|
|
|
|
|
|
|
chat_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get current agent state
|
|
|
|
|
|
|
|
Chat.agent_state(123)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
state: 'chat_disabled'
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
waiting_chat_count: 1,
|
|
|
|
waiting_chat_session_list: [
|
|
|
|
{
|
|
|
|
id: 17,
|
|
|
|
chat_id: 1,
|
|
|
|
session_id: "81e58184385aabe92508eb9233200b5e",
|
|
|
|
name: "",
|
|
|
|
state: "waiting",
|
|
|
|
user_id: nil,
|
|
|
|
preferences: {
|
|
|
|
"url: "http://localhost:3000/chat.html",
|
|
|
|
"participants: ["70332537618180"],
|
|
|
|
"remote_ip: nil,
|
|
|
|
"geo_ip: nil,
|
|
|
|
"dns_name: nil,
|
|
|
|
},
|
|
|
|
updated_by_id: nil,
|
|
|
|
created_by_id: nil,
|
|
|
|
created_at: Thu, 02 May 2019 10:10:45 UTC +00:00,
|
|
|
|
updated_at: Thu, 02 May 2019 10:10:45 UTC +00:00},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
running_chat_count: 0,
|
|
|
|
running_chat_session_list: [],
|
|
|
|
active_agent_count: 2,
|
|
|
|
active_agent_ids: [1, 2],
|
|
|
|
seads_available: 5,
|
|
|
|
seads_total: 15,
|
|
|
|
active: true, # agent is available for chats
|
|
|
|
assets: { ...related assets... },
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
def self.agent_state(user_id)
|
2016-01-05 13:47:41 +00:00
|
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
current_user = User.lookup(id: user_id)
|
|
|
|
return { error: "No such user with id: #{user_id}" } if !current_user
|
|
|
|
|
|
|
|
chat_ids = agent_active_chat_ids(current_user)
|
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
assets = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat.where(active: true).each do |chat|
|
2016-01-05 13:47:41 +00:00
|
|
|
assets = chat.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-11-12 16:06:42 +00:00
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
active_agent_ids = []
|
2019-11-12 16:06:42 +00:00
|
|
|
active_agents(chat_ids).each do |user|
|
2016-01-05 21:15:19 +00:00
|
|
|
active_agent_ids.push user.id
|
|
|
|
assets = user.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-11-12 16:06:42 +00:00
|
|
|
|
|
|
|
running_chat_session_list_local = running_chat_session_list(chat_ids)
|
|
|
|
running_chat_session_list_local.each do |session|
|
2016-01-05 21:15:19 +00:00
|
|
|
next if !session['user_id']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
user = User.lookup(id: session['user_id'])
|
|
|
|
next if !user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
assets = user.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-11-12 16:06:42 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
{
|
2019-11-12 16:06:42 +00:00
|
|
|
waiting_chat_count: waiting_chat_count(chat_ids),
|
|
|
|
waiting_chat_session_list: waiting_chat_session_list(chat_ids),
|
|
|
|
running_chat_count: running_chat_count(chat_ids),
|
|
|
|
running_chat_session_list: running_chat_session_list_local,
|
|
|
|
active_agent_count: active_agent_count(chat_ids),
|
2018-12-19 17:31:51 +00:00
|
|
|
active_agent_ids: active_agent_ids,
|
2019-11-12 16:06:42 +00:00
|
|
|
seads_available: seads_available(chat_ids),
|
|
|
|
seads_total: seads_total(chat_ids),
|
2018-12-19 17:31:51 +00:00
|
|
|
active: Chat::Agent.state(user_id),
|
|
|
|
assets: assets,
|
2016-01-05 13:47:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check if agent is available for chat_ids
|
|
|
|
|
|
|
|
chat_ids = Chat.agent_active_chat?(User.find(123), [1, 2])
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.agent_active_chat?(user, chat_ids)
|
|
|
|
return true if user.preferences[:chat].blank?
|
|
|
|
return true if user.preferences[:chat][:active].blank?
|
|
|
|
|
|
|
|
chat_ids.each do |chat_id|
|
|
|
|
return true if user.preferences[:chat][:active][chat_id] == 'on' || user.preferences[:chat][:active][chat_id.to_s] == 'on'
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
list all active sessins by user_id
|
|
|
|
|
|
|
|
Chat.agent_state_with_sessions(123)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
the same as Chat.agent_state(123) but with the following addition
|
|
|
|
|
|
|
|
active_sessions: [
|
|
|
|
{
|
|
|
|
id: 19,
|
|
|
|
chat_id: 1,
|
|
|
|
session_id: "f28b2704e381c668c9b59215e9481310",
|
|
|
|
name: "",
|
|
|
|
state: "running",
|
|
|
|
user_id: 3,
|
|
|
|
preferences: {
|
|
|
|
url: "http://localhost/chat.html",
|
|
|
|
participants: ["70332475730240", "70332481108980"],
|
|
|
|
remote_ip: nil,
|
|
|
|
geo_ip: nil,
|
|
|
|
dns_name: nil
|
|
|
|
},
|
|
|
|
updated_by_id: nil,
|
|
|
|
created_by_id: nil,
|
|
|
|
created_at: Thu, 02 May 2019 11:48:25 UTC +00:00,
|
|
|
|
updated_at: Thu, 02 May 2019 11:48:29 UTC +00:00,
|
|
|
|
messages: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
def self.agent_state_with_sessions(user_id)
|
2015-11-26 10:12:01 +00:00
|
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
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
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get count if waiting sessions in given chats
|
|
|
|
|
|
|
|
Chat.waiting_chat_count(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.waiting_chat_count(chat_ids)
|
|
|
|
Chat::Session.where(state: ['waiting'], chat_id: chat_ids).count
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
def self.waiting_chat_session_list(chat_ids)
|
2016-01-05 21:15:19 +00:00
|
|
|
sessions = []
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat::Session.where(state: ['waiting'], chat_id: chat_ids).each do |session|
|
2016-01-05 21:15:19 +00:00
|
|
|
sessions.push session.attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-05 21:15:19 +00:00
|
|
|
sessions
|
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get count running sessions in given chats
|
|
|
|
|
|
|
|
Chat.running_chat_count(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.running_chat_count(chat_ids)
|
|
|
|
Chat::Session.where(state: ['running'], chat_id: chat_ids).count
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
def self.running_chat_session_list(chat_ids)
|
2016-01-05 21:15:19 +00:00
|
|
|
sessions = []
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat::Session.where(state: ['running'], chat_id: chat_ids).each do |session|
|
2016-01-05 21:15:19 +00:00
|
|
|
sessions.push session.attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-05 21:15:19 +00:00
|
|
|
sessions
|
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get count of active sessions in given chats
|
|
|
|
|
|
|
|
Chat.active_chat_count(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.active_chat_count(chat_ids)
|
|
|
|
Chat::Session.where(state: %w[waiting running], chat_id: chat_ids).count
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get user agents with concurrent
|
|
|
|
|
|
|
|
Chat.available_agents_with_concurrent(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
123: 5,
|
|
|
|
124: 1,
|
|
|
|
125: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.available_agents_with_concurrent(chat_ids, diff = 2.minutes)
|
2015-11-10 14:01:04 +00:00
|
|
|
agents = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
|
2019-11-12 16:06:42 +00:00
|
|
|
user = User.lookup(id: record.updated_by_id)
|
|
|
|
next if !user
|
|
|
|
next if !agent_active_chat?(user, chat_ids)
|
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
agents[record.updated_by_id] = record.concurrent
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-11-10 14:01:04 +00:00
|
|
|
agents
|
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get count of active agents in given chats
|
|
|
|
|
|
|
|
Chat.active_agent_count(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.active_agent_count(chat_ids, diff = 2.minutes)
|
|
|
|
count = 0
|
|
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
|
|
|
|
user = User.lookup(id: record.updated_by_id)
|
|
|
|
next if !user
|
|
|
|
next if !agent_active_chat?(user, chat_ids)
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
count
|
2015-11-16 10:44:13 +00:00
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get active agents in given chats
|
|
|
|
|
|
|
|
Chat.active_agent_count(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
[User.find(123), User.find(345)]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.active_agents(chat_ids, diff = 2.minutes)
|
2016-01-05 21:15:19 +00:00
|
|
|
users = []
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
|
2016-01-05 21:15:19 +00:00
|
|
|
user = User.lookup(id: record.updated_by_id)
|
|
|
|
next if !user
|
2019-11-12 16:06:42 +00:00
|
|
|
next if !agent_active_chat?(user, chat_ids)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-05 21:15:19 +00:00
|
|
|
users.push user
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-05 21:15:19 +00:00
|
|
|
users
|
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get count all possible seads (possible chat sessions) in given chats
|
|
|
|
|
|
|
|
Chat.seads_total(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.seads_total(chat_ids, diff = 2.minutes)
|
2015-11-10 14:01:04 +00:00
|
|
|
total = 0
|
2019-11-12 16:06:42 +00:00
|
|
|
available_agents_with_concurrent(chat_ids, diff).each_value do |concurrent|
|
2015-11-10 14:01:04 +00:00
|
|
|
total += concurrent
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-11-10 14:01:04 +00:00
|
|
|
total
|
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get count all available seads (available chat sessions) in given chats
|
|
|
|
|
|
|
|
Chat.seads_available(chat_ids)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.seads_available(chat_ids, diff = 2.minutes)
|
|
|
|
seads_total(chat_ids, diff) - active_chat_count(chat_ids)
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
2015-12-10 10:35:00 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-01-06 00:45:03 +00:00
|
|
|
broadcast new agent status to all agents
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_agent_state_update(chat_ids)
|
2016-01-06 00:45:03 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
optional you can ignore it for dedicated user
|
2016-01-06 00:45:03 +00:00
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_agent_state_update(chat_ids, ignore_user_id)
|
2016-01-06 00:45:03 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
def self.broadcast_agent_state_update(chat_ids, ignore_user_id = nil)
|
2016-01-06 00:45:03 +00:00
|
|
|
|
|
|
|
# send broadcast to agents
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Agent.where('active = ? OR updated_at > ?', true, Time.zone.now - 8.hours).each do |item|
|
2016-01-06 00:45:03 +00:00
|
|
|
next if item.updated_by_id == ignore_user_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
user = User.lookup(id: item.updated_by_id)
|
|
|
|
next if !user
|
|
|
|
next if !agent_active_chat?(user, chat_ids)
|
|
|
|
|
2016-01-06 00:45:03 +00:00
|
|
|
data = {
|
|
|
|
event: 'chat_status_agent',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: Chat.agent_state(item.updated_by_id),
|
2016-01-06 00:45:03 +00:00
|
|
|
}
|
|
|
|
Sessions.send_to(item.updated_by_id, data)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-06 00:45:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
broadcast new customer queue position to all waiting customers
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_customer_state_update(chat_id)
|
2016-01-06 00:45:03 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
def self.broadcast_customer_state_update(chat_id)
|
2016-01-06 00:45:03 +00:00
|
|
|
|
|
|
|
# send position update to other waiting sessions
|
|
|
|
position = 0
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat::Session.where(state: 'waiting', chat_id: chat_id).order(created_at: :asc).each do |local_chat_session|
|
2016-01-06 00:45:03 +00:00
|
|
|
position += 1
|
|
|
|
data = {
|
|
|
|
event: 'chat_session_queue',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
|
|
|
state: 'queue',
|
|
|
|
position: position,
|
2016-01-06 00:45:03 +00:00
|
|
|
session_id: local_chat_session.session_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
local_chat_session.send_to_recipients(data)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-06 00:45:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-12-10 10:35:00 +00:00
|
|
|
cleanup old chat messages
|
|
|
|
|
|
|
|
Chat.cleanup
|
|
|
|
|
2016-08-17 11:24:51 +00:00
|
|
|
optional you can put the max oldest chat entries
|
2015-12-10 10:35:00 +00:00
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.cleanup(12.months)
|
2015-12-10 10:35:00 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2019-11-12 16:06:42 +00:00
|
|
|
def self.cleanup(diff = 12.months)
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Session.where(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each do |chat_session|
|
2015-12-10 10:35:00 +00:00
|
|
|
Chat::Message.where(chat_session_id: chat_session.id).delete_all
|
|
|
|
chat_session.destroy
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-10 10:35:00 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
close chat sessions where participants are offline
|
2015-12-10 10:35:00 +00:00
|
|
|
|
|
|
|
Chat.cleanup_close
|
|
|
|
|
2016-08-17 11:24:51 +00:00
|
|
|
optional you can put the max oldest chat sessions as argument
|
2015-12-10 10:35:00 +00:00
|
|
|
|
|
|
|
Chat.cleanup_close(5.minutes)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.cleanup_close(diff = 5.minutes)
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Session.where.not(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each do |chat_session|
|
2015-12-10 10:37:02 +00:00
|
|
|
next if chat_session.recipients_active?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-10 10:35:00 +00:00
|
|
|
chat_session.state = 'closed'
|
|
|
|
chat_session.save
|
|
|
|
message = {
|
|
|
|
event: 'chat_session_closed',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2015-12-10 10:35:00 +00:00
|
|
|
session_id: chat_session.session_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
realname: 'System',
|
2015-12-10 10:35:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
chat_session.send_to_recipients(message)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-10 10:35:00 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-01-28 23:52:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check if ip address is blocked for chat
|
|
|
|
|
|
|
|
chat = Chat.find(123)
|
|
|
|
chat.blocked_ip?(ip)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def blocked_ip?(ip)
|
|
|
|
return false if ip.blank?
|
|
|
|
return false if block_ip.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-01-28 23:52:02 +00:00
|
|
|
ips = block_ip.split(';')
|
|
|
|
ips.each do |local_ip|
|
|
|
|
return true if ip == local_ip.strip
|
|
|
|
return true if ip.match?(/#{local_ip.strip.gsub(/\*/, '.+?')}/)
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check if country is blocked for chat
|
|
|
|
|
|
|
|
chat = Chat.find(123)
|
|
|
|
chat.blocked_country?(ip)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def blocked_country?(ip)
|
|
|
|
return false if ip.blank?
|
2018-01-31 13:04:53 +00:00
|
|
|
return false if block_country.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-01-28 23:52:02 +00:00
|
|
|
geo_ip = Service::GeoIp.location(ip)
|
|
|
|
return false if geo_ip.blank?
|
|
|
|
return false if geo_ip['country_code'].blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-01-28 23:52:02 +00:00
|
|
|
countries = block_country.split(';')
|
|
|
|
countries.each do |local_country|
|
|
|
|
return true if geo_ip['country_code'] == local_country
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|