a629141358a05abde41672100734e0e13135e715
[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, nil}
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 # Unhandleable error: somebody is messing around, just drop the message.
51 _e ->
52 {:reject, nil}
53 end
54 else
55 {:ok, message}
56 end
57 end
58
59 defp check_strip_followers(message, actions) do
60 if :strip_followers in actions do
61 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
62 to = List.delete(message["to"], user.follower_address)
63 cc = List.delete(message["cc"], user.follower_address)
64
65 message =
66 message
67 |> Map.put("to", to)
68 |> Map.put("cc", cc)
69
70 {:ok, message}
71 else
72 # Unhandleable error: somebody is messing around, just drop the message.
73 _e ->
74 {:reject, nil}
75 end
76 else
77 {:ok, message}
78 end
79 end
80
81 @impl true
82 def filter(%{"type" => "Create", "published" => _} = message) do
83 with actions <- Config.get([:mrf_object_age, :actions]),
84 {:reject, _} <- check_date(message),
85 {:ok, message} <- check_reject(message, actions),
86 {:ok, message} <- check_delist(message, actions),
87 {:ok, message} <- check_strip_followers(message, actions) do
88 {:ok, message}
89 else
90 # check_date() is allowed to short-circuit the pipeline
91 e -> e
92 end
93 end
94
95 @impl true
96 def filter(message), do: {:ok, message}
97
98 @impl true
99 def describe do
100 mrf_object_age =
101 Config.get(:mrf_object_age)
102 |> Enum.into(%{})
103
104 {:ok, %{mrf_object_age: mrf_object_age}}
105 end
106 end