Hook up Damus Purple translation service
This commit integrates the Damus Purple translation service: - Automatically handles translation settings change after purchase - Asks for permission to override translation settings if the user already has translation setup - Translation settings can be changed with Damus Purple, if desired - Translation requests working with the Damus API server Testing -------- PASS Device: iPhone 15 simulator iOS: 17.2 Damus: This commit Damus Purple API server: `9397201d7d55ddcec4c18fcd337f759b61dce697` running on Ubuntu 22.04 LTS VM (npm run dev) iOS setting: English set as the only preferred language. Steps: 1. Enable Damus Purple feature flag on developer settings, set purple localhost mode, and restart app 2. Set translation setting to something other than none (e.g. DeepL) 3. Simulate Damus Purple purchase 4. Check that when dismissing welcome view, a confirmation prompt will ask the user whether they want to switch translator to Damus Purple. PASS 5. Click "Yes". 6. Go to translation settings. Check that translation settings are set to "Purple". PASS 7. Go to a non-English profile. Check that translations appear with "Mock translation" (Which is the translation text provided by the mock translation server). PASS 8. Reinstall app 9. Repeat the test, but this time starting with no translation settings. Make sure that translation settings will automatically switch to Damus Purple. PASS Feature flag testing -------------------- PASS Preconditions: Same as above Steps: 1. Turn off translation 2. Turn off Damus Purple feature flag 3. Go to translation settings. Make sure that Damus Purple is not an option. PASS Closes: https://github.com/damus-io/damus/issues/1836 Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Reviewed-by: William Casarin <jb55@jb55.com> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
39b6dfb47e
commit
9a547077c1
@@ -126,6 +126,36 @@ class DamusPurple: StoreObserverDelegate {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func translate(text: String, source source_language: String, target target_language: String) async throws -> String {
|
||||
var url = environment.get_base_url()
|
||||
url.append(path: "/translate")
|
||||
url.append(queryItems: [
|
||||
.init(name: "source", value: source_language),
|
||||
.init(name: "target", value: target_language),
|
||||
.init(name: "q", value: text)
|
||||
])
|
||||
let (data, response) = try await make_nip98_authenticated_request(
|
||||
method: .get,
|
||||
url: url,
|
||||
payload: nil,
|
||||
payload_type: nil,
|
||||
auth_keypair: self.keypair
|
||||
)
|
||||
|
||||
if let httpResponse = response as? HTTPURLResponse {
|
||||
switch httpResponse.statusCode {
|
||||
case 200:
|
||||
return try JSONDecoder().decode(TranslationResult.self, from: data).text
|
||||
default:
|
||||
Log.error("Translation error with Damus Purple. HTTP status code: %d; Response: %s", for: .damus_purple, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown")
|
||||
throw PurpleError.translation_error(status_code: httpResponse.statusCode, response: data)
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw PurpleError.translation_no_response
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: API types
|
||||
@@ -155,4 +185,13 @@ extension DamusPurple {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum PurpleError: Error {
|
||||
case translation_error(status_code: Int, response: Data)
|
||||
case translation_no_response
|
||||
}
|
||||
|
||||
struct TranslationResult: Codable {
|
||||
let text: String
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ enum TranslationService: String, CaseIterable, Identifiable, StringCodable {
|
||||
}
|
||||
|
||||
case none
|
||||
case purple
|
||||
case libretranslate
|
||||
case deepl
|
||||
case nokyctranslate
|
||||
@@ -38,6 +39,8 @@ enum TranslationService: String, CaseIterable, Identifiable, StringCodable {
|
||||
switch self {
|
||||
case .none:
|
||||
return .init(tag: self.rawValue, displayName: NSLocalizedString("none_translation_service", value: "None", comment: "Dropdown option for selecting no translation service."))
|
||||
case .purple:
|
||||
return .init(tag: self.rawValue, displayName: NSLocalizedString("Damus Purple", comment: "Dropdown option for selecting Damus Purple as a translation service."))
|
||||
case .libretranslate:
|
||||
return .init(tag: self.rawValue, displayName: NSLocalizedString("LibreTranslate (Open Source)", comment: "Dropdown option for selecting LibreTranslate as the translation service."))
|
||||
case .deepl:
|
||||
|
||||
@@ -296,6 +296,8 @@ class UserSettingsStore: ObservableObject {
|
||||
switch translation_service {
|
||||
case .none:
|
||||
return false
|
||||
case .purple:
|
||||
return true
|
||||
case .libretranslate:
|
||||
return URLComponents(string: libretranslate_url) != nil
|
||||
case .deepl:
|
||||
|
||||
Reference in New Issue
Block a user