Merge branch 'feature/message-rewrite-facility' into 'develop'

message rewrite facility

See merge request pleroma/pleroma!112
This commit is contained in:
lambda 2018-04-24 17:21:28 +00:00
commit 7a52c4549c
5 changed files with 93 additions and 2 deletions

View file

@ -52,7 +52,14 @@
limit: 5000, limit: 5000,
upload_limit: 16_000_000, upload_limit: 16_000_000,
registrations_open: true, registrations_open: true,
federating: true federating: true,
rewrite_policy: Pleroma.Web.ActivityPub.MRF.NoOpPolicy
config :pleroma, :mrf_simple,
media_removal: [],
media_nsfw: [],
federated_timeline_removal: [],
reject: []
config :pleroma, :media_proxy, config :pleroma, :media_proxy,
enabled: false, enabled: false,

View file

@ -10,6 +10,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
@httpoison Application.get_env(:pleroma, :httpoison) @httpoison Application.get_env(:pleroma, :httpoison)
@instance Application.get_env(:pleroma, :instance)
@rewrite_policy Keyword.get(@instance, :rewrite_policy)
def get_recipients(data) do def get_recipients(data) do
(data["to"] || []) ++ (data["cc"] || []) (data["to"] || []) ++ (data["cc"] || [])
end end
@ -17,7 +20,8 @@ def get_recipients(data) do
def insert(map, local \\ true) when is_map(map) do def insert(map, local \\ true) when is_map(map) do
with nil <- Activity.get_by_ap_id(map["id"]), with nil <- Activity.get_by_ap_id(map["id"]),
map <- lazy_put_activity_defaults(map), map <- lazy_put_activity_defaults(map),
:ok <- insert_full_object(map) do :ok <- insert_full_object(map),
{:ok, map} <- @rewrite_policy.filter(map) do
{:ok, activity} = {:ok, activity} =
Repo.insert(%Activity{ Repo.insert(%Activity{
data: map, data: map,

View file

@ -0,0 +1,8 @@
defmodule Pleroma.Web.ActivityPub.MRF.DropPolicy do
require Logger
def filter(object) do
Logger.info("REJECTING #{inspect(object)}")
{:reject, object}
end
end

View file

@ -0,0 +1,5 @@
defmodule Pleroma.Web.ActivityPub.MRF.NoOpPolicy do
def filter(object) do
{:ok, object}
end
end

View file

@ -0,0 +1,67 @@
defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
alias Pleroma.User
@mrf_policy Application.get_env(:pleroma, :mrf_simple)
@reject Keyword.get(@mrf_policy, :reject)
defp check_reject(actor_info, object) do
if actor_info.host in @reject do
{:reject, nil}
else
{:ok, object}
end
end
@media_removal Keyword.get(@mrf_policy, :media_removal)
defp check_media_removal(actor_info, object) do
if actor_info.host in @media_removal do
object = Map.delete(object, "attachments")
end
{:ok, object}
end
@media_nsfw Keyword.get(@mrf_policy, :media_nsfw)
defp check_media_nsfw(actor_info, object) do
child_object = object["object"]
if actor_info.host in @media_nsfw and child_object["attachment"] != nil and length(child_object["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)
end
{:ok, object}
end
@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
user = User.get_by_ap_id(object["actor"])
# flip to/cc relationship to make the post unlisted
if "https://www.w3.org/ns/activitystreams#Public" in object["to"] and user.follower_address in object["cc"] do
to = List.delete(object["to"], "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address]
cc = List.delete(object["cc"], user.follower_address) ++ ["https://www.w3.org/ns/activitystreams#Public"]
object = Map.put(object, "to", to)
object = Map.put(object, "cc", cc)
end
end
{:ok, object}
end
def filter(object) do
actor_info = URI.parse(object["actor"])
with {:ok, object} <- check_reject(actor_info, object),
{:ok, object} <- check_media_removal(actor_info, object),
{:ok, object} <- check_media_nsfw(actor_info, object),
{:ok, object} <- check_ftl_removal(actor_info, object) do
{:ok, object}
else
e -> {:reject, nil}
end
end
end