chat_base.rb 1.2 KB

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