2019-04-03 15:55:04 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-04-03 15:55:04 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-12-03 18:30:10 +00:00
|
|
|
defmodule Pleroma.Workers.ScheduledActivityWorker do
|
2019-04-03 15:55:04 +00:00
|
|
|
@moduledoc """
|
2019-12-03 18:30:10 +00:00
|
|
|
The worker to post scheduled activity.
|
2019-04-03 15:55:04 +00:00
|
|
|
"""
|
|
|
|
|
2019-12-03 18:30:10 +00:00
|
|
|
use Pleroma.Workers.WorkerHelper, 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
|
|
|
|
|
2019-11-27 06:26:37 +00:00
|
|
|
@impl Oban.Worker
|
2020-06-23 12:09:01 +00:00
|
|
|
def perform(%Job{args: %{"activity_id" => activity_id}}) do
|
2019-04-03 15:55:04 +00:00
|
|
|
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
|
2019-04-03 15:55:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-23 08:05:08 +00:00
|
|
|
defp post_activity(%ScheduledActivity{user_id: user_id, params: params} = scheduled_activity) do
|
2020-05-12 19:59:26 +00:00
|
|
|
params = Map.new(params, fn {key, value} -> {String.to_existing_atom(key), value} end)
|
|
|
|
|
2020-01-23 08:05:08 +00:00
|
|
|
with {:delete, {:ok, _}} <- {:delete, ScheduledActivity.delete(scheduled_activity)},
|
|
|
|
{:user, %User{} = user} <- {:user, User.get_cached_by_id(user_id)},
|
|
|
|
{:post, {:ok, _}} <- {:post, CommonAPI.post(user, params)} do
|
|
|
|
:ok
|
|
|
|
else
|
2019-04-03 15:55:04 +00:00
|
|
|
error ->
|
|
|
|
Logger.error(
|
|
|
|
"#{__MODULE__} Couldn't create a status from the scheduled activity: #{inspect(error)}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|