From 55fc13481755613ebc1976d571f15ab5e2140955 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sun, 28 Aug 2022 21:40:00 +0100 Subject: [PATCH] add deepl translation operation --- config/config.exs | 1 + config/description.exs | 6 ++ .../api_spec/operations/status_operation.ex | 55 +++++++++++++++++++ .../controllers/status_controller.ex | 24 +++++++- lib/pleroma/web/router.ex | 1 + 5 files changed, 86 insertions(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index b5eaba5e1..6a1769294 100644 --- a/config/config.exs +++ b/config/config.exs @@ -844,6 +844,7 @@ config :pleroma, Pleroma.Search.Elasticsearch.Cluster, } config :pleroma, :deepl, + enabled: false, # either :free or :pro tier: :free, api_key: "" diff --git a/config/description.exs b/config/description.exs index 4703748f8..47f80532d 100644 --- a/config/description.exs +++ b/config/description.exs @@ -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, diff --git a/lib/pleroma/web/api_spec/operations/status_operation.ex b/lib/pleroma/web/api_spec/operations/status_operation.ex index a5da8b58e..5d18dccf6 100644 --- a/lib/pleroma/web/api_spec/operations/status_operation.ex +++ b/lib/pleroma/web/api_spec/operations/status_operation.ex @@ -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 diff --git a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex index 9ab30742b..c497b4e91 100644 --- a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex @@ -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 diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 647d99278..aff7b67db 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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)