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