Load profiles in hashtag searches

Changelog-Fixed: Load profiles in hashtag searched
This commit is contained in:
William Casarin
2023-05-02 07:50:28 -07:00
parent 58e53631c6
commit 4f459d128a
4 changed files with 19 additions and 17 deletions

View File

@@ -206,7 +206,7 @@ struct ContentView: View {
var MaybeSearchView: some View { var MaybeSearchView: some View {
Group { Group {
if let search = self.active_search { if let search = self.active_search {
SearchView(appstate: damus_state!, search: SearchModel(contacts: damus_state!.contacts, pool: damus_state!.pool, search: search)) SearchView(appstate: damus_state!, search: SearchModel(state: damus_state!, search: search))
} else { } else {
EmptyView() EmptyView()
} }

View File

@@ -9,24 +9,23 @@ import Foundation
class SearchModel: ObservableObject { class SearchModel: ObservableObject {
let state: DamusState
var events: EventHolder = EventHolder() var events: EventHolder = EventHolder()
@Published var loading: Bool = false @Published var loading: Bool = false
@Published var channel_name: String? = nil @Published var channel_name: String? = nil
let pool: RelayPool
var search: NostrFilter var search: NostrFilter
let contacts: Contacts
let sub_id = UUID().description let sub_id = UUID().description
let profiles_subid = UUID().description
let limit: UInt32 = 500 let limit: UInt32 = 500
init(contacts: Contacts, pool: RelayPool, search: NostrFilter) { init(state: DamusState, search: NostrFilter) {
self.contacts = contacts self.state = state
self.pool = pool
self.search = search self.search = search
} }
func filter_muted() { func filter_muted() {
self.events.filter { should_show_event(contacts: contacts, ev: $0) } self.events.filter { should_show_event(contacts: state.contacts, ev: $0) }
self.objectWillChange.send() self.objectWillChange.send()
} }
@@ -38,13 +37,13 @@ class SearchModel: ObservableObject {
//likes_filter.ids = ref_events.referenced_ids! //likes_filter.ids = ref_events.referenced_ids!
print("subscribing to search '\(search)' with sub_id \(sub_id)") print("subscribing to search '\(search)' with sub_id \(sub_id)")
pool.register_handler(sub_id: sub_id, handler: handle_event) state.pool.register_handler(sub_id: sub_id, handler: handle_event)
loading = true loading = true
pool.send(.subscribe(.init(filters: [search], sub_id: sub_id))) state.pool.send(.subscribe(.init(filters: [search], sub_id: sub_id)))
} }
func unsubscribe() { func unsubscribe() {
self.pool.unsubscribe(sub_id: sub_id) state.pool.unsubscribe(sub_id: sub_id)
loading = false loading = false
print("unsubscribing from search '\(search)' with sub_id \(sub_id)") print("unsubscribing from search '\(search)' with sub_id \(sub_id)")
} }
@@ -54,7 +53,7 @@ class SearchModel: ObservableObject {
return return
} }
guard should_show_event(contacts: contacts, ev: ev) else { guard should_show_event(contacts: state.contacts, ev: ev) else {
return return
} }
@@ -74,7 +73,7 @@ class SearchModel: ObservableObject {
} }
func handle_event(relay_id: String, ev: NostrConnectionEvent) { func handle_event(relay_id: String, ev: NostrConnectionEvent) {
let (_, done) = handle_subid_event(pool: pool, relay_id: relay_id, ev: ev) { sub_id, ev in let (sub_id, done) = handle_subid_event(pool: state.pool, relay_id: relay_id, ev: ev) { sub_id, ev in
if ev.is_textlike && ev.should_show_event { if ev.is_textlike && ev.should_show_event {
self.add_event(ev) self.add_event(ev)
} else if ev.known_kind == .channel_create { } else if ev.known_kind == .channel_create {
@@ -84,8 +83,12 @@ class SearchModel: ObservableObject {
} }
} }
if done { guard done else {
loading = false return
}
if sub_id == self.sub_id {
load_profiles(profiles_subid: self.profiles_subid, relay_id: relay_id, load: .from_events(self.events.all_events), damus_state: state)
} }
} }
} }

View File

@@ -37,7 +37,7 @@ struct SearchResultsView: View {
} }
} }
case .hashtag(let ht): case .hashtag(let ht):
let search_model = SearchModel(contacts: damus_state.contacts, pool: damus_state.pool, search: .filter_hashtag([ht])) let search_model = SearchModel(state: damus_state, search: .filter_hashtag([ht]))
let dst = SearchView(appstate: damus_state, search: search_model) let dst = SearchView(appstate: damus_state, search: search_model)
NavigationLink(destination: dst) { NavigationLink(destination: dst) {
Text("Search hashtag: #\(ht)", comment: "Navigation link to search hashtag.") Text("Search hashtag: #\(ht)", comment: "Navigation link to search hashtag.")

View File

@@ -43,9 +43,8 @@ struct SearchView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
let test_state = test_damus_state() let test_state = test_damus_state()
let filter = NostrFilter.filter_hashtag(["bitcoin"]) let filter = NostrFilter.filter_hashtag(["bitcoin"])
let pool = test_state.pool
let model = SearchModel(contacts: test_state.contacts, pool: pool, search: filter) let model = SearchModel(state: test_state, search: filter)
SearchView(appstate: test_state, search: model) SearchView(appstate: test_state, search: model)
} }