spool.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Event::Spool < Sessions::Event::Base
  3. =begin
  4. Event module to serve spool messages and send them to new client connection.
  5. To execute this manually, just paste the following into the browser console
  6. App.WebSocket.send({event:'spool'})
  7. =end
  8. def run
  9. # error handling
  10. if @payload['timestamp']
  11. log 'info', "request spool data > '#{Time.at(@payload['timestamp']).utc.iso8601}'"
  12. else
  13. log 'info', 'request spool with init data'
  14. end
  15. if !@session || !@session['id']
  16. log 'error', "Can't send spool, session not authenticated"
  17. return {
  18. event: 'error',
  19. data: {
  20. error: __('Can\'t send spool, session not authenticated'),
  21. },
  22. }
  23. end
  24. spool = Sessions.spool_list(@payload['timestamp'], @session['id'])
  25. spool.each do |item|
  26. # create new msg to push to client
  27. if item[:type] == 'direct'
  28. log 'info', "send spool to (user_id=#{@session['id']})"
  29. else
  30. log 'info', 'send spool'
  31. end
  32. websocket_send(@client_id, item[:message])
  33. end
  34. # send spool:sent event to client
  35. log 'info', 'send spool:sent event'
  36. {
  37. event: 'spool:sent',
  38. data: {
  39. timestamp: Time.now.utc.to_i,
  40. },
  41. }
  42. end
  43. end