1 defmodule Pleroma.Web.Federator do
5 alias Pleroma.Web.{WebFinger, Websub}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.ActivityPub.Relay
8 alias Pleroma.Web.ActivityPub.Transmogrifier
9 alias Pleroma.Web.ActivityPub.Utils
12 @websub Application.get_env(:pleroma, :websub)
13 @ostatus Application.get_env(:pleroma, :ostatus)
14 @httpoison Application.get_env(:pleroma, :httpoison)
15 @instance Application.get_env(:pleroma, :instance)
16 @federating Keyword.get(@instance, :federating)
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 Logger.info(fn -> "Sending #{activity.data["id"]} out via WebSub" end)
69 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
71 Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
72 Pleroma.Web.Salmon.publish(actor, activity)
74 if Mix.env() != :test do
75 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
76 Pleroma.Web.ActivityPub.Relay.publish(activity)
80 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
81 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
85 def handle(:verify_websub, websub) do
87 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
90 @websub.verify(websub)
93 def handle(:incoming_doc, doc) do
94 Logger.info("Got document, trying to parse")
95 @ostatus.handle_incoming(doc)
98 def handle(:incoming_ap_doc, params) do
99 Logger.info("Handling incoming AP activity")
101 params = Utils.normalize_params(params)
103 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
104 nil <- Activity.normalize(params["id"]),
105 {:ok, _activity} <- Transmogrifier.handle_incoming(params) do
108 Logger.info("Already had #{params["id"]}")
111 # Just drop those for now
112 Logger.info("Unhandled activity")
113 Logger.info(Poison.encode!(params, pretty: 2))
117 def handle(:publish_single_ap, params) do
118 ActivityPub.publish_one(params)
121 def handle(:publish_single_websub, %{xml: xml, topic: topic, callback: callback, secret: secret}) do
122 signature = @websub.sign(secret || "", xml)
123 Logger.debug(fn -> "Pushing #{topic} to #{callback}" end)
125 with {:ok, %{status_code: code}} <-
130 {"Content-Type", "application/atom+xml"},
131 {"X-Hub-Signature", "sha1=#{signature}"}
135 hackney: [pool: :default]
137 Logger.debug(fn -> "Pushed to #{callback}, code #{code}" end)
140 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
144 def handle(type, _) do
145 Logger.debug(fn -> "Unknown task: #{type}" end)
146 {:error, "Don't know what to do with this"}
149 def enqueue(type, payload, priority \\ 1) do
151 if Mix.env() == :test do
152 handle(type, payload)
154 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
159 def maybe_start_job(running_jobs, queue) do
160 if :sets.size(running_jobs) < @max_jobs && queue != [] do
161 {{type, payload}, queue} = queue_pop(queue)
162 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
163 mref = Process.monitor(pid)
164 {:sets.add_element(mref, running_jobs), queue}
166 {running_jobs, queue}
170 def handle_cast({:enqueue, type, payload, _priority}, state)
171 when type in [:incoming_doc, :incoming_ap_doc] do
172 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
173 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
174 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
175 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
178 def handle_cast({:enqueue, type, payload, _priority}, state) do
179 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
180 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
181 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
182 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
185 def handle_cast(m, state) do
186 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
190 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
191 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
192 i_running_jobs = :sets.del_element(ref, i_running_jobs)
193 o_running_jobs = :sets.del_element(ref, o_running_jobs)
194 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
195 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
197 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
200 def enqueue_sorted(queue, element, priority) do
201 [%{item: element, priority: priority} | queue]
202 |> Enum.sort_by(fn %{priority: priority} -> priority end)
205 def queue_pop([%{item: element} | queue]) do
209 def ap_enabled_actor(id) do
210 user = User.get_by_ap_id(id)
212 if User.ap_enabled?(user) do
215 ActivityPub.make_user_from_ap_id(id)