Optimizes user search to account for names that start with the typed prefix

This commit is contained in:
Vitor Pamplona 2024-12-10 19:07:37 -05:00
parent 5aed1cee2c
commit c2243c53cf
4 changed files with 29 additions and 42 deletions

View File

@ -1751,14 +1751,24 @@ object LocalCache {
}
}
return users.filter { _, user: User ->
(
(user.anyNameStartsWith(username)) ||
user.pubkeyHex.startsWith(username, true) ||
user.pubkeyNpub().startsWith(username, true)
) &&
(forAccount == null || (!forAccount.isHidden(user) && !user.containsAny(forAccount.flowHiddenUsers.value.hiddenWordsCase)))
}
val finds =
users.filter { _, user: User ->
(
(user.anyNameStartsWith(username)) ||
user.pubkeyHex.startsWith(username, true) ||
user.pubkeyNpub().startsWith(username, true)
) &&
(forAccount == null || (!forAccount.isHidden(user) && !user.containsAny(forAccount.flowHiddenUsers.value.hiddenWordsCase)))
}
return finds.sortedWith(
compareBy(
{ forAccount?.isFollowing(it) == false },
{ !it.toBestDisplayName().startsWith(username, ignoreCase = true) },
{ it.toBestDisplayName().lowercase() },
{ it.pubkeyHex },
),
)
}
fun findNotesStartingWith(

View File

@ -315,11 +315,7 @@ open class EditPostViewModel : ViewModel() {
if (lastWord.startsWith("@") && lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@"))
viewModelScope.launch(Dispatchers.IO) {
userSuggestions =
LocalCache
.findUsersStartingWith(lastWord.removePrefix("@"), account)
.sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex }))
.reversed()
userSuggestions = LocalCache.findUsersStartingWith(lastWord.removePrefix("@"), account)
}
} else {
NostrSearchEventOrUserDataSource.clear()

View File

@ -1074,13 +1074,10 @@ open class NewPostViewModel : ViewModel() {
userSuggestionAnchor = it.selection
userSuggestionsMainMessage = UserSuggestionAnchor.MAIN_MESSAGE
if (lastWord.startsWith("@") && lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@"))
val prefix = lastWord.removePrefix("@")
NostrSearchEventOrUserDataSource.search(prefix)
viewModelScope.launch(Dispatchers.IO) {
userSuggestions =
LocalCache
.findUsersStartingWith(lastWord.removePrefix("@"), account)
.sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex }))
.reversed()
userSuggestions = LocalCache.findUsersStartingWith(prefix, account)
}
} else {
NostrSearchEventOrUserDataSource.clear()
@ -1103,13 +1100,12 @@ open class NewPostViewModel : ViewModel() {
userSuggestionAnchor = it.selection
userSuggestionsMainMessage = UserSuggestionAnchor.TO_USERS
if (lastWord.startsWith("@") && lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@"))
val prefix = lastWord.removePrefix("@")
NostrSearchEventOrUserDataSource.search(prefix)
viewModelScope.launch(Dispatchers.IO) {
userSuggestions =
LocalCache
.findUsersStartingWith(lastWord.removePrefix("@"), account)
.sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex }))
.reversed()
.findUsersStartingWith(prefix, account)
}
} else {
NostrSearchEventOrUserDataSource.clear()
@ -1131,18 +1127,11 @@ open class NewPostViewModel : ViewModel() {
userSuggestionAnchor = it.selection
userSuggestionsMainMessage = UserSuggestionAnchor.FORWARD_ZAPS
if (lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@"))
val prefix = lastWord.removePrefix("@")
NostrSearchEventOrUserDataSource.search(prefix)
viewModelScope.launch(Dispatchers.IO) {
userSuggestions =
LocalCache
.findUsersStartingWith(lastWord.removePrefix("@"), account)
.sortedWith(
compareBy(
{ account?.isFollowing(it) },
{ it.toBestDisplayName() },
{ it.pubkeyHex },
),
).reversed()
LocalCache.findUsersStartingWith(prefix, account)
}
} else {
NostrSearchEventOrUserDataSource.clear()

View File

@ -74,15 +74,7 @@ class SearchBarViewModel(
_hashtagResults.emit(findHashtags(searchValue))
_searchResultsUsers.emit(
LocalCache
.findUsersStartingWith(searchValue, account)
.sortedWith(
compareBy(
{ it.toBestDisplayName().startsWith(searchValue, true) },
{ account.isFollowing(it) },
{ it.toBestDisplayName() },
),
).reversed(),
LocalCache.findUsersStartingWith(searchValue, account),
)
_searchResultsNotes.emit(
LocalCache