client.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Client
  3. def initialize(client_id, node_id)
  4. @client_id = client_id
  5. @node_id = node_id
  6. log '---client start ws connection---'
  7. fetch
  8. log '---client exiting ws connection---'
  9. end
  10. def fetch
  11. backends = [
  12. 'Sessions::Backend::TicketOverviewList',
  13. 'Sessions::Backend::ActivityStream',
  14. ]
  15. asset_lookup = {}
  16. backend_pool = []
  17. user_id_last_run = nil
  18. user_updated_at_last_run = nil
  19. loop_count = 0
  20. loop do
  21. # check if session still exists
  22. return if !Sessions.session_exists?(@client_id)
  23. # get connection user
  24. session_data = Sessions.get(@client_id)
  25. return if !session_data
  26. return if !session_data[:user]
  27. return if !session_data[:user]['id']
  28. user = User.lookup(id: session_data[:user]['id'])
  29. return if !user
  30. UserInfo.current_user_id = user.id
  31. # init new backends
  32. if user_id_last_run != user.id
  33. user_id_last_run = user.id
  34. asset_lookup = {}
  35. # release old objects
  36. backend_pool.collect! do
  37. nil
  38. end
  39. # create new pool
  40. backend_pool = []
  41. backends.each do |backend|
  42. item = backend.constantize.new(user, asset_lookup, self, @client_id)
  43. backend_pool.push item
  44. end
  45. # update user if required
  46. elsif user_updated_at_last_run != user.updated_at
  47. user_updated_at_last_run = user.updated_at
  48. log "---client - updating user #{user.id} - #{user_updated_at_last_run}"
  49. backend_pool.each do |backend|
  50. backend.user = user
  51. end
  52. end
  53. loop_count += 1
  54. log "---client - looking for data of user #{user.id}"
  55. # push messages from backends
  56. backend_pool.each(&:push)
  57. log '---/client-'
  58. # start faster in the beginnig
  59. if loop_count < 20
  60. sleep 1
  61. else
  62. sleep 2.2
  63. end
  64. end
  65. end
  66. # send update to browser
  67. def send(data) # rubocop:disable Zammad/ForbidDefSend
  68. Sessions.send(@client_id, data)
  69. end
  70. def log(msg)
  71. Rails.logger.debug { "client(#{@node_id}.#{@client_id}) #{msg}" }
  72. end
  73. end