mirror of https://github.com/lukechilds/damus.git
Terry Yiu
2 years ago
committed by
William Casarin
5 changed files with 163 additions and 1 deletions
@ -0,0 +1,77 @@ |
|||
// |
|||
// RepostsModel.swift |
|||
// damus |
|||
// |
|||
// Created by Terry Yiu on 1/22/23. |
|||
// |
|||
|
|||
import Foundation |
|||
|
|||
class RepostsModel: ObservableObject { |
|||
let state: DamusState |
|||
let target: String |
|||
let sub_id: String |
|||
let profiles_id: String |
|||
|
|||
@Published var reposts: [NostrEvent] |
|||
|
|||
init (state: DamusState, target: String) { |
|||
self.state = state |
|||
self.target = target |
|||
self.sub_id = UUID().description |
|||
self.profiles_id = UUID().description |
|||
self.reposts = [] |
|||
} |
|||
|
|||
func get_filter() -> NostrFilter { |
|||
var filter = NostrFilter.filter_kinds([NostrKind.boost.rawValue]) |
|||
filter.referenced_ids = [target] |
|||
filter.limit = 500 |
|||
return filter |
|||
} |
|||
|
|||
func subscribe() { |
|||
let filter = get_filter() |
|||
let filters = [filter] |
|||
self.state.pool.subscribe(sub_id: sub_id, filters: filters, handler: handle_nostr_event) |
|||
} |
|||
|
|||
func unsubscribe() { |
|||
self.state.pool.unsubscribe(sub_id: sub_id) |
|||
} |
|||
|
|||
func handle_event(relay_id: String, ev: NostrEvent) { |
|||
guard ev.kind == NostrKind.boost.rawValue else { |
|||
return |
|||
} |
|||
|
|||
guard let reposted_event = last_etag(tags: ev.tags) else { |
|||
return |
|||
} |
|||
|
|||
guard reposted_event == self.target else { |
|||
return |
|||
} |
|||
|
|||
if insert_uniq_sorted_event(events: &self.reposts, new_ev: ev, cmp: { a, b in a.created_at < b.created_at } ) { |
|||
objectWillChange.send() |
|||
} |
|||
} |
|||
|
|||
func handle_nostr_event(relay_id: String, ev: NostrConnectionEvent) { |
|||
guard case .nostr_event(let nev) = ev else { |
|||
return |
|||
} |
|||
|
|||
switch nev { |
|||
case .event(_, let ev): |
|||
handle_event(relay_id: relay_id, ev: ev) |
|||
|
|||
case .notice(_): |
|||
break |
|||
case .eose(_): |
|||
load_profiles(profiles_subid: profiles_id, relay_id: relay_id, events: reposts, damus_state: state) |
|||
break |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
// |
|||
// RepostView.swift |
|||
// damus |
|||
// |
|||
// Created by Terry Yiu on 1/22/23. |
|||
// |
|||
|
|||
import SwiftUI |
|||
|
|||
struct RepostView: View { |
|||
let damus_state: DamusState |
|||
let repost: NostrEvent |
|||
|
|||
var body: some View { |
|||
FollowUserView(target: .pubkey(repost.pubkey), damus_state: damus_state) |
|||
} |
|||
} |
|||
|
|||
struct RepostView_Previews: PreviewProvider { |
|||
static var previews: some View { |
|||
RepostView(damus_state: test_damus_state(), repost: NostrEvent(id: "", content: "", pubkey: "")) |
|||
} |
|||
} |
|||
|
@ -0,0 +1,38 @@ |
|||
// |
|||
// RepostsView.swift |
|||
// damus |
|||
// |
|||
// Created by Terry Yiu on 1/22/23. |
|||
// |
|||
|
|||
import SwiftUI |
|||
|
|||
struct RepostsView: View { |
|||
let damus_state: DamusState |
|||
@StateObject var model: RepostsModel |
|||
|
|||
var body: some View { |
|||
ScrollView { |
|||
LazyVStack { |
|||
ForEach(model.reposts, id: \.id) { ev in |
|||
RepostView(damus_state: damus_state, repost: ev) |
|||
} |
|||
} |
|||
.padding() |
|||
} |
|||
.navigationBarTitle(NSLocalizedString("Reposts", comment: "Navigation bar title for Reposts view.")) |
|||
.onAppear { |
|||
model.subscribe() |
|||
} |
|||
.onDisappear { |
|||
model.unsubscribe() |
|||
} |
|||
} |
|||
} |
|||
|
|||
struct RepostsView_Previews: PreviewProvider { |
|||
static var previews: some View { |
|||
let state = test_damus_state() |
|||
RepostsView(damus_state: state, model: RepostsModel(state: state, target: "pubkey")) |
|||
} |
|||
} |
Loading…
Reference in new issue