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