base.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 remote_ip
  97. @headers&.fetch('X-Forwarded-For', nil).presence
  98. end
  99. def origin
  100. @headers&.fetch('Origin', nil).presence
  101. end
  102. def permission_check(key, event)
  103. user = current_user
  104. return if !user
  105. if !user.permissions?(key)
  106. error = {
  107. event: "#{event}_error",
  108. data: {
  109. state: 'no_permission',
  110. },
  111. }
  112. Sessions.send(@client_id, error)
  113. return
  114. end
  115. true
  116. end
  117. def log(level, data, client_id = nil)
  118. if !@options[:v]
  119. return if level == 'debug'
  120. end
  121. if !client_id
  122. client_id = @client_id
  123. end
  124. # rubocop:disable Rails/Output
  125. puts "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
  126. #puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
  127. # rubocop:enable Rails/Output
  128. #Rails.logger.info "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
  129. end
  130. def self.database_connection_required
  131. @database_connection = true
  132. end
  133. def destroy
  134. return if !@is_web_socket
  135. return if !self.class.instance_variable_get(:@database_connection)
  136. return if @reused_connection
  137. ActiveRecord::Base.remove_connection
  138. end
  139. end