Make normalize_markup multilang aware

This commit is contained in:
tusooa 2023-01-15 15:08:14 -05:00 committed by marcin mikołajczak
parent e958ec57db
commit fb2688952f
2 changed files with 39 additions and 4 deletions

View file

@ -16,11 +16,27 @@ def filter(%{"type" => type, "object" => child_object} = object)
when type in ["Create", "Update"] do
scrub_policy = Pleroma.Config.get([:mrf_normalize_markup, :scrub_policy])
content =
child_object["content"]
|> HTML.filter_tags(scrub_policy)
object =
with %{} = content_map <- child_object["contentMap"] do
fixed_content_map =
Enum.reduce(content_map, %{}, fn {lang, content}, acc ->
Map.put(acc, lang, HTML.filter_tags(content, scrub_policy))
end)
object = put_in(object, ["object", "content"], content)
object
|> put_in(["object", "contentMap"], fixed_content_map)
|> put_in(
["object", "content"],
Pleroma.MultiLanguage.map_to_str(fixed_content_map, multiline: true)
)
else
_ ->
content =
child_object["content"]
|> HTML.filter_tags(scrub_policy)
put_in(object, ["object", "content"], content)
end
{:ok, object}
end

View file

@ -38,6 +38,25 @@ test "it filter html tags" do
assert res["object"]["content"] == @expected
end
test "multilang-aware" do
message = %{
"type" => "Create",
"object" => %{
"content" => "some",
"contentMap" => %{
"a" => @html_sample,
"b" => @html_sample
}
}
}
assert {:ok, res} = NormalizeMarkup.filter(message)
assert res["object"]["contentMap"] == %{"a" => @expected, "b" => @expected}
assert res["object"]["content"] ==
Pleroma.MultiLanguage.map_to_str(res["object"]["contentMap"], multiline: true)
end
test "history-aware" do
message = %{
"type" => "Create",