add caching for translations
This commit is contained in:
parent
9af5fe78b1
commit
9e1300ccc1
|
@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
- support for setting instance languages in metadata
|
- support for setting instance languages in metadata
|
||||||
- support for reusing oauth tokens, and not requiring new authorizations
|
- support for reusing oauth tokens, and not requiring new authorizations
|
||||||
- the ability to obfuscate domains in your MRF descriptions
|
- the ability to obfuscate domains in your MRF descriptions
|
||||||
|
- automatic translation of statuses via DeepL or LibreTranslate
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- MFM parsing is now done on the backend by a modified version of ilja's parser -> https://akkoma.dev/AkkomaGang/mfm-parser
|
- MFM parsing is now done on the backend by a modified version of ilja's parser -> https://akkoma.dev/AkkomaGang/mfm-parser
|
||||||
|
|
|
@ -154,7 +154,8 @@ defmodule Pleroma.Application do
|
||||||
build_cachex("web_resp", limit: 2500),
|
build_cachex("web_resp", limit: 2500),
|
||||||
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
|
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
|
||||||
build_cachex("failed_proxy_url", limit: 2500),
|
build_cachex("failed_proxy_url", limit: 2500),
|
||||||
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000)
|
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
|
||||||
|
build_cachex("translations", default_ttl: :timer.hours(24 * 30), limit: 2500)
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
|
||||||
plug(:skip_public_check when action in [:index, :show])
|
plug(:skip_public_check when action in [:index, :show])
|
||||||
|
|
||||||
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
|
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
|
||||||
|
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
OAuthScopesPlug,
|
OAuthScopesPlug,
|
||||||
|
@ -427,7 +428,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
|
||||||
{:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
|
{:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
|
||||||
translation_module <- Config.get([:translator, :module]),
|
translation_module <- Config.get([:translator, :module]),
|
||||||
{:ok, detected, translation} <-
|
{:ok, detected, translation} <-
|
||||||
translation_module.translate(activity.object.data["content"], language) do
|
fetch_or_translate(
|
||||||
|
activity.id,
|
||||||
|
activity.object.data["content"],
|
||||||
|
language,
|
||||||
|
translation_module
|
||||||
|
) do
|
||||||
json(conn, %{detected_language: detected, text: translation})
|
json(conn, %{detected_language: detected, text: translation})
|
||||||
else
|
else
|
||||||
{:enabled, false} ->
|
{:enabled, false} ->
|
||||||
|
@ -443,6 +449,18 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp fetch_or_translate(status_id, text, language, translation_module) do
|
||||||
|
@cachex.fetch!(:user_cache, "translations:#{status_id}:#{language}", fn _ ->
|
||||||
|
value = translation_module.translate(text, language)
|
||||||
|
|
||||||
|
with {:ok, _, _} <- value do
|
||||||
|
value
|
||||||
|
else
|
||||||
|
_ -> {:ignore, value}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
defp put_application(params, %{assigns: %{token: %Token{user: %User{} = user} = token}} = _conn) do
|
defp put_application(params, %{assigns: %{token: %Token{user: %User{} = user} = token}} = _conn) do
|
||||||
if user.disclose_client do
|
if user.disclose_client do
|
||||||
%{client_name: client_name, website: website} = Repo.preload(token, :app).app
|
%{client_name: client_name, website: website} = Repo.preload(token, :app).app
|
||||||
|
|
|
@ -81,6 +81,9 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do
|
||||||
if Config.get([:instance, :profile_directory]) do
|
if Config.get([:instance, :profile_directory]) do
|
||||||
"profile_directory"
|
"profile_directory"
|
||||||
end,
|
end,
|
||||||
|
if Config.get([:translator, :enabled], false) do
|
||||||
|
"akkoma:machine_translation"
|
||||||
|
end,
|
||||||
"custom_emoji_reactions"
|
"custom_emoji_reactions"
|
||||||
]
|
]
|
||||||
|> Enum.filter(& &1)
|
|> Enum.filter(& &1)
|
||||||
|
|
|
@ -2083,7 +2083,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
|
||||||
test "should return text and detected language", %{conn: conn} do
|
test "should return text and detected language", %{conn: conn} do
|
||||||
clear_config([:deepl, :tier], :free)
|
clear_config([:deepl, :tier], :free)
|
||||||
|
|
||||||
Tesla.Mock.mock(fn
|
Tesla.Mock.mock_global(fn
|
||||||
%{method: :post, url: "https://api-free.deepl.com/v2/translate"} ->
|
%{method: :post, url: "https://api-free.deepl.com/v2/translate"} ->
|
||||||
%Tesla.Env{
|
%Tesla.Env{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
@ -2116,7 +2116,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
|
||||||
test "should not allow translating of statuses you cannot see", %{conn: conn} do
|
test "should not allow translating of statuses you cannot see", %{conn: conn} do
|
||||||
clear_config([:deepl, :tier], :free)
|
clear_config([:deepl, :tier], :free)
|
||||||
|
|
||||||
Tesla.Mock.mock(fn
|
Tesla.Mock.mock_global(fn
|
||||||
%{method: :post, url: "https://api-free.deepl.com/v2/translate"} ->
|
%{method: :post, url: "https://api-free.deepl.com/v2/translate"} ->
|
||||||
%Tesla.Env{
|
%Tesla.Env{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|
Loading…
Reference in New Issue