1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Federator do
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
18 @websub Application.get_env(:pleroma, :websub)
19 @ostatus Application.get_env(:pleroma, :ostatus)
29 Process.sleep(1000 * 60 * 1)
30 enqueue(:refresh_subscriptions, nil)
36 in: {:sets.new(), []},
37 out: {:sets.new(), []}
43 def handle(:refresh_subscriptions, _) do
44 Logger.debug("Federator running refresh subscriptions")
45 Websub.refresh_subscriptions()
49 Process.sleep(1000 * 60 * 60 * 6)
50 enqueue(:refresh_subscriptions, nil)
54 def handle(:request_subscription, websub) do
55 Logger.debug("Refreshing #{websub.topic}")
57 with {:ok, websub} <- Websub.request_subscription(websub) do
58 Logger.debug("Successfully refreshed #{websub.topic}")
60 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
64 def handle(:publish, activity) do
65 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
67 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
68 {:ok, actor} = WebFinger.ensure_keys_present(actor)
70 if ActivityPub.is_public?(activity) do
71 if OStatus.is_representable?(activity) do
72 Logger.info(fn -> "Sending #{activity.data["id"]} out via WebSub" end)
73 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
75 Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
76 Pleroma.Web.Salmon.publish(actor, activity)
79 if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
80 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
81 Relay.publish(activity)
85 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
86 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
90 def handle(:verify_websub, websub) do
92 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
95 @websub.verify(websub)
98 def handle(:incoming_doc, doc) do
99 Logger.info("Got document, trying to parse")
100 @ostatus.handle_incoming(doc)
103 def handle(:incoming_ap_doc, params) do
104 Logger.info("Handling incoming AP activity")
106 params = Utils.normalize_params(params)
108 # NOTE: we use the actor ID to do the containment, this is fine because an
109 # actor shouldn't be acting on objects outside their own AP server.
110 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
111 nil <- Activity.normalize(params["id"]),
112 :ok <- Transmogrifier.contain_origin_from_id(params["actor"], params),
113 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
117 Logger.info("Already had #{params["id"]}")
121 # Just drop those for now
122 Logger.info("Unhandled activity")
123 Logger.info(Poison.encode!(params, pretty: 2))
128 def handle(:publish_single_ap, params) do
129 case ActivityPub.publish_one(params) do
134 RetryQueue.enqueue(params, ActivityPub)
139 :publish_single_websub,
140 %{xml: _xml, topic: _topic, callback: _callback, secret: _secret} = params
142 case Websub.publish_one(params) do
147 RetryQueue.enqueue(params, Websub)
151 def handle(type, _) do
152 Logger.debug(fn -> "Unknown task: #{type}" end)
153 {:error, "Don't know what to do with this"}
156 if Mix.env() == :test do
157 def enqueue(type, payload, _priority \\ 1) do
158 if Pleroma.Config.get([:instance, :federating]) do
159 handle(type, payload)
163 def enqueue(type, payload, priority \\ 1) do
164 if Pleroma.Config.get([:instance, :federating]) do
165 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
170 def maybe_start_job(running_jobs, queue) do
171 if :sets.size(running_jobs) < @max_jobs && queue != [] do
172 {{type, payload}, queue} = queue_pop(queue)
173 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
174 mref = Process.monitor(pid)
175 {:sets.add_element(mref, running_jobs), queue}
177 {running_jobs, queue}
181 def handle_cast({:enqueue, type, payload, _priority}, state)
182 when type in [:incoming_doc, :incoming_ap_doc] do
183 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
184 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
185 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
186 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
189 def handle_cast({:enqueue, type, payload, _priority}, state) do
190 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
191 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
192 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
193 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
196 def handle_cast(m, state) do
197 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
201 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
202 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
203 i_running_jobs = :sets.del_element(ref, i_running_jobs)
204 o_running_jobs = :sets.del_element(ref, o_running_jobs)
205 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
206 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
208 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
211 def enqueue_sorted(queue, element, priority) do
212 [%{item: element, priority: priority} | queue]
213 |> Enum.sort_by(fn %{priority: priority} -> priority end)
216 def queue_pop([%{item: element} | queue]) do
220 def ap_enabled_actor(id) do
221 user = User.get_by_ap_id(id)
223 if User.ap_enabled?(user) do
226 ActivityPub.make_user_from_ap_id(id)