Make Federator options configurable.
[akkoma] / lib / pleroma / web / federator / federator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Federator do
6 use GenServer
7 alias Pleroma.User
8 alias Pleroma.Activity
9 alias Pleroma.Web.{WebFinger, Websub}
10 alias Pleroma.Web.Federator.RetryQueue
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Relay
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.OStatus
16 require Logger
17
18 @websub Application.get_env(:pleroma, :websub)
19 @ostatus Application.get_env(:pleroma, :ostatus)
20
21 def init(args) do
22 {:ok, args}
23 end
24
25 def start_link do
26 spawn(fn ->
27 # 1 minute
28 Process.sleep(1000 * 60 * 1)
29 enqueue(:refresh_subscriptions, nil)
30 end)
31
32 GenServer.start_link(
33 __MODULE__,
34 %{
35 in: {:sets.new(), []},
36 out: {:sets.new(), []}
37 },
38 name: __MODULE__
39 )
40 end
41
42 def handle(:refresh_subscriptions, _) do
43 Logger.debug("Federator running refresh subscriptions")
44 Websub.refresh_subscriptions()
45
46 spawn(fn ->
47 # 6 hours
48 Process.sleep(1000 * 60 * 60 * 6)
49 enqueue(:refresh_subscriptions, nil)
50 end)
51 end
52
53 def handle(:request_subscription, websub) do
54 Logger.debug("Refreshing #{websub.topic}")
55
56 with {:ok, websub} <- Websub.request_subscription(websub) do
57 Logger.debug("Successfully refreshed #{websub.topic}")
58 else
59 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
60 end
61 end
62
63 def handle(:publish, activity) do
64 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
65
66 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
67 {:ok, actor} = WebFinger.ensure_keys_present(actor)
68
69 if ActivityPub.is_public?(activity) do
70 if OStatus.is_representable?(activity) do
71 Logger.info(fn -> "Sending #{activity.data["id"]} out via WebSub" end)
72 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
73
74 Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
75 Pleroma.Web.Salmon.publish(actor, activity)
76 end
77
78 if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
79 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
80 Relay.publish(activity)
81 end
82 end
83
84 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
85 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
86 end
87 end
88
89 def handle(:verify_websub, websub) do
90 Logger.debug(fn ->
91 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
92 end)
93
94 @websub.verify(websub)
95 end
96
97 def handle(:incoming_doc, doc) do
98 Logger.info("Got document, trying to parse")
99 @ostatus.handle_incoming(doc)
100 end
101
102 def handle(:incoming_ap_doc, params) do
103 Logger.info("Handling incoming AP activity")
104
105 params = Utils.normalize_params(params)
106
107 # NOTE: we use the actor ID to do the containment, this is fine because an
108 # actor shouldn't be acting on objects outside their own AP server.
109 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
110 nil <- Activity.normalize(params["id"]),
111 :ok <- Transmogrifier.contain_origin_from_id(params["actor"], params),
112 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
113 {:ok, activity}
114 else
115 %Activity{} ->
116 Logger.info("Already had #{params["id"]}")
117 :error
118
119 _e ->
120 # Just drop those for now
121 Logger.info("Unhandled activity")
122 Logger.info(Poison.encode!(params, pretty: 2))
123 :error
124 end
125 end
126
127 def handle(:publish_single_ap, params) do
128 case ActivityPub.publish_one(params) do
129 {:ok, _} ->
130 :ok
131
132 {:error, _} ->
133 RetryQueue.enqueue(params, ActivityPub)
134 end
135 end
136
137 def handle(
138 :publish_single_websub,
139 %{xml: _xml, topic: _topic, callback: _callback, secret: _secret} = params
140 ) do
141 case Websub.publish_one(params) do
142 {:ok, _} ->
143 :ok
144
145 {:error, _} ->
146 RetryQueue.enqueue(params, Websub)
147 end
148 end
149
150 def handle(type, _) do
151 Logger.debug(fn -> "Unknown task: #{type}" end)
152 {:error, "Don't know what to do with this"}
153 end
154
155 if Mix.env() == :test do
156 def enqueue(type, payload, _priority \\ 1) do
157 if Pleroma.Config.get([:instance, :federating]) do
158 handle(type, payload)
159 end
160 end
161 else
162 def enqueue(type, payload, priority \\ 1) do
163 if Pleroma.Config.get([:instance, :federating]) do
164 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
165 end
166 end
167 end
168
169 def maybe_start_job(running_jobs, queue) do
170 if :sets.size(running_jobs) < Pleroma.Config.get([__MODULE__, :max_jobs]) && queue != [] do
171 {{type, payload}, queue} = queue_pop(queue)
172 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
173 mref = Process.monitor(pid)
174 {:sets.add_element(mref, running_jobs), queue}
175 else
176 {running_jobs, queue}
177 end
178 end
179
180 def handle_cast({:enqueue, type, payload, _priority}, state)
181 when type in [:incoming_doc, :incoming_ap_doc] do
182 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
183 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
184 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
185 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
186 end
187
188 def handle_cast({:enqueue, type, payload, _priority}, state) do
189 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
190 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
191 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
192 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
193 end
194
195 def handle_cast(m, state) do
196 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
197 {:noreply, state}
198 end
199
200 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
201 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
202 i_running_jobs = :sets.del_element(ref, i_running_jobs)
203 o_running_jobs = :sets.del_element(ref, o_running_jobs)
204 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
205 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
206
207 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
208 end
209
210 def enqueue_sorted(queue, element, priority) do
211 [%{item: element, priority: priority} | queue]
212 |> Enum.sort_by(fn %{priority: priority} -> priority end)
213 end
214
215 def queue_pop([%{item: element} | queue]) do
216 {element, queue}
217 end
218
219 def ap_enabled_actor(id) do
220 user = User.get_by_ap_id(id)
221
222 if User.ap_enabled?(user) do
223 {:ok, user}
224 else
225 ActivityPub.make_user_from_ap_id(id)
226 end
227 end
228 end