Fix MRF policies to also work with Update
[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/Update activities"
7 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
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?(%{"type" => type, "object" => %{"type" => "Note"}})
29 when type in ["Create", "Update"] do
30 true
31 end
32
33 defp note?(_) do
34 false
35 end
36
37 defp maybe_add_expiration(activity) do
38 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
39 expires_at = DateTime.utc_now() |> Timex.shift(days: days)
40
41 with %{"expires_at" => existing_expires_at} <- activity,
42 :lt <- DateTime.compare(existing_expires_at, expires_at) do
43 activity
44 else
45 _ -> Map.put(activity, "expires_at", expires_at)
46 end
47 end
48
49 @impl true
50 def config_description do
51 %{
52 key: :mrf_activity_expiration,
53 related_policy: "Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy",
54 label: "MRF Activity Expiration Policy",
55 description: "Adds automatic expiration to all local activities",
56 children: [
57 %{
58 key: :days,
59 type: :integer,
60 description: "Default global expiration time for all local activities (in days)",
61 suggestions: [90, 365]
62 }
63 ]
64 }
65 end
66 end