mirror of
https://github.com/mastodon/mastodon-ios.git
synced 2024-12-17 03:09:19 +01:00
feat: Implement /translate endpoint
This commit is contained in:
parent
9affb0f637
commit
d5be87992d
@ -18,7 +18,19 @@ extension DataSourceFacade {
|
|||||||
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
|
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
|
||||||
await selectionFeedbackGenerator.selectionChanged()
|
await selectionFeedbackGenerator.selectionChanged()
|
||||||
|
|
||||||
let status = status.object(in: provider.context.managedObjectContext)
|
guard
|
||||||
status?.translatedContent = "LOREM IPSUM TRANSLATED TEXT"
|
let status = status.object(in: provider.context.managedObjectContext)
|
||||||
|
else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = try await provider.context
|
||||||
|
.apiService
|
||||||
|
.translateStatus(
|
||||||
|
statusID: status.id,
|
||||||
|
authenticationBox: provider.authContext.mastodonAuthenticationBox
|
||||||
|
).value
|
||||||
|
|
||||||
|
status.translatedContent = result.content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
//
|
||||||
|
// APIService+Status+Translate.swift
|
||||||
|
// Mastodon
|
||||||
|
//
|
||||||
|
// Created by Marcus Kida on 02.12.2022.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import Combine
|
||||||
|
import CoreData
|
||||||
|
import CoreDataStack
|
||||||
|
import CommonOSLog
|
||||||
|
import MastodonSDK
|
||||||
|
|
||||||
|
extension APIService {
|
||||||
|
|
||||||
|
public func translateStatus(
|
||||||
|
statusID: Mastodon.Entity.Status.ID,
|
||||||
|
authenticationBox: MastodonAuthenticationBox
|
||||||
|
) async throws -> Mastodon.Response.Content<Mastodon.Entity.Translation> {
|
||||||
|
let domain = authenticationBox.domain
|
||||||
|
let authorization = authenticationBox.userAuthorization
|
||||||
|
|
||||||
|
let response = try await Mastodon.API.Statuses.translate(
|
||||||
|
session: session,
|
||||||
|
domain: domain,
|
||||||
|
statusID: statusID,
|
||||||
|
authorization: authorization
|
||||||
|
).singleOutput()
|
||||||
|
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
//
|
||||||
|
// Mastodon+API+Statuses+Translate.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Marcus Kida on 02.12.2022.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import Combine
|
||||||
|
|
||||||
|
extension Mastodon.API.Statuses {
|
||||||
|
|
||||||
|
private static func translateEndpointURL(domain: String, statusID: Mastodon.Entity.Status.ID) -> URL {
|
||||||
|
return Mastodon.API.endpointURL(domain: domain)
|
||||||
|
.appendingPathComponent("statuses")
|
||||||
|
.appendingPathComponent(statusID)
|
||||||
|
.appendingPathComponent("translate")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Translate Status
|
||||||
|
///
|
||||||
|
/// Translate a given Status.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - session: `URLSession`
|
||||||
|
/// - domain: Mastodon instance domain. e.g. "example.com"
|
||||||
|
/// - statusID: id for status
|
||||||
|
/// - authorization: User token. Could be nil if status is public
|
||||||
|
/// - Returns: `AnyPublisher` contains `Status` nested in the response
|
||||||
|
public static func translate(
|
||||||
|
session: URLSession,
|
||||||
|
domain: String,
|
||||||
|
statusID: Mastodon.Entity.Status.ID,
|
||||||
|
authorization: Mastodon.API.OAuth.Authorization?
|
||||||
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Translation>, Error> {
|
||||||
|
let request = Mastodon.API.post(
|
||||||
|
url: translateEndpointURL(domain: domain, statusID: statusID),
|
||||||
|
query: nil,
|
||||||
|
authorization: authorization
|
||||||
|
)
|
||||||
|
return session.dataTaskPublisher(for: request)
|
||||||
|
.tryMap { data, response in
|
||||||
|
let value = try Mastodon.API.decode(type: Mastodon.Entity.Translation.self, from: data, response: response)
|
||||||
|
return Mastodon.Response.Content(value: value, response: response)
|
||||||
|
}
|
||||||
|
.eraseToAnyPublisher()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Marcus Kida on 02.12.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
extension Mastodon.Entity {
|
||||||
|
public struct Translation: Codable {
|
||||||
|
public let content: String?
|
||||||
|
public let sourceLanguage: String?
|
||||||
|
public let provider: String?
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case content
|
||||||
|
case sourceLanguage = "detected_source_language"
|
||||||
|
case provider
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user