2019-04-03 09:55:04 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-04-03 09:55:04 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-12-03 11:30:10 -07:00
|
|
|
defmodule Pleroma.Workers.ScheduledActivityWorker do
|
2019-04-03 09:55:04 -06:00
|
|
|
@moduledoc """
|
2019-12-03 11:30:10 -07:00
|
|
|
The worker to post scheduled activity.
|
2019-04-03 09:55:04 -06:00
|
|
|
"""
|
|
|
|
|
2019-12-03 11:30:10 -07:00
|
|
|
use Pleroma.Workers.WorkerHelper, queue: "scheduled_activities"
|
|
|
|
|
2021-02-01 08:22:26 -07:00
|
|
|
alias Pleroma.Repo
|
2019-04-03 09:55:04 -06:00
|
|
|
alias Pleroma.ScheduledActivity
|
|
|
|
alias Pleroma.User
|
2019-08-13 11:20:26 -06:00
|
|
|
|
2019-04-03 09:55:04 -06:00
|
|
|
require Logger
|
|
|
|
|
2019-11-26 23:26:37 -07:00
|
|
|
@impl Oban.Worker
|
2020-06-23 06:09:01 -06:00
|
|
|
def perform(%Job{args: %{"activity_id" => activity_id}}) do
|
2021-02-01 08:22:26 -07:00
|
|
|
with %ScheduledActivity{} = scheduled_activity <- find_scheduled_activity(activity_id),
|
|
|
|
%User{} = user <- find_user(scheduled_activity.user_id) do
|
|
|
|
params = atomize_keys(scheduled_activity.params)
|
|
|
|
|
|
|
|
Repo.transaction(fn ->
|
|
|
|
{:ok, activity} = Pleroma.Web.CommonAPI.post(user, params)
|
|
|
|
{:ok, _} = ScheduledActivity.delete(scheduled_activity)
|
|
|
|
activity
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
{:error, :scheduled_activity_not_found} = error ->
|
|
|
|
Logger.error("#{__MODULE__} Couldn't find scheduled activity: #{activity_id}")
|
|
|
|
error
|
|
|
|
|
|
|
|
{:error, :user_not_found} = error ->
|
|
|
|
Logger.error("#{__MODULE__} Couldn't find user for scheduled activity: #{activity_id}")
|
|
|
|
error
|
2019-04-03 09:55:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-11 11:42:29 -07:00
|
|
|
@impl Oban.Worker
|
|
|
|
def timeout(_job), do: :timer.seconds(5)
|
|
|
|
|
2021-02-01 08:22:26 -07:00
|
|
|
defp find_scheduled_activity(id) do
|
|
|
|
with nil <- Repo.get(ScheduledActivity, id) do
|
|
|
|
{:error, :scheduled_activity_not_found}
|
|
|
|
end
|
|
|
|
end
|
2020-05-12 13:59:26 -06:00
|
|
|
|
2021-02-01 08:22:26 -07:00
|
|
|
defp find_user(id) do
|
|
|
|
with nil <- User.get_cached_by_id(id) do
|
|
|
|
{:error, :user_not_found}
|
2019-04-03 09:55:04 -06:00
|
|
|
end
|
|
|
|
end
|
2021-02-01 08:22:26 -07:00
|
|
|
|
|
|
|
defp atomize_keys(map) do
|
|
|
|
Map.new(map, fn
|
|
|
|
{key, value} when is_map(value) -> {String.to_existing_atom(key), atomize_keys(value)}
|
|
|
|
{key, value} -> {String.to_existing_atom(key), value}
|
|
|
|
end)
|
|
|
|
end
|
2019-04-03 09:55:04 -06:00
|
|
|
end
|