extend reject MRF to check if originating instance is blocked
[akkoma] / lib / pleroma / web / activity_pub / mrf / object_age_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy do
6 alias Pleroma.Config
7 alias Pleroma.User
8
9 require Pleroma.Constants
10
11 @moduledoc "Filter activities depending on their age"
12 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
13
14 defp check_date(%{"object" => %{"published" => published}} = message) do
15 with %DateTime{} = now <- DateTime.utc_now(),
16 {:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
17 max_ttl <- Config.get([:mrf_object_age, :threshold]),
18 {:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
19 {:ok, message}
20 else
21 {:ttl, true} ->
22 {:reject, nil}
23
24 e ->
25 {:error, e}
26 end
27 end
28
29 defp check_reject(message, actions) do
30 if :reject in actions do
31 {:reject, "[ObjectAgePolicy]"}
32 else
33 {:ok, message}
34 end
35 end
36
37 defp check_delist(message, actions) do
38 if :delist in actions do
39 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
40 to =
41 List.delete(message["to"] || [], Pleroma.Constants.as_public()) ++
42 [user.follower_address]
43
44 cc =
45 List.delete(message["cc"] || [], user.follower_address) ++
46 [Pleroma.Constants.as_public()]
47
48 message =
49 message
50 |> Map.put("to", to)
51 |> Map.put("cc", cc)
52 |> Kernel.put_in(["object", "to"], to)
53 |> Kernel.put_in(["object", "cc"], cc)
54
55 {:ok, message}
56 else
57 _e ->
58 {:reject, "[ObjectAgePolicy] Unhandled error"}
59 end
60 else
61 {:ok, message}
62 end
63 end
64
65 defp check_strip_followers(message, actions) do
66 if :strip_followers in actions do
67 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
68 to = List.delete(message["to"] || [], user.follower_address)
69 cc = List.delete(message["cc"] || [], user.follower_address)
70
71 message =
72 message
73 |> Map.put("to", to)
74 |> Map.put("cc", cc)
75 |> Kernel.put_in(["object", "to"], to)
76 |> Kernel.put_in(["object", "cc"], cc)
77
78 {:ok, message}
79 else
80 _e ->
81 {:reject, "[ObjectAgePolicy] Unhandled error"}
82 end
83 else
84 {:ok, message}
85 end
86 end
87
88 @impl true
89 def filter(%{"type" => "Create", "object" => %{"published" => _}} = message) do
90 with actions <- Config.get([:mrf_object_age, :actions]),
91 {:reject, _} <- check_date(message),
92 {:ok, message} <- check_reject(message, actions),
93 {:ok, message} <- check_delist(message, actions),
94 {:ok, message} <- check_strip_followers(message, actions) do
95 {:ok, message}
96 else
97 # check_date() is allowed to short-circuit the pipeline
98 e -> e
99 end
100 end
101
102 @impl true
103 def filter(message), do: {:ok, message}
104
105 @impl true
106 def describe do
107 mrf_object_age =
108 Config.get(:mrf_object_age)
109 |> Enum.into(%{})
110
111 {:ok, %{mrf_object_age: mrf_object_age}}
112 end
113
114 @impl true
115 def config_description do
116 %{
117 key: :mrf_object_age,
118 related_policy: "Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy",
119 label: "MRF Object Age",
120 description:
121 "Rejects or delists posts based on their timestamp deviance from your server's clock.",
122 children: [
123 %{
124 key: :threshold,
125 type: :integer,
126 description: "Required age (in seconds) of a post before actions are taken.",
127 suggestions: [172_800]
128 },
129 %{
130 key: :actions,
131 type: {:list, :atom},
132 description:
133 "A list of actions to apply to the post. `:delist` removes the post from public timelines; " <>
134 "`:strip_followers` removes followers from the ActivityPub recipient list ensuring they won't be delivered to home timelines; " <>
135 "`:reject` rejects the message entirely",
136 suggestions: [:delist, :strip_followers, :reject]
137 }
138 ]
139 }
140 end
141 end