commit fd8ad494e9ab6eefb5c455a5dd5fc459c4c4c039
parent f14ba7cce4e0b5baf3bc210fad1423f32efebad7
Author: William Casarin <jb55@jb55.com>
Date: Sat, 22 Apr 2023 12:10:57 -0700
Add partial support for different repost variants
Changelog-Added: Add partial support for different repost variants
Diffstat:
7 files changed, 33 insertions(+), 25 deletions(-)
diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift
@@ -229,7 +229,7 @@ class HomeModel: ObservableObject {
func handle_boost_event(sub_id: String, _ ev: NostrEvent) {
var boost_ev_id = ev.last_refid()?.ref_id
- if let inner_ev = ev.inner_event {
+ if let inner_ev = ev.get_inner_event(cache: damus_state.events) {
boost_ev_id = inner_ev.id
guard validate_event(ev: inner_ev) == .ok else {
@@ -488,7 +488,7 @@ class HomeModel: ObservableObject {
damus_state.events.insert(ev)
- if let inner_ev = ev.inner_event {
+ if let inner_ev = ev.get_inner_event(cache: damus_state.events) {
damus_state.events.insert(inner_ev)
}
@@ -1104,7 +1104,7 @@ func process_local_notification(damus_state: DamusState, event ev: NostrEvent) {
let notify = LocalNotification(type: .mention, event: ev, target: ev, content: content)
create_local_notification(profiles: damus_state.profiles, notify: notify )
}
- } else if type == .boost && damus_state.settings.repost_notification, let inner_ev = ev.inner_event {
+ } else if type == .boost && damus_state.settings.repost_notification, let inner_ev = ev.get_inner_event(cache: damus_state.events) {
let notify = LocalNotification(type: .repost, event: ev, target: inner_ev, content: inner_ev.content)
create_local_notification(profiles: damus_state.profiles, notify: notify)
} else if type == .like && damus_state.settings.like_notification,
diff --git a/damus/Models/NotificationsModel.swift b/damus/Models/NotificationsModel.swift
@@ -192,8 +192,8 @@ class NotificationsModel: ObservableObject, ScrollQueue {
}
- private func insert_repost(_ ev: NostrEvent) -> Bool {
- guard let reposted_ev = ev.inner_event else {
+ private func insert_repost(_ ev: NostrEvent, cache: EventCache) -> Bool {
+ guard let reposted_ev = ev.get_inner_event(cache: cache) else {
return false
}
@@ -235,9 +235,9 @@ class NotificationsModel: ObservableObject, ScrollQueue {
}
}
- private func insert_event_immediate(_ ev: NostrEvent) -> Bool {
+ private func insert_event_immediate(_ ev: NostrEvent, cache: EventCache) -> Bool {
if ev.known_kind == .boost {
- return insert_repost(ev)
+ return insert_repost(ev, cache: cache)
} else if ev.known_kind == .like {
return insert_reaction(ev)
} else if ev.known_kind == .text {
@@ -269,7 +269,7 @@ class NotificationsModel: ObservableObject, ScrollQueue {
return insert_uniq_sorted_event_created(events: &incoming_events, new_ev: ev)
}
- if insert_event_immediate(ev) {
+ if insert_event_immediate(ev, cache: damus_state.events) {
self.notifications = build_notifications()
return true
}
@@ -339,7 +339,7 @@ class NotificationsModel: ObservableObject, ScrollQueue {
}
for event in incoming_events {
- inserted = insert_event_immediate(event) || inserted
+ inserted = insert_event_immediate(event, cache: damus_state.events) || inserted
}
if inserted {
diff --git a/damus/Models/SearchHomeModel.swift b/damus/Models/SearchHomeModel.swift
@@ -101,10 +101,10 @@ func find_profiles_to_fetch_pk(profiles: Profiles, event_pubkeys: [String]) -> [
return Array(pubkeys)
}
-func find_profiles_to_fetch(profiles: Profiles, load: PubkeysToLoad) -> [String] {
+func find_profiles_to_fetch(profiles: Profiles, load: PubkeysToLoad, cache: EventCache) -> [String] {
switch load {
case .from_events(let events):
- return find_profiles_to_fetch_from_events(profiles: profiles, events: events)
+ return find_profiles_to_fetch_from_events(profiles: profiles, events: events, cache: cache)
case .from_keys(let pks):
return find_profiles_to_fetch_from_keys(profiles: profiles, pks: pks)
}
@@ -124,12 +124,12 @@ func find_profiles_to_fetch_from_keys(profiles: Profiles, pks: [String]) -> [Str
return Array(pubkeys)
}
-func find_profiles_to_fetch_from_events(profiles: Profiles, events: [NostrEvent]) -> [String] {
+func find_profiles_to_fetch_from_events(profiles: Profiles, events: [NostrEvent], cache: EventCache) -> [String] {
var pubkeys = Set<String>()
for ev in events {
// lookup profiles from boosted events
- if ev.known_kind == .boost, let bev = ev.inner_event, profiles.lookup(id: bev.pubkey) == nil {
+ if ev.known_kind == .boost, let bev = ev.get_inner_event(cache: cache), profiles.lookup(id: bev.pubkey) == nil {
pubkeys.insert(bev.pubkey)
}
@@ -148,7 +148,7 @@ enum PubkeysToLoad {
func load_profiles(profiles_subid: String, relay_id: String, load: PubkeysToLoad, damus_state: DamusState) {
var filter = NostrFilter.filter_profiles
- let authors = find_profiles_to_fetch(profiles: damus_state.profiles, load: load)
+ let authors = find_profiles_to_fetch(profiles: damus_state.profiles, load: load, cache: damus_state.events)
filter.authors = authors
guard !authors.isEmpty else {
diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift
@@ -111,14 +111,22 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
return parse_mentions(content: content, tags: self.tags)
}
- lazy var inner_event: NostrEvent? = {
- // don't try to deserialize an inner event if we know there won't be one
- if self.known_kind == .boost {
- return event_from_json(dat: self.content)
- }
- return nil
+ private lazy var inner_event: NostrEvent? = {
+ return event_from_json(dat: self.content)
}()
+ func get_inner_event(cache: EventCache) -> NostrEvent? {
+ guard self.known_kind == .boost else {
+ return nil
+ }
+
+ if self.content == "", let ref = self.referenced_ids.first {
+ return cache.lookup(ref.ref_id)
+ }
+
+ return self.inner_event
+ }
+
private var _event_refs: [EventRef]? = nil
func event_refs(_ privkey: String?) -> [EventRef] {
if let rs = _event_refs {
@@ -998,8 +1006,8 @@ func last_etag(tags: [[String]]) -> String? {
return e
}
-func inner_event_or_self(ev: NostrEvent) -> NostrEvent {
- guard let inner_ev = ev.inner_event else {
+func inner_event_or_self(ev: NostrEvent, cache: EventCache) -> NostrEvent {
+ guard let inner_ev = ev.get_inner_event(cache: cache) else {
return ev
}
diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift
@@ -32,7 +32,7 @@ struct EventView: View {
var body: some View {
VStack {
if event.known_kind == .boost {
- if let inner_ev = event.inner_event {
+ if let inner_ev = event.get_inner_event(cache: damus.events) {
RepostedEvent(damus: damus, event: event, inner_ev: inner_ev, options: options)
} else {
EmptyView()
diff --git a/damus/Views/Events/BuilderEventView.swift b/damus/Views/Events/BuilderEventView.swift
@@ -70,7 +70,7 @@ struct BuilderEventView: View {
var body: some View {
VStack {
if let event {
- let ev = event.inner_event ?? event
+ let ev = event.get_inner_event(cache: damus.events) ?? event
let thread = ThreadModel(event: ev, damus_state: damus)
let dest = ThreadView(state: damus, thread: thread)
NavigationLink(destination: dest) {
diff --git a/damus/Views/Timeline/InnerTimelineView.swift b/damus/Views/Timeline/InnerTimelineView.swift
@@ -47,7 +47,7 @@ struct InnerTimelineView: View {
ForEach(events.filter(filter), id: \.id) { (ev: NostrEvent) in
EventView(damus: damus, event: ev, options: event_options)
.onTapGesture {
- nav_target = ev.inner_event ?? ev
+ nav_target = ev.get_inner_event(cache: self.damus.events) ?? ev
navigating = true
}
.padding(.top, 7)