Merge branch 'feature/optimize_rich_media_parser' into 'develop'
[akkoma] / lib / pleroma / daemons / activity_expiration_daemon.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.Daemons.ActivityExpirationDaemon 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
13 require Logger
14 use GenServer
15 import Ecto.Query
16
17 @schedule_interval :timer.minutes(1)
18
19 def start_link(_) do
20 GenServer.start_link(__MODULE__, nil)
21 end
22
23 @impl true
24 def init(_) do
25 if Config.get([ActivityExpiration, :enabled]) do
26 schedule_next()
27 {:ok, nil}
28 else
29 :ignore
30 end
31 end
32
33 def perform(:execute, expiration_id) do
34 try do
35 expiration =
36 ActivityExpiration
37 |> where([e], e.id == ^expiration_id)
38 |> Repo.one!()
39
40 activity = Activity.get_by_id_with_object(expiration.activity_id)
41 user = User.get_by_ap_id(activity.object.data["actor"])
42 CommonAPI.delete(activity.id, user)
43 rescue
44 error ->
45 Logger.error("#{__MODULE__} Couldn't delete expired activity: #{inspect(error)}")
46 end
47 end
48
49 @impl true
50 def handle_info(:perform, state) do
51 ActivityExpiration.due_expirations(@schedule_interval)
52 |> Enum.each(fn expiration ->
53 Pleroma.Workers.ActivityExpirationWorker.enqueue(
54 "activity_expiration",
55 %{"activity_expiration_id" => expiration.id}
56 )
57 end)
58
59 schedule_next()
60 {:noreply, state}
61 end
62
63 defp schedule_next do
64 Process.send_after(self(), :perform, @schedule_interval)
65 end
66 end