Store remote post language information

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-10-29 22:19:47 +02:00
parent 6a8a8f2a2e
commit 972498cac4
5 changed files with 60 additions and 12 deletions

View file

@ -108,6 +108,7 @@ defp fix(data) do
|> fix_attachments()
|> Transmogrifier.fix_emoji()
|> Transmogrifier.fix_content_map()
|> Transmogrifier.maybe_add_language()
end
def changeset(struct, data) do

View file

@ -58,6 +58,7 @@ defmacro status_object_fields do
field(:like_count, :integer, default: 0)
field(:announcement_count, :integer, default: 0)
field(:quotes_count, :integer, default: 0)
field(:language, :string)
field(:inReplyTo, ObjectValidators.ObjectID)
field(:quoteUrl, ObjectValidators.ObjectID)
field(:url, ObjectValidators.Uri)

View file

@ -44,6 +44,7 @@ def fix_object(object, options \\ []) do
|> fix_content_map()
|> fix_addressing()
|> fix_summary()
|> maybe_add_language()
end
def fix_summary(%{"summary" => nil} = object) do
@ -1096,4 +1097,40 @@ defp maybe_add_content_map(%{"language" => language, "content" => content} = obj
end
defp maybe_add_content_map(object), do: object
def maybe_add_language(object) do
language =
get_language_from_context(object) |> Pleroma.Web.CommonAPI.Utils.get_valid_language() ||
get_language_from_content_map(object) |> Pleroma.Web.CommonAPI.Utils.get_valid_language()
if language do
Map.put(object, "language", language)
else
object
end
end
defp get_language_from_context(%{"@context" => context}) do
case context
|> Enum.find(fn
%{"@language" => language} -> language != "und"
_ -> nil
end) do
%{"@language" => language} -> language
_ -> nil
end
end
defp get_language_from_context(_), do: nil
defp get_language_from_content_map(%{"contentMap" => content_map, "content" => source_content}) do
content_groups = Map.to_list(content_map)
case Enum.find(content_groups, fn {_, content} -> content == source_content end) do
{language, _} -> language
_ -> nil
end
end
defp get_language_from_content_map(_), do: nil
end

View file

@ -14,15 +14,6 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
import Pleroma.Web.Gettext
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
@supported_locales ~w(
aa ab ae af ak am an ar as av ay az ba be bg bh bi bm bn bo br bs ca ce ch co cr cs cu cv cy da
de dv dz ee el en eo es et eu fa ff fi fj fo fr fy ga gd gl gu gv ha he hi ho hr ht hu hy hz ia
id ie ig ii ik io is it iu ja jv ka kg ki kj kk kl km kn ko kr ks ku kv kw ky la lb lg li ln lo
lt lu lv mg mh mi mk ml mn mr ms mt my na nb nd ne ng nl nn no nr nv ny oc oj om or os pa pi pl
ps pt qu rm rn ro ru rw sa sc sd se sg si sk sl sn so sq sr ss st su sv sw ta te tg th ti tk tl
tn to tr ts tt tw ty ug uk ur uz ve vi vo wa wo xh yi yo za zh zu ast ckb kab kmr zgh
)
defstruct valid?: true,
errors: [],
user: nil,
@ -236,9 +227,9 @@ defp sensitive(draft) do
end
defp language(%{params: %{language: language}} = draft) when not_empty_string(language) do
case language |> String.split("_") |> Enum.at(0) do
locale when locale in @supported_locales ->
%__MODULE__{draft | language: locale}
case Utils.get_valid_language(language) do
language when is_binary(language) ->
%__MODULE__{draft | language: language}
_ ->
draft

View file

@ -23,6 +23,15 @@ defmodule Pleroma.Web.CommonAPI.Utils do
require Logger
require Pleroma.Constants
@supported_locales ~w(
aa ab ae af ak am an ar as av ay az ba be bg bh bi bm bn bo br bs ca ce ch co cr cs cu cv cy da
de dv dz ee el en eo es et eu fa ff fi fj fo fr fy ga gd gl gu gv ha he hi ho hr ht hu hy hz ia
id ie ig ii ik io is it iu ja jv ka kg ki kj kk kl km kn ko kr ks ku kv kw ky la lb lg li ln lo
lt lu lv mg mh mi mk ml mn mr ms mt my na nb nd ne ng nl nn no nr nv ny oc oj om or os pa pi pl
ps pt qu rm rn ro ru rw sa sc sd se sg si sk sl sn so sq sr ss st su sv sw ta te tg th ti tk tl
tn to tr ts tt tw ty ug uk ur uz ve vi vo wa wo xh yi yo za zh zu ast ckb kab kmr zgh
)
def attachments_from_ids(%{media_ids: ids, descriptions: desc}) do
attachments_from_ids_descs(ids, desc)
end
@ -482,4 +491,13 @@ def validate_attachments_count(attachments) do
{:error, dgettext("errors", "Too many attachments")}
end
end
def get_valid_language(language) when is_binary(language) do
case language |> String.split("_") |> Enum.at(0) do
locale when locale in @supported_locales -> locale
_ -> nil
end
end
def get_valid_language(_), do: nil
end