ActivityPub: One queue item per server.
[akkoma] / lib / pleroma / web / federator / federator.ex
1 defmodule Pleroma.Web.Federator do
2 use GenServer
3 alias Pleroma.User
4 alias Pleroma.Web.{WebFinger, Websub}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 require Logger
7
8 @websub Application.get_env(:pleroma, :websub)
9 @ostatus Application.get_env(:pleroma, :ostatus)
10 @httpoison Application.get_env(:pleroma, :httpoison)
11 @max_jobs 10
12
13 def start_link do
14 spawn(fn ->
15 Process.sleep(1000 * 60 * 1) # 1 minute
16 enqueue(:refresh_subscriptions, nil)
17 end)
18 GenServer.start_link(__MODULE__, %{
19 in: {:sets.new(), []},
20 out: {:sets.new(), []}
21 }, name: __MODULE__)
22 end
23
24 def handle(:refresh_subscriptions, _) do
25 Logger.debug("Federator running refresh subscriptions")
26 Websub.refresh_subscriptions()
27 spawn(fn ->
28 Process.sleep(1000 * 60 * 60 * 6) # 6 hours
29 enqueue(:refresh_subscriptions, nil)
30 end)
31 end
32
33 def handle(:request_subscription, websub) do
34 Logger.debug("Refreshing #{websub.topic}")
35 with {:ok, websub } <- Websub.request_subscription(websub) do
36 Logger.debug("Successfully refreshed #{websub.topic}")
37 else
38 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
39 end
40 end
41
42 def handle(:publish, activity) do
43 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
44 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
45 {:ok, actor} = WebFinger.ensure_keys_present(actor)
46 if ActivityPub.is_public?(activity) do
47 Logger.info(fn -> "Sending #{activity.data["id"]} out via websub" end)
48 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
49
50 Logger.info(fn -> "Sending #{activity.data["id"]} out via salmon" end)
51 Pleroma.Web.Salmon.publish(actor, activity)
52 end
53
54 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
55 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
56 end
57 end
58
59 def handle(:verify_websub, websub) do
60 Logger.debug(fn -> "Running websub verification for #{websub.id} (#{websub.topic}, #{websub.callback})" end)
61 @websub.verify(websub)
62 end
63
64 def handle(:incoming_doc, doc) do
65 Logger.debug("Got document, trying to parse")
66 @ostatus.handle_incoming(doc)
67 end
68
69 def handle(:publish_single_ap, params) do
70 ActivityPub.publish_one(params)
71 end
72
73 def handle(:publish_single_websub, %{xml: xml, topic: topic, callback: callback, secret: secret}) do
74 signature = @websub.sign(secret || "", xml)
75 Logger.debug(fn -> "Pushing #{topic} to #{callback}" end)
76
77 with {:ok, %{status_code: code}} <- @httpoison.post(callback, xml, [
78 {"Content-Type", "application/atom+xml"},
79 {"X-Hub-Signature", "sha1=#{signature}"}
80 ], timeout: 10000, recv_timeout: 20000) do
81 Logger.debug(fn -> "Pushed to #{callback}, code #{code}" end)
82 else e ->
83 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
84 end
85 end
86
87 def handle(type, _) do
88 Logger.debug(fn -> "Unknown task: #{type}" end)
89 {:error, "Don't know what do do with this"}
90 end
91
92 def enqueue(type, payload, priority \\ 1) do
93 if Mix.env == :test do
94 handle(type, payload)
95 else
96 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
97 end
98 end
99
100 def maybe_start_job(running_jobs, queue) do
101 if (:sets.size(running_jobs) < @max_jobs) && queue != [] do
102 {{type, payload}, queue} = queue_pop(queue)
103 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
104 mref = Process.monitor(pid)
105 {:sets.add_element(mref, running_jobs), queue}
106 else
107 {running_jobs, queue}
108 end
109 end
110
111 def handle_cast({:enqueue, type, payload, priority}, state) when type in [:incoming_doc] do
112 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
113 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
114 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
115 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
116 end
117
118 def handle_cast({:enqueue, type, payload, priority}, state) do
119 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
120 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
121 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
122 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
123 end
124
125 def handle_cast(m, state) do
126 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
127 {:noreply, state}
128 end
129
130 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
131 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
132 i_running_jobs = :sets.del_element(ref, i_running_jobs)
133 o_running_jobs = :sets.del_element(ref, o_running_jobs)
134 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
135 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
136
137 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
138 end
139
140 def enqueue_sorted(queue, element, priority) do
141 [%{item: element, priority: priority} | queue]
142 |> Enum.sort_by(fn (%{priority: priority}) -> priority end)
143 end
144
145 def queue_pop([%{item: element} | queue]) do
146 {element, queue}
147 end
148 end