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