2015-12-09 13:09:37 +00:00
|
|
|
class Sessions::Event::ChatBase < Sessions::Event::Base
|
2015-11-11 13:10:26 +00:00
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def initialize(params)
|
|
|
|
super(params)
|
|
|
|
return if !@is_web_socket
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
ActiveRecord::Base.establish_connection
|
2015-11-11 13:10:26 +00:00
|
|
|
end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def destroy
|
|
|
|
return if !@is_web_socket
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2015-11-11 13:10:26 +00:00
|
|
|
|
|
|
|
# check if feature is enabled
|
2015-11-12 13:19:41 +00:00
|
|
|
return if Setting.get('chat')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-12 13:19:41 +00:00
|
|
|
{
|
|
|
|
event: 'chat_error',
|
|
|
|
data: {
|
|
|
|
state: 'chat_disabled',
|
|
|
|
},
|
|
|
|
}
|
2015-11-11 13:10:26 +00:00
|
|
|
end
|
|
|
|
|
2015-11-25 09:33:39 +00:00
|
|
|
def current_chat_session
|
2015-12-09 13:09:37 +00:00
|
|
|
Chat::Session.find_by(session_id: @payload['data']['session_id'])
|
2015-11-25 09:33:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_chat_session_exists
|
2015-12-09 13:09:37 +00:00
|
|
|
if !@payload['data'] || !@payload['data']['session_id']
|
2015-11-25 09:33:39 +00:00
|
|
|
error = {
|
|
|
|
event: 'chat_error',
|
|
|
|
data: {
|
|
|
|
state: 'Need session_id.',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return true if current_chat_session
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-25 09:33:39 +00:00
|
|
|
error = {
|
|
|
|
event: 'chat_error',
|
|
|
|
data: {
|
2015-12-09 13:09:37 +00:00
|
|
|
state: "No such session id #{@payload['data']['session_id']}",
|
2015-11-25 09:33:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_chat
|
2015-12-09 13:09:37 +00:00
|
|
|
Chat.find_by(id: @payload['data']['chat_id'])
|
2015-11-25 09:33:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_chat_exists
|
|
|
|
chat = current_chat
|
|
|
|
return true if chat
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-25 09:33:39 +00:00
|
|
|
error = {
|
|
|
|
event: 'chat_error',
|
|
|
|
data: {
|
|
|
|
state: 'no_such_chat',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
end
|