Merge remote-tracking branch 'pleroma/develop' into notice-routes
[akkoma] / lib / pleroma / web / activity_pub / mrf / activity_expiration_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.ActivityExpirationPolicy do
6 @moduledoc "Adds expiration to all local Create activities"
7 @behaviour Pleroma.Web.ActivityPub.MRF
8
9 @impl true
10 def filter(activity) do
11 activity =
12 if note?(activity) and local?(activity) do
13 maybe_add_expiration(activity)
14 else
15 activity
16 end
17
18 {:ok, activity}
19 end
20
21 @impl true
22 def describe, do: {:ok, %{}}
23
24 defp local?(%{"actor" => actor}) do
25 String.starts_with?(actor, Pleroma.Web.Endpoint.url())
26 end
27
28 defp note?(activity) do
29 match?(%{"type" => "Create", "object" => %{"type" => "Note"}}, activity)
30 end
31
32 defp maybe_add_expiration(activity) do
33 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
34 expires_at = DateTime.utc_now() |> Timex.shift(days: days)
35
36 with %{"expires_at" => existing_expires_at} <- activity,
37 :lt <- DateTime.compare(existing_expires_at, expires_at) do
38 activity
39 else
40 _ -> Map.put(activity, "expires_at", expires_at)
41 end
42 end
43
44 @impl true
45 def config_description do
46 %{
47 key: :mrf_activity_expiration,
48 related_policy: "Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy",
49 label: "MRF Activity Expiration Policy",
50 description: "Adds automatic expiration to all local activities",
51 children: [
52 %{
53 key: :days,
54 type: :integer,
55 description: "Default global expiration time for all local activities (in days)",
56 suggestions: [90, 365]
57 }
58 ]
59 }
60 end
61 end