MRF Policies: Return a {:reject, reason} instead of {:reject, nil}
[akkoma] / lib / pleroma / web / activity_pub / mrf / object_age_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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 = List.delete(message["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
41 cc = List.delete(message["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
42
43 message =
44 message
45 |> Map.put("to", to)
46 |> Map.put("cc", cc)
47
48 {:ok, message}
49 else
50 _e ->
51 {:reject, "[ObjectAgePolicy] Unhandled error"}
52 end
53 else
54 {:ok, message}
55 end
56 end
57
58 defp check_strip_followers(message, actions) do
59 if :strip_followers in actions do
60 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
61 to = List.delete(message["to"], user.follower_address)
62 cc = List.delete(message["cc"], user.follower_address)
63
64 message =
65 message
66 |> Map.put("to", to)
67 |> Map.put("cc", cc)
68
69 {:ok, message}
70 else
71 _e ->
72 {:reject, "[ObjectAgePolicy] Unhandled error"}
73 end
74 else
75 {:ok, message}
76 end
77 end
78
79 @impl true
80 def filter(%{"type" => "Create", "published" => _} = message) do
81 with actions <- Config.get([:mrf_object_age, :actions]),
82 {:reject, _} <- check_date(message),
83 {:ok, message} <- check_reject(message, actions),
84 {:ok, message} <- check_delist(message, actions),
85 {:ok, message} <- check_strip_followers(message, actions) do
86 {:ok, message}
87 else
88 # check_date() is allowed to short-circuit the pipeline
89 e -> e
90 end
91 end
92
93 @impl true
94 def filter(message), do: {:ok, message}
95
96 @impl true
97 def describe do
98 mrf_object_age =
99 Config.get(:mrf_object_age)
100 |> Enum.into(%{})
101
102 {:ok, %{mrf_object_age: mrf_object_age}}
103 end
104 end