session.rb 3.1 KB

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