1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
7 @moduledoc "Filter activities depending on their origin instance"
8 @behaviour Pleroma.Web.ActivityPub.MRF
10 defp check_accept(%{host: actor_host} = _actor_info, object) do
11 accepts = Pleroma.Config.get([:mrf_simple, :accept])
14 accepts == [] -> {:ok, object}
15 actor_host == Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host]) -> {:ok, object}
16 Enum.member?(accepts, actor_host) -> {:ok, object}
17 true -> {:reject, nil}
21 defp check_reject(%{host: actor_host} = _actor_info, object) do
22 if Enum.member?(Pleroma.Config.get([:mrf_simple, :reject]), actor_host) do
29 defp check_media_removal(
30 %{host: actor_host} = _actor_info,
31 %{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object
33 when length(child_attachment) > 0 do
35 if Enum.member?(Pleroma.Config.get([:mrf_simple, :media_removal]), actor_host) do
36 child_object = Map.delete(object["object"], "attachment")
37 Map.put(object, "object", child_object)
45 defp check_media_removal(_actor_info, object), do: {:ok, object}
47 defp check_media_nsfw(
48 %{host: actor_host} = _actor_info,
51 "object" => child_object
55 if Enum.member?(Pleroma.Config.get([:mrf_simple, :media_nsfw]), actor_host) do
56 tags = (child_object["tag"] || []) ++ ["nsfw"]
57 child_object = Map.put(child_object, "tag", tags)
58 child_object = Map.put(child_object, "sensitive", true)
59 Map.put(object, "object", child_object)
67 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
69 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
73 Pleroma.Config.get([:mrf_simple, :federated_timeline_removal]),
76 user <- User.get_cached_by_ap_id(object["actor"]),
77 true <- "https://www.w3.org/ns/activitystreams#Public" in object["to"],
78 true <- user.follower_address in object["cc"] do
80 List.delete(object["to"], "https://www.w3.org/ns/activitystreams#Public") ++
81 [user.follower_address]
84 List.delete(object["cc"], user.follower_address) ++
85 ["https://www.w3.org/ns/activitystreams#Public"]
97 defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
98 if actor_host in Pleroma.Config.get([:mrf_simple, :report_removal]) do
105 defp check_report_removal(_actor_info, object), do: {:ok, object}
107 defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do
108 if actor_host in Pleroma.Config.get([:mrf_simple, :avatar_removal]) do
109 {:ok, Map.delete(object, "icon")}
115 defp check_avatar_removal(_actor_info, object), do: {:ok, object}
117 defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do
118 if actor_host in Pleroma.Config.get([:mrf_simple, :banner_removal]) do
119 {:ok, Map.delete(object, "image")}
125 defp check_banner_removal(_actor_info, object), do: {:ok, object}
128 def filter(%{"actor" => actor} = object) do
129 actor_info = URI.parse(actor)
131 with {:ok, object} <- check_accept(actor_info, object),
132 {:ok, object} <- check_reject(actor_info, object),
133 {:ok, object} <- check_media_removal(actor_info, object),
134 {:ok, object} <- check_media_nsfw(actor_info, object),
135 {:ok, object} <- check_ftl_removal(actor_info, object),
136 {:ok, object} <- check_report_removal(actor_info, object) do
143 def filter(%{"id" => actor, "type" => obj_type} = object)
144 when obj_type in ["Application", "Group", "Organization", "Person", "Service"] do
145 actor_info = URI.parse(actor)
147 with {:ok, object} <- check_avatar_removal(actor_info, object),
148 {:ok, object} <- check_banner_removal(actor_info, object) do
155 def filter(object), do: {:ok, object}