Auto-expire Create activities only
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 20 Feb 2020 18:04:02 +0000 (22:04 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Thu, 20 Feb 2020 18:04:02 +0000 (22:04 +0400)
lib/pleroma/web/activity_pub/mrf/activity_expiration_policy.ex
test/web/activity_pub/activity_pub_test.exs
test/web/activity_pub/mrf/activity_expiration_policy_test.exs
test/workers/cron/purge_expired_activities_worker_test.exs

index 5d823f2c7dc0de90a9253746c159b7630fb064d6..274bb9a5c1ef9658b763d71ed42448a936254265 100644 (file)
@@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy do
   @impl true
   def filter(activity) do
     activity =
-      if local?(activity) do
+      if activity["type"] == "Create" && local?(activity) do
         maybe_add_expiration(activity)
       else
         activity
index ce68e7d0e51d6cb3994464b53b223f4ceaa8e173..2cd908a8744c68a7786a46f39faf09489a14ef27 100644 (file)
@@ -1784,4 +1784,20 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
                ActivityPub.move(old_user, new_user)
     end
   end
+
+  describe "global activity expiration" do
+    clear_config([:instance, :rewrite_policy])
+
+    test "creates an activity expiration for local Create activities" do
+      Pleroma.Config.put(
+        [:instance, :rewrite_policy],
+        Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy
+      )
+
+      {:ok, %{id: id_create}} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
+      {:ok, _follow} = ActivityBuilder.insert(%{"type" => "Follow", "context" => "3hu"})
+
+      assert [%{activity_id: ^id_create}] = Pleroma.ActivityExpiration |> Repo.all()
+    end
+  end
 end
index 2f2f90b44a2e119b82f22b89f4645d8d5566ab86..0d3bcc45790c78cf929286f776212df85198c03b 100644 (file)
@@ -9,7 +9,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do
   @id Pleroma.Web.Endpoint.url() <> "/activities/cofe"
 
   test "adds `expires_at` property" do
-    assert {:ok, %{"expires_at" => expires_at}} = ActivityExpirationPolicy.filter(%{"id" => @id})
+    assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
+             ActivityExpirationPolicy.filter(%{"id" => @id, "type" => "Create"})
 
     assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364
   end
@@ -17,21 +18,43 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do
   test "keeps existing `expires_at` if it less than the config setting" do
     expires_at = NaiveDateTime.utc_now() |> Timex.shift(days: 1)
 
-    assert {:ok, %{"expires_at" => ^expires_at}} =
-             ActivityExpirationPolicy.filter(%{"id" => @id, "expires_at" => expires_at})
+    assert {:ok, %{"type" => "Create", "expires_at" => ^expires_at}} =
+             ActivityExpirationPolicy.filter(%{
+               "id" => @id,
+               "type" => "Create",
+               "expires_at" => expires_at
+             })
   end
 
   test "overwrites existing `expires_at` if it greater than the config setting" do
     too_distant_future = NaiveDateTime.utc_now() |> Timex.shift(years: 2)
 
-    assert {:ok, %{"expires_at" => expires_at}} =
-             ActivityExpirationPolicy.filter(%{"id" => @id, "expires_at" => too_distant_future})
+    assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =
+             ActivityExpirationPolicy.filter(%{
+               "id" => @id,
+               "type" => "Create",
+               "expires_at" => too_distant_future
+             })
 
     assert Timex.diff(expires_at, NaiveDateTime.utc_now(), :days) == 364
   end
 
   test "ignores remote activities" do
-    assert {:ok, activity} = ActivityExpirationPolicy.filter(%{"id" => "https://example.com/123"})
+    assert {:ok, activity} =
+             ActivityExpirationPolicy.filter(%{
+               "id" => "https://example.com/123",
+               "type" => "Create"
+             })
+
+    refute Map.has_key?(activity, "expires_at")
+  end
+
+  test "ignores non-Create activities" do
+    assert {:ok, activity} =
+             ActivityExpirationPolicy.filter(%{
+               "id" => "https://example.com/123",
+               "type" => "Follow"
+             })
 
     refute Map.has_key?(activity, "expires_at")
   end
index c2561683e6e0062ea3f50d092c9c6217350fc22d..c6c7ff388fa1eac2d867f094992b38406b4a66da 100644 (file)
@@ -12,6 +12,7 @@ defmodule Pleroma.Workers.Cron.PurgeExpiredActivitiesWorkerTest do
   import ExUnit.CaptureLog
 
   clear_config([ActivityExpiration, :enabled])
+  clear_config([:instance, :rewrite_policy])
 
   test "deletes an expiration activity" do
     Pleroma.Config.put([ActivityExpiration, :enabled], true)
@@ -36,6 +37,35 @@ defmodule Pleroma.Workers.Cron.PurgeExpiredActivitiesWorkerTest do
     refute Pleroma.Repo.get(Pleroma.ActivityExpiration, expiration.id)
   end
 
+  test "works with ActivityExpirationPolicy" do
+    Pleroma.Config.put([ActivityExpiration, :enabled], true)
+
+    Pleroma.Config.put(
+      [:instance, :rewrite_policy],
+      Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy
+    )
+
+    user = insert(:user)
+
+    days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
+
+    {:ok, %{id: id} = activity} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofe"})
+
+    past_date =
+      NaiveDateTime.utc_now() |> Timex.shift(days: -days) |> NaiveDateTime.truncate(:second)
+
+    activity
+    |> Repo.preload(:expiration)
+    |> Map.get(:expiration)
+    |> Ecto.Changeset.change(%{scheduled_at: past_date})
+    |> Repo.update!()
+
+    Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker.perform(:ops, :pid)
+
+    assert [%{data: %{"type" => "Delete", "deleted_activity_id" => ^id}}] =
+             Pleroma.Repo.all(Pleroma.Activity)
+  end
+
   describe "delete_activity/1" do
     test "adds log message if activity isn't find" do
       assert capture_log([level: :error], fn ->