Merge branch 'refactor/mrf-pattern-matching' into 'develop'

MRF: significant refactoring

See merge request pleroma/pleroma!340
This commit is contained in:
kaniini 2018-09-10 01:22:06 +00:00
commit b0a5637254
2 changed files with 85 additions and 89 deletions

View file

@ -7,43 +7,42 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublic do
@allow_direct Keyword.get(@mrf_rejectnonpublic, :allow_direct) @allow_direct Keyword.get(@mrf_rejectnonpublic, :allow_direct)
@impl true @impl true
def filter(object) do def filter(%{"type" => "Create"} = object) do
if object["type"] == "Create" do user = User.get_cached_by_ap_id(object["actor"])
user = User.get_cached_by_ap_id(object["actor"]) public = "https://www.w3.org/ns/activitystreams#Public"
public = "https://www.w3.org/ns/activitystreams#Public"
# Determine visibility # Determine visibility
visibility = visibility =
cond do cond do
public in object["to"] -> "public" public in object["to"] -> "public"
public in object["cc"] -> "unlisted" public in object["cc"] -> "unlisted"
user.follower_address in object["to"] -> "followers" user.follower_address in object["to"] -> "followers"
true -> "direct" true -> "direct"
end
case visibility do
"public" ->
{:ok, object}
"unlisted" ->
{:ok, object}
"followers" ->
with true <- @allow_followersonly do
{:ok, object}
else
_e -> {:reject, nil}
end end
case visibility do "direct" ->
"public" -> with true <- @allow_direct do
{:ok, object} {:ok, object}
else
"unlisted" -> _e -> {:reject, nil}
{:ok, object} end
"followers" ->
with true <- @allow_followersonly do
{:ok, object}
else
_e -> {:reject, nil}
end
"direct" ->
with true <- @allow_direct do
{:ok, object}
else
_e -> {:reject, nil}
end
end
else
{:ok, object}
end end
end end
@impl true
def filter(object), do: {:ok, object}
end end

View file

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