67871977a82254b4500fcf6bb274d7efd277eed1
[akkoma] / lib / pleroma / workers / publisher.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Workers.Publisher do
6 alias Pleroma.Activity
7 alias Pleroma.User
8
9 # Note: `max_attempts` is intended to be overridden in `new/1` call
10 use Oban.Worker,
11 queue: "federator_outgoing",
12 max_attempts: Pleroma.Config.get([:workers, :retries, :compile_time_default])
13
14 @impl Oban.Worker
15 def perform(%{"op" => "publish", "activity_id" => activity_id}) do
16 with %Activity{} = activity <- Activity.get_by_id(activity_id) do
17 perform_publish(activity)
18 else
19 _ -> raise "Non-existing activity: #{activity_id}"
20 end
21 end
22
23 def perform(%{"op" => "publish_one", "module" => module_name, "params" => params}) do
24 module_name
25 |> String.to_atom()
26 |> apply(:publish_one, [params])
27 end
28
29 def perform_publish(%Activity{} = activity) do
30 with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
31 {:ok, actor} <- User.ensure_keys_present(actor) do
32 Pleroma.Web.Federator.Publisher.publish(actor, activity)
33 end
34 end
35 end