spc-pleroma/lib/pleroma/workers/scheduled_activity_worker.ex

45 lines
1.3 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2019-12-03 18:30:10 +00:00
defmodule Pleroma.Workers.ScheduledActivityWorker do
@moduledoc """
2019-12-03 18:30:10 +00:00
The worker to post scheduled activity.
"""
2019-12-03 18:30:10 +00:00
use Pleroma.Workers.WorkerHelper, queue: "scheduled_activities"
alias Pleroma.Config
alias Pleroma.ScheduledActivity
alias Pleroma.User
alias Pleroma.Web.CommonAPI
require Logger
@impl Oban.Worker
2019-12-03 18:30:10 +00:00
def perform(%{"activity_id" => activity_id}, _job) do
if Config.get([ScheduledActivity, :enabled]) do
2019-12-03 18:30:10 +00:00
case Pleroma.Repo.get(ScheduledActivity, activity_id) do
%ScheduledActivity{} = scheduled_activity ->
post_activity(scheduled_activity)
_ ->
Logger.error("#{__MODULE__} Couldn't find scheduled activity: #{activity_id}")
end
end
end
2019-12-03 18:30:10 +00:00
defp post_activity(%ScheduledActivity{} = scheduled_activity) do
try do
{:ok, scheduled_activity} = ScheduledActivity.delete(scheduled_activity)
%User{} = user = User.get_cached_by_id(scheduled_activity.user_id)
{:ok, _result} = CommonAPI.post(user, scheduled_activity.params)
rescue
error ->
Logger.error(
"#{__MODULE__} Couldn't create a status from the scheduled activity: #{inspect(error)}"
)
end
end
end