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>
This commit is contained in:
Daniel D’Aquino
2025-04-09 22:49:37 -07:00
parent 8f32c81b6c
commit b5afa3c0b4
5 changed files with 21 additions and 6 deletions

View File

@@ -66,7 +66,7 @@ func note_artifact_is_separated(kind: NostrKind?) -> Bool {
return kind != .longform 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 { guard let blocks = ev.blocks(ndb: ndb) else {
return .separated(.just_content(ev.get_content(keypair))) 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)) 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. // 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. // 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. // Block previews should actually be rendered in the position of the note content where it was found.

View File

@@ -295,7 +295,7 @@ struct NoteContentView: View {
} }
await preload_event(plan: plan, state: damus_state) await preload_event(plan: plan, state: damus_state)
} else if force_artifacts { } 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) self.artifacts_model.state = .loaded(arts)
} }
} }

View File

@@ -48,7 +48,7 @@ let test_longform_event = LongformEvent.parse(from: NostrEvent(
struct LongformView_Previews: PreviewProvider { struct LongformView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
let st = test_damus_state 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)) let model = NoteArtifactsModel(state: .loaded(artifacts))
ScrollView { ScrollView {

View File

@@ -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 { func render_notification_content_preview(ndb: Ndb, ev: NostrEvent, profiles: Profiles, keypair: Keypair) -> String {
let prefix_len = 300 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 // special case for longform events
if ev.known_kind == .longform { if ev.known_kind == .longform {

View File

@@ -376,7 +376,7 @@ func preload_event(plan: PreloadPlan, state: DamusState) async {
//print("Preloading event \(plan.event.content)") //print("Preloading event \(plan.event.content)")
if artifacts == nil && plan.load_artifacts { 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 artifacts = arts
// we need these asap // 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) { 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 // only separated artifacts have previews
if case .separated(let sep) = arts { if case .separated(let sep) = arts {