object age policy: remove debug logging
[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 Pleroma.Constants
11
12 @moduledoc "Filter activities depending on their age"
13 @behaviour MRF
14
15 defp check_date(%{"published" => published} = message) do
16 with %DateTime{} = now <- DateTime.utc_now(),
17 {:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
18 max_ttl <- Config.get([:mrf_object_age, :threshold]),
19 {:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
20 {:ok, message}
21 else
22 {:ttl, true} ->
23 {:reject, nil}
24
25 e ->
26 {:error, e}
27 end
28 end
29
30 defp check_reject(message, actions) do
31 if :reject in actions do
32 {:reject, nil}
33 else
34 {:ok, message}
35 end
36 end
37
38 defp check_delist(message, actions) do
39 if :delist in actions do
40 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
41 to = List.delete(message["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
42 cc = List.delete(message["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
43
44 message =
45 message
46 |> Map.put("to", to)
47 |> Map.put("cc", cc)
48
49 {:ok, message}
50 else
51 # Unhandleable error: somebody is messing around, just drop the message.
52 _e ->
53 {:reject, nil}
54 end
55 else
56 {:ok, message}
57 end
58 end
59
60 defp check_strip_followers(message, actions) do
61 if :strip_followers in actions do
62 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
63 to = List.delete(message["to"], user.follower_address)
64 cc = List.delete(message["cc"], user.follower_address)
65
66 message =
67 message
68 |> Map.put("to", to)
69 |> Map.put("cc", cc)
70
71 {:ok, message}
72 else
73 # Unhandleable error: somebody is messing around, just drop the message.
74 _e ->
75 {:reject, nil}
76 end
77 else
78 {:ok, message}
79 end
80 end
81
82 @impl true
83 def filter(%{"type" => "Create", "published" => _} = message) do
84 with actions <- Config.get([:mrf_object_age, :actions]),
85 {:reject, _} <- check_date(message),
86 {:ok, message} <- check_reject(message, actions),
87 {:ok, message} <- check_delist(message, actions),
88 {:ok, message} <- check_strip_followers(message, actions) do
89 {:ok, message}
90 else
91 # check_date() is allowed to short-circuit the pipeline
92 e -> e
93 end
94 end
95
96 @impl true
97 def filter(message), do: {:ok, message}
98
99 @impl true
100 def describe, do: {:ok, %{}}
101 end