mirror of https://github.com/lukechilds/damus.git
William Casarin
2 years ago
26 changed files with 250 additions and 95 deletions
@ -0,0 +1,74 @@ |
|||
// |
|||
// Timeline.swift |
|||
// damus |
|||
// |
|||
// Created by William Casarin on 2022-05-09. |
|||
// |
|||
|
|||
import Foundation |
|||
|
|||
|
|||
class SearchModel: ObservableObject { |
|||
@Published var events: [NostrEvent] = [] |
|||
let pool: RelayPool |
|||
let search: NostrFilter |
|||
let sub_id = UUID().description |
|||
|
|||
init(pool: RelayPool, search: NostrFilter) { |
|||
self.pool = pool |
|||
self.search = search |
|||
} |
|||
|
|||
func subscribe() { |
|||
// since 2 month |
|||
var filter = NostrFilter.copy(from: search) |
|||
filter.since = Int64(Date.now.timeIntervalSince1970) - 2629800 * 2 |
|||
|
|||
//likes_filter.ids = ref_events.referenced_ids! |
|||
|
|||
print("subscribing to search '\(filter)' with sub_id \(sub_id)") |
|||
pool.register_handler(sub_id: sub_id, handler: handle_event) |
|||
pool.send(.subscribe(.init(filters: [filter], sub_id: sub_id))) |
|||
} |
|||
|
|||
func unsubscribe() { |
|||
self.pool.unsubscribe(sub_id: sub_id) |
|||
print("unsubscribing from search '\(search)' with sub_id \(sub_id)") |
|||
} |
|||
|
|||
func add_event(_ ev: NostrEvent) { |
|||
if insert_uniq_sorted_event(events: &self.events, new_ev: ev) { |
|||
objectWillChange.send() |
|||
} |
|||
} |
|||
|
|||
func handle_event(relay_id: String, ev: NostrConnectionEvent) { |
|||
handle_subid_event(pool: pool, sub_id: sub_id, relay_id: relay_id, ev: ev) { ev in |
|||
if ev.known_kind == .text { |
|||
self.add_event(ev) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
func handle_subid_event(pool: RelayPool, sub_id: String, relay_id: String, ev: NostrConnectionEvent, handle: (NostrEvent) -> ()) { |
|||
switch ev { |
|||
case .ws_event: |
|||
break |
|||
case .nostr_event(let res): |
|||
switch res { |
|||
case .event(let ev_subid, let ev): |
|||
if ev_subid == sub_id { |
|||
handle(ev) |
|||
} |
|||
|
|||
case .notice(let note): |
|||
if note.contains("Too many subscription filters") { |
|||
// TODO: resend filters? |
|||
pool.reconnect(to: [relay_id]) |
|||
} |
|||
break |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,29 @@ |
|||
// |
|||
// InsertSort.swift |
|||
// damus |
|||
// |
|||
// Created by William Casarin on 2022-05-09. |
|||
// |
|||
|
|||
import Foundation |
|||
|
|||
|
|||
func insert_uniq_sorted_event(events: inout [NostrEvent], new_ev: NostrEvent) -> Bool { |
|||
var i: Int = 0 |
|||
|
|||
for event in events { |
|||
// don't insert duplicate events |
|||
if new_ev.id == event.id { |
|||
return false |
|||
} |
|||
|
|||
if new_ev.created_at < event.created_at { |
|||
events.insert(new_ev, at: i) |
|||
return true |
|||
} |
|||
i += 1 |
|||
} |
|||
|
|||
events.append(new_ev) |
|||
return true |
|||
} |
@ -0,0 +1,32 @@ |
|||
// |
|||
// SearchView.swift |
|||
// damus |
|||
// |
|||
// Created by William Casarin on 2022-05-09. |
|||
// |
|||
|
|||
import SwiftUI |
|||
|
|||
struct SearchView: View { |
|||
let appstate: DamusState |
|||
@StateObject var search: SearchModel |
|||
|
|||
var body: some View { |
|||
TimelineView(events: $search.events, damus: appstate) |
|||
.padding([.leading, .trailing], 6) |
|||
.onAppear() { |
|||
search.subscribe() |
|||
} |
|||
.onDisappear() { |
|||
search.unsubscribe() |
|||
} |
|||
} |
|||
} |
|||
|
|||
/* |
|||
struct SearchView_Previews: PreviewProvider { |
|||
static var previews: some View { |
|||
SearchView() |
|||
} |
|||
} |
|||
*/ |
Loading…
Reference in new issue