damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit a0caf9ce07e64fb583816083fb6a6bfbfb930f3c
parent 3277aac220f13b0c132f30c8e9f645c2b67524c0
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  7 Jun 2023 07:55:15 +0200

find_event: refactor with more explicit types

Diffstat:
Mdamus/ContentView.swift | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
Mdamus/Models/HomeModel.swift | 6+++---
Mdamus/Views/Search/SearchingEventView.swift | 20++++++++------------
3 files changed, 72 insertions(+), 36 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -746,24 +746,58 @@ func setup_notifications() { } } -func find_event(state: DamusState, evid: String, search_type: SearchType, find_from: [String]?, callback: @escaping (NostrEvent?) -> ()) { - if let ev = state.events.lookup(evid) { - callback(ev) - return +struct FindEvent { + let type: FindEventType + let find_from: [String]? + + static func profile(pubkey: String, find_from: [String]? = nil) -> FindEvent { + return FindEvent(type: .profile(pubkey), find_from: find_from) } - let subid = UUID().description + static func event(evid: String, find_from: [String]? = nil) -> FindEvent { + return FindEvent(type: .event(evid), find_from: find_from) + } +} + +enum FindEventType { + case profile(String) + case event(String) +} + +enum FoundEvent { + case profile(Profile, NostrEvent) + case invalid_profile(NostrEvent) + case event(NostrEvent) +} + +func find_event(state: DamusState, query query_: FindEvent, callback: @escaping (FoundEvent?) -> ()) { - var has_event = false + var filter: NostrFilter? = nil + let find_from = query_.find_from + let query = query_.type - var filter = search_type == .event ? NostrFilter(ids: [evid]) : NostrFilter(authors: [evid]) + switch query { + case .profile(let pubkey): + if let profile = state.profiles.lookup_with_timestamp(id: pubkey) { + callback(.profile(profile.profile, profile.event)) + return + } + filter = NostrFilter(kinds: [.metadata], limit: 1, authors: [pubkey]) + + case .event(let evid): + if let ev = state.events.lookup(evid) { + callback(.event(ev)) + return + } - if search_type == .profile { - filter.kinds = [.metadata] + var attempts = 0 + filter = NostrFilter(ids: [evid], limit: 1) } - filter.limit = 1 - var attempts = 0 + let subid = UUID().description + var attempts: Int = 0 + var has_event = false + guard let filter else { return } state.pool.subscribe_to(sub_id: subid, filters: [filter], to: find_from) { relay_id, res in guard case .nostr_event(let ev) = res else { @@ -779,15 +813,22 @@ func find_event(state: DamusState, evid: String, search_type: SearchType, find_f break case .event(_, let ev): has_event = true - state.pool.unsubscribe(sub_id: subid) - if search_type == .profile && ev.known_kind == .metadata { - process_metadata_event(events: state.events, our_pubkey: state.pubkey, profiles: state.profiles, ev: ev) { - callback(ev) + switch query { + case .profile: + if ev.known_kind == .metadata { + process_metadata_event(events: state.events, our_pubkey: state.pubkey, profiles: state.profiles, ev: ev) { profile in + guard let profile else { + callback(.invalid_profile(ev)) + return + } + callback(.profile(profile, ev)) + return + } } - } else { - callback(ev) + case .event: + callback(.event(ev)) } case .eose: if !has_event { @@ -912,10 +953,9 @@ func on_open_url(state: DamusState, url: URL, result: @escaping (OpenResult?) -> if ref.key == "p" { result(.profile(ref.ref_id)) } else if ref.key == "e" { - find_event(state: state, evid: ref.ref_id, search_type: .event, find_from: nil) { ev in - if let ev { - result(.event(ev)) - } + find_event(state: state, query: .event(evid: ref.ref_id)) { res in + guard let res, case .event(let ev) = res else { return } + result(.event(ev)) } } case .filter(let filt): diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -804,11 +804,11 @@ func guard_valid_event(events: EventCache, ev: NostrEvent, callback: @escaping ( } } -func process_metadata_event(events: EventCache, our_pubkey: String, profiles: Profiles, ev: NostrEvent, completion: (() -> Void)? = nil) { +func process_metadata_event(events: EventCache, our_pubkey: String, profiles: Profiles, ev: NostrEvent, completion: ((Profile?) -> Void)? = nil) { guard_valid_event(events: events, ev: ev) { DispatchQueue.global(qos: .background).async { guard let profile: Profile = decode_data(Data(ev.content.utf8)) else { - completion?() + completion?(nil) return } @@ -816,7 +816,7 @@ func process_metadata_event(events: EventCache, our_pubkey: String, profiles: Pr DispatchQueue.main.async { process_metadata_profile(our_pubkey: our_pubkey, profiles: profiles, profile: profile, ev: ev) - completion?() + completion?(profile) } } } diff --git a/damus/Views/Search/SearchingEventView.swift b/damus/Views/Search/SearchingEventView.swift @@ -72,24 +72,20 @@ struct SearchingEventView: View { } case .event: - if let ev = state.events.lookup(evid) { - self.search_state = .found(ev) - return - } - find_event(state: state, evid: evid, search_type: search_type, find_from: nil) { ev in - if let ev { - self.search_state = .found(ev) - } else { + find_event(state: state, query: .event(evid: evid)) { res in + guard case .event(let ev) = res else { self.search_state = .not_found + return } + self.search_state = .found(ev) } case .profile: - find_event(state: state, evid: evid, search_type: search_type, find_from: nil) { ev in - if state.profiles.lookup(id: evid) != nil { - self.search_state = .found_profile(evid) - } else { + find_event(state: state, query: .profile(pubkey: evid)) { res in + guard case .profile(_, let ev) = res else { self.search_state = .not_found + return } + self.search_state = .found_profile(ev.pubkey) } } }