From 87712a5b347d34780e17f2a5be52f90e2561954c Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 12 Nov 2015 23:56:56 +0100 Subject: [PATCH] Broadcast agent state update after closing or adding a chat session. --- lib/sessions/event/chat_base.rb | 12 ++++++++++++ lib/sessions/event/chat_session_close.rb | 15 +++++++++++++-- lib/sessions/event/chat_session_start.rb | 2 ++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/sessions/event/chat_base.rb b/lib/sessions/event/chat_base.rb index bdc790cdd..e687cf2a8 100644 --- a/lib/sessions/event/chat_base.rb +++ b/lib/sessions/event/chat_base.rb @@ -23,4 +23,16 @@ class Sessions::Event::ChatBase false end + def broadcast_agent_state_update + + # send broadcast to agents + Chat::Agent.where(active: true).each {|item| + data = { + event: 'chat_status_agent', + data: Chat.agent_state(item.updated_by_id), + } + Sessions.send_to(item.updated_by_id, data) + } + end + end diff --git a/lib/sessions/event/chat_session_close.rb b/lib/sessions/event/chat_session_close.rb index 4856dfe63..d0f0005ad 100644 --- a/lib/sessions/event/chat_session_close.rb +++ b/lib/sessions/event/chat_session_close.rb @@ -26,8 +26,14 @@ class Sessions::Event::ChatSessionClose < Sessions::Event::ChatBase realname = User.find(@session['id']).fullname end - # notify about "leaving" - if @session && chat_session.user_id == @session['id'] + # check count of participents + participents_count = 0 + if chat_session.preferences[:participents] + participents_count = chat_session.preferences[:participents].count + end + + # notify about "closing" + if participents_count < 2 || (@session && chat_session.user_id == @session['id']) message = { event: 'chat_session_closed', data: { @@ -39,6 +45,11 @@ class Sessions::Event::ChatSessionClose < Sessions::Event::ChatBase # close session if host is closing it chat_session.state = 'closed' chat_session.save + + # set state update to all agents + broadcast_agent_state_update + + # notify about "leaving" else message = { event: 'chat_session_left', diff --git a/lib/sessions/event/chat_session_start.rb b/lib/sessions/event/chat_session_start.rb index 8de9fce3f..4f14df7f9 100644 --- a/lib/sessions/event/chat_session_start.rb +++ b/lib/sessions/event/chat_session_start.rb @@ -53,6 +53,8 @@ class Sessions::Event::ChatSessionStart < Sessions::Event::ChatBase local_chat_session.send_to_recipients(data) } + broadcast_agent_state_update + nil end end