damus

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

commit b5afa3c0b4df8374c0b17de017ed7d56f5be0c1c
parent 8f32c81b6cd0c386bdb9d8c20644fd0fd2d517fa
Author: Daniel D’Aquino <daniel@daquino.me>
Date:   Wed,  9 Apr 2025 22:49:37 -0700

Wait for note in NostrDB before rendering it

Closes: https://github.com/damus-io/damus/issues/2885
Changelog-Changed: Use NostrDB for rendering note contents
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>

Diffstat:
Mdamus/Features/Events/Models/NoteContent.swift | 11++++++++++-
Mdamus/Features/Events/NoteContentView.swift | 2+-
Mdamus/Features/Longform/Views/LongformView.swift | 2+-
Mdamus/Features/Notifications/Models/NotificationsManager.swift | 2+-
Mdamus/Shared/Utilities/EventCache.swift | 10++++++++--
5 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/damus/Features/Events/Models/NoteContent.swift b/damus/Features/Events/Models/NoteContent.swift @@ -66,7 +66,7 @@ func note_artifact_is_separated(kind: NostrKind?) -> Bool { return kind != .longform } -func render_note_content(ndb: Ndb, ev: NostrEvent, profiles: Profiles, keypair: Keypair) -> NoteArtifacts { +func render_immediately_available_note_content(ndb: Ndb, ev: NostrEvent, profiles: Profiles, keypair: Keypair) -> NoteArtifacts { guard let blocks = ev.blocks(ndb: ndb) else { return .separated(.just_content(ev.get_content(keypair))) } @@ -78,6 +78,15 @@ func render_note_content(ndb: Ndb, ev: NostrEvent, profiles: Profiles, keypair: return .separated(render_blocks(blocks: blocks.unsafeUnownedValue, profiles: profiles, note: ev, can_hide_last_previewable_refs: true)) } +actor ContentRenderer { + func render_note_content(ndb: Ndb, ev: NostrEvent, profiles: Profiles, keypair: Keypair) async -> NoteArtifacts { + guard let result = try? await ndb.waitFor(noteId: ev.id, timeout: 10) else { + return .separated(.just_content(ev.get_content(keypair))) + } + return render_immediately_available_note_content(ndb: ndb, ev: ev, profiles: profiles, keypair: keypair) + } +} + // FIXME(tyiu): There are a lot of hacks to get this function to render the blocks correctly. // However, the entire note content rendering logic just needs to be rewritten. // Block previews should actually be rendered in the position of the note content where it was found. diff --git a/damus/Features/Events/NoteContentView.swift b/damus/Features/Events/NoteContentView.swift @@ -295,7 +295,7 @@ struct NoteContentView: View { } await preload_event(plan: plan, state: damus_state) } else if force_artifacts { - let arts = render_note_content(ndb: damus_state.ndb, ev: event, profiles: damus_state.profiles, keypair: damus_state.keypair) + let arts = await ContentRenderer().render_note_content(ndb: damus_state.ndb, ev: event, profiles: damus_state.profiles, keypair: damus_state.keypair) self.artifacts_model.state = .loaded(arts) } } diff --git a/damus/Features/Longform/Views/LongformView.swift b/damus/Features/Longform/Views/LongformView.swift @@ -48,7 +48,7 @@ let test_longform_event = LongformEvent.parse(from: NostrEvent( struct LongformView_Previews: PreviewProvider { static var previews: some View { let st = test_damus_state - let artifacts = render_note_content(ndb: st.ndb, ev: test_longform_event.event, profiles: st.profiles, keypair: Keypair(pubkey: .empty, privkey: nil)) + let artifacts = render_immediately_available_note_content(ndb: st.ndb, ev: test_longform_event.event, profiles: st.profiles, keypair: Keypair(pubkey: .empty, privkey: nil)) let model = NoteArtifactsModel(state: .loaded(artifacts)) ScrollView { diff --git a/damus/Features/Notifications/Models/NotificationsManager.swift b/damus/Features/Notifications/Models/NotificationsManager.swift @@ -149,7 +149,7 @@ func create_local_notification(profiles: Profiles, notify: LocalNotification) { func render_notification_content_preview(ndb: Ndb, ev: NostrEvent, profiles: Profiles, keypair: Keypair) -> String { let prefix_len = 300 - let artifacts = render_note_content(ndb: ndb, ev: ev, profiles: profiles, keypair: keypair) + let artifacts = render_immediately_available_note_content(ndb: ndb, ev: ev, profiles: profiles, keypair: keypair) // special case for longform events if ev.known_kind == .longform { diff --git a/damus/Shared/Utilities/EventCache.swift b/damus/Shared/Utilities/EventCache.swift @@ -376,7 +376,7 @@ func preload_event(plan: PreloadPlan, state: DamusState) async { //print("Preloading event \(plan.event.content)") if artifacts == nil && plan.load_artifacts { - let arts = render_note_content(ndb: state.ndb, ev: plan.event, profiles: profiles, keypair: our_keypair) + let arts = await ContentRenderer().render_note_content(ndb: state.ndb, ev: plan.event, profiles: profiles, keypair: our_keypair) artifacts = arts // we need these asap @@ -397,7 +397,13 @@ func preload_event(plan: PreloadPlan, state: DamusState) async { } if plan.load_preview, note_artifact_is_separated(kind: plan.event.known_kind) { - let arts = artifacts ?? render_note_content(ndb: state.ndb, ev: plan.event, profiles: profiles, keypair: our_keypair) + let arts: NoteArtifacts + if let artifacts { + arts = artifacts + } + else { + arts = await ContentRenderer().render_note_content(ndb: state.ndb, ev: plan.event, profiles: profiles, keypair: our_keypair) + } // only separated artifacts have previews if case .separated(let sep) = arts {