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]
|
|
|
|
@is_web_socket = true
|
|
|
|
end
|
|
|
|
|
|
|
|
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',
|
|
|
|
data: {
|
|
|
|
state: 'no_session',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if !@session['id']
|
|
|
|
error = {
|
|
|
|
event: 'error',
|
|
|
|
data: {
|
|
|
|
state: 'no_session_user_id',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
def permission_check(key, event)
|
2016-05-25 07:19:45 +00:00
|
|
|
if !@session
|
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
|
|
|
data: {
|
|
|
|
state: 'no_session',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if !@session['id']
|
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
|
|
|
data: {
|
|
|
|
state: 'no_session_user_id',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
user = User.lookup(id: @session['id'])
|
|
|
|
if !user
|
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
|
|
|
data: {
|
|
|
|
state: 'no_such_user',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
2016-08-12 16:39:09 +00:00
|
|
|
if !user.permissions?(key)
|
2016-05-25 07:19:45 +00:00
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
|
|
|
data: {
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|