2015-12-09 13:09:37 +00:00
|
|
|
class Sessions::Event::Base
|
|
|
|
|
|
|
|
def initialize(params)
|
2017-10-01 12:25:52 +00:00
|
|
|
params.each do |key, value|
|
2015-12-09 13:09:37 +00:00
|
|
|
instance_variable_set "@#{key}", value
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
|
|
|
@is_web_socket = false
|
|
|
|
return if !@clients[@client_id]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
@is_web_socket = true
|
2018-11-02 17:42:57 +00:00
|
|
|
|
|
|
|
return if !self.class.instance_variable_get(:@database_connection)
|
|
|
|
|
2018-12-03 11:34:56 +00:00
|
|
|
if ActiveRecord::Base.connected?
|
|
|
|
@reused_connection = true
|
|
|
|
else
|
|
|
|
@reused_connection = false
|
|
|
|
ActiveRecord::Base.establish_connection
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
end
|
|
|
|
|
2018-11-17 06:34:45 +00:00
|
|
|
def self.inherited(subclass)
|
|
|
|
subclass.instance_variable_set(:@database_connection, @database_connection)
|
|
|
|
end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def websocket_send(recipient_client_id, data)
|
2016-01-15 17:22:57 +00:00
|
|
|
msg = if data.class != Array
|
|
|
|
"[#{data.to_json}]"
|
|
|
|
else
|
|
|
|
data.to_json
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
if @clients[recipient_client_id]
|
|
|
|
log 'debug', "ws send #{msg}", recipient_client_id
|
|
|
|
@clients[recipient_client_id][:websocket].send(msg)
|
|
|
|
else
|
|
|
|
log 'debug', "fs send #{msg}", recipient_client_id
|
|
|
|
Sessions.send(recipient_client_id, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
def valid_session?
|
|
|
|
if !@session
|
|
|
|
error = {
|
|
|
|
event: 'error',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-03-03 01:51:24 +00:00
|
|
|
state: 'no_session',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if !@session['id']
|
|
|
|
error = {
|
|
|
|
event: 'error',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-03-03 01:51:24 +00:00
|
|
|
state: 'no_session_user_id',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-12-18 03:36:56 +00:00
|
|
|
def current_user_id
|
2016-05-25 07:19:45 +00:00
|
|
|
if !@session
|
|
|
|
error = {
|
2019-11-21 07:47:05 +00:00
|
|
|
event: "#{@event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_session',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
if @session['id'].blank?
|
2016-05-25 07:19:45 +00:00
|
|
|
error = {
|
2019-11-21 07:47:05 +00:00
|
|
|
event: "#{@event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_session_user_id',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
@session['id']
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
user_id = current_user_id
|
|
|
|
return if !user_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-12-18 03:36:56 +00:00
|
|
|
user = User.find_by(id: user_id)
|
2016-05-25 07:19:45 +00:00
|
|
|
if !user
|
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_such_user',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def permission_check(key, event)
|
|
|
|
user = current_user
|
|
|
|
return if !user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
if !user.permissions?(key)
|
2016-05-25 07:19:45 +00:00
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_permission',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def log(level, data, client_id = nil)
|
|
|
|
if !@options[:v]
|
|
|
|
return if level == 'debug'
|
|
|
|
end
|
|
|
|
if !client_id
|
|
|
|
client_id = @client_id
|
|
|
|
end
|
|
|
|
# rubocop:disable Rails/Output
|
|
|
|
puts "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
|
|
|
|
#puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
|
|
|
|
# rubocop:enable Rails/Output
|
2018-11-02 17:42:57 +00:00
|
|
|
#Rails.logger.info "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.database_connection_required
|
|
|
|
@database_connection = true
|
2015-12-09 13:09:37 +00:00
|
|
|
end
|
|
|
|
|
2018-11-02 17:42:57 +00:00
|
|
|
def destroy
|
|
|
|
return if !@is_web_socket
|
|
|
|
return if !self.class.instance_variable_get(:@database_connection)
|
2018-12-03 11:34:56 +00:00
|
|
|
return if @reused_connection
|
2018-11-02 17:42:57 +00:00
|
|
|
|
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
|
|
|
end
|