Merge branch 'fix/tootdon-mentions' into 'develop'
[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 Logger.debug(fn -> "Sending #{activity.data["id"]} out via websub" end)
45 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
46
47 {:ok, actor} = WebFinger.ensure_keys_present(actor)
48 Logger.debug(fn -> "Sending #{activity.data["id"]} out via salmon" end)
49 Pleroma.Web.Salmon.publish(actor, activity)
50 end
51 end
52
53 def handle(:verify_websub, websub) do
54 Logger.debug(fn -> "Running websub verification for #{websub.id} (#{websub.topic}, #{websub.callback})" end)
55 @websub.verify(websub)
56 end
57
58 def handle(:incoming_doc, doc) do
59 Logger.debug("Got document, trying to parse")
60 @ostatus.handle_incoming(doc)
61 end
62
63 def handle(:publish_single_websub, %{xml: xml, topic: topic, callback: callback, secret: secret}) do
64 signature = @websub.sign(secret || "", xml)
65 Logger.debug(fn -> "Pushing #{topic} to #{callback}" end)
66
67 with {:ok, %{status_code: code}} <- @httpoison.post(callback, xml, [
68 {"Content-Type", "application/atom+xml"},
69 {"X-Hub-Signature", "sha1=#{signature}"}
70 ], timeout: 10000, recv_timeout: 20000) do
71 Logger.debug(fn -> "Pushed to #{callback}, code #{code}" end)
72 else e ->
73 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
74 end
75 end
76
77 def handle(type, _) do
78 Logger.debug(fn -> "Unknown task: #{type}" end)
79 {:error, "Don't know what do do with this"}
80 end
81
82 def enqueue(type, payload, priority \\ 1) do
83 if Mix.env == :test do
84 handle(type, payload)
85 else
86 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
87 end
88 end
89
90 def maybe_start_job(running_jobs, queue) do
91 if (:sets.size(running_jobs) < @max_jobs) && queue != [] do
92 {{type, payload}, queue} = queue_pop(queue)
93 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
94 mref = Process.monitor(pid)
95 {:sets.add_element(mref, running_jobs), queue}
96 else
97 {running_jobs, queue}
98 end
99 end
100
101 def handle_cast({:enqueue, type, payload, priority}, state) when type in [:incoming_doc] do
102 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
103 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
104 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
105 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
106 end
107
108 def handle_cast({:enqueue, type, payload, priority}, state) do
109 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
110 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
111 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
112 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
113 end
114
115 def handle_cast(m, state) do
116 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
117 {:noreply, state}
118 end
119
120 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
121 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
122 i_running_jobs = :sets.del_element(ref, i_running_jobs)
123 o_running_jobs = :sets.del_element(ref, o_running_jobs)
124 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
125 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
126
127 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
128 end
129
130 def enqueue_sorted(queue, element, priority) do
131 [%{item: element, priority: priority} | queue]
132 |> Enum.sort_by(fn (%{priority: priority}) -> priority end)
133 end
134
135 def queue_pop([%{item: element} | queue]) do
136 {element, queue}
137 end
138 end