session.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Chat::Session < ApplicationModel
  3. include HasSearchIndexBackend
  4. include HasTags
  5. include Chat::Session::Search
  6. include Chat::Session::SearchIndex
  7. include Chat::Session::Assets
  8. # rubocop:disable Rails/InverseOf
  9. has_many :messages, class_name: 'Chat::Message', foreign_key: 'chat_session_id', dependent: :destroy
  10. belongs_to :user, class_name: 'User', optional: true
  11. belongs_to :chat, class_name: 'Chat'
  12. # rubocop:enable Rails/InverseOf
  13. before_create :generate_session_id
  14. store :preferences
  15. def agent_user
  16. return if user_id.blank?
  17. user = User.lookup(id: user_id)
  18. return if user.blank?
  19. fullname = user.fullname
  20. chat_preferences = user.preferences[:chat] || {}
  21. if chat_preferences[:alternative_name].present?
  22. fullname = chat_preferences[:alternative_name]
  23. end
  24. url = nil
  25. if user.image && user.image != 'none' && chat_preferences[:avatar_state] != 'disabled'
  26. url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/users/image/#{user.image}"
  27. end
  28. {
  29. name: fullname,
  30. avatar: url,
  31. }
  32. end
  33. def generate_session_id
  34. self.session_id = Digest::MD5.hexdigest(SecureRandom.uuid)
  35. end
  36. def add_recipient(client_id, store = false)
  37. if !preferences[:participants]
  38. preferences[:participants] = []
  39. end
  40. return preferences[:participants] if preferences[:participants].include?(client_id)
  41. preferences[:participants].push client_id
  42. if store
  43. save
  44. end
  45. preferences[:participants]
  46. end
  47. def recipients_active?
  48. return true if !preferences
  49. return true if !preferences[:participants]
  50. count = 0
  51. preferences[:participants].each do |client_id|
  52. next if !Sessions.session_exists?(client_id)
  53. count += 1
  54. end
  55. return true if count >= 2
  56. false
  57. end
  58. def send_to_recipients(message, ignore_client_id = nil)
  59. preferences[:participants].each do |local_client_id|
  60. next if local_client_id == ignore_client_id
  61. Sessions.send(local_client_id, message)
  62. end
  63. true
  64. end
  65. def position
  66. return if state != 'waiting'
  67. position = 0
  68. Chat::Session.where(state: 'waiting').order(created_at: :asc).each do |chat_session|
  69. position += 1
  70. break if chat_session.id == id
  71. end
  72. position
  73. end
  74. def self.messages_by_session_id(session_id)
  75. chat_session = Chat::Session.find_by(session_id: session_id)
  76. return if !chat_session
  77. session_attributes = []
  78. Chat::Message.where(chat_session_id: chat_session.id).order(created_at: :asc).each do |message|
  79. session_attributes.push message.attributes
  80. end
  81. session_attributes
  82. end
  83. def self.active_chats_by_user_id(user_id)
  84. actice_sessions = []
  85. Chat::Session.where(state: 'running', user_id: user_id).order(created_at: :asc).each do |session|
  86. session_attributes = session.attributes
  87. session_attributes['messages'] = []
  88. Chat::Message.where(chat_session_id: session.id).order(created_at: :asc).each do |message|
  89. session_attributes['messages'].push message.attributes
  90. end
  91. actice_sessions.push session_attributes
  92. end
  93. actice_sessions
  94. end
  95. end