chat_base.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Event::ChatBase < Sessions::Event::Base
  3. database_connection_required
  4. def run
  5. # check if feature is enabled
  6. return if Setting.get('chat')
  7. {
  8. event: 'chat_error',
  9. data: {
  10. state: 'chat_disabled',
  11. },
  12. }
  13. end
  14. def current_chat_session
  15. Chat::Session.find_by(session_id: @payload['data']['session_id'])
  16. end
  17. def check_chat_session_exists
  18. if !@payload['data'] || !@payload['data']['session_id']
  19. error = {
  20. event: 'chat_error',
  21. data: {
  22. state: __('Need session_id.'),
  23. },
  24. }
  25. Sessions.send(@client_id, error)
  26. return
  27. end
  28. return true if current_chat_session
  29. error = {
  30. event: 'chat_error',
  31. data: {
  32. state: "No such session id #{@payload['data']['session_id']}",
  33. },
  34. }
  35. Sessions.send(@client_id, error)
  36. false
  37. end
  38. def current_chat
  39. Chat.find_by(id: @payload['data']['chat_id'])
  40. end
  41. def check_chat_exists
  42. chat = current_chat
  43. return true if chat
  44. error = {
  45. event: 'chat_error',
  46. data: {
  47. state: 'no_such_chat',
  48. },
  49. }
  50. Sessions.send(@client_id, error)
  51. false
  52. end
  53. end