Added customer reconnect possibility on server side.
This commit is contained in:
parent
edbbce1f50
commit
d9a6c835b0
5 changed files with 105 additions and 72 deletions
|
@ -4,9 +4,17 @@ class Chat < ApplicationModel
|
||||||
has_many :chat_topics
|
has_many :chat_topics
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
|
|
||||||
def state
|
def customer_state(session_id = nil)
|
||||||
return { state: 'chat_disabled' } if !Setting.get('chat')
|
return { state: 'chat_disabled' } if !Setting.get('chat')
|
||||||
|
|
||||||
|
if session_id
|
||||||
|
session = Chat.session_state(session_id)
|
||||||
|
return {
|
||||||
|
state: 'reconnect',
|
||||||
|
session: session,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
if Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - 2.minutes).count > 0
|
if Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - 2.minutes).count > 0
|
||||||
if active_chat_count >= max_queue
|
if active_chat_count >= max_queue
|
||||||
return {
|
return {
|
||||||
|
@ -21,6 +29,15 @@ class Chat < ApplicationModel
|
||||||
{ state: 'offline' }
|
{ state: 'offline' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.session_state(session_id)
|
||||||
|
session_attributes = []
|
||||||
|
chat_session_id = Chat::Session.find_by(session_id: session_id)
|
||||||
|
Chat::Message.where(chat_session_id: chat_session_id.id).each { |message|
|
||||||
|
session_attributes.push message.attributes
|
||||||
|
}
|
||||||
|
session_attributes
|
||||||
|
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')
|
||||||
actice_sessions = []
|
actice_sessions = []
|
||||||
|
|
|
@ -13,9 +13,14 @@ class Sessions::Event::ChatStatusCustomer < Sessions::Event::ChatBase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# check if it's a chat sessin reconnect
|
||||||
|
session_id = nil
|
||||||
|
if @data['data']['session_id']
|
||||||
|
session_id = @data['data']['session_id']
|
||||||
|
end
|
||||||
{
|
{
|
||||||
event: 'chat_status_customer',
|
event: 'chat_status_customer',
|
||||||
data: chat.state,
|
data: chat.customer_state(session_id),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -129,6 +129,9 @@ do($ = window.jQuery, window) ->
|
||||||
@log 'debug', 'Zammad Chat: Chat is disabled'
|
@log 'debug', 'Zammad Chat: Chat is disabled'
|
||||||
when 'no_seats_available'
|
when 'no_seats_available'
|
||||||
@log 'debug', 'Zammad Chat: Too many clients in queue. Clients in queue: ', pipe.data.queue
|
@log 'debug', 'Zammad Chat: Too many clients in queue. Clients in queue: ', pipe.data.queue
|
||||||
|
when 'reconnect'
|
||||||
|
# old messages
|
||||||
|
@log 'debug', 'old messages', pipe.data.session
|
||||||
|
|
||||||
onReady: =>
|
onReady: =>
|
||||||
if @options.show
|
if @options.show
|
||||||
|
@ -153,7 +156,8 @@ do($ = window.jQuery, window) ->
|
||||||
# send typing start event
|
# send typing start event
|
||||||
if !@isTyping
|
if !@isTyping
|
||||||
@isTyping = true
|
@isTyping = true
|
||||||
@send 'chat_session_typing', {session_id: @session_id}
|
@send 'chat_session_typing',
|
||||||
|
session_id: @session_id
|
||||||
|
|
||||||
onTypingEnd: =>
|
onTypingEnd: =>
|
||||||
@isTyping = false
|
@isTyping = false
|
||||||
|
@ -235,7 +239,8 @@ do($ = window.jQuery, window) ->
|
||||||
@disconnect()
|
@disconnect()
|
||||||
@isOpen = false
|
@isOpen = false
|
||||||
|
|
||||||
@send 'chat_session_close', {session_id: @session_id}
|
@send 'chat_session_close',
|
||||||
|
session_id: @session_id
|
||||||
|
|
||||||
hide: ->
|
hide: ->
|
||||||
@el.removeClass('zammad-chat-is-visible')
|
@el.removeClass('zammad-chat-is-visible')
|
||||||
|
@ -319,7 +324,8 @@ do($ = window.jQuery, window) ->
|
||||||
@ws = new window.WebSocket(@host)
|
@ws = new window.WebSocket(@host)
|
||||||
@ws.onopen = =>
|
@ws.onopen = =>
|
||||||
@log 'debug', 'ws connected'
|
@log 'debug', 'ws connected'
|
||||||
@send 'chat_status_customer'
|
@send 'chat_status_customer',
|
||||||
|
session_id: @session_id
|
||||||
@setAgentOnlineState(true)
|
@setAgentOnlineState(true)
|
||||||
|
|
||||||
@ws.onmessage = @onWebSocketMessage
|
@ws.onmessage = @onWebSocketMessage
|
||||||
|
|
|
@ -1,68 +1,3 @@
|
||||||
if (!window.zammadChatTemplates) {
|
|
||||||
window.zammadChatTemplates = {};
|
|
||||||
}
|
|
||||||
window.zammadChatTemplates["agent"] = function (__obj) {
|
|
||||||
if (!__obj) __obj = {};
|
|
||||||
var __out = [], __capture = function(callback) {
|
|
||||||
var out = __out, result;
|
|
||||||
__out = [];
|
|
||||||
callback.call(this);
|
|
||||||
result = __out.join('');
|
|
||||||
__out = out;
|
|
||||||
return __safe(result);
|
|
||||||
}, __sanitize = function(value) {
|
|
||||||
if (value && value.ecoSafe) {
|
|
||||||
return value;
|
|
||||||
} else if (typeof value !== 'undefined' && value != null) {
|
|
||||||
return __escape(value);
|
|
||||||
} else {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
|
|
||||||
__safe = __obj.safe = function(value) {
|
|
||||||
if (value && value.ecoSafe) {
|
|
||||||
return value;
|
|
||||||
} else {
|
|
||||||
if (!(typeof value !== 'undefined' && value != null)) value = '';
|
|
||||||
var result = new String(value);
|
|
||||||
result.ecoSafe = true;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (!__escape) {
|
|
||||||
__escape = __obj.escape = function(value) {
|
|
||||||
return ('' + value)
|
|
||||||
.replace(/&/g, '&')
|
|
||||||
.replace(/</g, '<')
|
|
||||||
.replace(/>/g, '>')
|
|
||||||
.replace(/"/g, '"');
|
|
||||||
};
|
|
||||||
}
|
|
||||||
(function() {
|
|
||||||
(function() {
|
|
||||||
if (this.agent.avatar) {
|
|
||||||
__out.push('\n<img class="zammad-chat-agent-avatar" src="');
|
|
||||||
__out.push(__sanitize(this.agent.avatar));
|
|
||||||
__out.push('">\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
__out.push('\n<span class="zammad-chat-agent-sentence">\n <span class="zammad-chat-agent-name">');
|
|
||||||
|
|
||||||
__out.push(__sanitize(this.agent.name));
|
|
||||||
|
|
||||||
__out.push('</span> ');
|
|
||||||
|
|
||||||
__out.push(this.agentPhrase);
|
|
||||||
|
|
||||||
__out.push('\n</span>');
|
|
||||||
|
|
||||||
}).call(this);
|
|
||||||
|
|
||||||
}).call(__obj);
|
|
||||||
__obj.safe = __objSafe, __obj.escape = __escape;
|
|
||||||
return __out.join('');
|
|
||||||
};
|
|
||||||
|
|
||||||
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
||||||
slice = [].slice;
|
slice = [].slice;
|
||||||
|
|
||||||
|
@ -267,6 +202,9 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
|
||||||
break;
|
break;
|
||||||
case 'no_seats_available':
|
case 'no_seats_available':
|
||||||
this.log('debug', 'Zammad Chat: Too many clients in queue. Clients in queue: ', pipe.data.queue);
|
this.log('debug', 'Zammad Chat: Too many clients in queue. Clients in queue: ', pipe.data.queue);
|
||||||
|
break;
|
||||||
|
case 'reconnect':
|
||||||
|
this.log('debug', 'old messages', pipe.data.session);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -479,7 +417,9 @@ 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() {
|
||||||
_this.log('debug', 'ws connected');
|
_this.log('debug', 'ws connected');
|
||||||
_this.send('chat_status_customer');
|
_this.send('chat_status_customer', {
|
||||||
|
session_id: _this.session_id
|
||||||
|
});
|
||||||
return _this.setAgentOnlineState(true);
|
return _this.setAgentOnlineState(true);
|
||||||
};
|
};
|
||||||
})(this);
|
})(this);
|
||||||
|
@ -559,6 +499,71 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
|
||||||
});
|
});
|
||||||
})(window.jQuery, window);
|
})(window.jQuery, window);
|
||||||
|
|
||||||
|
if (!window.zammadChatTemplates) {
|
||||||
|
window.zammadChatTemplates = {};
|
||||||
|
}
|
||||||
|
window.zammadChatTemplates["agent"] = function (__obj) {
|
||||||
|
if (!__obj) __obj = {};
|
||||||
|
var __out = [], __capture = function(callback) {
|
||||||
|
var out = __out, result;
|
||||||
|
__out = [];
|
||||||
|
callback.call(this);
|
||||||
|
result = __out.join('');
|
||||||
|
__out = out;
|
||||||
|
return __safe(result);
|
||||||
|
}, __sanitize = function(value) {
|
||||||
|
if (value && value.ecoSafe) {
|
||||||
|
return value;
|
||||||
|
} else if (typeof value !== 'undefined' && value != null) {
|
||||||
|
return __escape(value);
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
|
||||||
|
__safe = __obj.safe = function(value) {
|
||||||
|
if (value && value.ecoSafe) {
|
||||||
|
return value;
|
||||||
|
} else {
|
||||||
|
if (!(typeof value !== 'undefined' && value != null)) value = '';
|
||||||
|
var result = new String(value);
|
||||||
|
result.ecoSafe = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (!__escape) {
|
||||||
|
__escape = __obj.escape = function(value) {
|
||||||
|
return ('' + value)
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"');
|
||||||
|
};
|
||||||
|
}
|
||||||
|
(function() {
|
||||||
|
(function() {
|
||||||
|
if (this.agent.avatar) {
|
||||||
|
__out.push('\n<img class="zammad-chat-agent-avatar" src="');
|
||||||
|
__out.push(__sanitize(this.agent.avatar));
|
||||||
|
__out.push('">\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
__out.push('\n<span class="zammad-chat-agent-sentence">\n <span class="zammad-chat-agent-name">');
|
||||||
|
|
||||||
|
__out.push(__sanitize(this.agent.name));
|
||||||
|
|
||||||
|
__out.push('</span> ');
|
||||||
|
|
||||||
|
__out.push(this.agentPhrase);
|
||||||
|
|
||||||
|
__out.push('\n</span>');
|
||||||
|
|
||||||
|
}).call(this);
|
||||||
|
|
||||||
|
}).call(__obj);
|
||||||
|
__obj.safe = __objSafe, __obj.escape = __escape;
|
||||||
|
return __out.join('');
|
||||||
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* ----------------------------------------------------------------------------
|
* ----------------------------------------------------------------------------
|
||||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||||
|
|
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