damus

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

commit df6911f9cba7768ea5b920a6516ac775b23718ba
parent 1ca0519e257f29cef7fb20db30d50d2d2555bc41
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 30 Apr 2023 21:40:11 -0700

cache note language

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 24++++++++++++------------
Mdamus/Components/TranslateView.swift | 17+++++------------
Mdamus/Util/EventCache.swift | 29++++++++++++++++++++++-------
3 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -38,12 +38,12 @@ 4C0A3F8F280F640A000448DE /* ThreadModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F8E280F640A000448DE /* ThreadModel.swift */; }; 4C0A3F91280F6528000448DE /* ChatView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F90280F6528000448DE /* ChatView.swift */; }; 4C0A3F93280F66F5000448DE /* ReplyMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F92280F66F5000448DE /* ReplyMap.swift */; }; - 4C198DF829F89323004C165C /* BinaryParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C198DF729F89323004C165C /* BinaryParser.swift */; }; 4C198DEF29F88C6B004C165C /* BlurHashEncode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C198DEB29F88C6B004C165C /* BlurHashEncode.swift */; }; 4C198DF029F88C6B004C165C /* Readme.md in Resources */ = {isa = PBXBuildFile; fileRef = 4C198DEC29F88C6B004C165C /* Readme.md */; }; 4C198DF129F88C6B004C165C /* License.txt in Resources */ = {isa = PBXBuildFile; fileRef = 4C198DED29F88C6B004C165C /* License.txt */; }; 4C198DF229F88C6B004C165C /* BlurHashDecode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C198DEE29F88C6B004C165C /* BlurHashDecode.swift */; }; 4C198DF529F88D2E004C165C /* ImageMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C198DF429F88D2E004C165C /* ImageMetadata.swift */; }; + 4C198DF829F89323004C165C /* BinaryParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C198DF729F89323004C165C /* BinaryParser.swift */; }; 4C1A9A1A29DCA17E00516EAC /* ReplyCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C1A9A1929DCA17E00516EAC /* ReplyCounter.swift */; }; 4C1A9A1D29DDCF9B00516EAC /* NotificationSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C1A9A1C29DDCF9B00516EAC /* NotificationSettingsView.swift */; }; 4C1A9A1F29DDD24B00516EAC /* AppearanceSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C1A9A1E29DDD24B00516EAC /* AppearanceSettingsView.swift */; }; @@ -422,12 +422,12 @@ 4C0A3F8E280F640A000448DE /* ThreadModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadModel.swift; sourceTree = "<group>"; }; 4C0A3F90280F6528000448DE /* ChatView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatView.swift; sourceTree = "<group>"; }; 4C0A3F92280F66F5000448DE /* ReplyMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyMap.swift; sourceTree = "<group>"; }; - 4C198DF729F89323004C165C /* BinaryParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryParser.swift; sourceTree = "<group>"; }; 4C198DEB29F88C6B004C165C /* BlurHashEncode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashEncode.swift; sourceTree = "<group>"; }; 4C198DEC29F88C6B004C165C /* Readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = Readme.md; sourceTree = "<group>"; }; 4C198DED29F88C6B004C165C /* License.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = License.txt; sourceTree = "<group>"; }; 4C198DEE29F88C6B004C165C /* BlurHashDecode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashDecode.swift; sourceTree = "<group>"; }; 4C198DF429F88D2E004C165C /* ImageMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageMetadata.swift; sourceTree = "<group>"; }; + 4C198DF729F89323004C165C /* BinaryParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryParser.swift; sourceTree = "<group>"; }; 4C1A9A1929DCA17E00516EAC /* ReplyCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyCounter.swift; sourceTree = "<group>"; }; 4C1A9A1C29DDCF9B00516EAC /* NotificationSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationSettingsView.swift; sourceTree = "<group>"; }; 4C1A9A1E29DDD24B00516EAC /* AppearanceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppearanceSettingsView.swift; sourceTree = "<group>"; }; @@ -867,14 +867,6 @@ path = Models; sourceTree = "<group>"; }; - 4C198DF629F89317004C165C /* Parser */ = { - isa = PBXGroup; - children = ( - 4C198DF729F89323004C165C /* BinaryParser.swift */, - ); - path = Parser; - sourceTree = "<group>"; - }; 4C198DEA29F88C6B004C165C /* BlurHash */ = { isa = PBXGroup; children = ( @@ -894,6 +886,14 @@ path = Images; sourceTree = "<group>"; }; + 4C198DF629F89317004C165C /* Parser */ = { + isa = PBXGroup; + children = ( + 4C198DF729F89323004C165C /* BinaryParser.swift */, + ); + path = Parser; + sourceTree = "<group>"; + }; 4C1A9A1B29DDCF8B00516EAC /* Settings */ = { isa = PBXGroup; children = ( @@ -2084,7 +2084,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = damus/damus.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 15; + CURRENT_PROJECT_VERSION = 16; DEVELOPMENT_ASSET_PATHS = "\"damus/Preview Content\""; DEVELOPMENT_TEAM = XK7H4JAB3D; ENABLE_PREVIEWS = YES; @@ -2131,7 +2131,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = damus/damus.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 15; + CURRENT_PROJECT_VERSION = 16; DEVELOPMENT_ASSET_PATHS = "\"damus/Preview Content\""; DEVELOPMENT_TEAM = XK7H4JAB3D; ENABLE_PREVIEWS = YES; diff --git a/damus/Components/TranslateView.swift b/damus/Components/TranslateView.swift @@ -60,7 +60,7 @@ struct TranslateView: View { func translate() { Task { - let res = await translate_note(profiles: damus_state.profiles, privkey: damus_state.keypair.privkey, event: event, settings: damus_state.settings) + let res = await translate_note(profiles: damus_state.profiles, privkey: damus_state.keypair.privkey, event: event, settings: damus_state.settings, note_lang: translations_model.note_language) DispatchQueue.main.async { self.translations_model.state = res } @@ -68,7 +68,7 @@ struct TranslateView: View { } func attempt_translation() { - guard should_translate(event: event, our_keypair: damus_state.keypair, settings: damus_state.settings) else { + guard should_translate(event: event, our_keypair: damus_state.keypair, settings: damus_state.settings, note_lang: self.translations_model.note_language) else { return } @@ -81,7 +81,7 @@ struct TranslateView: View { case .havent_tried: if damus_state.settings.auto_translate { Text("") - } else { + } else if should_translate(event: event, our_keypair: damus_state.keypair, settings: damus_state.settings, note_lang: translations_model.note_language ?? current_language()) { TranslateButton } case .translating: @@ -118,16 +118,9 @@ struct TranslateView_Previews: PreviewProvider { } } -func translate_note(profiles: Profiles, privkey: String?, event: NostrEvent, settings: UserSettingsStore) async -> TranslateStatus { - let note_lang = await event.note_language(privkey) ?? current_language() +func translate_note(profiles: Profiles, privkey: String?, event: NostrEvent, settings: UserSettingsStore, note_lang: String?) async -> TranslateStatus { - let preferredLanguages = Set(Locale.preferredLanguages.map { localeToLanguage($0) }) - - // Don't translate if its in our preferred languages - guard !preferredLanguages.contains(note_lang) else { - // if its the same, give up and don't retry - return .not_needed - } + let note_lang = note_lang ?? current_language() // If the note language is different from our preferred languages, send a translation request. let translator = Translator(settings) diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift @@ -37,10 +37,12 @@ enum ImageMetaProcessState { } class TranslationModel: ObservableObject { + var note_language: String? @Published var state: TranslateStatus init(state: TranslateStatus) { self.state = state + self.note_language = nil } } @@ -92,7 +94,6 @@ class EventData { var preview_model: PreviewModel var zaps_model : ZapsDataModel var relative_time: RelativeTimeModel - var validated: ValidationResult var translations: TranslateStatus { @@ -258,7 +259,7 @@ class EventCache { } } -func should_translate(event: NostrEvent, our_keypair: Keypair, settings: UserSettingsStore) -> Bool { +func should_translate(event: NostrEvent, our_keypair: Keypair, settings: UserSettingsStore, note_lang: String?) -> Bool { guard settings.can_translate else { return false } @@ -271,15 +272,25 @@ func should_translate(event: NostrEvent, our_keypair: Keypair, settings: UserSet return false } + if let note_lang { + let preferredLanguages = Set(Locale.preferredLanguages.map { localeToLanguage($0) }) + + // Don't translate if its in our preferred languages + guard !preferredLanguages.contains(note_lang) else { + // if its the same, give up and don't retry + return false + } + } + // we should start translating if we have auto_translate on return settings.auto_translate } -func should_preload_translation(event: NostrEvent, our_keypair: Keypair, current_status: TranslateStatus, settings: UserSettingsStore) -> Bool { +func should_preload_translation(event: NostrEvent, our_keypair: Keypair, current_status: TranslateStatus, settings: UserSettingsStore, note_lang: String?) -> Bool { switch current_status { case .havent_tried: - return should_translate(event: event, our_keypair: our_keypair, settings: settings) + return should_translate(event: event, our_keypair: our_keypair, settings: settings, note_lang: note_lang) case .translating: return false case .translated: return false case .not_needed: return false @@ -294,6 +305,7 @@ struct PreloadResult { let translations: TranslateStatus? let preview: Preview? let timeago: String + let note_language: String } @@ -319,7 +331,7 @@ func get_preload_plan(cache: EventData, ev: NostrEvent, our_keypair: Keypair, se cache.artifacts_model.state = .loading } - let load_translations = should_preload_translation(event: ev, our_keypair: our_keypair, current_status: cache.translations, settings: settings) + let load_translations = should_preload_translation(event: ev, our_keypair: our_keypair, current_status: cache.translations, settings: settings, note_lang: cache.translations_model.note_language) if load_translations { cache.translations_model.state = .translating } @@ -363,11 +375,13 @@ func preload_event(plan: PreloadPlan, profiles: Profiles, our_keypair: Keypair, } } + let note_language = plan.event.note_language(our_keypair.privkey) ?? current_language() + if plan.load_translations { - translations = await translate_note(profiles: profiles, privkey: our_keypair.privkey, event: plan.event, settings: settings) + translations = await translate_note(profiles: profiles, privkey: our_keypair.privkey, event: plan.event, settings: settings, note_lang: note_language) } - return PreloadResult(event: plan.event, artifacts: artifacts, translations: translations, preview: preview, timeago: format_relative_time(plan.event.created_at)) + return PreloadResult(event: plan.event, artifacts: artifacts, translations: translations, preview: preview, timeago: format_relative_time(plan.event.created_at), note_language: note_language) } func set_preload_results(plan: PreloadPlan, res: PreloadResult, privkey: String?) { @@ -396,6 +410,7 @@ func set_preload_results(plan: PreloadPlan, res: PreloadResult, privkey: String? } } + plan.data.translations_model.note_language = res.note_language plan.data.relative_time.value = res.timeago }