2019-04-03 15:55:04 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-11-27 06:26:37 +00:00
|
|
|
defmodule Pleroma.Workers.Cron.ScheduledActivityWorker do
|
2019-04-03 15:55:04 +00:00
|
|
|
@moduledoc """
|
2019-11-27 06:26:37 +00:00
|
|
|
The worker to post scheduled actvities.
|
2019-04-03 15:55:04 +00:00
|
|
|
"""
|
|
|
|
|
2019-11-27 06:26:37 +00:00
|
|
|
use Oban.Worker, queue: "scheduled_activities"
|
2019-04-03 15:55:04 +00:00
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.ScheduledActivity
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-08-13 17:20:26 +00:00
|
|
|
|
2019-04-03 15:55:04 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@schedule_interval :timer.minutes(1)
|
|
|
|
|
2019-11-27 06:26:37 +00:00
|
|
|
@impl Oban.Worker
|
|
|
|
def perform(_opts, _job) do
|
2019-04-03 15:55:04 +00:00
|
|
|
if Config.get([ScheduledActivity, :enabled]) do
|
2019-11-27 06:26:37 +00:00
|
|
|
@schedule_interval
|
|
|
|
|> ScheduledActivity.due_activities()
|
|
|
|
|> Enum.each(&post_activity/1)
|
2019-04-03 15:55:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-27 06:26:37 +00:00
|
|
|
def post_activity(scheduled_activity) do
|
2019-04-03 15:55:04 +00:00
|
|
|
try do
|
2019-11-27 06:26:37 +00:00
|
|
|
{:ok, scheduled_activity} = ScheduledActivity.delete(scheduled_activity)
|
2019-04-03 15:55:04 +00:00
|
|
|
%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
|