Make NoPlaceholderTextPolicy history-aware

This commit is contained in:
Tusooa Zhu 2022-07-23 22:41:04 -04:00
parent dce7e42928
commit fc7ce5f93c
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
2 changed files with 46 additions and 2 deletions

View File

@ -6,14 +6,17 @@ 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
@impl true
def history_awareness, do: :auto
@impl true
def filter(
%{
"type" => "Create",
"type" => type,
"object" => %{"content" => content, "attachment" => _} = _child_object
} = object
)
when content in [".", "<p>.</p>"] do
when type in ["Create", "Update"] and content in [".", "<p>.</p>"] do
{:ok, put_in(object, ["object", "content"], "")}
end

View File

@ -4,6 +4,7 @@
defmodule Pleroma.Web.ActivityPub.MRF.NoPlaceholderTextPolicyTest do
use Pleroma.DataCase, async: true
alias Pleroma.Web.ActivityPub.MRF
alias Pleroma.Web.ActivityPub.MRF.NoPlaceholderTextPolicy
test "it clears content object" do
@ -20,6 +21,46 @@ defmodule Pleroma.Web.ActivityPub.MRF.NoPlaceholderTextPolicyTest do
assert res["object"]["content"] == ""
end
test "history-aware" do
message = %{
"type" => "Create",
"object" => %{
"content" => ".",
"attachment" => "image",
"formerRepresentations" => %{
"orderedItems" => [%{"content" => ".", "attachment" => "image"}]
}
}
}
assert {:ok, res} = MRF.filter_one(NoPlaceholderTextPolicy, message)
assert %{
"content" => "",
"formerRepresentations" => %{"orderedItems" => [%{"content" => ""}]}
} = res["object"]
end
test "works with Updates" do
message = %{
"type" => "Update",
"object" => %{
"content" => ".",
"attachment" => "image",
"formerRepresentations" => %{
"orderedItems" => [%{"content" => ".", "attachment" => "image"}]
}
}
}
assert {:ok, res} = MRF.filter_one(NoPlaceholderTextPolicy, message)
assert %{
"content" => "",
"formerRepresentations" => %{"orderedItems" => [%{"content" => ""}]}
} = res["object"]
end
@messages [
%{
"type" => "Create",