akkoma/lib/pleroma/web/activity_pub/mrf/activity_expiration_policy.ex

40 lines
1 KiB
Elixir
Raw Normal View History

2020-02-11 09:53:24 +00:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy do
@moduledoc "Adds expiration to all local activities"
@behaviour Pleroma.Web.ActivityPub.MRF
@impl true
2020-02-14 11:50:31 +00:00
def filter(activity) do
2020-02-11 09:53:24 +00:00
activity =
2020-02-14 11:50:31 +00:00
if local?(activity) do
2020-02-11 09:53:24 +00:00
maybe_add_expiration(activity)
else
activity
end
{:ok, activity}
end
@impl true
def describe, do: {:ok, %{}}
2020-02-14 11:50:31 +00:00
defp local?(%{"id" => id}) do
String.starts_with?(id, Pleroma.Web.Endpoint.url())
end
2020-02-11 09:53:24 +00:00
defp maybe_add_expiration(activity) do
days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
expires_at = NaiveDateTime.utc_now() |> Timex.shift(days: days)
with %{"expires_at" => existing_expires_at} <- activity,
:lt <- NaiveDateTime.compare(existing_expires_at, expires_at) do
activity
else
_ -> Map.put(activity, "expires_at", expires_at)
end
end
end