9a211fd447fc4d9020dd38f9e880b226c7344de7
[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
53 {:ok, message}
54 else
55 _e ->
56 {:reject, "[ObjectAgePolicy] Unhandled error"}
57 end
58 else
59 {:ok, message}
60 end
61 end
62
63 defp check_strip_followers(message, actions) do
64 if :strip_followers in actions do
65 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
66 to = List.delete(message["to"] || [], user.follower_address)
67 cc = List.delete(message["cc"] || [], user.follower_address)
68
69 message =
70 message
71 |> Map.put("to", to)
72 |> Map.put("cc", cc)
73
74 {:ok, message}
75 else
76 _e ->
77 {:reject, "[ObjectAgePolicy] Unhandled error"}
78 end
79 else
80 {:ok, message}
81 end
82 end
83
84 @impl true
85 def filter(%{"type" => "Create", "published" => _} = message) do
86 with actions <- Config.get([:mrf_object_age, :actions]),
87 {:reject, _} <- check_date(message),
88 {:ok, message} <- check_reject(message, actions),
89 {:ok, message} <- check_delist(message, actions),
90 {:ok, message} <- check_strip_followers(message, actions) do
91 {:ok, message}
92 else
93 # check_date() is allowed to short-circuit the pipeline
94 e -> e
95 end
96 end
97
98 @impl true
99 def filter(message), do: {:ok, message}
100
101 @impl true
102 def describe do
103 mrf_object_age =
104 Config.get(:mrf_object_age)
105 |> Enum.into(%{})
106
107 {:ok, %{mrf_object_age: mrf_object_age}}
108 end
109
110 @impl true
111 def config_description do
112 %{
113 key: :mrf_object_age,
114 related_policy: "Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy",
115 label: "MRF Object Age",
116 description:
117 "Rejects or delists posts based on their timestamp deviance from your server's clock.",
118 children: [
119 %{
120 key: :threshold,
121 type: :integer,
122 description: "Required age (in seconds) of a post before actions are taken.",
123 suggestions: [172_800]
124 },
125 %{
126 key: :actions,
127 type: {:list, :atom},
128 description:
129 "A list of actions to apply to the post. `:delist` removes the post from public timelines; " <>
130 "`:strip_followers` removes followers from the ActivityPub recipient list ensuring they won't be delivered to home timelines; " <>
131 "`:reject` rejects the message entirely",
132 suggestions: [:delist, :strip_followers, :reject]
133 }
134 ]
135 }
136 end
137 end