Add nip05 search
Changelog-Added: Added ability to lookup users by nip05 identifiers
This commit is contained in:
@@ -17,12 +17,14 @@ enum SearchState {
|
||||
enum SearchType {
|
||||
case event
|
||||
case profile
|
||||
case nip05
|
||||
}
|
||||
|
||||
struct SearchingEventView: View {
|
||||
let state: DamusState
|
||||
let evid: String
|
||||
let search_type: SearchType
|
||||
|
||||
@State var search_state: SearchState = .searching
|
||||
|
||||
var bech32_evid: String {
|
||||
@@ -35,6 +37,8 @@ struct SearchingEventView: View {
|
||||
|
||||
var search_name: String {
|
||||
switch search_type {
|
||||
case .nip05:
|
||||
return "nip05"
|
||||
case .profile:
|
||||
return "profile"
|
||||
case .event:
|
||||
@@ -67,9 +71,39 @@ struct SearchingEventView: View {
|
||||
Text("\(search_name.capitalized) not found", comment: "When a note or profile is not found when searching for it via its note id")
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
.onChange(of: evid, debounceTime: 0.5) { evid in
|
||||
self.search_state = .searching
|
||||
|
||||
switch search_type {
|
||||
case .nip05:
|
||||
if let pk = state.profiles.nip05_pubkey[evid] {
|
||||
if state.profiles.lookup(id: pk) != nil {
|
||||
self.search_state = .found_profile(pk)
|
||||
}
|
||||
} else {
|
||||
Task.init {
|
||||
guard let nip05 = NIP05.parse(evid) else {
|
||||
self.search_state = .not_found
|
||||
return
|
||||
}
|
||||
guard let nip05_resp = await fetch_nip05(nip05: nip05) else {
|
||||
DispatchQueue.main.async {
|
||||
self.search_state = .not_found
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
guard let pk = nip05_resp.names[nip05.username] else {
|
||||
self.search_state = .not_found
|
||||
return
|
||||
}
|
||||
|
||||
self.search_state = .found_profile(pk)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case .event:
|
||||
if let ev = state.events.lookup(evid) {
|
||||
self.search_state = .found(ev)
|
||||
|
||||
@@ -12,6 +12,7 @@ enum Search {
|
||||
case hashtag(String)
|
||||
case profile(String)
|
||||
case note(String)
|
||||
case nip05(String)
|
||||
case hex(String)
|
||||
}
|
||||
|
||||
@@ -41,6 +42,10 @@ struct SearchResultsView: View {
|
||||
NavigationLink(destination: dst) {
|
||||
Text("Search hashtag: #\(ht)", comment: "Navigation link to search hashtag.")
|
||||
}
|
||||
|
||||
case .nip05(let addr):
|
||||
SearchingEventView(state: damus_state, evid: addr, search_type: .nip05)
|
||||
|
||||
case .profile(let prof):
|
||||
let decoded = try? bech32_decode(prof)
|
||||
let hex = hex_encode(decoded!.data)
|
||||
@@ -95,6 +100,12 @@ func search_for_string(profiles: Profiles, _ new: String) -> Search? {
|
||||
return nil
|
||||
}
|
||||
|
||||
let splitted = new.split(separator: "@")
|
||||
|
||||
if splitted.count == 2 {
|
||||
return .nip05(new)
|
||||
}
|
||||
|
||||
if new.first! == "#" {
|
||||
let ht = String(new.dropFirst().filter{$0 != " "})
|
||||
return .hashtag(ht)
|
||||
|
||||
Reference in New Issue
Block a user