From c58a73f68421266cf6dec4bd42392992dd5d47ba Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 23 Oct 2016 20:40:55 +0200 Subject: [PATCH] Fixed issue#277 - chat history wrong on app/page reload. --- app/models/chat/session.rb | 8 ++--- test/unit/chat_test.rb | 62 +++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/app/models/chat/session.rb b/app/models/chat/session.rb index 14d5a2d91..7f7b41e7b 100644 --- a/app/models/chat/session.rb +++ b/app/models/chat/session.rb @@ -41,7 +41,7 @@ class Chat::Session < ApplicationModel def position return if state != 'waiting' position = 0 - Chat::Session.where(state: 'waiting').order('created_at ASC').each { |chat_session| + Chat::Session.where(state: 'waiting').order(created_at: :asc).each { |chat_session| position += 1 break if chat_session.id == id } @@ -52,7 +52,7 @@ class Chat::Session < ApplicationModel chat_session = Chat::Session.find_by(session_id: session_id) return if !chat_session session_attributes = [] - Chat::Message.where(chat_session_id: chat_session.id).each { |message| + Chat::Message.where(chat_session_id: chat_session.id).order(created_at: :asc).each { |message| session_attributes.push message.attributes } session_attributes @@ -60,10 +60,10 @@ class Chat::Session < ApplicationModel def self.active_chats_by_user_id(user_id) actice_sessions = [] - Chat::Session.where(state: 'running', user_id: user_id).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['messages'] = [] - Chat::Message.where(chat_session_id: session.id).each { |message| + Chat::Message.where(chat_session_id: session.id).order(created_at: :asc).each { |message| session_attributes['messages'].push message.attributes } actice_sessions.push session_attributes diff --git a/test/unit/chat_test.rb b/test/unit/chat_test.rb index faab5a402..b94b16477 100644 --- a/test/unit/chat_test.rb +++ b/test/unit/chat_test.rb @@ -199,18 +199,64 @@ class ChatTest < ActiveSupport::TestCase assert_equal(6, agent_state[:seads_total]) assert_equal(true, agent_state[:active]) + chat_session6.user_id = agent1.id chat_session6.state = 'running' chat_session6.save + Chat::Message.create( + chat_session_id: chat_session6.id, + content: 'message 1', + created_by_id: agent1.id, + ) + sleep 1 + Chat::Message.create( + chat_session_id: chat_session6.id, + content: 'message 2', + created_by_id: agent1.id, + ) + sleep 1 + Chat::Message.create( + chat_session_id: chat_session6.id, + content: 'message 3', + created_by_id: agent1.id, + ) + sleep 1 + Chat::Message.create( + chat_session_id: chat_session6.id, + content: 'message 4', + created_by_id: nil, + ) + # check customer state - assert_equal('no_seats_available', chat.customer_state[:state]) - assert_equal(5, chat.customer_state[:queue]) + customer_state = chat.customer_state + assert_equal('no_seats_available', customer_state[:state]) + assert_equal(5, customer_state[:queue]) + + # customer chat state + customer_state = chat.customer_state(chat_session6.session_id) + assert_equal('reconnect', customer_state[:state]) + assert(customer_state[:session]) + assert_equal(Array, customer_state[:session].class) + assert_equal('message 1', customer_state[:session][0]['content']) + assert_equal('message 2', customer_state[:session][1]['content']) + assert_equal('message 3', customer_state[:session][2]['content']) + assert_equal('message 4', customer_state[:session][3]['content']) + assert_equal('Notification Agent1', customer_state[:agent][:name]) + assert_equal(nil, customer_state[:agent][:avatar]) # check agent1 state 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]) + assert_equal(Array, agent_state[:active_sessions].class) + assert_equal(chat.id, agent_state[:active_sessions][0]['chat_id']) + assert_equal(agent1.id, agent_state[:active_sessions][0]['user_id']) + assert(agent_state[:active_sessions][0]['messages']) + assert_equal(Array, agent_state[:active_sessions][0]['messages'].class) + assert_equal('message 1', agent_state[:active_sessions][0]['messages'][0]['content']) + assert_equal('message 2', agent_state[:active_sessions][0]['messages'][1]['content']) + assert_equal('message 3', agent_state[:active_sessions][0]['messages'][2]['content']) + assert_equal('message 4', agent_state[:active_sessions][0]['messages'][3]['content']) assert_equal(0, agent_state[:seads_available]) assert_equal(6, agent_state[:seads_total]) assert_equal(true, agent_state[:active]) @@ -235,7 +281,15 @@ class ChatTest < ActiveSupport::TestCase 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]) + assert_equal(Array, agent_state[:active_sessions].class) + assert_equal(chat.id, agent_state[:active_sessions][0]['chat_id']) + assert_equal(agent1.id, agent_state[:active_sessions][0]['user_id']) + assert(agent_state[:active_sessions][0]['messages']) + assert_equal(Array, agent_state[:active_sessions][0]['messages'].class) + assert_equal('message 1', agent_state[:active_sessions][0]['messages'][0]['content']) + assert_equal('message 2', agent_state[:active_sessions][0]['messages'][1]['content']) + assert_equal('message 3', agent_state[:active_sessions][0]['messages'][2]['content']) + assert_equal('message 4', agent_state[:active_sessions][0]['messages'][3]['content']) assert_equal(-2, agent_state[:seads_available]) assert_equal(4, agent_state[:seads_total]) assert_equal(true, agent_state[:active])