Merge branch 'fix/mrf-tag-typo' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / mrf / simple_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
6 alias Pleroma.User
7 @moduledoc "Filter activities depending on their origin instance"
8 @behaviour Pleroma.Web.ActivityPub.MRF
9
10 defp check_accept(%{host: actor_host} = _actor_info, object) do
11 accepts = Pleroma.Config.get([:mrf_simple, :accept])
12
13 cond do
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}
18 end
19 end
20
21 defp check_reject(%{host: actor_host} = _actor_info, object) do
22 if Enum.member?(Pleroma.Config.get([:mrf_simple, :reject]), actor_host) do
23 {:reject, nil}
24 else
25 {:ok, object}
26 end
27 end
28
29 defp check_media_removal(
30 %{host: actor_host} = _actor_info,
31 %{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object
32 )
33 when length(child_attachment) > 0 do
34 object =
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)
38 else
39 object
40 end
41
42 {:ok, object}
43 end
44
45 defp check_media_removal(_actor_info, object), do: {:ok, object}
46
47 defp check_media_nsfw(
48 %{host: actor_host} = _actor_info,
49 %{
50 "type" => "Create",
51 "object" => %{"attachment" => child_attachment} = child_object
52 } = object
53 )
54 when length(child_attachment) > 0 do
55 object =
56 if Enum.member?(Pleroma.Config.get([:mrf_simple, :media_nsfw]), actor_host) do
57 tags = (child_object["tag"] || []) ++ ["nsfw"]
58 child_object = Map.put(child_object, "tag", tags)
59 child_object = Map.put(child_object, "sensitive", true)
60 Map.put(object, "object", child_object)
61 else
62 object
63 end
64
65 {:ok, object}
66 end
67
68 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
69
70 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
71 object =
72 with true <-
73 Enum.member?(
74 Pleroma.Config.get([:mrf_simple, :federated_timeline_removal]),
75 actor_host
76 ),
77 user <- User.get_cached_by_ap_id(object["actor"]),
78 true <- "https://www.w3.org/ns/activitystreams#Public" in object["to"],
79 true <- user.follower_address in object["cc"] do
80 to =
81 List.delete(object["to"], "https://www.w3.org/ns/activitystreams#Public") ++
82 [user.follower_address]
83
84 cc =
85 List.delete(object["cc"], user.follower_address) ++
86 ["https://www.w3.org/ns/activitystreams#Public"]
87
88 object
89 |> Map.put("to", to)
90 |> Map.put("cc", cc)
91 else
92 _ -> object
93 end
94
95 {:ok, object}
96 end
97
98 @impl true
99 def filter(object) do
100 actor_info = URI.parse(object["actor"])
101
102 with {:ok, object} <- check_accept(actor_info, object),
103 {:ok, object} <- check_reject(actor_info, object),
104 {:ok, object} <- check_media_removal(actor_info, object),
105 {:ok, object} <- check_media_nsfw(actor_info, object),
106 {:ok, object} <- check_ftl_removal(actor_info, object) do
107 {:ok, object}
108 else
109 _e -> {:reject, nil}
110 end
111 end
112 end