feat(subject-search): Use basic LIKE query and date ordering to find likely emails

Summary: Begins to fix T1866, but for now only doing a LIKE instead of FTS4, because there are many, many other things to think about there.

Test Plan: No new tests. I still want to rewrite the whole search bar.

Reviewers: evan

Reviewed By: evan

Maniphest Tasks: T1866

Differential Revision: https://phab.nylas.com/D1634
This commit is contained in:
Ben Gotow 2015-06-15 18:22:41 -07:00
parent 646e159a69
commit e090dd428a
3 changed files with 63 additions and 16 deletions

View file

@ -64,6 +64,8 @@ class SearchBar extends React.Component
<Menu.Item divider={item.divider} />
else if item.contact
<Menu.NameEmailItem name={item.contact.name} email={item.contact.email} />
else if item.thread
item.thread.subject
else
item.label
@ -73,7 +75,7 @@ class SearchBar extends React.Component
headerComponents={headerComponents}
items={@state.suggestions}
itemContent={itemContentFunc}
itemKey={ (item) -> item.label }
itemKey={ (item) -> item.id ? item.label }
onSelect={@_onSelectSuggestion}
/>
</div>
@ -109,7 +111,11 @@ class SearchBar extends React.Component
@_onClearSearch()
_onSelectSuggestion: (item) =>
Actions.searchQueryCommitted(item.value)
if item.thread?
Actions.searchQueryCommitted(null)
Actions.setFocus({collection: 'thread', item: item.thread})
else
Actions.searchQueryCommitted(item.value)
_onClearSearch: (event) =>
Actions.searchQueryCommitted(null)

View file

@ -1,6 +1,8 @@
Reflux = require 'reflux'
{Actions,
Contact,
Thread,
DatabaseStore,
ContactStore} = require 'nylas-exports'
_ = require 'underscore'
@ -11,9 +13,9 @@ _ = require 'underscore'
SearchSuggestionStore = Reflux.createStore
init: ->
@_suggestions = []
@_query = ""
@_committedQuery = ""
@_clearResults()
@listenTo Actions.searchQueryChanged, @onSearchQueryChanged
@listenTo Actions.searchQueryCommitted, @onSearchQueryCommitted
@ -21,38 +23,69 @@ SearchSuggestionStore = Reflux.createStore
onSearchQueryChanged: (query) ->
@_query = query
@repopulate()
@_rebuildResults()
onSearchQueryCommitted: (query) ->
@_query = query
@_committedQuery = query
@_suggestions = []
@_clearResults()
@trigger()
onSearchBlurred: ->
@_suggestions = []
@_clearResults()
@trigger()
repopulate: ->
_clearResults: ->
@_threadResults = null
@_contactResults = null
@_suggestions = []
term = @_query?[0]
return @trigger(@) unless term
key = Object.keys(term)[0]
val = term[key]
return @trigger(@) unless val
_rebuildResults: ->
{key, val} = @queryKeyAndVal()
return @trigger(@) unless key and val
contactResults = ContactStore.searchContacts(val, limit:10)
@_contactResults = ContactStore.searchContacts(val, limit:10)
@_rebuildThreadResults()
@_compileSuggestions()
_rebuildThreadResults: ->
{key, val} = @queryKeyAndVal()
# Don't update thread results if a previous query is still running, it'll
# just make performance even worse. When the old result comes in, re-run
return if @_threadQueryInFlight
@_threadQueryInFlight = true
DatabaseStore.findAll(Thread, [Thread.attributes.subject.like(val)])
.order(Thread.attributes.lastMessageTimestamp.descending())
.limit(4)
.then (results) =>
@_threadQueryInFlight = false
if val is @queryKeyAndVal().val
@_threadResults = results
@_compileSuggestions()
else
@_rebuildThreadResults()
_compileSuggestions: ->
{key, val} = @queryKeyAndVal()
return unless key and val
@_suggestions = []
@_suggestions.push
label: "Message Contains: #{val}"
value: [{"all": val}]
if contactResults.length
if @_threadResults?.length
@_suggestions.push
divider: 'Threads'
_.each @_threadResults, (thread) =>
@_suggestions.push({thread: thread})
if @_contactResults?.length
@_suggestions.push
divider: 'People'
_.each contactResults, (contact) =>
_.each @_contactResults, (contact) =>
@_suggestions.push
contact: contact
value: [{"participants": contact.email}]
@ -63,6 +96,13 @@ SearchSuggestionStore = Reflux.createStore
query: -> @_query
queryKeyAndVal: ->
return {} unless @_query and @_query.length > 0
term = @_query[0]
key = Object.keys(term)[0]
val = term[key]
{key, val}
committedQuery: -> @_committedQuery
suggestions: ->

View file

@ -47,6 +47,7 @@ class Thread extends Model
modelKey: 'snippet'
'subject': Attributes.String
queryable: true
modelKey: 'subject'
'unread': Attributes.Boolean