damus

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

commit 49cf56f4c2f347dc35c5e3c5047a99f6b0be981c
parent 98c7bf5afc8e8e6621566705f6e18df5dc3879a8
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 13 Feb 2023 17:50:50 -0800

Show other people's zaps

Changelog-Fixed: Fix a bug where zaps on other people's posts weren't showing

Diffstat:
Mdamus/Models/ActionBarModel.swift | 11+++++++++++
Mdamus/Models/HomeModel.swift | 4++--
Mdamus/Util/Notifications.swift | 3+++
Mdamus/Util/Zaps.swift | 2++
Mdamus/Views/ActionBar/EventActionBar.swift | 17++++++++++++++---
Mdamus/Views/ActionBar/EventDetailBar.swift | 11++++++++++-
Mdamus/Views/ChatView.swift | 2+-
Mdamus/Views/EventView.swift | 18++++++++----------
Mdamus/Views/Events/BuilderEventView.swift | 3+--
Mdamus/Views/Events/SelectedEventView.swift | 24++++++++++++++++++++----
Mdamus/Views/Events/TextEvent.swift | 4+---
11 files changed, 73 insertions(+), 26 deletions(-)

diff --git a/damus/Models/ActionBarModel.swift b/damus/Models/ActionBarModel.swift @@ -31,6 +31,17 @@ class ActionBarModel: ObservableObject { self.our_zap = our_zap } + func update(damus: DamusState, evid: String) { + self.likes = damus.likes.counts[evid] ?? 0 + self.boosts = damus.boosts.counts[evid] ?? 0 + self.zaps = damus.zaps.event_counts[evid] ?? 0 + self.zap_total = damus.zaps.event_totals[evid] ?? 0 + self.our_like = damus.likes.our_events[evid] + self.our_boost = damus.boosts.our_events[evid] + self.our_zap = damus.zaps.our_zaps[evid]?.first + self.objectWillChange.send() + } + var is_empty: Bool { return likes == 0 && boosts == 0 && zaps == 0 } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -142,12 +142,12 @@ class HomeModel: ObservableObject { return } - if let local_zapper = damus_state.profiles.lookup_zapper(pubkey: damus_state.pubkey) { + if let local_zapper = damus_state.profiles.lookup_zapper(pubkey: ptag) { handle_zap_event_with_zapper(ev, our_pubkey: damus_state.pubkey, zapper: local_zapper) return } - guard let profile = damus_state.profiles.lookup(id: damus_state.pubkey) else { + guard let profile = damus_state.profiles.lookup(id: ptag) else { return } diff --git a/damus/Util/Notifications.swift b/damus/Util/Notifications.swift @@ -98,6 +98,9 @@ extension Notification.Name { static var deleted_account: Notification.Name { return Notification.Name("deleted_account") } + static var new_zap: Notification.Name { + return Notification.Name("new_zap") + } } func handle_notify(_ name: Notification.Name) -> NotificationCenter.Publisher { diff --git a/damus/Util/Zaps.swift b/damus/Util/Zaps.swift @@ -60,6 +60,8 @@ class Zaps { event_counts[id] = event_counts[id]! + 1 event_totals[id] = event_totals[id]! + zap.invoice.amount + notify(.new_zap, zap) + return } } diff --git a/damus/Views/ActionBar/EventActionBar.swift b/damus/Views/ActionBar/EventActionBar.swift @@ -28,13 +28,14 @@ struct EventActionBar: View { @State var sheet: ActionBarSheet? = nil @State var confirm_boost: Bool = false @State var show_share_sheet: Bool = false - @StateObject var bar: ActionBarModel - init(damus_state: DamusState, event: NostrEvent, bar: ActionBarModel, test_lnurl: String? = nil) { + @ObservedObject var bar: ActionBarModel + + init(damus_state: DamusState, event: NostrEvent, bar: ActionBarModel? = nil, test_lnurl: String? = nil) { self.damus_state = damus_state self.event = event self.test_lnurl = test_lnurl - _bar = StateObject.init(wrappedValue: bar) + _bar = ObservedObject(wrappedValue: bar ?? make_actionbar_model(ev: event.id, damus: damus_state)) } var lnurl: String? { @@ -110,6 +111,16 @@ struct EventActionBar: View { } message: { Text("Are you sure you want to repost this?", comment: "Alert message to ask if user wants to repost a post.") } + .onReceive(handle_notify(.new_zap)) { n in + let zap = n.object as! Zap + guard case .note(let note_target) = zap.target else { + return + } + guard note_target.note_id == self.event.id else { + return + } + self.bar.update(damus: self.damus_state, evid: self.event.id) + } .onReceive(handle_notify(.liked)) { n in let liked = n.object as! Counted if liked.id != event.id { diff --git a/damus/Views/ActionBar/EventDetailBar.swift b/damus/Views/ActionBar/EventDetailBar.swift @@ -11,8 +11,17 @@ struct EventDetailBar: View { let state: DamusState let target: String let target_pk: String + @ObservedObject var bar: ActionBarModel + init (state: DamusState, target: String, target_pk: String) { + self.state = state + self.target = target + self.target_pk = target_pk + self._bar = ObservedObject(wrappedValue: make_actionbar_model(ev: target, damus: state)) + + } + var body: some View { HStack { if bar.boosts > 0 { @@ -42,6 +51,6 @@ struct EventDetailBar: View { struct EventDetailBar_Previews: PreviewProvider { static var previews: some View { - EventDetailBar(state: test_damus_state(), target: "", target_pk: "", bar: ActionBarModel.empty()) + EventDetailBar(state: test_damus_state(), target: "", target_pk: "") } } diff --git a/damus/Views/ChatView.swift b/damus/Views/ChatView.swift @@ -116,7 +116,7 @@ struct ChatView: View { size: .normal) if is_active || next_ev == nil || next_ev!.pubkey != event.pubkey { - let bar = make_actionbar_model(ev: event, damus: damus_state) + let bar = make_actionbar_model(ev: event.id, damus: damus_state) EventActionBar(damus_state: damus_state, event: event, bar: bar) } diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift @@ -152,16 +152,14 @@ func format_date(_ created_at: Int64) -> String { return dateFormatter.string(from: date) } - - -func make_actionbar_model(ev: NostrEvent, damus: DamusState) -> ActionBarModel { - let likes = damus.likes.counts[ev.id] - let boosts = damus.boosts.counts[ev.id] - let zaps = damus.zaps.event_counts[ev.id] - let zap_total = damus.zaps.event_totals[ev.id] - let our_like = damus.likes.our_events[ev.id] - let our_boost = damus.boosts.our_events[ev.id] - let our_zap = damus.zaps.our_zaps[ev.id] +func make_actionbar_model(ev: String, damus: DamusState) -> ActionBarModel { + let likes = damus.likes.counts[ev] + let boosts = damus.boosts.counts[ev] + let zaps = damus.zaps.event_counts[ev] + let zap_total = damus.zaps.event_totals[ev] + let our_like = damus.likes.our_events[ev] + let our_boost = damus.boosts.our_events[ev] + let our_zap = damus.zaps.our_zaps[ev] return ActionBarModel(likes: likes ?? 0, boosts: boosts ?? 0, diff --git a/damus/Views/Events/BuilderEventView.swift b/damus/Views/Events/BuilderEventView.swift @@ -53,8 +53,7 @@ struct BuilderEventView: View { NostrFilter(ids: [self.event_id], limit: 1), NostrFilter( kinds: [NostrKind.zap.rawValue], - referenced_ids: [self.event_id], - limit: 500 + referenced_ids: [self.event_id] ) ]) } diff --git a/damus/Views/Events/SelectedEventView.swift b/damus/Views/Events/SelectedEventView.swift @@ -15,6 +15,14 @@ struct SelectedEventView: View { event.pubkey } + @StateObject var bar: ActionBarModel + + init(damus: DamusState, event: NostrEvent) { + self.damus = damus + self.event = event + self._bar = StateObject(wrappedValue: make_actionbar_model(ev: event.id, damus: damus)) + } + var body: some View { HStack(alignment: .top) { let profile = damus.profiles.lookup(id: pubkey) @@ -35,19 +43,27 @@ struct SelectedEventView: View { Divider() .padding([.bottom], 4) - let bar = make_actionbar_model(ev: event, damus: damus) - if !bar.is_empty { - EventDetailBar(state: damus, target: event.id, target_pk: event.pubkey, bar: bar) + EventDetailBar(state: damus, target: event.id, target_pk: event.pubkey) Divider() } - EventActionBar(damus_state: damus, event: event, bar: bar) + EventActionBar(damus_state: damus, event: event) .padding([.top], 4) Divider() .padding([.top], 4) } + .onReceive(handle_notify(.new_zap)) { n in + let zap = n.object as! Zap + guard case .note(let note_target) = zap.target else { + return + } + guard note_target.note_id == self.event.id else { + return + } + self.bar.update(damus: self.damus, evid: self.event.id) + } .padding([.leading], 2) .event_context_menu(event, keypair: damus.keypair, target_pubkey: event.pubkey) } diff --git a/damus/Views/Events/TextEvent.swift b/damus/Views/Events/TextEvent.swift @@ -48,9 +48,7 @@ struct TextEvent: View { if has_action_bar { Rectangle().frame(height: 2).opacity(0) - let bar = make_actionbar_model(ev: event, damus: damus) - - EventActionBar(damus_state: damus, event: event, bar: bar) + EventActionBar(damus_state: damus, event: event) .padding([.top], 4) } }