MRF: simple policy: refactor module to use guards and pattern matching

This commit is contained in:
William Pitcock 2018-09-10 01:06:44 +00:00
parent 97253df3ee
commit 88094c266d

View File

@ -5,59 +5,51 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
@mrf_policy Application.get_env(:pleroma, :mrf_simple)
@accept Keyword.get(@mrf_policy, :accept)
defp check_accept(actor_info, object) do
if length(@accept) > 0 and not (actor_info.host in @accept) do
defp check_accept(%{host: actor_host} = actor_info, object)
when length(@accept) > 0 and not (actor_host in @accept) do
{:reject, nil}
else
{:ok, object}
end
end
defp check_accept(actor_info, object), do: {:ok, object}
@reject Keyword.get(@mrf_policy, :reject)
defp check_reject(actor_info, object) do
if actor_info.host in @reject do
defp check_reject(%{host: actor_host} = actor_info, object) when actor_host in @reject do
{:reject, nil}
else
{:ok, object}
end
end
defp check_reject(actor_info, object), do: {:ok, object}
@media_removal Keyword.get(@mrf_policy, :media_removal)
defp check_media_removal(actor_info, %{"type" => activity_type} = object)
when activity_type == "Create" do
if actor_info.host in @media_removal do
defp check_media_removal(%{host: actor_host} = actor_info, %{"type" => "Create"} = object)
when actor_host in @media_removal do
child_object = Map.delete(object["object"], "attachment")
object = Map.put(object, "object", child_object)
{:ok, object}
else
{:ok, object}
end
end
defp check_media_removal(actor_info, object), do: {:ok, object}
@media_nsfw Keyword.get(@mrf_policy, :media_nsfw)
defp check_media_nsfw(actor_info, %{"type" => activity_type} = object)
when activity_type == "Create" do
child_object = object["object"]
if actor_info.host in @media_nsfw and child_object["attachment"] != nil and
length(child_object["attachment"]) > 0 do
defp check_media_nsfw(
%{host: actor_host} = actor_info,
%{
"type" => "Create",
"object" => %{"attachment" => child_attachment} = child_object
} = object
)
when actor_host in @media_nsfw and length(child_attachment) > 0 do
tags = (child_object["tag"] || []) ++ ["nsfw"]
child_object = Map.put(child_object, "tags", tags)
child_object = Map.put(child_object, "sensitive", true)
object = Map.put(object, "object", child_object)
{:ok, object}
else
{:ok, object}
end
end
defp check_media_nsfw(actor_info, object), do: {:ok, object}
@ftl_removal Keyword.get(@mrf_policy, :federated_timeline_removal)
defp check_ftl_removal(actor_info, object) do
if actor_info.host in @ftl_removal do
defp check_ftl_removal(%{host: actor_host} = actor_info, object)
when actor_host in @ftl_removal do
user = User.get_by_ap_id(object["actor"])
# flip to/cc relationship to make the post unlisted
@ -80,11 +72,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
end
{:ok, object}
else
{:ok, object}
end
end
defp check_ftl_removal(actor_info, object), do: {:ok, object}
@impl true
def filter(object) do
actor_info = URI.parse(object["actor"])