Fixed new message counter, fixed active/not active state of agent and refactoring of web socket event backends.
This commit is contained in:
parent
600649d51a
commit
1979376d21
17 changed files with 163 additions and 187 deletions
|
@ -32,6 +32,7 @@ class App.CustomerChat extends App.Controller
|
||||||
(data) =>
|
(data) =>
|
||||||
@meta = data
|
@meta = data
|
||||||
@updateMeta()
|
@updateMeta()
|
||||||
|
@interval(@pushState, 20000, 'pushState')
|
||||||
)
|
)
|
||||||
App.Event.bind(
|
App.Event.bind(
|
||||||
'chat_session_start'
|
'chat_session_start'
|
||||||
|
@ -43,8 +44,6 @@ class App.CustomerChat extends App.Controller
|
||||||
|
|
||||||
App.WebSocket.send(event:'chat_status_agent')
|
App.WebSocket.send(event:'chat_status_agent')
|
||||||
|
|
||||||
@interval(@pushState, 16000)
|
|
||||||
|
|
||||||
pushState: =>
|
pushState: =>
|
||||||
App.WebSocket.send(
|
App.WebSocket.send(
|
||||||
event:'chat_agent_state'
|
event:'chat_agent_state'
|
||||||
|
@ -59,13 +58,23 @@ class App.CustomerChat extends App.Controller
|
||||||
@navupdate '#customer_chat'
|
@navupdate '#customer_chat'
|
||||||
|
|
||||||
counter: =>
|
counter: =>
|
||||||
if @meta.waiting_chat_count
|
counter = 0
|
||||||
if @waitingChatCountLast isnt @meta.waiting_chat_count
|
|
||||||
@sounds.chat_new.play()
|
|
||||||
@waitingChatCountLast = @meta.waiting_chat_count
|
|
||||||
|
|
||||||
return @messageCounter + @meta.waiting_chat_count
|
# get count of controller messages
|
||||||
@messageCounter
|
if @meta.waiting_chat_count
|
||||||
|
counter += @meta.waiting_chat_count
|
||||||
|
|
||||||
|
# play on changes
|
||||||
|
if @lastWaitingChatCount isnt counter
|
||||||
|
@sounds.chat_new.play()
|
||||||
|
@lastWaitingChatCount = counter
|
||||||
|
|
||||||
|
# collect chat window messages
|
||||||
|
for key, value of @chatWindows
|
||||||
|
if value
|
||||||
|
counter += value.unreadMessages()
|
||||||
|
|
||||||
|
@messageCounter = counter
|
||||||
|
|
||||||
switch: (state = undefined) =>
|
switch: (state = undefined) =>
|
||||||
|
|
||||||
|
@ -85,7 +94,7 @@ class App.CustomerChat extends App.Controller
|
||||||
updateNavMenu: =>
|
updateNavMenu: =>
|
||||||
delay = ->
|
delay = ->
|
||||||
App.Event.trigger('menu:render')
|
App.Event.trigger('menu:render')
|
||||||
@delay(delay, 200)
|
@delay(delay, 200, 'updateNavMenu')
|
||||||
|
|
||||||
updateMeta: =>
|
updateMeta: =>
|
||||||
if @meta.waiting_chat_count
|
if @meta.waiting_chat_count
|
||||||
|
@ -107,7 +116,8 @@ class App.CustomerChat extends App.Controller
|
||||||
chat = new chatWindow
|
chat = new chatWindow
|
||||||
name: "#{session.created_at}"
|
name: "#{session.created_at}"
|
||||||
session: session
|
session: session
|
||||||
callback: @removeChat
|
removeCallback: @removeChat
|
||||||
|
messageCallback: @updateNavMenu
|
||||||
|
|
||||||
@on 'layout-has-changed', @propagateLayoutChange
|
@on 'layout-has-changed', @propagateLayoutChange
|
||||||
|
|
||||||
|
@ -115,7 +125,6 @@ class App.CustomerChat extends App.Controller
|
||||||
@chatWindows[session.session_id] = chat
|
@chatWindows[session.session_id] = chat
|
||||||
|
|
||||||
removeChat: (session_id) =>
|
removeChat: (session_id) =>
|
||||||
console.log('removeChat', session_id, @chatWindows[session_id])
|
|
||||||
delete @chatWindows[session_id]
|
delete @chatWindows[session_id]
|
||||||
|
|
||||||
propagateLayoutChange: (event) =>
|
propagateLayoutChange: (event) =>
|
||||||
|
@ -177,6 +186,7 @@ class chatWindow extends App.Controller
|
||||||
@lastAddedType
|
@lastAddedType
|
||||||
@isTyping = false
|
@isTyping = false
|
||||||
@render()
|
@render()
|
||||||
|
@resetUnreadMessages()
|
||||||
|
|
||||||
@on 'layout-change', @scrollToBottom
|
@on 'layout-change', @scrollToBottom
|
||||||
|
|
||||||
|
@ -231,8 +241,8 @@ class chatWindow extends App.Controller
|
||||||
data:
|
data:
|
||||||
session_id: @session.session_id
|
session_id: @session.session_id
|
||||||
)
|
)
|
||||||
if @callback
|
if @removeCallback
|
||||||
@callback(@session.session_id)
|
@removeCallback(@session.session_id)
|
||||||
|
|
||||||
release: =>
|
release: =>
|
||||||
@trigger 'closed'
|
@trigger 'closed'
|
||||||
|
@ -241,6 +251,7 @@ class chatWindow extends App.Controller
|
||||||
clearUnread: =>
|
clearUnread: =>
|
||||||
@$('.chat-message--new').removeClass('chat-message--new')
|
@$('.chat-message--new').removeClass('chat-message--new')
|
||||||
@updateModified(false)
|
@updateModified(false)
|
||||||
|
@resetUnreadMessages()
|
||||||
|
|
||||||
onKeydown: (event) =>
|
onKeydown: (event) =>
|
||||||
TABKEY = 9;
|
TABKEY = 9;
|
||||||
|
@ -304,9 +315,23 @@ class chatWindow extends App.Controller
|
||||||
@addMessage(message, 'customer', !isFocused)
|
@addMessage(message, 'customer', !isFocused)
|
||||||
|
|
||||||
if !isFocused
|
if !isFocused
|
||||||
|
@addUnreadMessages()
|
||||||
@updateModified(true)
|
@updateModified(true)
|
||||||
@sounds.message.play()
|
@sounds.message.play()
|
||||||
|
|
||||||
|
unreadMessages: =>
|
||||||
|
@unreadMessagesCounter
|
||||||
|
|
||||||
|
addUnreadMessages: =>
|
||||||
|
if @messageCallback
|
||||||
|
@messageCallback(@session.session_id)
|
||||||
|
@unreadMessagesCounter += 1
|
||||||
|
|
||||||
|
resetUnreadMessages: =>
|
||||||
|
if @messageCallback
|
||||||
|
@messageCallback(@session.session_id)
|
||||||
|
@unreadMessagesCounter = 0
|
||||||
|
|
||||||
addMessage: (message, sender, isNew) =>
|
addMessage: (message, sender, isNew) =>
|
||||||
@maybeAddTimestamp()
|
@maybeAddTimestamp()
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Chat < ApplicationModel
|
||||||
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')
|
||||||
actice_sessions = []
|
actice_sessions = []
|
||||||
Chat::Session.where(state: 'running').order('created_at ASC').each {|session|
|
Chat::Session.where(state: 'running', user_id: user_id).order('created_at ASC').each {|session|
|
||||||
session_attributes = session.attributes
|
session_attributes = session.attributes
|
||||||
session_attributes['messages'] = []
|
session_attributes['messages'] = []
|
||||||
Chat::Message.where(chat_session_id: session.id).each { |message|
|
Chat::Message.where(chat_session_id: session.id).each { |message|
|
||||||
|
@ -127,6 +127,26 @@ class Chat::Session < ApplicationModel
|
||||||
def generate_session_id
|
def generate_session_id
|
||||||
self.session_id = Digest::MD5.hexdigest(Time.zone.now.to_s + rand(99_999_999_999_999).to_s)
|
self.session_id = Digest::MD5.hexdigest(Time.zone.now.to_s + rand(99_999_999_999_999).to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_recipient(client_id, store = false)
|
||||||
|
if !self.preferences[:participants]
|
||||||
|
self.preferences[:participants] = []
|
||||||
|
end
|
||||||
|
return self.preferences[:participants] if self.preferences[:participants].include?(client_id)
|
||||||
|
self.preferences[:participants].push client_id
|
||||||
|
if store
|
||||||
|
self.save
|
||||||
|
end
|
||||||
|
self.preferences[:participants]
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_to_recipients(message, ignore_client_id)
|
||||||
|
self.preferences[:participants].each {|local_client_id|
|
||||||
|
next if local_client_id == ignore_client_id
|
||||||
|
Sessions.send(local_client_id, message)
|
||||||
|
}
|
||||||
|
true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Chat::Message < ApplicationModel
|
class Chat::Message < ApplicationModel
|
||||||
|
|
|
@ -1,12 +1,5 @@
|
||||||
class CreateChat < ActiveRecord::Migration
|
class CreateChat < ActiveRecord::Migration
|
||||||
def up
|
def up
|
||||||
=begin
|
|
||||||
ActiveRecord::Migration.drop_table :chats
|
|
||||||
ActiveRecord::Migration.drop_table :chat_topics
|
|
||||||
ActiveRecord::Migration.drop_table :chat_sessions
|
|
||||||
ActiveRecord::Migration.drop_table :chat_messages
|
|
||||||
ActiveRecord::Migration.drop_table :chat_agents
|
|
||||||
=end
|
|
||||||
create_table :chats do |t|
|
create_table :chats do |t|
|
||||||
t.string :name, limit: 250, null: true
|
t.string :name, limit: 250, null: true
|
||||||
t.integer :max_queue, null: false, default: 5
|
t.integer :max_queue, null: false, default: 5
|
||||||
|
|
|
@ -9,8 +9,11 @@ class Sessions::Event
|
||||||
return { error: "No such event #{event}" }
|
return { error: "No such event #{event}" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
instance = backend.new(data, session, client_id)
|
||||||
|
result = instance.pre_check
|
||||||
|
return result if result
|
||||||
ActiveRecord::Base.establish_connection
|
ActiveRecord::Base.establish_connection
|
||||||
result = backend.run(data, session, client_id)
|
result = instance.run
|
||||||
ActiveRecord::Base.remove_connection
|
ActiveRecord::Base.remove_connection
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,21 +1,11 @@
|
||||||
class Sessions::Event::ChatAgentState
|
class Sessions::Event::ChatAgentState < Sessions::Event::ChatBase
|
||||||
|
|
||||||
def self.run(data, session, _client_id)
|
def run
|
||||||
|
|
||||||
# check if feature is enabled
|
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_agent_state',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# only agents can do this
|
# only agents can do this
|
||||||
chat_id = 1
|
chat_id = 1
|
||||||
chat = Chat.find_by(id: chat_id)
|
chat = Chat.find_by(id: chat_id)
|
||||||
if !session['id']
|
if !@session['id']
|
||||||
return {
|
return {
|
||||||
event: 'chat_agent_state',
|
event: 'chat_agent_state',
|
||||||
data: {
|
data: {
|
||||||
|
@ -25,13 +15,13 @@ class Sessions::Event::ChatAgentState
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
Chat::Agent.state(session['id'], data['data']['active'])
|
Chat::Agent.state(@session['id'], @data['data']['active'])
|
||||||
|
|
||||||
{
|
{
|
||||||
event: 'chat_agent_state',
|
event: 'chat_agent_state',
|
||||||
data: {
|
data: {
|
||||||
state: 'ok',
|
state: 'ok',
|
||||||
active: data['data']['active'],
|
active: @data['data']['active'],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
25
lib/sessions/event/chat_base.rb
Normal file
25
lib/sessions/event/chat_base.rb
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
class Sessions::Event::ChatBase
|
||||||
|
|
||||||
|
def initialize(data, session, client_id)
|
||||||
|
@data = data
|
||||||
|
@session = session
|
||||||
|
@client_id = client_id
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def pre_check
|
||||||
|
|
||||||
|
# check if feature is enabled
|
||||||
|
if !Setting.get('chat')
|
||||||
|
return {
|
||||||
|
event: 'chat_error',
|
||||||
|
data: {
|
||||||
|
state: 'chat_disabled',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -1,18 +1,8 @@
|
||||||
class Sessions::Event::ChatSessionClose
|
class Sessions::Event::ChatSessionClose < Sessions::Event::ChatBase
|
||||||
|
|
||||||
def self.run(data, _session, _client_id)
|
def run
|
||||||
|
|
||||||
# check if feature is enabled
|
if !@data['data'] || !@data['data']['session_id']
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_status_close',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
if !data['data'] || !data['data']['session_id']
|
|
||||||
return {
|
return {
|
||||||
event: 'chat_status_close',
|
event: 'chat_status_close',
|
||||||
data: {
|
data: {
|
||||||
|
@ -21,7 +11,7 @@ class Sessions::Event::ChatSessionClose
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
chat_session = Chat::Session.find_by(session_id: data['data']['session_id'])
|
chat_session = Chat::Session.find_by(session_id: @data['data']['session_id'])
|
||||||
if !chat_session
|
if !chat_session
|
||||||
return {
|
return {
|
||||||
event: 'chat_status_close',
|
event: 'chat_status_close',
|
||||||
|
|
|
@ -1,16 +1,6 @@
|
||||||
class Sessions::Event::ChatSessionInit
|
class Sessions::Event::ChatSessionInit < Sessions::Event::ChatBase
|
||||||
|
|
||||||
def self.run(data, _session, client_id)
|
def run
|
||||||
|
|
||||||
# check if feature is enabled
|
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_session_init',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
chat_id = 1
|
chat_id = 1
|
||||||
chat = Chat.find_by(id: chat_id)
|
chat = Chat.find_by(id: chat_id)
|
||||||
|
@ -29,17 +19,17 @@ class Sessions::Event::ChatSessionInit
|
||||||
name: '',
|
name: '',
|
||||||
state: 'waiting',
|
state: 'waiting',
|
||||||
preferences: {
|
preferences: {
|
||||||
participants: [client_id],
|
participants: [@client_id],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# send update to agents
|
# send broadcast to agents
|
||||||
User.where(active: true).each {|user|
|
Chat::Agent.where(active: true).each {|item|
|
||||||
data = {
|
data = {
|
||||||
event: 'chat_status_agent',
|
event: 'chat_status_agent',
|
||||||
data: Chat.agent_state(user.id),
|
data: Chat.agent_state(item.updated_by_id),
|
||||||
}
|
}
|
||||||
Sessions.send_to(user.id, data)
|
Sessions.send_to(item.updated_by_id, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
# return new session
|
# return new session
|
||||||
|
|
|
@ -1,18 +1,8 @@
|
||||||
class Sessions::Event::ChatSessionMessage
|
class Sessions::Event::ChatSessionMessage < Sessions::Event::ChatBase
|
||||||
|
|
||||||
def self.run(data, session, client_id)
|
def run
|
||||||
|
|
||||||
# check if feature is enabled
|
if !@data['data'] || !@data['data']['session_id']
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_session_message',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
if !data['data'] || !data['data']['session_id']
|
|
||||||
return {
|
return {
|
||||||
event: 'chat_session_message',
|
event: 'chat_session_message',
|
||||||
data: {
|
data: {
|
||||||
|
@ -21,7 +11,7 @@ class Sessions::Event::ChatSessionMessage
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
chat_session = Chat::Session.find_by(session_id: data['data']['session_id'])
|
chat_session = Chat::Session.find_by(session_id: @data['data']['session_id'])
|
||||||
if !chat_session
|
if !chat_session
|
||||||
return {
|
return {
|
||||||
event: 'chat_session_message',
|
event: 'chat_session_message',
|
||||||
|
@ -32,12 +22,12 @@ class Sessions::Event::ChatSessionMessage
|
||||||
end
|
end
|
||||||
|
|
||||||
user_id = nil
|
user_id = nil
|
||||||
if session
|
if @session
|
||||||
user_id = session['id']
|
user_id = @session['id']
|
||||||
end
|
end
|
||||||
chat_message = Chat::Message.create(
|
chat_message = Chat::Message.create(
|
||||||
chat_session_id: chat_session.id,
|
chat_session_id: chat_session.id,
|
||||||
content: data['data']['content'],
|
content: @data['data']['content'],
|
||||||
created_by_id: user_id,
|
created_by_id: user_id,
|
||||||
)
|
)
|
||||||
message = {
|
message = {
|
||||||
|
@ -49,10 +39,7 @@ class Sessions::Event::ChatSessionMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
# send to participents
|
# send to participents
|
||||||
chat_session.preferences[:participants].each {|local_client_id|
|
chat_session.send_to_recipients(message, @client_id)
|
||||||
next if local_client_id == client_id
|
|
||||||
Sessions.send(local_client_id, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
# send chat_session_init to agent
|
# send chat_session_init to agent
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,16 +1,6 @@
|
||||||
class Sessions::Event::ChatSessionStart
|
class Sessions::Event::ChatSessionStart < Sessions::Event::ChatBase
|
||||||
|
|
||||||
def self.run(data, session, client_id)
|
def run
|
||||||
|
|
||||||
# check if feature is enabled
|
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_session_start',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# find first in waiting list
|
# find first in waiting list
|
||||||
chat_session = Chat::Session.where(state: 'waiting').order('created_at ASC').first
|
chat_session = Chat::Session.where(state: 'waiting').order('created_at ASC').first
|
||||||
|
@ -23,9 +13,9 @@ class Sessions::Event::ChatSessionStart
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
chat_session.user_id = session['id']
|
chat_session.user_id = @session['id']
|
||||||
chat_session.state = 'running'
|
chat_session.state = 'running'
|
||||||
chat_session.preferences[:participants].push client_id
|
chat_session.preferences[:participants] = chat_session.add_recipient(@client_id)
|
||||||
chat_session.save
|
chat_session.save
|
||||||
|
|
||||||
# send chat_session_init to client
|
# send chat_session_init to client
|
||||||
|
@ -42,11 +32,7 @@ class Sessions::Event::ChatSessionStart
|
||||||
session_id: chat_session.session_id,
|
session_id: chat_session.session_id,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
chat_session.send_to_recipients(data, @client_id)
|
||||||
chat_session.preferences[:participants].each {|local_client_id|
|
|
||||||
next if local_client_id == client_id
|
|
||||||
Sessions.send(local_client_id, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
# send chat_session_init to agent
|
# send chat_session_init to agent
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,18 +1,8 @@
|
||||||
class Sessions::Event::ChatSessionTyping
|
class Sessions::Event::ChatSessionTyping < Sessions::Event::ChatBase
|
||||||
|
|
||||||
def self.run(data, session, client_id)
|
def run
|
||||||
|
|
||||||
# check if feature is enabled
|
if !@data['data'] || !@data['data']['session_id']
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_session_typing',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
if !data['data'] || !data['data']['session_id']
|
|
||||||
return {
|
return {
|
||||||
event: 'chat_session_typing',
|
event: 'chat_session_typing',
|
||||||
data: {
|
data: {
|
||||||
|
@ -21,7 +11,7 @@ class Sessions::Event::ChatSessionTyping
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
chat_session = Chat::Session.find_by(session_id: data['data']['session_id'])
|
chat_session = Chat::Session.find_by(session_id: @data['data']['session_id'])
|
||||||
if !chat_session
|
if !chat_session
|
||||||
return {
|
return {
|
||||||
event: 'chat_session_typing',
|
event: 'chat_session_typing',
|
||||||
|
@ -32,8 +22,8 @@ class Sessions::Event::ChatSessionTyping
|
||||||
end
|
end
|
||||||
|
|
||||||
user_id = nil
|
user_id = nil
|
||||||
if session
|
if @session
|
||||||
user_id = session['id']
|
user_id = @session['id']
|
||||||
end
|
end
|
||||||
message = {
|
message = {
|
||||||
event: 'chat_session_typing',
|
event: 'chat_session_typing',
|
||||||
|
@ -44,10 +34,7 @@ class Sessions::Event::ChatSessionTyping
|
||||||
}
|
}
|
||||||
|
|
||||||
# send to participents
|
# send to participents
|
||||||
chat_session.preferences[:participants].each {|local_client_id|
|
chat_session.send_to_recipients(message, @client_id)
|
||||||
next if local_client_id == client_id
|
|
||||||
Sessions.send(local_client_id, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
# send chat_session_init to agent
|
# send chat_session_init to agent
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
class Sessions::Event::ChatStatus
|
|
||||||
|
|
||||||
def self.run(_data, _session, _client_id)
|
|
||||||
|
|
||||||
# check if feature is enabled
|
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_status',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
chat_id = 1
|
|
||||||
chat = Chat.find_by(id: chat_id)
|
|
||||||
if !chat
|
|
||||||
return {
|
|
||||||
event: 'chat_status',
|
|
||||||
data: {
|
|
||||||
state: 'no_such_chat',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
{
|
|
||||||
event: 'chat_status',
|
|
||||||
data: chat.state,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,26 +1,22 @@
|
||||||
class Sessions::Event::ChatStatusAgent
|
class Sessions::Event::ChatStatusAgent < Sessions::Event::ChatBase
|
||||||
|
|
||||||
def self.run(_data, session, _client_id)
|
def run
|
||||||
|
|
||||||
# check if user has permissions
|
# check if user has permissions
|
||||||
|
|
||||||
# check if feature is enabled
|
|
||||||
if !Setting.get('chat')
|
|
||||||
return {
|
|
||||||
event: 'chat_status_agent',
|
|
||||||
data: {
|
|
||||||
state: 'chat_disabled',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# renew timestamps
|
# renew timestamps
|
||||||
state = Chat::Agent.state(session['id'])
|
state = Chat::Agent.state(@session['id'])
|
||||||
Chat::Agent.state(session['id'], state)
|
Chat::Agent.state(@session['id'], state)
|
||||||
|
|
||||||
|
|
||||||
|
# update recipients of existing sessions
|
||||||
|
Chat::Session.where(state: 'running', user_id: @session['id']).order('created_at ASC').each {|chat_session|
|
||||||
|
chat_session.add_recipient(@client_id, true)
|
||||||
|
}
|
||||||
{
|
{
|
||||||
event: 'chat_status_agent',
|
event: 'chat_status_agent',
|
||||||
data: Chat.agent_state(session['id']),
|
data: Chat.agent_state(@session['id']),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
21
lib/sessions/event/chat_status_customer.rb
Normal file
21
lib/sessions/event/chat_status_customer.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
class Sessions::Event::ChatStatusCustomer < Sessions::Event::ChatBase
|
||||||
|
|
||||||
|
def run
|
||||||
|
|
||||||
|
chat_id = 1
|
||||||
|
chat = Chat.find_by(id: chat_id)
|
||||||
|
if !chat
|
||||||
|
return {
|
||||||
|
event: 'chat_status_customer',
|
||||||
|
data: {
|
||||||
|
state: 'no_such_chat',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
{
|
||||||
|
event: 'chat_status_customer',
|
||||||
|
data: chat.state,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
|
@ -64,7 +64,6 @@ do($ = window.jQuery, window) ->
|
||||||
console.log('ws connected')
|
console.log('ws connected')
|
||||||
@send 'chat_status_customer'
|
@send 'chat_status_customer'
|
||||||
|
|
||||||
|
|
||||||
@ws.onmessage = @onWebSocketMessage
|
@ws.onmessage = @onWebSocketMessage
|
||||||
|
|
||||||
@ws.onclose = (e) =>
|
@ws.onclose = (e) =>
|
||||||
|
|
|
@ -162,7 +162,7 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
|
||||||
this.ws.onopen = (function(_this) {
|
this.ws.onopen = (function(_this) {
|
||||||
return function() {
|
return function() {
|
||||||
console.log('ws connected');
|
console.log('ws connected');
|
||||||
return _this.send('chat_status');
|
return _this.send('chat_status_customer');
|
||||||
};
|
};
|
||||||
})(this);
|
})(this);
|
||||||
this.ws.onmessage = this.onWebSocketMessage;
|
this.ws.onmessage = this.onWebSocketMessage;
|
||||||
|
@ -196,7 +196,7 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
ZammadChat.prototype.onWebSocketMessage = function(e) {
|
ZammadChat.prototype.onWebSocketMessage = function(e) {
|
||||||
var delay, i, len, pipe, pipes;
|
var i, len, pipe, pipes;
|
||||||
pipes = JSON.parse(e.data);
|
pipes = JSON.parse(e.data);
|
||||||
console.log('debug', 'ws:onmessage', pipes);
|
console.log('debug', 'ws:onmessage', pipes);
|
||||||
for (i = 0, len = pipes.length; i < len; i++) {
|
for (i = 0, len = pipes.length; i < len; i++) {
|
||||||
|
@ -213,15 +213,6 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.onAgentTypingStart();
|
this.onAgentTypingStart();
|
||||||
if (this.stopTypingId) {
|
|
||||||
clearTimeout(this.stopTypingId);
|
|
||||||
}
|
|
||||||
delay = (function(_this) {
|
|
||||||
return function() {
|
|
||||||
return _this.onAgentTypingEnd();
|
|
||||||
};
|
|
||||||
})(this);
|
|
||||||
this.stopTypingId = setTimeout(delay, 3000);
|
|
||||||
break;
|
break;
|
||||||
case 'chat_session_start':
|
case 'chat_session_start':
|
||||||
switch (pipe.data.state) {
|
switch (pipe.data.state) {
|
||||||
|
@ -239,7 +230,7 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
|
||||||
this.session_id = pipe.data.session_id;
|
this.session_id = pipe.data.session_id;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'chat_status':
|
case 'chat_status_customer':
|
||||||
switch (pipe.data.state) {
|
switch (pipe.data.state) {
|
||||||
case 'online':
|
case 'online':
|
||||||
this.onReady();
|
this.onReady();
|
||||||
|
@ -393,6 +384,10 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
ZammadChat.prototype.onAgentTypingStart = function() {
|
ZammadChat.prototype.onAgentTypingStart = function() {
|
||||||
|
if (this.stopTypingId) {
|
||||||
|
clearTimeout(this.stopTypingId);
|
||||||
|
}
|
||||||
|
this.stopTypingId = setTimeout(this.onAgentTypingEnd, 3000);
|
||||||
if (this.el.find('.zammad-chat-message--typing').size()) {
|
if (this.el.find('.zammad-chat-message--typing').size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
2
public/assets/chat/chat.min.js
vendored
2
public/assets/chat/chat.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue