1 defmodule Pleroma.Web.Federator do
4 alias Pleroma.Web.{WebFinger, Websub}
5 alias Pleroma.Web.ActivityPub.ActivityPub
8 @websub Application.get_env(:pleroma, :websub)
9 @ostatus Application.get_env(:pleroma, :ostatus)
10 @httpoison Application.get_env(:pleroma, :httpoison)
15 Process.sleep(1000 * 60 * 1) # 1 minute
16 enqueue(:refresh_subscriptions, nil)
18 GenServer.start_link(__MODULE__, %{
19 in: {:sets.new(), []},
20 out: {:sets.new(), []}
24 def handle(:refresh_subscriptions, _) do
25 Logger.debug("Federator running refresh subscriptions")
26 Websub.refresh_subscriptions()
28 Process.sleep(1000 * 60 * 60 * 6) # 6 hours
29 enqueue(:refresh_subscriptions, nil)
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}")
38 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
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)
50 Logger.info(fn -> "Sending #{activity.data["id"]} out via salmon" end)
51 Pleroma.Web.Salmon.publish(actor, activity)
54 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
55 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
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)
64 def handle(:incoming_doc, doc) do
65 Logger.debug("Got document, trying to parse")
66 @ostatus.handle_incoming(doc)
69 def handle(:publish_single_ap, params) do
70 ActivityPub.publish_one(params)
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)
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)
83 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
87 def handle(type, _) do
88 Logger.debug(fn -> "Unknown task: #{type}" end)
89 {:error, "Don't know what do do with this"}
92 def enqueue(type, payload, priority \\ 1) do
93 if Mix.env == :test do
96 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
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}
107 {running_jobs, queue}
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}}}
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}}}
125 def handle_cast(m, state) do
126 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
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)
137 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
140 def enqueue_sorted(queue, element, priority) do
141 [%{item: element, priority: priority} | queue]
142 |> Enum.sort_by(fn (%{priority: priority}) -> priority end)
145 def queue_pop([%{item: element} | queue]) do