|
|
@ -14,16 +14,19 @@ class SearchHomeModel: ObservableObject { |
|
|
|
@Published var loading: Bool = false |
|
|
|
|
|
|
|
var seen_pubkey: Set<String> = Set() |
|
|
|
let profiles: Profiles |
|
|
|
let pool: RelayPool |
|
|
|
let sub_id = UUID().description |
|
|
|
let base_subid = UUID().description |
|
|
|
let profiles_subid = UUID().description |
|
|
|
let limit: UInt32 = 250 |
|
|
|
|
|
|
|
init(pool: RelayPool) { |
|
|
|
init(pool: RelayPool, profiles: Profiles) { |
|
|
|
self.pool = pool |
|
|
|
self.profiles = profiles |
|
|
|
} |
|
|
|
|
|
|
|
func get_base_filter() -> NostrFilter { |
|
|
|
var filter = NostrFilter.filter_text |
|
|
|
var filter = NostrFilter.filter_kinds([1, 42]) |
|
|
|
filter.limit = self.limit |
|
|
|
filter.until = Int64(Date.now.timeIntervalSince1970) |
|
|
|
return filter |
|
|
@ -31,12 +34,22 @@ class SearchHomeModel: ObservableObject { |
|
|
|
|
|
|
|
func subscribe() { |
|
|
|
loading = true |
|
|
|
pool.subscribe(sub_id: sub_id, filters: [get_base_filter()], handler: handle_event) |
|
|
|
pool.subscribe(sub_id: base_subid, filters: [get_base_filter()], handler: handle_event) |
|
|
|
} |
|
|
|
|
|
|
|
func unsubscribe() { |
|
|
|
loading = false |
|
|
|
pool.unsubscribe(sub_id: sub_id) |
|
|
|
pool.unsubscribe(sub_id: base_subid) |
|
|
|
} |
|
|
|
|
|
|
|
func load_profiles(relay_id: String) { |
|
|
|
var filter = NostrFilter.filter_profiles |
|
|
|
let authors = find_profiles_to_fetch(profiles: profiles, events: events) |
|
|
|
filter.authors = authors |
|
|
|
|
|
|
|
if !authors.isEmpty { |
|
|
|
pool.subscribe(sub_id: profiles_subid, filters: [filter], handler: handle_event) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func handle_event(relay_id: String, conn_ev: NostrConnectionEvent) { |
|
|
@ -46,7 +59,7 @@ class SearchHomeModel: ObservableObject { |
|
|
|
case .nostr_event(let event): |
|
|
|
switch event { |
|
|
|
case .event(let sub_id, let ev): |
|
|
|
guard sub_id == self.sub_id else { |
|
|
|
guard sub_id == self.base_subid || sub_id == self.profiles_subid else { |
|
|
|
return |
|
|
|
} |
|
|
|
if ev.kind == NostrKind.text.rawValue { |
|
|
@ -57,13 +70,50 @@ class SearchHomeModel: ObservableObject { |
|
|
|
let _ = insert_uniq_sorted_event(events: &events, new_ev: ev) { |
|
|
|
$0.created_at > $1.created_at |
|
|
|
} |
|
|
|
} else if ev.known_kind == .metadata { |
|
|
|
process_metadata_event(profiles: self.profiles, ev: ev) |
|
|
|
} |
|
|
|
case .notice(let msg): |
|
|
|
print("search home notice: \(msg)") |
|
|
|
case .eose: |
|
|
|
case .eose(let sub_id): |
|
|
|
loading = false |
|
|
|
|
|
|
|
if sub_id == self.base_subid { |
|
|
|
load_profiles(relay_id: relay_id) |
|
|
|
} else if sub_id == self.profiles_subid { |
|
|
|
pool.unsubscribe(sub_id: self.profiles_subid) |
|
|
|
} |
|
|
|
|
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func find_profiles_to_fetch_pk(profiles: Profiles, event_pubkeys: [String]) -> [String] { |
|
|
|
var pubkeys = Set<String>() |
|
|
|
|
|
|
|
for pk in event_pubkeys { |
|
|
|
if profiles.lookup(id: pk) != nil { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
pubkeys.insert(pk) |
|
|
|
} |
|
|
|
|
|
|
|
return Array(pubkeys) |
|
|
|
} |
|
|
|
|
|
|
|
func find_profiles_to_fetch(profiles: Profiles, events: [NostrEvent]) -> [String] { |
|
|
|
var pubkeys = Set<String>() |
|
|
|
|
|
|
|
for ev in events { |
|
|
|
if profiles.lookup(id: ev.pubkey) != nil { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
pubkeys.insert(ev.pubkey) |
|
|
|
} |
|
|
|
|
|
|
|
return Array(pubkeys) |
|
|
|
} |
|
|
|