Chatwoot/app/finders/text_search.rb

45 lines
1.5 KiB
Ruby
Raw Normal View History

2022-12-11 09:47:01 +00:00
class TextSearch
attr_reader :current_user, :current_account, :params
DEFAULT_STATUS = 'open'.freeze
def initialize(current_user, params)
2022-12-23 10:13:01 +00:00
@current_user = current_user
@current_account = @current_user.account || Current.account
2022-12-11 09:47:01 +00:00
@params = params
end
def perform
2022-12-23 10:13:01 +00:00
set_inboxes
2022-12-11 09:47:01 +00:00
{
messages: filter_messages,
conversations: filter_conversations,
contacts: filter_contacts
}
end
2022-12-23 10:13:01 +00:00
def set_inboxes
@inbox_ids = @current_user.assigned_inboxes.pluck(:id)
end
2022-12-11 09:47:01 +00:00
private
def filter_conversations
2022-12-23 10:13:01 +00:00
@conversations = PgSearch.multisearch((@params[:q]).to_s).where(
inbox_id: @inbox_ids, account_id: @current_account, searchable_type: 'Conversation'
).joins('INNER JOIN conversations ON pg_search_documents.searchable_id = conversations.id').includes(:searchable).limit(20).collect(&:searchable)
2022-12-11 09:47:01 +00:00
end
def filter_messages
2022-12-23 10:13:01 +00:00
@messages = PgSearch.multisearch((@params[:q]).to_s).where(
inbox_id: @inbox_ids, account_id: @current_account, searchable_type: 'Message'
).joins('INNER JOIN messages ON pg_search_documents.searchable_id = messages.id').includes(:searchable).limit(20).collect(&:searchable)
2022-12-11 09:47:01 +00:00
end
def filter_contacts
2022-12-23 10:13:01 +00:00
@contacts = PgSearch.multisearch((@params[:q]).to_s).where(
inbox_id: @inbox_ids, account_id: @current_account, searchable_type: 'Contact'
).joins('INNER JOIN contacts ON pg_search_documents.searchable_id = contacts.id').includes(:searchable).limit(20).collect(&:searchable)
2022-12-11 09:47:01 +00:00
end
end