Restrict ActivityExpirationPolicy to Notes only

This commit is contained in:
Egor Kislitsyn 2020-06-08 17:56:34 +04:00
parent da22119c2f
commit d44843e677
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
2 changed files with 26 additions and 6 deletions

View file

@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy do
@impl true @impl true
def filter(activity) do def filter(activity) do
activity = activity =
if activity["type"] == "Create" && local?(activity) do if note?(activity) and local?(activity) do
maybe_add_expiration(activity) maybe_add_expiration(activity)
else else
activity activity
@ -25,6 +25,10 @@ defp local?(%{"id" => id}) do
String.starts_with?(id, Pleroma.Web.Endpoint.url()) String.starts_with?(id, Pleroma.Web.Endpoint.url())
end end
defp note?(activity) do
match?(%{"type" => "Create", "object" => %{"type" => "Note"}}, activity)
end
defp maybe_add_expiration(activity) do defp maybe_add_expiration(activity) do
days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365) days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
expires_at = NaiveDateTime.utc_now() |> Timex.shift(days: days) expires_at = NaiveDateTime.utc_now() |> Timex.shift(days: days)

View file

@ -10,7 +10,11 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do
test "adds `expires_at` property" do test "adds `expires_at` property" do
assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} = assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
ActivityExpirationPolicy.filter(%{"id" => @id, "type" => "Create"}) ActivityExpirationPolicy.filter(%{
"id" => @id,
"type" => "Create",
"object" => %{"type" => "Note"}
})
assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364 assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364
end end
@ -22,7 +26,8 @@ test "keeps existing `expires_at` if it less than the config setting" do
ActivityExpirationPolicy.filter(%{ ActivityExpirationPolicy.filter(%{
"id" => @id, "id" => @id,
"type" => "Create", "type" => "Create",
"expires_at" => expires_at "expires_at" => expires_at,
"object" => %{"type" => "Note"}
}) })
end end
@ -33,7 +38,8 @@ test "overwrites existing `expires_at` if it greater than the config setting" do
ActivityExpirationPolicy.filter(%{ ActivityExpirationPolicy.filter(%{
"id" => @id, "id" => @id,
"type" => "Create", "type" => "Create",
"expires_at" => too_distant_future "expires_at" => too_distant_future,
"object" => %{"type" => "Note"}
}) })
assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364 assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364
@ -43,13 +49,14 @@ test "ignores remote activities" do
assert {:ok, activity} = assert {:ok, activity} =
ActivityExpirationPolicy.filter(%{ ActivityExpirationPolicy.filter(%{
"id" => "https://example.com/123", "id" => "https://example.com/123",
"type" => "Create" "type" => "Create",
"object" => %{"type" => "Note"}
}) })
refute Map.has_key?(activity, "expires_at") refute Map.has_key?(activity, "expires_at")
end end
test "ignores non-Create activities" do test "ignores non-Create/Note activities" do
assert {:ok, activity} = assert {:ok, activity} =
ActivityExpirationPolicy.filter(%{ ActivityExpirationPolicy.filter(%{
"id" => "https://example.com/123", "id" => "https://example.com/123",
@ -57,5 +64,14 @@ test "ignores non-Create activities" do
}) })
refute Map.has_key?(activity, "expires_at") refute Map.has_key?(activity, "expires_at")
assert {:ok, activity} =
ActivityExpirationPolicy.filter(%{
"id" => "https://example.com/123",
"type" => "Create",
"object" => %{"type" => "Cofe"}
})
refute Map.has_key?(activity, "expires_at")
end end
end end