logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

scheduled_activity_worker.ex (1794B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Workers.ScheduledActivityWorker do
  5. @moduledoc """
  6. The worker to post scheduled activity.
  7. """
  8. use Oban.Worker, queue: :federator_outgoing, max_attempts: 5
  9. alias Pleroma.Repo
  10. alias Pleroma.ScheduledActivity
  11. alias Pleroma.User
  12. require Logger
  13. @impl true
  14. def perform(%Job{args: %{"activity_id" => activity_id}}) do
  15. with %ScheduledActivity{} = scheduled_activity <- find_scheduled_activity(activity_id),
  16. %User{} = user <- find_user(scheduled_activity.user_id) do
  17. params = atomize_keys(scheduled_activity.params)
  18. Repo.transaction(fn ->
  19. {:ok, activity} = Pleroma.Web.CommonAPI.post(user, params)
  20. {:ok, _} = ScheduledActivity.delete(scheduled_activity)
  21. activity
  22. end)
  23. else
  24. {:error, :scheduled_activity_not_found} = error ->
  25. Logger.error("#{__MODULE__} Couldn't find scheduled activity: #{activity_id}")
  26. error
  27. {:error, :user_not_found} = error ->
  28. Logger.error("#{__MODULE__} Couldn't find user for scheduled activity: #{activity_id}")
  29. error
  30. end
  31. end
  32. @impl true
  33. def timeout(_job), do: :timer.seconds(5)
  34. defp find_scheduled_activity(id) do
  35. with nil <- Repo.get(ScheduledActivity, id) do
  36. {:error, :scheduled_activity_not_found}
  37. end
  38. end
  39. defp find_user(id) do
  40. with nil <- User.get_cached_by_id(id) do
  41. {:error, :user_not_found}
  42. end
  43. end
  44. defp atomize_keys(map) do
  45. Map.new(map, fn
  46. {key, value} when is_map(value) -> {String.to_existing_atom(key), atomize_keys(value)}
  47. {key, value} -> {String.to_existing_atom(key), value}
  48. end)
  49. end
  50. end