MRF: add ObjectAgePolicy which deals with old posts being imported
[akkoma] / lib / pleroma / web / activity_pub / mrf / object_age_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.ObjectAgePolicy do
6 alias Pleroma.Config
7 alias Pleroma.User
8 alias Pleroma.Web.ActivityPub.MRF
9
10 require Logger
11 require Pleroma.Constants
12
13 @moduledoc "Filter activities depending on their age"
14 @behaviour MRF
15
16 defp check_date(%{"published" => published} = message) do
17 with %DateTime{} = now <- DateTime.utc_now(),
18 {:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
19 max_ttl <- Config.get([:mrf_object_age, :threshold]),
20 {:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
21 {:ok, message}
22 else
23 {:ttl, true} ->
24 {:reject, nil}
25
26 e ->
27 {:error, e}
28 end
29 end
30
31 defp check_reject(message, actions) do
32 if :reject in actions do
33 {:reject, nil}
34 else
35 {:ok, message}
36 end
37 end
38
39 defp check_delist(message, actions) do
40 if :delist in actions do
41 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
42 to = List.delete(message["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
43 cc = List.delete(message["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
44
45 message =
46 message
47 |> Map.put("to", to)
48 |> Map.put("cc", cc)
49
50 {:ok, message}
51 else
52 # Unhandleable error: somebody is messing around, just drop the message.
53 e ->
54 Logger.error("ERROR: #{inspect(e)}")
55 {:reject, nil}
56 end
57 else
58 {:ok, message}
59 end
60 end
61
62 defp check_strip_followers(message, actions) do
63 if :strip_followers in actions do
64 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
65 to = List.delete(message["to"], user.follower_address)
66 cc = List.delete(message["cc"], user.follower_address)
67
68 message =
69 message
70 |> Map.put("to", to)
71 |> Map.put("cc", cc)
72
73 {:ok, message}
74 else
75 # Unhandleable error: somebody is messing around, just drop the message.
76 _e ->
77 {:reject, nil}
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: {:ok, %{}}
103 end