55 lines
1.0 KiB
Go
Raw Normal View History

2019-06-03 09:32:43 +00:00
package search
import (
"sort"
"strings"
"github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/arn/stringutils"
)
// Users searches all users.
func Users(originalTerm string, maxLength int) []*arn.User {
term := strings.ToLower(stringutils.RemoveSpecialCharacters(originalTerm))
results := make([]*Result, 0, maxLength)
for user := range arn.StreamUsers() {
if user.ID == originalTerm {
return []*arn.User{user}
}
text := strings.ToLower(user.Nick)
// Similarity check
similarity := stringutils.AdvancedStringSimilarity(term, text)
2019-08-31 07:52:42 +00:00
if similarity < MinStringSimilarity {
2019-06-03 09:32:43 +00:00
continue
}
results = append(results, &Result{
obj: user,
similarity: similarity,
})
}
// Sort
sort.Slice(results, func(i, j int) bool {
return results[i].similarity > results[j].similarity
})
// Limit
if len(results) >= maxLength {
results = results[:maxLength]
}
// Final list
final := make([]*arn.User, len(results))
for i, result := range results {
final[i] = result.obj.(*arn.User)
}
return final
}