base.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. class Sessions::Event::Base
  2. def initialize(params)
  3. params.each do |key, value|
  4. instance_variable_set "@#{key}", value
  5. end
  6. @is_web_socket = false
  7. return if !@clients[@client_id]
  8. @is_web_socket = true
  9. return if !self.class.instance_variable_get(:@database_connection)
  10. if ActiveRecord::Base.connected?
  11. @reused_connection = true
  12. else
  13. @reused_connection = false
  14. ActiveRecord::Base.establish_connection
  15. end
  16. end
  17. def self.inherited(subclass)
  18. subclass.instance_variable_set(:@database_connection, @database_connection)
  19. end
  20. def websocket_send(recipient_client_id, data)
  21. msg = if data.class != Array
  22. "[#{data.to_json}]"
  23. else
  24. data.to_json
  25. end
  26. if @clients[recipient_client_id]
  27. log 'debug', "ws send #{msg}", recipient_client_id
  28. @clients[recipient_client_id][:websocket].send(msg)
  29. else
  30. log 'debug', "fs send #{msg}", recipient_client_id
  31. Sessions.send(recipient_client_id, data)
  32. end
  33. end
  34. def valid_session?
  35. if !@session
  36. error = {
  37. event: 'error',
  38. data: {
  39. state: 'no_session',
  40. },
  41. }
  42. Sessions.send(@client_id, error)
  43. return
  44. end
  45. if !@session['id']
  46. error = {
  47. event: 'error',
  48. data: {
  49. state: 'no_session_user_id',
  50. },
  51. }
  52. Sessions.send(@client_id, error)
  53. return
  54. end
  55. true
  56. end
  57. def current_user_id
  58. if !@session
  59. error = {
  60. event: "#{event}_error",
  61. data: {
  62. state: 'no_session',
  63. },
  64. }
  65. Sessions.send(@client_id, error)
  66. return
  67. end
  68. if @session['id'].blank?
  69. error = {
  70. event: "#{event}_error",
  71. data: {
  72. state: 'no_session_user_id',
  73. },
  74. }
  75. Sessions.send(@client_id, error)
  76. return
  77. end
  78. @session['id']
  79. end
  80. def current_user
  81. user_id = current_user_id
  82. return if !user_id
  83. user = User.find_by(id: user_id)
  84. if !user
  85. error = {
  86. event: "#{event}_error",
  87. data: {
  88. state: 'no_such_user',
  89. },
  90. }
  91. Sessions.send(@client_id, error)
  92. return
  93. end
  94. user
  95. end
  96. def permission_check(key, event)
  97. user = current_user
  98. return if !user
  99. if !user.permissions?(key)
  100. error = {
  101. event: "#{event}_error",
  102. data: {
  103. state: 'no_permission',
  104. },
  105. }
  106. Sessions.send(@client_id, error)
  107. return
  108. end
  109. true
  110. end
  111. def log(level, data, client_id = nil)
  112. if !@options[:v]
  113. return if level == 'debug'
  114. end
  115. if !client_id
  116. client_id = @client_id
  117. end
  118. # rubocop:disable Rails/Output
  119. puts "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
  120. #puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
  121. # rubocop:enable Rails/Output
  122. #Rails.logger.info "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
  123. end
  124. def self.database_connection_required
  125. @database_connection = true
  126. end
  127. def destroy
  128. return if !@is_web_socket
  129. return if !self.class.instance_variable_get(:@database_connection)
  130. return if @reused_connection
  131. ActiveRecord::Base.remove_connection
  132. end
  133. end