2020-02-11 09:53:24 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-02-11 09:53:24 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy do
|
2022-12-08 22:12:27 +00:00
|
|
|
@moduledoc "Adds expiration to all local Create/Update activities"
|
2021-06-07 19:22:08 +00:00
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
2020-02-11 09:53:24 +00:00
|
|
|
|
|
|
|
@impl true
|
2020-02-14 11:50:31 +00:00
|
|
|
def filter(activity) do
|
2020-02-11 09:53:24 +00:00
|
|
|
activity =
|
2020-06-08 13:56:34 +00:00
|
|
|
if note?(activity) and 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-08-04 14:26:37 +00:00
|
|
|
defp local?(%{"actor" => actor}) do
|
|
|
|
String.starts_with?(actor, Pleroma.Web.Endpoint.url())
|
2020-02-14 11:50:31 +00:00
|
|
|
end
|
|
|
|
|
2022-12-08 22:12:27 +00:00
|
|
|
defp note?(%{"type" => type, "object" => %{"type" => "Note"}})
|
|
|
|
when type in ["Create", "Update"] do
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
defp note?(_) do
|
|
|
|
false
|
2020-06-08 13:56:34 +00:00
|
|
|
end
|
|
|
|
|
2020-02-11 09:53:24 +00:00
|
|
|
defp maybe_add_expiration(activity) do
|
|
|
|
days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
|
2020-08-22 17:46:01 +00:00
|
|
|
expires_at = DateTime.utc_now() |> Timex.shift(days: days)
|
2020-02-11 09:53:24 +00:00
|
|
|
|
|
|
|
with %{"expires_at" => existing_expires_at} <- activity,
|
2020-08-22 17:46:01 +00:00
|
|
|
:lt <- DateTime.compare(existing_expires_at, expires_at) do
|
2020-02-11 09:53:24 +00:00
|
|
|
activity
|
|
|
|
else
|
|
|
|
_ -> Map.put(activity, "expires_at", expires_at)
|
|
|
|
end
|
|
|
|
end
|
2020-11-10 16:18:53 +00:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def config_description do
|
|
|
|
%{
|
|
|
|
key: :mrf_activity_expiration,
|
|
|
|
related_policy: "Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy",
|
|
|
|
label: "MRF Activity Expiration Policy",
|
|
|
|
description: "Adds automatic expiration to all local activities",
|
|
|
|
children: [
|
|
|
|
%{
|
|
|
|
key: :days,
|
|
|
|
type: :integer,
|
|
|
|
description: "Default global expiration time for all local activities (in days)",
|
|
|
|
suggestions: [90, 365]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
2020-02-11 09:53:24 +00:00
|
|
|
end
|