Restore translated note rendering
Changelog-Fixed: Fixed broken automatic translations Closes: https://github.com/damus-io/damus/issues/3406 Signed-off-by: alltheseas <64376233+alltheseas@users.noreply.github.com>
This commit is contained in:
committed by
Daniel D’Aquino
parent
48143f859a
commit
5058fb33d7
@@ -307,7 +307,7 @@ struct NoteContentView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// always reload artifacts on load
|
// always reload artifacts on load
|
||||||
let plan = get_preload_plan(evcache: damus_state.events, ev: event, our_keypair: damus_state.keypair, settings: damus_state.settings)
|
let plan = get_preload_plan(ndb: damus_state.ndb, evcache: damus_state.events, ev: event, our_keypair: damus_state.keypair, settings: damus_state.settings)
|
||||||
|
|
||||||
// TODO: make this cleaner
|
// TODO: make this cleaner
|
||||||
Task {
|
Task {
|
||||||
|
|||||||
@@ -158,15 +158,15 @@ func translate_note(profiles: Profiles, keypair: Keypair, event: NostrEvent, set
|
|||||||
return .not_needed
|
return .not_needed
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render translated note
|
// Render translated note using the same NostrDB block parser so links/mentions/media stay consistent.
|
||||||
// TODO: fix translated blocks
|
let artifacts: NoteArtifactsSeparated
|
||||||
//let translated_blocks = parse_note_content(content: .content(translated_note, event.tags))
|
if let translated_blocks = try? NdbBlockGroup.parse(content: translated_note) {
|
||||||
//let artifacts = render_blocks(blocks: translated_blocks, profiles: profiles, can_hide_last_previewable_refs: true)
|
artifacts = render_blocks(blocks: translated_blocks, profiles: profiles, can_hide_last_previewable_refs: true)
|
||||||
|
} else {
|
||||||
|
artifacts = .just_content(translated_note)
|
||||||
|
}
|
||||||
|
|
||||||
return .not_needed
|
return .translated(Translated(artifacts: artifacts, language: note_lang))
|
||||||
|
|
||||||
// and cache it
|
|
||||||
//return .translated(Translated(artifacts: artifacts, language: note_lang))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func current_language() -> String {
|
func current_language() -> String {
|
||||||
|
|||||||
@@ -280,6 +280,11 @@ func can_and_should_translate(event: NostrEvent, our_keypair: Keypair, settings:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func should_preload_translation(event: NostrEvent, our_keypair: Keypair, current_status: TranslateStatus, settings: UserSettingsStore, note_lang: String?) -> Bool {
|
func should_preload_translation(event: NostrEvent, our_keypair: Keypair, current_status: TranslateStatus, settings: UserSettingsStore, note_lang: String?) -> Bool {
|
||||||
|
guard let note_lang else {
|
||||||
|
// Without a detected language we can't safely auto-translate.
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
switch current_status {
|
switch current_status {
|
||||||
case .havent_tried:
|
case .havent_tried:
|
||||||
return can_and_should_translate(event: event, our_keypair: our_keypair, settings: settings, note_lang: note_lang) && settings.auto_translate
|
return can_and_should_translate(event: event, our_keypair: our_keypair, settings: settings, note_lang: note_lang) && settings.auto_translate
|
||||||
@@ -306,7 +311,13 @@ func load_preview(artifacts: NoteArtifactsSeparated) async -> Preview? {
|
|||||||
return Preview(meta: meta)
|
return Preview(meta: meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
func get_preload_plan(evcache: EventCache, ev: NostrEvent, our_keypair: Keypair, settings: UserSettingsStore) -> PreloadPlan? {
|
@MainActor
|
||||||
|
private func compute_note_language(ndb: Ndb, ev: NostrEvent, keypair: Keypair) -> String? {
|
||||||
|
return ev.note_language(ndb: ndb, keypair)
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
func get_preload_plan(ndb: Ndb, evcache: EventCache, ev: NostrEvent, our_keypair: Keypair, settings: UserSettingsStore) -> PreloadPlan? {
|
||||||
let cache = evcache.get_cache_data(ev.id)
|
let cache = evcache.get_cache_data(ev.id)
|
||||||
let load_artifacts = cache.artifacts.should_preload
|
let load_artifacts = cache.artifacts.should_preload
|
||||||
if load_artifacts {
|
if load_artifacts {
|
||||||
@@ -314,7 +325,10 @@ func get_preload_plan(evcache: EventCache, ev: NostrEvent, our_keypair: Keypair,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cached event might not have the note language determined yet, so determine the language here before figuring out if translations should be preloaded.
|
// Cached event might not have the note language determined yet, so determine the language here before figuring out if translations should be preloaded.
|
||||||
let note_lang = cache.translations_model.note_language ?? /*ev.note_language(our_keypair.privkey)*/ current_language()
|
let note_lang = cache.translations_model.note_language ?? compute_note_language(ndb: ndb, ev: ev, keypair: our_keypair)
|
||||||
|
if cache.translations_model.note_language == nil {
|
||||||
|
cache.translations_model.note_language = note_lang
|
||||||
|
}
|
||||||
|
|
||||||
let load_translations = should_preload_translation(event: ev, our_keypair: our_keypair, current_status: cache.translations, settings: settings, note_lang: note_lang)
|
let load_translations = should_preload_translation(event: ev, our_keypair: our_keypair, current_status: cache.translations, settings: settings, note_lang: note_lang)
|
||||||
if load_translations {
|
if load_translations {
|
||||||
@@ -418,11 +432,19 @@ func preload_event(plan: PreloadPlan, state: DamusState) async {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let note_language = plan.data.translations_model.note_language ?? plan.event.note_language(ndb: state.ndb, our_keypair) ?? current_language()
|
let note_language: String?
|
||||||
|
if let cached_language = plan.data.translations_model.note_language {
|
||||||
|
note_language = cached_language
|
||||||
|
} else {
|
||||||
|
note_language = await compute_note_language(ndb: state.ndb, ev: plan.event, keypair: our_keypair)
|
||||||
|
}
|
||||||
|
|
||||||
var translations: TranslateStatus? = nil
|
var translations: TranslateStatus? = nil
|
||||||
// We have to recheck should_translate here now that we have note_language
|
// We have to recheck should_translate here now that we have note_language
|
||||||
if plan.load_translations && can_and_should_translate(event: plan.event, our_keypair: our_keypair, settings: settings, note_lang: note_language) && settings.auto_translate
|
if plan.load_translations,
|
||||||
|
let note_language,
|
||||||
|
can_and_should_translate(event: plan.event, our_keypair: our_keypair, settings: settings, note_lang: note_language),
|
||||||
|
settings.auto_translate
|
||||||
{
|
{
|
||||||
translations = await translate_note(profiles: profiles, keypair: our_keypair, event: plan.event, settings: settings, note_lang: note_language, purple: state.purple)
|
translations = await translate_note(profiles: profiles, keypair: our_keypair, event: plan.event, settings: settings, note_lang: note_language, purple: state.purple)
|
||||||
}
|
}
|
||||||
@@ -446,18 +468,29 @@ func preload_events(state: DamusState, events: [NostrEvent]) {
|
|||||||
let our_keypair = state.keypair
|
let our_keypair = state.keypair
|
||||||
let settings = state.settings
|
let settings = state.settings
|
||||||
|
|
||||||
let plans = events.compactMap { ev in
|
|
||||||
get_preload_plan(evcache: event_cache, ev: ev, our_keypair: our_keypair, settings: settings)
|
|
||||||
}
|
|
||||||
|
|
||||||
if plans.count == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
|
let plans = await withTaskGroup(of: PreloadPlan?.self) { group in
|
||||||
|
for ev in events {
|
||||||
|
group.addTask {
|
||||||
|
await get_preload_plan(ndb: state.ndb, evcache: event_cache, ev: ev, our_keypair: our_keypair, settings: settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var results: [PreloadPlan] = []
|
||||||
|
for await plan in group {
|
||||||
|
if let plan {
|
||||||
|
results.append(plan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
if plans.count == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for plan in plans {
|
for plan in plans {
|
||||||
await preload_event(plan: plan, state: state)
|
await preload_event(plan: plan, state: state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -556,8 +556,8 @@ extension NdbNote {
|
|||||||
return thread_reply() != nil
|
return thread_reply() != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
func note_language(ndb: Ndb, _ keypair: Keypair) -> String? {
|
func note_language(ndb: Ndb, _ keypair: Keypair) -> String? {
|
||||||
assert(!Thread.isMainThread, "This function must not be run on the main thread.")
|
|
||||||
|
|
||||||
// Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in
|
// Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in
|
||||||
// and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer.
|
// and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer.
|
||||||
|
|||||||
Reference in New Issue
Block a user