fix: fix autosuggested accounts order (#1187)

new order is local first, followed by remote, and each sorted alphabetically
This commit is contained in:
Nolan Lawson 2019-05-06 08:34:12 -07:00 committed by GitHub
parent 2abe15cc6f
commit 75c3060912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 13 deletions

View File

@ -2,23 +2,17 @@ import { database } from '../_database/database'
import { store } from '../_store/store' import { store } from '../_store/store'
import { search } from '../_api/search' import { search } from '../_api/search'
import { SEARCH_RESULTS_LIMIT } from '../_static/autosuggest' import { SEARCH_RESULTS_LIMIT } from '../_static/autosuggest'
import { USERNAME_LOWERCASE } from '../_database/constants'
import { concat } from '../_utils/arrays' import { concat } from '../_utils/arrays'
import uniqBy from 'lodash-es/uniqBy' import uniqBy from 'lodash-es/uniqBy'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask' import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
const DATABASE_SEARCH_RESULTS_LIMIT = 30 const DATABASE_SEARCH_RESULTS_LIMIT = 30
function byAccountRelevance (a, b) { function byUsername (a, b) {
// accounts you're following go first let usernameA = a.acct.toLowerCase()
if (a.following !== b.following) { let usernameB = b.acct.toLowerCase()
return a.following ? -1 : 1
} return usernameA < usernameB ? -1 : usernameA === usernameB ? 0 : 1
// after that, just sort by username
if (a[USERNAME_LOWERCASE] !== b[USERNAME_LOWERCASE]) {
return a[USERNAME_LOWERCASE] < b[USERNAME_LOWERCASE] ? -1 : 1
}
return 0
} }
function byAccountId (a) { function byAccountId (a) {
@ -41,9 +35,22 @@ export function doAccountSearch (searchText) {
} }
function mergeAndTruncateResults () { function mergeAndTruncateResults () {
return uniqBy(concat(localResults || [], remoteResults || []), byAccountId) // Always include local results; they are more likely to be relevant
.sort(byAccountRelevance) // because the user has seen their content before. Otherwise, sort by username.
let results = (localResults || [])
.slice()
.sort(byUsername)
.slice(0, SEARCH_RESULTS_LIMIT) .slice(0, SEARCH_RESULTS_LIMIT)
if (results.length < SEARCH_RESULTS_LIMIT) {
let topRemoteResults = (remoteResults || [])
.sort(byUsername)
.slice(0, SEARCH_RESULTS_LIMIT - results.length)
results = concat(results, topRemoteResults)
results = uniqBy(results, byAccountId)
}
return results
} }
function onNewResults () { function onNewResults () {