Extend user tagging search to all local profiles

Changelog-Added: Extend user tagging search to all local profiles
Changelog-Fixed: Show @ mentions for users with display_names and no username
Changelog-Fixed: Make user search case insensitive
This commit is contained in:
William Casarin
2023-03-15 08:44:03 -06:00
parent c05223ca2b
commit 1533be77d8
2 changed files with 72 additions and 28 deletions

View File

@@ -8,7 +8,7 @@
import SwiftUI
enum Search {
case profiles([(String, Profile)])
case profiles([SearchedUser])
case hashtag(String)
case profile(String)
case note(String)
@@ -21,7 +21,7 @@ struct SearchResultsView: View {
@State var result: Search? = nil
func ProfileSearchResult(pk: String, res: Profile) -> some View {
func ProfileSearchResult(pk: String) -> some View {
FollowUserView(target: .pubkey(pk), damus_state: damus_state)
}
@@ -31,8 +31,8 @@ struct SearchResultsView: View {
switch result {
case .profiles(let results):
LazyVStack {
ForEach(results, id: \.0) { prof in
ProfileSearchResult(pk: prof.0, res: prof.1)
ForEach(results) { prof in
ProfileSearchResult(pk: prof.pubkey)
}
}
case .hashtag(let ht):
@@ -119,22 +119,34 @@ func search_for_string(profiles: Profiles, _ new: String) -> Search? {
return .profiles(search_profiles(profiles: profiles, search: new))
}
func search_profiles(profiles: Profiles, search new: String) -> [(String, Profile)] {
func search_profiles(profiles: Profiles, search: String) -> [SearchedUser] {
let new = search.lowercased()
return profiles.profiles.enumerated().reduce(into: []) { acc, els in
let pk = els.element.key
let prof = els.element.value.profile
let lowname = prof.name.map { $0.lowercased() }
let lownip05 = profiles.is_validated(pk).map { $0.host.lowercased() }
let lowdisp = prof.display_name.map { $0.lowercased() }
let ok = new.count == 1 ?
((lowname?.starts(with: new) ?? false) ||
(lownip05?.starts(with: new) ?? false) ||
(lowdisp?.starts(with: new) ?? false)) : (pk.starts(with: new) || String(new.dropFirst()) == pk
|| lowname?.contains(new) ?? false
|| lownip05?.contains(new) ?? false
|| lowdisp?.contains(new) ?? false)
if ok {
acc.append((pk, prof))
if let searched = profile_search_matches(profiles: profiles, profile: prof, pubkey: pk, search: new) {
acc.append(searched)
}
}
}
func profile_search_matches(profiles: Profiles, profile prof: Profile, pubkey pk: String, search new: String) -> SearchedUser? {
let lowname = prof.name.map { $0.lowercased() }
let lownip05 = profiles.is_validated(pk).map { $0.host.lowercased() }
let lowdisp = prof.display_name.map { $0.lowercased() }
let ok = new.count == 1 ?
((lowname?.starts(with: new) ?? false) ||
(lownip05?.starts(with: new) ?? false) ||
(lowdisp?.starts(with: new) ?? false)) : (pk.starts(with: new) || String(new.dropFirst()) == pk
|| lowname?.contains(new) ?? false
|| lownip05?.contains(new) ?? false
|| lowdisp?.contains(new) ?? false)
if ok {
return SearchedUser(petname: nil, profile: prof, pubkey: pk)
}
return nil
}