2018-05-07 01:25:17 +02:00
|
|
|
import {
|
|
|
|
searchAccountsByUsername as searchAccountsByUsernameInDatabase
|
2018-08-26 07:03:33 +02:00
|
|
|
} from '../../_database/accounts'
|
2018-05-07 01:25:17 +02:00
|
|
|
|
|
|
|
const SEARCH_RESULTS_LIMIT = 4
|
|
|
|
const DATABASE_SEARCH_RESULTS_LIMIT = 30
|
|
|
|
|
|
|
|
async function searchAccounts (store, searchText) {
|
|
|
|
searchText = searchText.substring(1)
|
|
|
|
let { currentInstance } = store.get()
|
|
|
|
let results = await searchAccountsByUsernameInDatabase(
|
|
|
|
currentInstance, searchText, DATABASE_SEARCH_RESULTS_LIMIT)
|
|
|
|
return results.slice(0, SEARCH_RESULTS_LIMIT)
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchEmoji (store, searchText) {
|
|
|
|
searchText = searchText.toLowerCase().substring(1)
|
|
|
|
let { currentCustomEmoji } = store.get()
|
|
|
|
let results = currentCustomEmoji.filter(emoji => emoji.shortcode.toLowerCase().startsWith(searchText))
|
|
|
|
.sort((a, b) => a.shortcode.toLowerCase() < b.shortcode.toLowerCase() ? -1 : 1)
|
|
|
|
.slice(0, SEARCH_RESULTS_LIMIT)
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
export function autosuggestObservers (store) {
|
|
|
|
store.observe('autosuggestSearchText', async autosuggestSearchText => {
|
|
|
|
let { composeFocused } = store.get()
|
|
|
|
if (!composeFocused || !autosuggestSearchText) {
|
|
|
|
return
|
|
|
|
}
|
2018-05-07 02:35:22 +02:00
|
|
|
let autosuggestType = autosuggestSearchText.startsWith('@') ? 'account' : 'emoji'
|
|
|
|
let results = (autosuggestType === 'account')
|
2018-05-07 01:25:17 +02:00
|
|
|
? await searchAccounts(store, autosuggestSearchText)
|
|
|
|
: await searchEmoji(store, autosuggestSearchText)
|
|
|
|
store.setForCurrentAutosuggest({
|
2018-05-07 02:35:22 +02:00
|
|
|
autosuggestType,
|
2018-05-07 01:25:17 +02:00
|
|
|
autosuggestSelected: 0,
|
2018-05-07 02:35:22 +02:00
|
|
|
autosuggestSearchResults: results
|
2018-05-07 01:25:17 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|