Make no_placeholder_text_policy multilang-aware

This commit is contained in:
tusooa 2023-01-15 15:19:32 -05:00 committed by marcin mikołajczak
parent fb2688952f
commit 3d3162f3e6
2 changed files with 65 additions and 1 deletions

View file

@ -6,9 +6,49 @@ defmodule Pleroma.Web.ActivityPub.MRF.NoPlaceholderTextPolicy do
@moduledoc "Ensure no content placeholder is present (such as the dot from mastodon)"
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
@placeholders [".", "<p>.</p>"]
@impl true
def history_awareness, do: :auto
@impl true
def filter(
%{
"type" => type,
"object" => %{"contentMap" => %{} = content_map, "attachment" => _} = _child_object
} = object
)
when type in ["Create", "Update"] do
fixed_content_map =
Enum.reduce(content_map, %{}, fn {lang, content}, acc ->
if content in @placeholders do
acc
else
Map.put(acc, lang, content)
end
end)
fixed_object =
if fixed_content_map == %{} do
Map.put(
object,
"object",
object["object"]
|> Map.drop(["contentMap"])
|> Map.put("content", "")
)
else
object
|> put_in(["object", "contentMap"], fixed_content_map)
|> put_in(
["object", "content"],
Pleroma.MultiLanguage.map_to_str(fixed_content_map, multiline: true)
)
end
{:ok, fixed_object}
end
@impl true
def filter(
%{
@ -16,7 +56,7 @@ def filter(
"object" => %{"content" => content, "attachment" => _} = _child_object
} = object
)
when type in ["Create", "Update"] and content in [".", "<p>.</p>"] do
when type in ["Create", "Update"] and content in @placeholders do
{:ok, put_in(object, ["object", "content"], "")}
end

View file

@ -21,6 +21,30 @@ test "it clears content object" do
assert res["object"]["content"] == ""
end
test "multilang aware" do
message = %{
"type" => "Create",
"object" => %{
"content" => ".",
"contentMap" => %{"a" => ".", "b" => "lol"},
"attachment" => "image"
}
}
assert {:ok, res} = NoPlaceholderTextPolicy.filter(message)
assert res["object"]["content"] == "lol"
assert res["object"]["contentMap"] == %{"b" => "lol"}
message = %{
"type" => "Create",
"object" => %{"content" => ".", "contentMap" => %{"a" => "."}, "attachment" => "image"}
}
assert {:ok, res} = NoPlaceholderTextPolicy.filter(message)
assert res["object"]["content"] == ""
assert res["object"]["contentMap"] == nil
end
test "history-aware" do
message = %{
"type" => "Create",