Merge branch 'from/upstream/develop/tusooa/mrf-updates' into 'develop'

MRFs with Updates

See merge request pleroma/pleroma!3808
This commit is contained in:
lain 2022-12-20 00:51:41 +00:00
commit c6dff687c0
4 changed files with 66 additions and 11 deletions

View File

@ -40,9 +40,9 @@ defp check_reject(%{host: actor_host} = _actor_info, object) do
defp check_media_removal( defp check_media_removal(
%{host: actor_host} = _actor_info, %{host: actor_host} = _actor_info,
%{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object %{"type" => type, "object" => %{"attachment" => child_attachment}} = object
) )
when length(child_attachment) > 0 do when length(child_attachment) > 0 and type in ["Create", "Update"] do
media_removal = media_removal =
instance_list(:media_removal) instance_list(:media_removal)
|> MRF.subdomains_regex() |> MRF.subdomains_regex()
@ -63,10 +63,11 @@ defp check_media_removal(_actor_info, object), do: {:ok, object}
defp check_media_nsfw( defp check_media_nsfw(
%{host: actor_host} = _actor_info, %{host: actor_host} = _actor_info,
%{ %{
"type" => "Create", "type" => type,
"object" => %{} = _child_object "object" => %{} = _child_object
} = object } = object
) do )
when type in ["Create", "Update"] do
media_nsfw = media_nsfw =
instance_list(:media_nsfw) instance_list(:media_nsfw)
|> MRF.subdomains_regex() |> MRF.subdomains_regex()

View File

@ -27,22 +27,22 @@ defp get_tags(_), do: []
defp process_tag( defp process_tag(
"mrf_tag:media-force-nsfw", "mrf_tag:media-force-nsfw",
%{ %{
"type" => "Create", "type" => type,
"object" => %{"attachment" => child_attachment} "object" => %{"attachment" => child_attachment}
} = message } = message
) )
when length(child_attachment) > 0 do when length(child_attachment) > 0 and type in ["Create", "Update"] do
{:ok, Kernel.put_in(message, ["object", "sensitive"], true)} {:ok, Kernel.put_in(message, ["object", "sensitive"], true)}
end end
defp process_tag( defp process_tag(
"mrf_tag:media-strip", "mrf_tag:media-strip",
%{ %{
"type" => "Create", "type" => type,
"object" => %{"attachment" => child_attachment} = object "object" => %{"attachment" => child_attachment} = object
} = message } = message
) )
when length(child_attachment) > 0 do when length(child_attachment) > 0 and type in ["Create", "Update"] do
object = Map.delete(object, "attachment") object = Map.delete(object, "attachment")
message = Map.put(message, "object", object) message = Map.put(message, "object", object)
@ -152,7 +152,7 @@ def filter(%{"object" => target_actor, "type" => "Follow"} = message),
do: filter_message(target_actor, message) do: filter_message(target_actor, message)
@impl true @impl true
def filter(%{"actor" => actor, "type" => "Create"} = message), def filter(%{"actor" => actor, "type" => type} = message) when type in ["Create", "Update"],
do: filter_message(actor, message) do: filter_message(actor, message)
@impl true @impl true

View File

@ -57,6 +57,16 @@ test "match with wildcard domain" do
assert SimplePolicy.filter(local_message) == {:ok, local_message} assert SimplePolicy.filter(local_message) == {:ok, local_message}
end end
test "works with Updates" do
clear_config([:mrf_simple, :media_removal], [{"remote.instance", "Some reason"}])
media_message = build_media_message(type: "Update")
assert SimplePolicy.filter(media_message) ==
{:ok,
media_message
|> Map.put("object", Map.delete(media_message["object"], "attachment"))}
end
end end
describe "when :media_nsfw" do describe "when :media_nsfw" do
@ -90,12 +100,20 @@ test "match with wildcard domain" do
assert SimplePolicy.filter(local_message) == {:ok, local_message} assert SimplePolicy.filter(local_message) == {:ok, local_message}
end end
test "works with Updates" do
clear_config([:mrf_simple, :media_nsfw], [{"remote.instance", "Whetever"}])
media_message = build_media_message(type: "Update")
assert SimplePolicy.filter(media_message) ==
{:ok, put_in(media_message, ["object", "sensitive"], true)}
end
end end
defp build_media_message do defp build_media_message(opts \\ []) do
%{ %{
"actor" => "https://remote.instance/users/bob", "actor" => "https://remote.instance/users/bob",
"type" => "Create", "type" => opts[:type] || "Create",
"object" => %{ "object" => %{
"attachment" => [%{}], "attachment" => [%{}],
"tag" => ["foo"], "tag" => ["foo"],

View File

@ -99,6 +99,24 @@ test "removes attachments" do
assert TagPolicy.filter(message) == {:ok, except_message} assert TagPolicy.filter(message) == {:ok, except_message}
end end
test "removes attachments in Updates" do
actor = insert(:user, tags: ["mrf_tag:media-strip"])
message = %{
"actor" => actor.ap_id,
"type" => "Update",
"object" => %{"attachment" => ["file1"]}
}
except_message = %{
"actor" => actor.ap_id,
"type" => "Update",
"object" => %{}
}
assert TagPolicy.filter(message) == {:ok, except_message}
end
end end
describe "mrf_tag:media-force-nsfw" do describe "mrf_tag:media-force-nsfw" do
@ -119,5 +137,23 @@ test "Mark as sensitive on presence of attachments" do
assert TagPolicy.filter(message) == {:ok, except_message} assert TagPolicy.filter(message) == {:ok, except_message}
end end
test "Mark as sensitive on presence of attachments in Updates" do
actor = insert(:user, tags: ["mrf_tag:media-force-nsfw"])
message = %{
"actor" => actor.ap_id,
"type" => "Update",
"object" => %{"tag" => ["test"], "attachment" => ["file1"]}
}
except_message = %{
"actor" => actor.ap_id,
"type" => "Update",
"object" => %{"tag" => ["test"], "attachment" => ["file1"], "sensitive" => true}
}
assert TagPolicy.filter(message) == {:ok, except_message}
end
end end
end end