diff --git a/lib/pleroma/web/activity_pub/mrf/no_placeholder_text_policy.ex b/lib/pleroma/web/activity_pub/mrf/no_placeholder_text_policy.ex index f81e9e52a0..2dab4f44fd 100644 --- a/lib/pleroma/web/activity_pub/mrf/no_placeholder_text_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/no_placeholder_text_policy.ex @@ -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 [".", "
.
"] + @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 [".", ".
"] do + when type in ["Create", "Update"] and content in @placeholders do {:ok, put_in(object, ["object", "content"], "")} end diff --git a/test/pleroma/web/activity_pub/mrf/no_placeholder_text_policy_test.exs b/test/pleroma/web/activity_pub/mrf/no_placeholder_text_policy_test.exs index 3533c2bc8c..6fb5e656da 100644 --- a/test/pleroma/web/activity_pub/mrf/no_placeholder_text_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/no_placeholder_text_policy_test.exs @@ -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",