2022-08-30 08:58:54 -06:00
|
|
|
defmodule Pleroma.Web.AkkomaAPI.TranslationController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
|
|
|
|
|
|
|
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
|
|
|
|
|
|
|
|
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{@unauthenticated_access | scopes: ["read:statuses"]}
|
|
|
|
when action in [
|
|
|
|
:languages
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TranslationOperation
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
|
|
|
@doc "GET /api/v1/akkoma/translation/languages"
|
|
|
|
def languages(conn, _params) do
|
2022-08-30 10:59:33 -06:00
|
|
|
with {:ok, source_languages, dest_languages} <- get_languages() do
|
2022-08-30 08:58:54 -06:00
|
|
|
conn
|
2022-08-30 10:59:33 -06:00
|
|
|
|> json(%{source: source_languages, target: dest_languages})
|
2022-08-30 08:58:54 -06:00
|
|
|
else
|
|
|
|
e -> IO.inspect(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_languages do
|
|
|
|
module = Pleroma.Config.get([:translator, :module])
|
|
|
|
|
|
|
|
@cachex.fetch!(:translations_cache, "languages:#{module}}", fn _ ->
|
2022-08-30 10:59:33 -06:00
|
|
|
with {:ok, source_languages, dest_languages} <- module.languages() do
|
|
|
|
{:ok, source_languages, dest_languages}
|
2022-08-30 08:58:54 -06:00
|
|
|
else
|
|
|
|
{:error, err} -> {:ignore, {:error, err}}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|