Automatic status translation #187
|
@ -844,6 +844,7 @@ config :pleroma, Pleroma.Search.Elasticsearch.Cluster,
|
|||
}
|
||||
|
||||
config :pleroma, :deepl,
|
||||
enabled: false,
|
||||
# either :free or :pro
|
||||
tier: :free,
|
||||
api_key: ""
|
||||
|
|
|
@ -3356,6 +3356,12 @@ config :pleroma, :config_description, [
|
|||
type: :group,
|
||||
description: "DeepL settings.",
|
||||
children: [
|
||||
%{
|
||||
key: :enabled,
|
||||
type: :boolean,
|
||||
description: "Is translation enabled?",
|
||||
suggestion: [true, false]
|
||||
},
|
||||
%{
|
||||
key: :tier,
|
||||
type: :atom,
|
||||
|
|
|
@ -406,6 +406,22 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
|
|||
}
|
||||
end
|
||||
|
||||
def translate_operation do
|
||||
%Operation{
|
||||
tags: ["Retrieve status translation"],
|
||||
summary: "Translate status",
|
||||
description: "View the translation of a given status",
|
||||
operationId: "StatusController.translation",
|
||||
security: [%{"oAuth" => ["read:statuses"]}],
|
||||
parameters: [id_param(), language_param()],
|
||||
responses: %{
|
||||
200 => Operation.response("Translation", "application/json", translation()),
|
||||
400 => Operation.response("Error", "application/json", ApiError),
|
||||
404 => Operation.response("Not Found", "application/json", ApiError)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def array_of_statuses do
|
||||
%Schema{type: :array, items: Status, example: [Status.schema().example]}
|
||||
end
|
||||
|
@ -552,6 +568,10 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
|
|||
)
|
||||
end
|
||||
|
||||
defp language_param do
|
||||
Operation.parameter(:language, :path, :string, "ISO 639 language code", example: "en")
|
||||
end
|
||||
|
||||
defp status_response do
|
||||
Operation.response("Status", "application/json", Status)
|
||||
end
|
||||
|
@ -573,4 +593,39 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
|
|||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp translation_result do
|
||||
%Schema{
|
||||
title: "StatusTranslation",
|
||||
description: "The translation of a status.",
|
||||
type: :object,
|
||||
required: [:detected_source_language, :text],
|
||||
properties: %{
|
||||
detected_source_language: %Schema{
|
||||
type: :string,
|
||||
description: "The detected source language of the status."
|
||||
},
|
||||
text: %Schema{
|
||||
type: :string,
|
||||
description: "The translated text of the status."
|
||||
}
|
||||
},
|
||||
example: %{
|
||||
"detected_source_language" => "en",
|
||||
"text" => "Hear, Feel, Think"
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp translation do
|
||||
%Schema{
|
||||
title: "StatusTranslation",
|
||||
description: "The translation of a status.",
|
||||
type: :object,
|
||||
required: [:translations],
|
||||
properties: %{
|
||||
translations: %Schema{type: :array, items: translation_result()}
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,6 +14,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
|
|||
alias Pleroma.Bookmark
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Config
|
||||
alias Pleroma.ScheduledActivity
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
|
@ -37,7 +38,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
|
|||
when action in [
|
||||
:index,
|
||||
:show,
|
||||
:context
|
||||
:context,
|
||||
:translate
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -418,6 +420,26 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
|
|||
)
|
||||
end
|
||||
|
||||
@doc "GET /api/v1/statuses/:id/translations/:language"
|
||||
def translate(%{assigns: %{user: user}} = conn, %{id: id, language: language}) do
|
||||
with {:enabled, true} <- {:enabled, Config.get([:deepl, :enabled])},
|
||||
%Activity{} = activity <- IO.inspect(Activity.get_by_id_with_object(id)),
|
||||
{:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
|
||||
api_key <- Config.get([:deepl, :api_key]),
|
||||
tier <- Config.get([:deepl, :tier]),
|
||||
{:ok, translation} <- DeepLex.translate(api_key, tier, activity.object.data["content"], language) do
|
||||
json(conn, translation)
|
||||
else
|
||||
{:enabled, false} ->
|
||||
conn
|
||||
|> put_status(:bad_request)
|
||||
|> json(%{"error" => "DeepL is not enabled"})
|
||||
|
||||
{:visible, false} ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
defp put_application(params, %{assigns: %{token: %Token{user: %User{} = user} = token}} = _conn) do
|
||||
if user.disclose_client do
|
||||
%{client_name: client_name, website: website} = Repo.preload(token, :app).app
|
||||
|
|
|
@ -553,6 +553,7 @@ defmodule Pleroma.Web.Router do
|
|||
post("/statuses/:id/unbookmark", StatusController, :unbookmark)
|
||||
post("/statuses/:id/mute", StatusController, :mute_conversation)
|
||||
post("/statuses/:id/unmute", StatusController, :unmute_conversation)
|
||||
get("/statuses/:id/translations/:language", StatusController, :translate)
|
||||
|
||||
post("/push/subscription", SubscriptionController, :create)
|
||||
get("/push/subscription", SubscriptionController, :show)
|
||||
|
|
Loading…
Reference in New Issue