Add ActivityExpirationPolicy
[akkoma] / lib / pleroma / web / activity_pub / mrf / activity_expiration_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.ActivityExpirationPolicy do
6 @moduledoc "Adds expiration to all local activities"
7 @behaviour Pleroma.Web.ActivityPub.MRF
8
9 @impl true
10 def filter(%{"id" => id} = activity) do
11 activity =
12 if String.starts_with?(id, Pleroma.Web.Endpoint.url()) 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 maybe_add_expiration(activity) do
25 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
26 expires_at = NaiveDateTime.utc_now() |> Timex.shift(days: days)
27
28 with %{"expires_at" => existing_expires_at} <- activity,
29 :lt <- NaiveDateTime.compare(existing_expires_at, expires_at) do
30 activity
31 else
32 _ -> Map.put(activity, "expires_at", expires_at)
33 end
34 end
35 end