From 325a100a4616161f509c9d46a7f6409aba6345bf Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 5 Jan 2016 14:47:41 +0100 Subject: [PATCH] Improved chat performance. Also show geo data in title. --- .../javascripts/app/controllers/chat.coffee | 5 +++ app/models/chat.rb | 17 ++++++++++ lib/sessions/event/chat_session_start.rb | 18 +++++++--- lib/sessions/event/chat_status_agent.rb | 2 +- test/unit/chat_test.rb | 34 +++++++++---------- 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/app/controllers/chat.coffee b/app/assets/javascripts/app/controllers/chat.coffee index 235aa02e2..d86bf1cf2 100644 --- a/app/assets/javascripts/app/controllers/chat.coffee +++ b/app/assets/javascripts/app/controllers/chat.coffee @@ -265,6 +265,11 @@ class ChatWindow extends App.Controller chat = App.Chat.find(@session.chat_id) @name = "#{chat.displayName()} [##{@session.id}]" + if @session && @session.preferences && @session.preferences.geo_ip + if @session.preferences.geo_ip.country_name + @name += " #{@session.preferences.geo_ip.country_name}" + if @session.preferences.geo_ip.city_name + @name += " #{@session.preferences.geo_ip.city_name}" @on 'layout-change', @scrollToBottom diff --git a/app/models/chat.rb b/app/models/chat.rb index d99032799..1104bcbb5 100644 --- a/app/models/chat.rb +++ b/app/models/chat.rb @@ -63,6 +63,23 @@ class Chat < ApplicationModel end def self.agent_state(user_id) + return { state: 'chat_disabled' } if !Setting.get('chat') + assets = {} + Chat.where(active: true).each {|chat| + assets = chat.assets(assets) + } + { + waiting_chat_count: waiting_chat_count, + running_chat_count: running_chat_count, + active_agents: active_agents, + 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) return { state: 'chat_disabled' } if !Setting.get('chat') assets = {} Chat.where(active: true).each {|chat| diff --git a/lib/sessions/event/chat_session_start.rb b/lib/sessions/event/chat_session_start.rb index 518b4fc41..202228939 100644 --- a/lib/sessions/event/chat_session_start.rb +++ b/lib/sessions/event/chat_session_start.rb @@ -39,14 +39,24 @@ class Sessions::Event::ChatSessionStart < Sessions::Event::ChatBase chat_id: chat_session.chat_id, }, } - chat_session.send_to_recipients(data) + # send to customer + chat_session.send_to_recipients(data, @client_id) + + # send to agent + data = { + event: 'chat_session_start', + data: { + session: chat_session.attributes, + }, + } + Sessions.send(@client_id, data) + + # send state update with sessions to agents + broadcast_agent_state_update # send position update to other waiting sessions broadcast_customer_state_update - # send state update to agents - broadcast_agent_state_update - nil end diff --git a/lib/sessions/event/chat_status_agent.rb b/lib/sessions/event/chat_status_agent.rb index 220f25723..dc27868f8 100644 --- a/lib/sessions/event/chat_status_agent.rb +++ b/lib/sessions/event/chat_status_agent.rb @@ -16,7 +16,7 @@ class Sessions::Event::ChatStatusAgent < Sessions::Event::ChatBase } { event: 'chat_status_agent', - data: Chat.agent_state(@session['id']), + data: Chat.agent_state_with_sessions(@session['id']), } end diff --git a/test/unit/chat_test.rb b/test/unit/chat_test.rb index 77fd9ed40..327c96948 100644 --- a/test/unit/chat_test.rb +++ b/test/unit/chat_test.rb @@ -58,7 +58,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal('offline', chat.customer_state[:state]) # check agent state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(0, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -78,7 +78,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal('online', chat.customer_state[:state]) # check agent state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(0, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -97,7 +97,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal('online', chat.customer_state[:state]) # check agent state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(1, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -117,7 +117,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal('online', chat.customer_state[:state]) # check agent1 state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(1, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -126,7 +126,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(true, agent_state[:active]) # check agent2 state - agent_state = Chat.agent_state(agent2.id) + agent_state = Chat.agent_state_with_sessions(agent2.id) assert_equal(1, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -143,7 +143,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal('online', chat.customer_state[:state]) # check agent1 state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(2, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -152,7 +152,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(true, agent_state[:active]) # check agent2 state - agent_state = Chat.agent_state(agent2.id) + agent_state = Chat.agent_state_with_sessions(agent2.id) assert_equal(2, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -178,7 +178,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal('no_seats_available', chat.customer_state[:state]) # check agent1 state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(6, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -187,7 +187,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(true, agent_state[:active]) # check agent2 state - agent_state = Chat.agent_state(agent2.id) + agent_state = Chat.agent_state_with_sessions(agent2.id) assert_equal(6, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -203,7 +203,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(5, chat.customer_state[:queue]) # check agent1 state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(5, agent_state[:waiting_chat_count]) assert_equal(1, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -212,7 +212,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(true, agent_state[:active]) # check agent2 state - agent_state = Chat.agent_state(agent2.id) + agent_state = Chat.agent_state_with_sessions(agent2.id) assert_equal(5, agent_state[:waiting_chat_count]) assert_equal(1, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -228,7 +228,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(5, chat.customer_state[:queue]) # check agent1 state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(5, agent_state[:waiting_chat_count]) assert_equal(1, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -237,7 +237,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(true, agent_state[:active]) # check agent2 state - agent_state = Chat.agent_state(agent2.id) + agent_state = Chat.agent_state_with_sessions(agent2.id) assert_equal(5, agent_state[:waiting_chat_count]) assert_equal(1, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -253,7 +253,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(5, chat.customer_state[:queue]) # check agent1 state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(5, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -262,7 +262,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(true, agent_state[:active]) # check agent2 state - agent_state = Chat.agent_state(agent2.id) + agent_state = Chat.agent_state_with_sessions(agent2.id) assert_equal(5, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -277,7 +277,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal('online', chat.customer_state[:state]) # check agent1 state - agent_state = Chat.agent_state(agent1.id) + agent_state = Chat.agent_state_with_sessions(agent1.id) assert_equal(3, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions]) @@ -286,7 +286,7 @@ class ChatTest < ActiveSupport::TestCase assert_equal(true, agent_state[:active]) # check agent2 state - agent_state = Chat.agent_state(agent2.id) + agent_state = Chat.agent_state_with_sessions(agent2.id) assert_equal(3, agent_state[:waiting_chat_count]) assert_equal(0, agent_state[:running_chat_count]) assert_equal([], agent_state[:active_sessions])