Gitlab: Run benchmark in CI.
[akkoma] / lib / pleroma / activity_expiration_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ActivityExpirationWorker do
6 alias Pleroma.Activity
7 alias Pleroma.ActivityExpiration
8 alias Pleroma.Config
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12 require Logger
13 use GenServer
14 import Ecto.Query
15
16 @schedule_interval :timer.minutes(1)
17
18 def start_link(_) do
19 GenServer.start_link(__MODULE__, nil)
20 end
21
22 @impl true
23 def init(_) do
24 if Config.get([ActivityExpiration, :enabled]) do
25 schedule_next()
26 {:ok, nil}
27 else
28 :ignore
29 end
30 end
31
32 def perform(:execute, expiration_id) do
33 try do
34 expiration =
35 ActivityExpiration
36 |> where([e], e.id == ^expiration_id)
37 |> Repo.one!()
38
39 activity = Activity.get_by_id_with_object(expiration.activity_id)
40 user = User.get_by_ap_id(activity.object.data["actor"])
41 CommonAPI.delete(activity.id, user)
42 rescue
43 error ->
44 Logger.error("#{__MODULE__} Couldn't delete expired activity: #{inspect(error)}")
45 end
46 end
47
48 @impl true
49 def handle_info(:perform, state) do
50 ActivityExpiration.due_expirations(@schedule_interval)
51 |> Enum.each(fn expiration ->
52 PleromaJobQueue.enqueue(:activity_expiration, __MODULE__, [:execute, expiration.id])
53 end)
54
55 schedule_next()
56 {:noreply, state}
57 end
58
59 defp schedule_next do
60 Process.send_after(self(), :perform, @schedule_interval)
61 end
62 end