Merge branch 'translations-config' into 'develop'

Allow to enable translations for unauthenticated users or disable translating remote content

See merge request soapbox-pub/rebased!228
This commit is contained in:
marcin mikołajczak 2022-12-19 23:57:13 +00:00
commit 2eaa1976ce
5 changed files with 37 additions and 8 deletions

View file

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

View file

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

View file

@ -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")

View file

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

View file

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