2015-11-13 14:15:44 +00:00
|
|
|
class Chat::Session < ApplicationModel
|
2017-12-18 03:36:56 +00:00
|
|
|
include HasSearchIndexBackend
|
|
|
|
include HasTags
|
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
include Chat::Session::Search
|
2017-12-18 03:36:56 +00:00
|
|
|
include Chat::Session::SearchIndex
|
|
|
|
include Chat::Session::Assets
|
|
|
|
|
2018-04-12 14:57:37 +00:00
|
|
|
# rubocop:disable Rails/InverseOf
|
2020-07-10 13:30:44 +00:00
|
|
|
has_many :messages, class_name: 'Chat::Message', foreign_key: 'chat_session_id'
|
|
|
|
belongs_to :user, class_name: 'User', optional: true
|
|
|
|
belongs_to :chat, class_name: 'Chat'
|
2018-04-12 14:57:37 +00:00
|
|
|
# rubocop:enable Rails/InverseOf
|
2017-12-18 03:36:56 +00:00
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
before_create :generate_session_id
|
2018-04-12 14:57:37 +00:00
|
|
|
|
|
|
|
store :preferences
|
2015-11-13 14:15:44 +00:00
|
|
|
|
2019-11-21 07:47:05 +00:00
|
|
|
def agent_user
|
|
|
|
return if user_id.blank?
|
|
|
|
|
|
|
|
user = User.lookup(id: user_id)
|
|
|
|
return if user.blank?
|
|
|
|
|
|
|
|
fullname = user.fullname
|
|
|
|
chat_preferences = user.preferences[:chat] || {}
|
|
|
|
if chat_preferences[:alternative_name].present?
|
|
|
|
fullname = chat_preferences[:alternative_name]
|
|
|
|
end
|
|
|
|
url = nil
|
|
|
|
if user.image && user.image != 'none' && chat_preferences[:avatar_state] != 'disabled'
|
|
|
|
url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/users/image/#{user.image}"
|
|
|
|
end
|
|
|
|
{
|
|
|
|
name: fullname,
|
|
|
|
avatar: url,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
def generate_session_id
|
|
|
|
self.session_id = Digest::MD5.hexdigest(Time.zone.now.to_s + rand(99_999_999_999_999).to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_recipient(client_id, store = false)
|
|
|
|
if !preferences[:participants]
|
|
|
|
preferences[:participants] = []
|
|
|
|
end
|
|
|
|
return preferences[:participants] if preferences[:participants].include?(client_id)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
preferences[:participants].push client_id
|
|
|
|
if store
|
|
|
|
save
|
|
|
|
end
|
|
|
|
preferences[:participants]
|
|
|
|
end
|
|
|
|
|
2015-12-05 19:41:14 +00:00
|
|
|
def recipients_active?
|
|
|
|
return true if !preferences
|
|
|
|
return true if !preferences[:participants]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-05 19:41:14 +00:00
|
|
|
count = 0
|
2017-10-01 12:25:52 +00:00
|
|
|
preferences[:participants].each do |client_id|
|
2015-12-05 19:41:14 +00:00
|
|
|
next if !Sessions.session_exists?(client_id)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-05 19:41:14 +00:00
|
|
|
count += 1
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-05 19:41:14 +00:00
|
|
|
return true if count >= 2
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-05 19:41:14 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
def send_to_recipients(message, ignore_client_id = nil)
|
2017-10-01 12:25:52 +00:00
|
|
|
preferences[:participants].each do |local_client_id|
|
2015-11-13 14:15:44 +00:00
|
|
|
next if local_client_id == ignore_client_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
Sessions.send(local_client_id, message)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-11-13 14:15:44 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def position
|
|
|
|
return if state != 'waiting'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
position = 0
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Session.where(state: 'waiting').order(created_at: :asc).each do |chat_session|
|
2015-11-13 14:15:44 +00:00
|
|
|
position += 1
|
|
|
|
break if chat_session.id == id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-11-13 14:15:44 +00:00
|
|
|
position
|
|
|
|
end
|
2015-11-13 16:03:58 +00:00
|
|
|
|
|
|
|
def self.messages_by_session_id(session_id)
|
|
|
|
chat_session = Chat::Session.find_by(session_id: session_id)
|
|
|
|
return if !chat_session
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-13 16:03:58 +00:00
|
|
|
session_attributes = []
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Message.where(chat_session_id: chat_session.id).order(created_at: :asc).each do |message|
|
2015-11-13 16:03:58 +00:00
|
|
|
session_attributes.push message.attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-11-13 16:03:58 +00:00
|
|
|
session_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.active_chats_by_user_id(user_id)
|
|
|
|
actice_sessions = []
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Session.where(state: 'running', user_id: user_id).order(created_at: :asc).each do |session|
|
2015-11-13 16:03:58 +00:00
|
|
|
session_attributes = session.attributes
|
|
|
|
session_attributes['messages'] = []
|
2017-10-01 12:25:52 +00:00
|
|
|
Chat::Message.where(chat_session_id: session.id).order(created_at: :asc).each do |message|
|
2015-11-13 16:03:58 +00:00
|
|
|
session_attributes['messages'].push message.attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-11-13 16:03:58 +00:00
|
|
|
actice_sessions.push session_attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-11-13 16:03:58 +00:00
|
|
|
actice_sessions
|
|
|
|
end
|
2015-11-13 14:15:44 +00:00
|
|
|
end
|