2020-02-11 02:53:24 -07: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
|
2020-02-21 03:42:43 -07:00
|
|
|
@moduledoc "Adds expiration to all local Create activities"
|
2020-02-11 02:53:24 -07:00
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF
|
|
|
|
|
|
|
|
@impl true
|
2020-02-14 04:50:31 -07:00
|
|
|
def filter(activity) do
|
2020-02-11 02:53:24 -07:00
|
|
|
activity =
|
2020-06-08 07:56:34 -06:00
|
|
|
if note?(activity) and local?(activity) do
|
2020-02-11 02:53:24 -07:00
|
|
|
maybe_add_expiration(activity)
|
|
|
|
else
|
|
|
|
activity
|
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def describe, do: {:ok, %{}}
|
|
|
|
|
2020-08-04 08:26:37 -06:00
|
|
|
defp local?(%{"actor" => actor}) do
|
|
|
|
String.starts_with?(actor, Pleroma.Web.Endpoint.url())
|
2020-02-14 04:50:31 -07:00
|
|
|
end
|
|
|
|
|
2020-06-08 07:56:34 -06:00
|
|
|
defp note?(activity) do
|
|
|
|
match?(%{"type" => "Create", "object" => %{"type" => "Note"}}, activity)
|
|
|
|
end
|
|
|
|
|
2020-02-11 02:53:24 -07: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
|