Federate non-public over ActivityPub only, do some better signing.
[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.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
54 Logger.debug(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_websub, %{xml: xml, topic: topic, callback: callback, secret: secret}) do
70 signature = @websub.sign(secret || "", xml)
71 Logger.debug(fn -> "Pushing #{topic} to #{callback}" end)
72
73 with {:ok, %{status_code: code}} <- @httpoison.post(callback, xml, [
74 {"Content-Type", "application/atom+xml"},
75 {"X-Hub-Signature", "sha1=#{signature}"}
76 ], timeout: 10000, recv_timeout: 20000) do
77 Logger.debug(fn -> "Pushed to #{callback}, code #{code}" end)
78 else e ->
79 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
80 end
81 end
82
83 def handle(type, _) do
84 Logger.debug(fn -> "Unknown task: #{type}" end)
85 {:error, "Don't know what do do with this"}
86 end
87
88 def enqueue(type, payload, priority \\ 1) do
89 if Mix.env == :test do
90 handle(type, payload)
91 else
92 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
93 end
94 end
95
96 def maybe_start_job(running_jobs, queue) do
97 if (:sets.size(running_jobs) < @max_jobs) && queue != [] do
98 {{type, payload}, queue} = queue_pop(queue)
99 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
100 mref = Process.monitor(pid)
101 {:sets.add_element(mref, running_jobs), queue}
102 else
103 {running_jobs, queue}
104 end
105 end
106
107 def handle_cast({:enqueue, type, payload, priority}, state) when type in [:incoming_doc] do
108 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
109 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
110 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
111 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
112 end
113
114 def handle_cast({:enqueue, type, payload, priority}, state) do
115 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
116 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
117 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
118 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
119 end
120
121 def handle_cast(m, state) do
122 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
123 {:noreply, state}
124 end
125
126 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
127 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
128 i_running_jobs = :sets.del_element(ref, i_running_jobs)
129 o_running_jobs = :sets.del_element(ref, o_running_jobs)
130 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
131 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
132
133 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
134 end
135
136 def enqueue_sorted(queue, element, priority) do
137 [%{item: element, priority: priority} | queue]
138 |> Enum.sort_by(fn (%{priority: priority}) -> priority end)
139 end
140
141 def queue_pop([%{item: element} | queue]) do
142 {element, queue}
143 end
144 end