1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-16 10:48:49 +01:00

feat: Implement /translate endpoint

This commit is contained in:
Marcus Kida 2022-12-02 12:12:46 +01:00
parent 9affb0f637
commit d5be87992d
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
4 changed files with 118 additions and 2 deletions

View File

@ -18,7 +18,19 @@ extension DataSourceFacade {
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
await selectionFeedbackGenerator.selectionChanged()
guard
let status = status.object(in: provider.context.managedObjectContext)
status?.translatedContent = "LOREM IPSUM TRANSLATED TEXT"
else {
return
}
let result = try await provider.context
.apiService
.translateStatus(
statusID: status.id,
authenticationBox: provider.authContext.mastodonAuthenticationBox
).value
status.translatedContent = result.content
}
}

View File

@ -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
}
}

View File

@ -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()
}
}

View File

@ -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
}
}
}