2019-07-22 12:18:58 +00:00
|
|
|
# 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
|
2019-08-31 11:25:43 +00:00
|
|
|
alias Pleroma.Workers.BackgroundWorker
|
|
|
|
|
2019-07-22 12:18:58 +00:00
|
|
|
require Logger
|
|
|
|
use GenServer
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-08-31 11:25:43 +00:00
|
|
|
defdelegate worker_args(queue), to: Pleroma.Workers.Helper
|
|
|
|
|
2019-07-22 12:18:58 +00:00
|
|
|
@schedule_interval :timer.minutes(1)
|
|
|
|
|
2019-08-24 15:22:26 +00:00
|
|
|
def start_link(_) do
|
2019-07-22 12:18:58 +00:00
|
|
|
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 ->
|
2019-08-31 11:25:43 +00:00
|
|
|
%{
|
|
|
|
"op" => "activity_expiration",
|
|
|
|
"activity_expiration_id" => expiration.id
|
|
|
|
}
|
|
|
|
|> BackgroundWorker.new(worker_args(:activity_expiration))
|
|
|
|
|> Repo.insert()
|
2019-07-22 12:18:58 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
schedule_next()
|
|
|
|
{:noreply, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp schedule_next do
|
|
|
|
Process.send_after(self(), :perform, @schedule_interval)
|
|
|
|
end
|
|
|
|
end
|