trabajo-afectivo/lib/sessions/event/chat_session_close.rb

74 lines
1.7 KiB
Ruby
Raw Normal View History

class Sessions::Event::ChatSessionClose < Sessions::Event::ChatBase
2015-11-10 14:01:04 +00:00
def run
2015-11-10 14:01:04 +00:00
if !@data['data'] || !@data['data']['session_id']
2015-11-10 14:01:04 +00:00
return {
event: 'chat_status_close',
data: {
state: 'Need session_id.',
},
}
end
chat_session = Chat::Session.find_by(session_id: @data['data']['session_id'])
2015-11-10 14:01:04 +00:00
if !chat_session
return {
event: 'chat_status_close',
data: {
state: "No such session id #{@data['data']['session_id']}",
2015-11-10 14:01:04 +00:00
},
}
end
realname = 'anonymous'
if @session && @session['id']
realname = User.find(@session['id']).fullname
end
# 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: {
session_id: chat_session.session_id,
realname: realname,
},
}
# 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',
data: {
session_id: chat_session.session_id,
realname: realname,
},
}
end
chat_session.send_to_recipients(message, @client_id)
2015-11-10 14:01:04 +00:00
# notifiy participients
2015-11-10 14:01:04 +00:00
{
event: 'chat_status_close',
data: {
state: 'ok',
session_id: chat_session.session_id,
},
}
end
end