|
@@ -4,6 +4,60 @@ class Chat < ApplicationModel
|
|
validates :name, presence: true
|
|
validates :name, presence: true
|
|
store :preferences
|
|
store :preferences
|
|
|
|
|
|
|
|
+=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
|
|
|
|
+
|
|
def customer_state(session_id = nil)
|
|
def customer_state(session_id = nil)
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
|
|
|
|
|
@@ -45,16 +99,16 @@ class Chat < ApplicationModel
|
|
end
|
|
end
|
|
|
|
|
|
# check if agents are available
|
|
# check if agents are available
|
|
- available_agents = Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - 2.minutes).count
|
|
|
|
- if available_agents.zero?
|
|
|
|
|
|
+ if Chat.active_agent_count([id]).zero?
|
|
return { state: 'offline' }
|
|
return { state: 'offline' }
|
|
end
|
|
end
|
|
|
|
|
|
# if all seads are used
|
|
# if all seads are used
|
|
- if Chat.waiting_chat_count >= max_queue
|
|
|
|
|
|
+ waiting_count = Chat.waiting_chat_count(id)
|
|
|
|
+ if waiting_count >= max_queue
|
|
return {
|
|
return {
|
|
state: 'no_seats_available',
|
|
state: 'no_seats_available',
|
|
- queue: Chat.waiting_chat_count,
|
|
|
|
|
|
+ queue: waiting_count,
|
|
}
|
|
}
|
|
end
|
|
end
|
|
|
|
|
|
@@ -62,20 +116,101 @@ class Chat < ApplicationModel
|
|
{ state: 'online' }
|
|
{ state: 'online' }
|
|
end
|
|
end
|
|
|
|
|
|
|
|
+=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
|
|
|
|
+
|
|
def self.agent_state(user_id)
|
|
def self.agent_state(user_id)
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
|
|
|
|
|
|
|
+ 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)
|
|
|
|
+
|
|
assets = {}
|
|
assets = {}
|
|
Chat.where(active: true).each do |chat|
|
|
Chat.where(active: true).each do |chat|
|
|
assets = chat.assets(assets)
|
|
assets = chat.assets(assets)
|
|
end
|
|
end
|
|
|
|
+
|
|
active_agent_ids = []
|
|
active_agent_ids = []
|
|
- active_agents.each do |user|
|
|
|
|
|
|
+ active_agents(chat_ids).each do |user|
|
|
active_agent_ids.push user.id
|
|
active_agent_ids.push user.id
|
|
assets = user.assets(assets)
|
|
assets = user.assets(assets)
|
|
end
|
|
end
|
|
- runningchat_session_list_local = running_chat_session_list
|
|
|
|
- runningchat_session_list_local.each do |session|
|
|
|
|
|
|
+
|
|
|
|
+ running_chat_session_list_local = running_chat_session_list(chat_ids)
|
|
|
|
+ running_chat_session_list_local.each do |session|
|
|
next if !session['user_id']
|
|
next if !session['user_id']
|
|
|
|
|
|
user = User.lookup(id: session['user_id'])
|
|
user = User.lookup(id: session['user_id'])
|
|
@@ -83,20 +218,78 @@ class Chat < ApplicationModel
|
|
|
|
|
|
assets = user.assets(assets)
|
|
assets = user.assets(assets)
|
|
end
|
|
end
|
|
|
|
+
|
|
{
|
|
{
|
|
- waiting_chat_count: waiting_chat_count,
|
|
|
|
- waiting_chat_session_list: waiting_chat_session_list,
|
|
|
|
- running_chat_count: running_chat_count,
|
|
|
|
- running_chat_session_list: runningchat_session_list_local,
|
|
|
|
- active_agent_count: active_agent_count,
|
|
|
|
|
|
+ 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),
|
|
active_agent_ids: active_agent_ids,
|
|
active_agent_ids: active_agent_ids,
|
|
- seads_available: seads_available,
|
|
|
|
- seads_total: seads_total,
|
|
|
|
|
|
+ seads_available: seads_available(chat_ids),
|
|
|
|
+ seads_total: seads_total(chat_ids),
|
|
active: Chat::Agent.state(user_id),
|
|
active: Chat::Agent.state(user_id),
|
|
assets: assets,
|
|
assets: assets,
|
|
}
|
|
}
|
|
end
|
|
end
|
|
|
|
|
|
|
|
+=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
|
|
|
|
+
|
|
def self.agent_state_with_sessions(user_id)
|
|
def self.agent_state_with_sessions(user_id)
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
|
|
|
|
|
@@ -105,87 +298,204 @@ class Chat < ApplicationModel
|
|
result
|
|
result
|
|
end
|
|
end
|
|
|
|
|
|
- def self.waiting_chat_count
|
|
|
|
- Chat::Session.where(state: ['waiting']).count
|
|
|
|
|
|
+=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
|
|
end
|
|
end
|
|
|
|
|
|
- def self.waiting_chat_session_list
|
|
|
|
|
|
+ def self.waiting_chat_session_list(chat_ids)
|
|
sessions = []
|
|
sessions = []
|
|
- Chat::Session.where(state: ['waiting']).each do |session|
|
|
|
|
|
|
+ Chat::Session.where(state: ['waiting'], chat_id: chat_ids).each do |session|
|
|
sessions.push session.attributes
|
|
sessions.push session.attributes
|
|
end
|
|
end
|
|
sessions
|
|
sessions
|
|
end
|
|
end
|
|
|
|
|
|
- def self.running_chat_count
|
|
|
|
- Chat::Session.where(state: ['running']).count
|
|
|
|
|
|
+=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
|
|
end
|
|
end
|
|
|
|
|
|
- def self.running_chat_session_list
|
|
|
|
|
|
+ def self.running_chat_session_list(chat_ids)
|
|
sessions = []
|
|
sessions = []
|
|
- Chat::Session.where(state: ['running']).each do |session|
|
|
|
|
|
|
+ Chat::Session.where(state: ['running'], chat_id: chat_ids).each do |session|
|
|
sessions.push session.attributes
|
|
sessions.push session.attributes
|
|
end
|
|
end
|
|
sessions
|
|
sessions
|
|
end
|
|
end
|
|
|
|
|
|
- def self.active_chat_count
|
|
|
|
- Chat::Session.where(state: %w[waiting running]).count
|
|
|
|
|
|
+=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
|
|
end
|
|
end
|
|
|
|
|
|
- def self.available_agents(diff = 2.minutes)
|
|
|
|
|
|
+=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)
|
|
agents = {}
|
|
agents = {}
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
|
|
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)
|
|
|
|
+
|
|
agents[record.updated_by_id] = record.concurrent
|
|
agents[record.updated_by_id] = record.concurrent
|
|
end
|
|
end
|
|
agents
|
|
agents
|
|
end
|
|
end
|
|
|
|
|
|
- def self.active_agent_count(diff = 2.minutes)
|
|
|
|
- Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).count
|
|
|
|
|
|
+=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
|
|
end
|
|
end
|
|
|
|
|
|
- def self.active_agents(diff = 2.minutes)
|
|
|
|
|
|
+=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)
|
|
users = []
|
|
users = []
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
|
|
Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
|
|
user = User.lookup(id: record.updated_by_id)
|
|
user = User.lookup(id: record.updated_by_id)
|
|
next if !user
|
|
next if !user
|
|
|
|
+ next if !agent_active_chat?(user, chat_ids)
|
|
|
|
|
|
users.push user
|
|
users.push user
|
|
end
|
|
end
|
|
users
|
|
users
|
|
end
|
|
end
|
|
|
|
|
|
- def self.seads_total(diff = 2.minutes)
|
|
|
|
|
|
+=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)
|
|
total = 0
|
|
total = 0
|
|
- available_agents(diff).each_value do |concurrent|
|
|
|
|
|
|
+ available_agents_with_concurrent(chat_ids, diff).each_value do |concurrent|
|
|
total += concurrent
|
|
total += concurrent
|
|
end
|
|
end
|
|
total
|
|
total
|
|
end
|
|
end
|
|
|
|
|
|
- def self.seads_available(diff = 2.minutes)
|
|
|
|
- seads_total(diff) - active_chat_count
|
|
|
|
|
|
+=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)
|
|
end
|
|
end
|
|
|
|
|
|
=begin
|
|
=begin
|
|
|
|
|
|
broadcast new agent status to all agents
|
|
broadcast new agent status to all agents
|
|
|
|
|
|
- Chat.broadcast_agent_state_update
|
|
|
|
|
|
+ Chat.broadcast_agent_state_update(chat_ids)
|
|
|
|
|
|
optional you can ignore it for dedicated user
|
|
optional you can ignore it for dedicated user
|
|
|
|
|
|
- Chat.broadcast_agent_state_update(ignore_user_id)
|
|
|
|
|
|
+ Chat.broadcast_agent_state_update(chat_ids, ignore_user_id)
|
|
|
|
|
|
=end
|
|
=end
|
|
|
|
|
|
- def self.broadcast_agent_state_update(ignore_user_id = nil)
|
|
|
|
|
|
+ def self.broadcast_agent_state_update(chat_ids, ignore_user_id = nil)
|
|
|
|
|
|
# send broadcast to agents
|
|
# send broadcast to agents
|
|
Chat::Agent.where('active = ? OR updated_at > ?', true, Time.zone.now - 8.hours).each do |item|
|
|
Chat::Agent.where('active = ? OR updated_at > ?', true, Time.zone.now - 8.hours).each do |item|
|
|
next if item.updated_by_id == ignore_user_id
|
|
next if item.updated_by_id == ignore_user_id
|
|
|
|
|
|
|
|
+ user = User.lookup(id: item.updated_by_id)
|
|
|
|
+ next if !user
|
|
|
|
+ next if !agent_active_chat?(user, chat_ids)
|
|
|
|
+
|
|
data = {
|
|
data = {
|
|
event: 'chat_status_agent',
|
|
event: 'chat_status_agent',
|
|
data: Chat.agent_state(item.updated_by_id),
|
|
data: Chat.agent_state(item.updated_by_id),
|
|
@@ -198,15 +508,15 @@ optional you can ignore it for dedicated user
|
|
|
|
|
|
broadcast new customer queue position to all waiting customers
|
|
broadcast new customer queue position to all waiting customers
|
|
|
|
|
|
- Chat.broadcast_customer_state_update
|
|
|
|
|
|
+ Chat.broadcast_customer_state_update(chat_id)
|
|
|
|
|
|
=end
|
|
=end
|
|
|
|
|
|
- def self.broadcast_customer_state_update
|
|
|
|
|
|
+ def self.broadcast_customer_state_update(chat_id)
|
|
|
|
|
|
# send position update to other waiting sessions
|
|
# send position update to other waiting sessions
|
|
position = 0
|
|
position = 0
|
|
- Chat::Session.where(state: 'waiting').order(created_at: :asc).each do |local_chat_session|
|
|
|
|
|
|
+ Chat::Session.where(state: 'waiting', chat_id: chat_id).order(created_at: :asc).each do |local_chat_session|
|
|
position += 1
|
|
position += 1
|
|
data = {
|
|
data = {
|
|
event: 'chat_session_queue',
|
|
event: 'chat_session_queue',
|
|
@@ -228,11 +538,11 @@ cleanup old chat messages
|
|
|
|
|
|
optional you can put the max oldest chat entries
|
|
optional you can put the max oldest chat entries
|
|
|
|
|
|
- Chat.cleanup(3.months)
|
|
|
|
|
|
+ Chat.cleanup(12.months)
|
|
|
|
|
|
=end
|
|
=end
|
|
|
|
|
|
- def self.cleanup(diff = 3.months)
|
|
|
|
|
|
+ def self.cleanup(diff = 12.months)
|
|
Chat::Session.where(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each do |chat_session|
|
|
Chat::Session.where(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each do |chat_session|
|
|
Chat::Message.where(chat_session_id: chat_session.id).delete_all
|
|
Chat::Message.where(chat_session_id: chat_session.id).delete_all
|
|
chat_session.destroy
|
|
chat_session.destroy
|