From 1f981686cd68f36485ab6ceb4a42e192414be779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 19 Dec 2022 23:55:09 +0100 Subject: [PATCH] Allow to enable translations for unauthenticated or disable translating remote content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- config/config.exs | 2 ++ config/description.exs | 12 +++++++++++ .../controllers/status_controller.ex | 21 +++++++++++++++---- .../web/mastodon_api/views/instance_view.ex | 8 ++++--- lib/pleroma/web/router.ex | 2 +- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/config/config.exs b/config/config.exs index eb8efe00d6..27e2da4510 100644 --- a/config/config.exs +++ b/config/config.exs @@ -879,6 +879,8 @@ config :pleroma, Pleroma.Web.WebFinger, domain: nil, update_nickname_on_user_fetch: false +config :pleroma, Pleroma.Language.Translation, allow_unauthenticated: false, allow_remote: true + config :geospatial, Geospatial.Service, service: Geospatial.Providers.Nominatim config :geospatial, Geospatial.Providers.GoogleMaps, diff --git a/config/description.exs b/config/description.exs index cf582e5271..616ce51680 100644 --- a/config/description.exs +++ b/config/description.exs @@ -3524,6 +3524,18 @@ Pleroma.Language.Translation.Libretranslate ] }, + %{ + key: :allow_unauthenticated, + type: :boolean, + label: "Allow unauthenticated", + description: "Whether to let unauthenticated users translate posts" + }, + %{ + key: :allow_remote, + type: :boolean, + label: "Allow remote", + description: "Whether to allow translation of remote posts" + }, %{ group: {:subgroup, Pleroma.Language.Translation.Deepl}, key: :base_url, diff --git a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex index b3015a6632..520def08e0 100644 --- a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex @@ -40,12 +40,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do :card, :context, :show_history, - :show_source + :show_source, + :translate ] ) - plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action == :translate) - plug( OAuthScopesPlug, %{scopes: ["write:statuses"]} @@ -460,9 +459,17 @@ def context(%{assigns: %{user: user}} = conn, %{id: id}) do @doc "POST /api/v1/statuses/:id/translate" def translate(%{body_params: params, assigns: %{user: user}} = conn, %{id: status_id}) do - with %Activity{object: object} <- Activity.get_by_id_with_object(status_id), + with {:authentication, true} <- + {:authentication, + !is_nil(user) || + Pleroma.Config.get([Pleroma.Language.Translation, :allow_unauthenticated])}, + %Activity{object: object} <- Activity.get_by_id_with_object(status_id), {:visibility, visibility} when visibility in ["public", "unlisted"] <- {:visibility, Visibility.get_visibility(object)}, + {:allow_remote, true} <- + {:allow_remote, + Object.local?(object) || + Pleroma.Config.get([Pleroma.Language.Translation, :allow_remote])}, {:language, language} when is_binary(language) <- {:language, Map.get(params, :target_language) || user.language}, {:ok, result} <- @@ -473,6 +480,12 @@ def translate(%{body_params: params, assigns: %{user: user}} = conn, %{id: statu ) do render(conn, "translation.json", result) else + {:authentication, false} -> + render_error(conn, :unauthorized, "Authorization is required to translate statuses") + + {:allow_remote, false} -> + render_error(conn, :bad_request, "You can't translate remote posts") + {:language, nil} -> render_error(conn, :bad_request, "Language not specified") diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index 3c9697905b..e75d3a8ee3 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -254,7 +254,7 @@ defp pleroma_configuration(instance) do birthday_required: Config.get([:instance, :birthday_required]), birthday_min_age: Config.get([:instance, :birthday_min_age]), migration_cooldown_period: Config.get([:instance, :migration_cooldown_period]), - translation: supported_languages() + translation: translation_configuration() }, stats: %{mau: Pleroma.User.active_user_count()}, vapid_public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key), @@ -283,7 +283,7 @@ defp pleroma_configuration2(instance) do }) end - defp supported_languages do + defp translation_configuration do enabled = Pleroma.Language.Translation.configured?() source_languages = @@ -304,7 +304,9 @@ defp supported_languages do %{ source_languages: source_languages, - target_languages: target_languages + target_languages: target_languages, + allow_unauthenticated: Config.get([Pleroma.Language.Translation, :allow_unauthenticated]), + allow_remote: Config.get([Pleroma.Language.Translation, :allow_remote]) } end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 1006329fac..283e191c99 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -643,7 +643,6 @@ 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) - post("/statuses/:id/translate", StatusController, :translate) post("/push/subscription", SubscriptionController, :create) get("/push/subscription", SubscriptionController, :show) @@ -696,6 +695,7 @@ defmodule Pleroma.Web.Router do get("/statuses/:id/reblogged_by", StatusController, :reblogged_by) get("/statuses/:id/history", StatusController, :show_history) get("/statuses/:id/source", StatusController, :show_source) + post("/statuses/:id/translate", StatusController, :translate) get("/custom_emojis", CustomEmojiController, :index)