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
@@ -1751,14 +1751,24 @@ object LocalCache {
} }
} }
return users.filter { _, user: User -> val finds =
( users.filter { _, user: User ->
(user.anyNameStartsWith(username)) || (
user.pubkeyHex.startsWith(username, true) || (user.anyNameStartsWith(username)) ||
user.pubkeyNpub().startsWith(username, true) user.pubkeyHex.startsWith(username, true) ||
) && user.pubkeyNpub().startsWith(username, true)
(forAccount == null || (!forAccount.isHidden(user) && !user.containsAny(forAccount.flowHiddenUsers.value.hiddenWordsCase))) ) &&
} (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( fun findNotesStartingWith(
@@ -315,11 +315,7 @@ open class EditPostViewModel : ViewModel() {
if (lastWord.startsWith("@") && lastWord.length > 2) { if (lastWord.startsWith("@") && lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@"))
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
userSuggestions = userSuggestions = LocalCache.findUsersStartingWith(lastWord.removePrefix("@"), account)
LocalCache
.findUsersStartingWith(lastWord.removePrefix("@"), account)
.sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex }))
.reversed()
} }
} else { } else {
NostrSearchEventOrUserDataSource.clear() NostrSearchEventOrUserDataSource.clear()
@@ -1074,13 +1074,10 @@ open class NewPostViewModel : ViewModel() {
userSuggestionAnchor = it.selection userSuggestionAnchor = it.selection
userSuggestionsMainMessage = UserSuggestionAnchor.MAIN_MESSAGE userSuggestionsMainMessage = UserSuggestionAnchor.MAIN_MESSAGE
if (lastWord.startsWith("@") && lastWord.length > 2) { if (lastWord.startsWith("@") && lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) val prefix = lastWord.removePrefix("@")
NostrSearchEventOrUserDataSource.search(prefix)
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
userSuggestions = userSuggestions = LocalCache.findUsersStartingWith(prefix, account)
LocalCache
.findUsersStartingWith(lastWord.removePrefix("@"), account)
.sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex }))
.reversed()
} }
} else { } else {
NostrSearchEventOrUserDataSource.clear() NostrSearchEventOrUserDataSource.clear()
@@ -1103,13 +1100,12 @@ open class NewPostViewModel : ViewModel() {
userSuggestionAnchor = it.selection userSuggestionAnchor = it.selection
userSuggestionsMainMessage = UserSuggestionAnchor.TO_USERS userSuggestionsMainMessage = UserSuggestionAnchor.TO_USERS
if (lastWord.startsWith("@") && lastWord.length > 2) { if (lastWord.startsWith("@") && lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) val prefix = lastWord.removePrefix("@")
NostrSearchEventOrUserDataSource.search(prefix)
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
userSuggestions = userSuggestions =
LocalCache LocalCache
.findUsersStartingWith(lastWord.removePrefix("@"), account) .findUsersStartingWith(prefix, account)
.sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex }))
.reversed()
} }
} else { } else {
NostrSearchEventOrUserDataSource.clear() NostrSearchEventOrUserDataSource.clear()
@@ -1131,18 +1127,11 @@ open class NewPostViewModel : ViewModel() {
userSuggestionAnchor = it.selection userSuggestionAnchor = it.selection
userSuggestionsMainMessage = UserSuggestionAnchor.FORWARD_ZAPS userSuggestionsMainMessage = UserSuggestionAnchor.FORWARD_ZAPS
if (lastWord.length > 2) { if (lastWord.length > 2) {
NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) val prefix = lastWord.removePrefix("@")
NostrSearchEventOrUserDataSource.search(prefix)
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
userSuggestions = userSuggestions =
LocalCache LocalCache.findUsersStartingWith(prefix, account)
.findUsersStartingWith(lastWord.removePrefix("@"), account)
.sortedWith(
compareBy(
{ account?.isFollowing(it) },
{ it.toBestDisplayName() },
{ it.pubkeyHex },
),
).reversed()
} }
} else { } else {
NostrSearchEventOrUserDataSource.clear() NostrSearchEventOrUserDataSource.clear()
@@ -74,15 +74,7 @@ class SearchBarViewModel(
_hashtagResults.emit(findHashtags(searchValue)) _hashtagResults.emit(findHashtags(searchValue))
_searchResultsUsers.emit( _searchResultsUsers.emit(
LocalCache LocalCache.findUsersStartingWith(searchValue, account),
.findUsersStartingWith(searchValue, account)
.sortedWith(
compareBy(
{ it.toBestDisplayName().startsWith(searchValue, true) },
{ account.isFollowing(it) },
{ it.toBestDisplayName() },
),
).reversed(),
) )
_searchResultsNotes.emit( _searchResultsNotes.emit(
LocalCache LocalCache