1 defmodule Pleroma.Web.Federator do
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
14 @websub Application.get_env(:pleroma, :websub)
15 @ostatus Application.get_env(:pleroma, :ostatus)
16 @httpoison Application.get_env(:pleroma, :httpoison)
26 Process.sleep(1000 * 60 * 1)
27 enqueue(:refresh_subscriptions, nil)
33 in: {:sets.new(), []},
34 out: {:sets.new(), []}
40 def handle(:refresh_subscriptions, _) do
41 Logger.debug("Federator running refresh subscriptions")
42 Websub.refresh_subscriptions()
46 Process.sleep(1000 * 60 * 60 * 6)
47 enqueue(:refresh_subscriptions, nil)
51 def handle(:request_subscription, websub) do
52 Logger.debug("Refreshing #{websub.topic}")
54 with {:ok, websub} <- Websub.request_subscription(websub) do
55 Logger.debug("Successfully refreshed #{websub.topic}")
57 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
61 def handle(:publish, activity) do
62 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
64 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
65 {:ok, actor} = WebFinger.ensure_keys_present(actor)
67 if ActivityPub.is_public?(activity) do
68 if OStatus.is_representable?(activity) do
69 Logger.info(fn -> "Sending #{activity.data["id"]} out via WebSub" end)
70 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
72 Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
73 Pleroma.Web.Salmon.publish(actor, activity)
76 if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
77 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
78 Relay.publish(activity)
82 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
83 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
87 def handle(:verify_websub, websub) do
89 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
92 @websub.verify(websub)
95 def handle(:incoming_doc, doc) do
96 Logger.info("Got document, trying to parse")
97 @ostatus.handle_incoming(doc)
100 def handle(:incoming_ap_doc, params) do
101 Logger.info("Handling incoming AP activity")
103 params = Utils.normalize_params(params)
105 # NOTE: we use the actor ID to do the containment, this is fine because an
106 # actor shouldn't be acting on objects outside their own AP server.
107 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
108 nil <- Activity.normalize(params["id"]),
109 :ok <- Transmogrifier.contain_origin_from_id(params["actor"], params),
110 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
114 Logger.info("Already had #{params["id"]}")
118 # Just drop those for now
119 Logger.info("Unhandled activity")
120 Logger.info(Poison.encode!(params, pretty: 2))
125 def handle(:publish_single_ap, params) do
126 case ActivityPub.publish_one(params) do
131 RetryQueue.enqueue(params, :activitypub)
136 :publish_single_websub,
137 %{xml: xml, topic: topic, callback: callback, secret: secret} = params
139 case Websub.publish_one(params) do
144 RetryQueue.enqueue(params, :websub)
148 def handle(type, _) do
149 Logger.debug(fn -> "Unknown task: #{type}" end)
150 {:error, "Don't know what to do with this"}
153 def enqueue(type, payload, priority \\ 1) do
154 if Pleroma.Config.get([:instance, :federating]) do
155 if Mix.env() == :test do
156 handle(type, payload)
158 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
163 def maybe_start_job(running_jobs, queue) do
164 if :sets.size(running_jobs) < @max_jobs && queue != [] do
165 {{type, payload}, queue} = queue_pop(queue)
166 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
167 mref = Process.monitor(pid)
168 {:sets.add_element(mref, running_jobs), queue}
170 {running_jobs, queue}
174 def handle_cast({:enqueue, type, payload, _priority}, state)
175 when type in [:incoming_doc, :incoming_ap_doc] do
176 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
177 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
178 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
179 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
182 def handle_cast({:enqueue, type, payload, _priority}, state) do
183 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
184 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
185 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
186 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
189 def handle_cast(m, state) do
190 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
194 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
195 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
196 i_running_jobs = :sets.del_element(ref, i_running_jobs)
197 o_running_jobs = :sets.del_element(ref, o_running_jobs)
198 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
199 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
201 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
204 def enqueue_sorted(queue, element, priority) do
205 [%{item: element, priority: priority} | queue]
206 |> Enum.sort_by(fn %{priority: priority} -> priority end)
209 def queue_pop([%{item: element} | queue]) do
213 def ap_enabled_actor(id) do
214 user = User.get_by_ap_id(id)
216 if User.ap_enabled?(user) do
219 ActivityPub.make_user_from_ap_id(id)