Merge branch 'docs/ap_c2s' into 'develop'
[akkoma] / lib / pleroma / workers / scheduled_activity_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Workers.ScheduledActivityWorker do
6 @moduledoc """
7 The worker to post scheduled activity.
8 """
9
10 use Pleroma.Workers.WorkerHelper, queue: "scheduled_activities"
11
12 alias Pleroma.Config
13 alias Pleroma.ScheduledActivity
14 alias Pleroma.User
15 alias Pleroma.Web.CommonAPI
16
17 require Logger
18
19 @impl Oban.Worker
20 def perform(%Job{args: %{"activity_id" => activity_id}}) do
21 if Config.get([ScheduledActivity, :enabled]) do
22 case Pleroma.Repo.get(ScheduledActivity, activity_id) do
23 %ScheduledActivity{} = scheduled_activity ->
24 post_activity(scheduled_activity)
25
26 _ ->
27 Logger.error("#{__MODULE__} Couldn't find scheduled activity: #{activity_id}")
28 end
29 end
30 end
31
32 defp post_activity(%ScheduledActivity{user_id: user_id, params: params} = scheduled_activity) do
33 params = Map.new(params, fn {key, value} -> {String.to_existing_atom(key), value} end)
34
35 with {:delete, {:ok, _}} <- {:delete, ScheduledActivity.delete(scheduled_activity)},
36 {:user, %User{} = user} <- {:user, User.get_cached_by_id(user_id)},
37 {:post, {:ok, _}} <- {:post, CommonAPI.post(user, params)} do
38 :ok
39 else
40 error ->
41 Logger.error(
42 "#{__MODULE__} Couldn't create a status from the scheduled activity: #{inspect(error)}"
43 )
44 end
45 end
46 end