Improve auto translate
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
664f64a3fc
commit
ac76bc011a
6 changed files with 40 additions and 40 deletions
|
@ -7,8 +7,8 @@ defmodule Pleroma.StatusTranslation do
|
||||||
|
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
alias Pleroma.Activity
|
||||||
alias Pleroma.Language.Translation
|
alias Pleroma.Language.Translation
|
||||||
alias Pleroma.Object
|
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.StatusTranslation
|
alias Pleroma.StatusTranslation
|
||||||
|
|
||||||
|
@ -18,20 +18,20 @@ defmodule Pleroma.StatusTranslation do
|
||||||
field(:detected_source_language, :string)
|
field(:detected_source_language, :string)
|
||||||
field(:content, {:map, :string})
|
field(:content, {:map, :string})
|
||||||
|
|
||||||
belongs_to(:object, Object)
|
belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
def changeset(%StatusTranslation{} = status_translation, params \\ %{}) do
|
def changeset(%StatusTranslation{} = status_translation, params \\ %{}) do
|
||||||
status_translation
|
status_translation
|
||||||
|> cast(params, [:object_id, :language, :provider, :detected_source_language, :content])
|
|> cast(params, [:activity_id, :language, :provider, :detected_source_language, :content])
|
||||||
|> validate_required([:object_id, :language, :content])
|
|> validate_required([:activity_id, :language, :content])
|
||||||
|> unique_constraint([:object_id, :language])
|
|> unique_constraint([:activity_id, :language])
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(%Object{} = object, language, options \\ [fetch: true]) do
|
def get(%Activity{} = activity, language, options \\ [fetch: true]) do
|
||||||
translation = Repo.get_by(StatusTranslation, object_id: object.id, language: language)
|
translation = Repo.get_by(StatusTranslation, activity_id: activity.id, language: language)
|
||||||
|
|
||||||
if translation || not Keyword.get(options, :fetch) do
|
if translation || not Keyword.get(options, :fetch) do
|
||||||
translation
|
translation
|
||||||
|
@ -43,13 +43,13 @@ def get(%Object{} = object, language, options \\ [fetch: true]) do
|
||||||
provider: provider
|
provider: provider
|
||||||
}} <-
|
}} <-
|
||||||
Translation.translate(
|
Translation.translate(
|
||||||
object.data["content"],
|
activity.object.data["content"],
|
||||||
object.data["language"],
|
activity.object.data["language"],
|
||||||
language
|
language
|
||||||
) do
|
) do
|
||||||
%StatusTranslation{}
|
%StatusTranslation{}
|
||||||
|> changeset(%{
|
|> changeset(%{
|
||||||
object_id: object.id,
|
activity_id: activity.id,
|
||||||
language: language,
|
language: language,
|
||||||
provider: provider,
|
provider: provider,
|
||||||
detected_source_language: detected_source_language,
|
detected_source_language: detected_source_language,
|
||||||
|
|
|
@ -466,23 +466,16 @@ def translate_many_operation do
|
||||||
description: "Translate status with an external API",
|
description: "Translate status with an external API",
|
||||||
operationId: "StatusController.translate_many",
|
operationId: "StatusController.translate_many",
|
||||||
security: [%{"oAuth" => ["read:statuses"]}],
|
security: [%{"oAuth" => ["read:statuses"]}],
|
||||||
parameters: [
|
|
||||||
Operation.parameter(
|
|
||||||
:id,
|
|
||||||
:query,
|
|
||||||
%Schema{
|
|
||||||
oneOf: [%Schema{type: :array, items: %Schema{type: :string}}, %Schema{type: :string}]
|
|
||||||
},
|
|
||||||
"Status IDs",
|
|
||||||
example: "123"
|
|
||||||
)
|
|
||||||
],
|
|
||||||
requestBody:
|
requestBody:
|
||||||
request_body(
|
request_body(
|
||||||
"Parameters",
|
"Parameters",
|
||||||
%Schema{
|
%Schema{
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: %{
|
properties: %{
|
||||||
|
ids: %Schema{
|
||||||
|
type: :array,
|
||||||
|
items: %Schema{type: :string}
|
||||||
|
},
|
||||||
target_language: %Schema{
|
target_language: %Schema{
|
||||||
type: :string,
|
type: :string,
|
||||||
nullable: true,
|
nullable: true,
|
||||||
|
|
|
@ -622,18 +622,18 @@ def translate(
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc "POST /api/v1/pleroma/statuses/translate"
|
@doc "POST /api/v1/bigbuffet/statuses/translate"
|
||||||
def translate_many(%{body_params: params, assigns: %{user: user}} = conn, %{id: status_id}) do
|
def translate_many(%{body_params: %{ids: status_ids} = params, assigns: %{user: user}} = conn, _) do
|
||||||
with {:list, status_ids} <- {:list, List.wrap(status_id)},
|
with {:language, language} when is_binary(language) <-
|
||||||
{:language, language} when is_binary(language) <-
|
|
||||||
{:language, Map.get(params, :target_language) || user.language},
|
{:language, Map.get(params, :target_language) || user.language},
|
||||||
{:results, results} <-
|
{:results, results} <-
|
||||||
{:results, Enum.each(status_ids, &translate_one(&1, language, user))},
|
{:results, Enum.map(status_ids, &translate_one(&1, language, user))},
|
||||||
{:filtered_results, results} <-
|
{:filtered_results, results} <-
|
||||||
Enum.filter(results, fn
|
{:filtered_results,
|
||||||
%StatusTranslation{} -> true
|
Enum.filter(results, fn
|
||||||
_ -> false
|
%StatusTranslation{} -> true
|
||||||
end) do
|
_ -> false
|
||||||
|
end)} do
|
||||||
render(conn, "translations.json", results: results)
|
render(conn, "translations.json", results: results)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -643,7 +643,7 @@ defp translate_one(status_id, language, user \\ nil) do
|
||||||
{:authentication,
|
{:authentication,
|
||||||
!is_nil(user) ||
|
!is_nil(user) ||
|
||||||
Pleroma.Config.get([Pleroma.Language.Translation, :allow_unauthenticated])},
|
Pleroma.Config.get([Pleroma.Language.Translation, :allow_unauthenticated])},
|
||||||
%Activity{object: object} <- Activity.get_by_id_with_object(status_id),
|
%Activity{object: object} = activity <- Activity.get_by_id_with_object(status_id),
|
||||||
{:visibility, visibility} when visibility in ["public", "unlisted"] <-
|
{:visibility, visibility} when visibility in ["public", "unlisted"] <-
|
||||||
{:visibility, Visibility.get_visibility(object)},
|
{:visibility, Visibility.get_visibility(object)},
|
||||||
{:allow_remote, true} <-
|
{:allow_remote, true} <-
|
||||||
|
@ -651,7 +651,7 @@ defp translate_one(status_id, language, user \\ nil) do
|
||||||
Object.local?(object) ||
|
Object.local?(object) ||
|
||||||
Pleroma.Config.get([Pleroma.Language.Translation, :allow_remote])},
|
Pleroma.Config.get([Pleroma.Language.Translation, :allow_remote])},
|
||||||
%StatusTranslation{} = result <-
|
%StatusTranslation{} = result <-
|
||||||
StatusTranslation.get(object, language) do
|
StatusTranslation.get(activity, language) do
|
||||||
result
|
result
|
||||||
else
|
else
|
||||||
result -> result
|
result -> result
|
||||||
|
|
|
@ -244,7 +244,7 @@ def render(
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
|
def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
|
||||||
object = Object.normalize(activity, fetch: false)
|
%Activity{object: object} = activity = Activity.get_by_id_with_object(activity.id)
|
||||||
|
|
||||||
user = CommonAPI.get_user(activity.data["actor"])
|
user = CommonAPI.get_user(activity.data["actor"])
|
||||||
user_follower_address = user.follower_address
|
user_follower_address = user.follower_address
|
||||||
|
@ -473,7 +473,7 @@ def render("show.json", %{activity: %{data: %{"object" => _object}} = activity}
|
||||||
quotes_count: object.data["quotesCount"] || 0,
|
quotes_count: object.data["quotesCount"] || 0,
|
||||||
event: build_event(object.data, opts[:for]),
|
event: build_event(object.data, opts[:for]),
|
||||||
bookmark_folder: bookmark_folder,
|
bookmark_folder: bookmark_folder,
|
||||||
translation: get_translation(object, opts[:language])
|
translation: get_translation(activity, opts[:language])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -698,20 +698,27 @@ def render("translations.json", %{results: results}) do
|
||||||
|
|
||||||
def render("translation.json", %{
|
def render("translation.json", %{
|
||||||
result: %{
|
result: %{
|
||||||
|
activity_id: id,
|
||||||
content: %{"content" => content},
|
content: %{"content" => content},
|
||||||
detected_source_language: detected_source_language,
|
detected_source_language: detected_source_language,
|
||||||
provider: provider
|
provider: provider
|
||||||
}
|
}
|
||||||
}) do
|
}) do
|
||||||
%{content: content, detected_source_language: detected_source_language, provider: provider}
|
%{
|
||||||
|
id: id,
|
||||||
|
content: content,
|
||||||
|
detected_source_language: detected_source_language,
|
||||||
|
provider: provider
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_translation(object, language) when is_binary(language) or is_list(language) do
|
defp get_translation(%Activity{object: object} = activity, language)
|
||||||
|
when is_binary(language) or is_list(language) do
|
||||||
with languages <- List.wrap(language),
|
with languages <- List.wrap(language),
|
||||||
true <- is_binary(object.data["language"]),
|
true <- is_binary(object.data["language"]),
|
||||||
true <- object.data["language"] not in languages,
|
true <- object.data["language"] not in languages,
|
||||||
%StatusTranslation{} = translation <-
|
%StatusTranslation{} = translation <-
|
||||||
StatusTranslation.get(object, language, fetch: false) do
|
StatusTranslation.get(activity, language, fetch: false) do
|
||||||
render("translation.json", %{result: translation})
|
render("translation.json", %{result: translation})
|
||||||
else
|
else
|
||||||
_ -> nil
|
_ -> nil
|
||||||
|
|
|
@ -235,7 +235,7 @@ defmodule Pleroma.Web.Router do
|
||||||
post("/uploader_callback/:upload_path", UploaderController, :callback)
|
post("/uploader_callback/:upload_path", UploaderController, :callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api/v1/pleroma", Pleroma.Web.MastodonAPI do
|
scope "/api/v1/bigbuffet", Pleroma.Web.MastodonAPI do
|
||||||
pipe_through(:api)
|
pipe_through(:api)
|
||||||
|
|
||||||
post("/statuses/translate", StatusController, :translate_many)
|
post("/statuses/translate", StatusController, :translate_many)
|
||||||
|
|
|
@ -7,7 +7,7 @@ defmodule Pleroma.Repo.Migrations.CreateStatusTranslations do
|
||||||
|
|
||||||
def change do
|
def change do
|
||||||
create_if_not_exists table(:status_translations) do
|
create_if_not_exists table(:status_translations) do
|
||||||
add(:object_id, references(:objects, on_delete: :delete_all), null: false)
|
add(:activity_id, references(:activities, type: :uuid, on_delete: :delete_all), null: false)
|
||||||
add(:language, :text, null: false)
|
add(:language, :text, null: false)
|
||||||
add(:provider, :text)
|
add(:provider, :text)
|
||||||
add(:detected_source_language, :text)
|
add(:detected_source_language, :text)
|
||||||
|
@ -16,6 +16,6 @@ def change do
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create_if_not_exists(unique_index(:status_translations, [:object_id, :language]))
|
create_if_not_exists(unique_index(:status_translations, [:activity_id, :language]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue