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-12 07:46:27 +00:00
|
|
|
@current_account = current_user.account || Current.account
|
2022-12-11 09:47:01 +00:00
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
{
|
|
|
|
messages: filter_messages,
|
|
|
|
conversations: filter_conversations,
|
|
|
|
contacts: filter_contacts
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def filter_conversations
|
2022-12-15 10:55:25 +00:00
|
|
|
conversation_ids = PgSearch.multisearch((@params[:q]).to_s).where(account_id: @current_account,
|
|
|
|
searchable_type: 'Conversation').pluck(:searchable_id)
|
2022-12-11 09:47:01 +00:00
|
|
|
@conversations = Conversation.where(id: conversation_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_messages
|
2022-12-15 10:55:25 +00:00
|
|
|
message_ids = PgSearch.multisearch((@params[:q]).to_s).where(account_id: @current_account, searchable_type: 'Message').pluck(:searchable_id)
|
2022-12-11 09:47:01 +00:00
|
|
|
@messages = Message.where(id: message_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_contacts
|
2022-12-15 10:55:25 +00:00
|
|
|
contact_ids = PgSearch.multisearch((@params[:q]).to_s).where(account_id: @current_account, searchable_type: 'Contact').pluck(:searchable_id)
|
2022-12-11 09:47:01 +00:00
|
|
|
@contacts = Contact.where(id: contact_ids)
|
|
|
|
end
|
|
|
|
end
|