chat_base.rb 1.3 KB

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