mrf: simple policy: fix matching imported activitypub and ostatus statuses
[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" => child_object
52 } = object
53 ) do
54 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)
60 else
61 object
62 end
63
64 {:ok, object}
65 end
66
67 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
68
69 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
70 object =
71 with true <-
72 Enum.member?(
73 Pleroma.Config.get([:mrf_simple, :federated_timeline_removal]),
74 actor_host
75 ),
76 user <- User.get_cached_by_ap_id(object["actor"]),
77 true <- "https://www.w3.org/ns/activitystreams#Public" in object["to"] do
78 to =
79 List.delete(object["to"], "https://www.w3.org/ns/activitystreams#Public") ++
80 [user.follower_address]
81
82 cc =
83 List.delete(object["cc"], user.follower_address) ++
84 ["https://www.w3.org/ns/activitystreams#Public"]
85
86 object
87 |> Map.put("to", to)
88 |> Map.put("cc", cc)
89 else
90 _ -> object
91 end
92
93 {:ok, object}
94 end
95
96 defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
97 if actor_host in Pleroma.Config.get([:mrf_simple, :report_removal]) do
98 {:reject, nil}
99 else
100 {:ok, object}
101 end
102 end
103
104 defp check_report_removal(_actor_info, object), do: {:ok, object}
105
106 defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do
107 if actor_host in Pleroma.Config.get([:mrf_simple, :avatar_removal]) do
108 {:ok, Map.delete(object, "icon")}
109 else
110 {:ok, object}
111 end
112 end
113
114 defp check_avatar_removal(_actor_info, object), do: {:ok, object}
115
116 defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do
117 if actor_host in Pleroma.Config.get([:mrf_simple, :banner_removal]) do
118 {:ok, Map.delete(object, "image")}
119 else
120 {:ok, object}
121 end
122 end
123
124 defp check_banner_removal(_actor_info, object), do: {:ok, object}
125
126 @impl true
127 def filter(%{"actor" => actor} = object) do
128 actor_info = URI.parse(actor)
129
130 with {:ok, object} <- check_accept(actor_info, object),
131 {:ok, object} <- check_reject(actor_info, object),
132 {:ok, object} <- check_media_removal(actor_info, object),
133 {:ok, object} <- check_media_nsfw(actor_info, object),
134 {:ok, object} <- check_ftl_removal(actor_info, object),
135 {:ok, object} <- check_report_removal(actor_info, object) do
136 {:ok, object}
137 else
138 _e -> {:reject, nil}
139 end
140 end
141
142 def filter(%{"id" => actor, "type" => obj_type} = object)
143 when obj_type in ["Application", "Group", "Organization", "Person", "Service"] do
144 actor_info = URI.parse(actor)
145
146 with {:ok, object} <- check_avatar_removal(actor_info, object),
147 {:ok, object} <- check_banner_removal(actor_info, object) do
148 {:ok, object}
149 else
150 _e -> {:reject, nil}
151 end
152 end
153
154 def filter(object), do: {:ok, object}
155 end