Add activity expiration worker
authorMike Verdone <spiral@arcseconds.net>
Mon, 22 Jul 2019 12:18:58 +0000 (14:18 +0200)
committerMike Verdone <spiral@arcseconds.net>
Wed, 24 Jul 2019 11:14:52 +0000 (13:14 +0200)
This is a worker that runs every minute and deletes expired activities.
It's based heavily on the scheduled activities worker.

config/config.exs
docs/config.md
lib/pleroma/activity_expiration_worker.ex [new file with mode: 0644]
lib/pleroma/application.ex
test/activity_expiration_worker_test.exs [new file with mode: 0644]

index 5694118668942c3c3ac4686e5a998e028b21957e..2887353fbf045e77ded80be8324c363b26782c73 100644 (file)
@@ -447,6 +447,7 @@ config :pleroma, Pleroma.Web.Federator.RetryQueue,
   max_retries: 5
 
 config :pleroma_job_queue, :queues,
+  activity_expiration: 10,
   federator_incoming: 50,
   federator_outgoing: 50,
   web_push: 50,
@@ -536,6 +537,8 @@ config :pleroma, :rate_limit,
   status_id_action: {60_000, 3},
   password_reset: {1_800_000, 5}
 
+config :pleroma, Pleroma.ActivityExpiration, enabled: true
+
 # Import environment specific config. This must remain at the bottom
 # of this file so it overrides the configuration defined above.
 import_config "#{Mix.env()}.exs"
index 02f86dc169abfb6f9be9215c9d7a949b885e40de..a20ed704fd8aa8d7edd50ffdf2de3c7607617321 100644 (file)
@@ -484,6 +484,10 @@ config :auto_linker,
 * `total_user_limit`: the number of scheduled activities a user is allowed to create in total (Default: `300`)
 * `enabled`: whether scheduled activities are sent to the job queue to be executed
 
+## Pleroma.ActivityExpiration
+
+# `enabled`: whether expired activities will be sent to the job queue to be deleted
+
 ## Pleroma.Web.Auth.Authenticator
 
 * `Pleroma.Web.Auth.PleromaAuthenticator`: default database authenticator
diff --git a/lib/pleroma/activity_expiration_worker.ex b/lib/pleroma/activity_expiration_worker.ex
new file mode 100644 (file)
index 0000000..a341f58
--- /dev/null
@@ -0,0 +1,62 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.ActivityExpirationWorker do
+  alias Pleroma.Activity
+  alias Pleroma.ActivityExpiration
+  alias Pleroma.Config
+  alias Pleroma.Repo
+  alias Pleroma.User
+  alias Pleroma.Web.CommonAPI
+  require Logger
+  use GenServer
+  import Ecto.Query
+
+  @schedule_interval :timer.minutes(1)
+
+  def start_link do
+    GenServer.start_link(__MODULE__, nil)
+  end
+
+  @impl true
+  def init(_) do
+    if Config.get([ActivityExpiration, :enabled]) do
+      schedule_next()
+      {:ok, nil}
+    else
+      :ignore
+    end
+  end
+
+  def perform(:execute, expiration_id) do
+    try do
+      expiration =
+        ActivityExpiration
+        |> where([e], e.id == ^expiration_id)
+        |> Repo.one!()
+
+      activity = Activity.get_by_id_with_object(expiration.activity_id)
+      user = User.get_by_ap_id(activity.object.data["actor"])
+      CommonAPI.delete(activity.id, user)
+    rescue
+      error ->
+        Logger.error("#{__MODULE__} Couldn't delete expired activity: #{inspect(error)}")
+    end
+  end
+
+  @impl true
+  def handle_info(:perform, state) do
+    ActivityExpiration.due_expirations(@schedule_interval)
+    |> Enum.each(fn expiration ->
+      PleromaJobQueue.enqueue(:activity_expiration, __MODULE__, [:execute, expiration.id])
+    end)
+
+    schedule_next()
+    {:noreply, state}
+  end
+
+  defp schedule_next do
+    Process.send_after(self(), :perform, @schedule_interval)
+  end
+end
index 0353314914ba82c35d062adde2cf6db7caeedf46..42e4a1dfa4c13b8e964dc1b1983badf3d9e5fb38 100644 (file)
@@ -115,6 +115,10 @@ defmodule Pleroma.Application do
         %{
           id: Pleroma.ScheduledActivityWorker,
           start: {Pleroma.ScheduledActivityWorker, :start_link, []}
+        },
+        %{
+          id: Pleroma.ActivityExpirationWorker,
+          start: {Pleroma.ActivityExpirationWorker, :start_link, []}
         }
       ] ++
         hackney_pool_children() ++
diff --git a/test/activity_expiration_worker_test.exs b/test/activity_expiration_worker_test.exs
new file mode 100644 (file)
index 0000000..939d912
--- /dev/null
@@ -0,0 +1,17 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.ActivityExpirationWorkerTest do
+  use Pleroma.DataCase
+  alias Pleroma.Activity
+  import Pleroma.Factory
+
+  test "deletes an activity" do
+    activity = insert(:note_activity)
+    expiration = insert(:expiration_in_the_past, %{activity_id: activity.id})
+    Pleroma.ActivityExpirationWorker.perform(:execute, expiration.id)
+
+    refute Repo.get(Activity, activity.id)
+  end
+end