2017-12-18 03:36:56 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Chat::Session
|
|
|
|
module Search
|
|
|
|
|
|
|
|
=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
|
|
|
|
|
|
|
|
def search_preferences(current_user)
|
|
|
|
return false if Setting.get('chat') != true || !current_user.permissions?('chat.agent')
|
|
|
|
{
|
|
|
|
prio: 900,
|
|
|
|
direct_search_index: true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=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
|
|
|
|
|
|
|
|
def search(params)
|
|
|
|
|
|
|
|
# get params
|
|
|
|
query = params[:query]
|
|
|
|
limit = params[:limit] || 10
|
2018-04-13 07:22:55 +00:00
|
|
|
offset = params[:offset] || 0
|
2017-12-18 03:36:56 +00:00
|
|
|
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-04-13 07:22:55 +00:00
|
|
|
items = SearchIndexBackend.search(query, limit, 'Chat::Session', {}, offset)
|
2017-12-18 03:36:56 +00:00
|
|
|
chat_sessions = []
|
|
|
|
items.each do |item|
|
|
|
|
chat_session = Chat::Session.lookup(id: item[:id])
|
|
|
|
next if !chat_session
|
|
|
|
chat_sessions.push chat_session
|
|
|
|
end
|
|
|
|
return chat_sessions
|
|
|
|
end
|
|
|
|
|
|
|
|
# fallback do sql query
|
|
|
|
# - stip out * we already search for *query* -
|
|
|
|
query.delete! '*'
|
|
|
|
chat_sessions = Chat::Session.where(
|
|
|
|
'name LIKE ?', "%#{query}%"
|
2018-04-13 07:22:55 +00:00
|
|
|
).order('name').offset(offset).limit(limit).to_a
|
2017-12-18 03:36:56 +00:00
|
|
|
chat_sessions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|