2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-12-18 03:36:56 +00:00
|
|
|
class Chat::Session
|
|
|
|
module Search
|
2018-04-26 08:55:53 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
2017-12-18 03:36:56 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
search organizations preferences
|
|
|
|
|
|
|
|
result = Chat::Session.search_preferences(user_model)
|
|
|
|
|
|
|
|
returns if user has permissions to search
|
|
|
|
|
|
|
|
result = {
|
|
|
|
prio: 1000,
|
|
|
|
direct_search_index: true
|
|
|
|
}
|
|
|
|
|
|
|
|
returns if user has no permissions to search
|
|
|
|
|
|
|
|
result = false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
def search_preferences(current_user)
|
|
|
|
return false if Setting.get('chat') != true || !current_user.permissions?('chat.agent')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
prio: 900,
|
2018-04-26 08:55:53 +00:00
|
|
|
direct_search_index: true,
|
|
|
|
}
|
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
search organizations
|
|
|
|
|
|
|
|
result = Chat::Session.search(
|
|
|
|
current_user: User.find(123),
|
|
|
|
query: 'search something',
|
|
|
|
limit: 15,
|
2018-04-13 07:22:55 +00:00
|
|
|
offset: 100,
|
2017-12-18 03:36:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [organization_model1, organization_model2]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
def search(params)
|
|
|
|
|
|
|
|
# get params
|
|
|
|
query = params[:query]
|
|
|
|
limit = params[:limit] || 10
|
|
|
|
offset = params[:offset] || 0
|
|
|
|
current_user = params[:current_user]
|
|
|
|
|
|
|
|
# enable search only for agents and admins
|
|
|
|
return [] if !search_preferences(current_user)
|
|
|
|
|
|
|
|
# try search index backend
|
|
|
|
if SearchIndexBackend.enabled?
|
2018-11-06 16:11:10 +00:00
|
|
|
items = SearchIndexBackend.search(query, 'Chat::Session', limit: limit, from: offset)
|
2018-04-26 08:55:53 +00:00
|
|
|
chat_sessions = []
|
|
|
|
items.each do |item|
|
|
|
|
chat_session = Chat::Session.lookup(id: item[:id])
|
|
|
|
next if !chat_session
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
chat_sessions.push chat_session
|
|
|
|
end
|
|
|
|
return chat_sessions
|
2017-12-18 03:36:56 +00:00
|
|
|
end
|
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
# fallback do sql query
|
|
|
|
# - stip out * we already search for *query* -
|
|
|
|
query.delete! '*'
|
2020-07-07 06:30:20 +00:00
|
|
|
Chat::Session.where(
|
2018-04-26 08:55:53 +00:00
|
|
|
'name LIKE ?', "%#{query}%"
|
|
|
|
).order('name').offset(offset).limit(limit).to_a
|
2020-07-07 06:30:20 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|