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 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
75 Pleroma.Web.ActivityPub.Relay.publish(activity)
78 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
79 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
83 def handle(:verify_websub, websub) do
85 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
88 @websub.verify(websub)
91 def handle(:incoming_doc, doc) do
92 Logger.info("Got document, trying to parse")
93 @ostatus.handle_incoming(doc)
96 def handle(:incoming_ap_doc, params) do
97 Logger.info("Handling incoming AP activity")
99 params = Utils.normalize_params(params)
101 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
102 nil <- Activity.normalize(params["id"]),
103 {:ok, _activity} <- Transmogrifier.handle_incoming(params) do
106 Logger.info("Already had #{params["id"]}")
109 # Just drop those for now
110 Logger.info("Unhandled activity")
111 Logger.info(Poison.encode!(params, pretty: 2))
115 def handle(:publish_single_ap, params) do
116 ActivityPub.publish_one(params)
119 def handle(:publish_single_websub, %{xml: xml, topic: topic, callback: callback, secret: secret}) do
120 signature = @websub.sign(secret || "", xml)
121 Logger.debug(fn -> "Pushing #{topic} to #{callback}" end)
123 with {:ok, %{status_code: code}} <-
128 {"Content-Type", "application/atom+xml"},
129 {"X-Hub-Signature", "sha1=#{signature}"}
133 hackney: [pool: :default]
135 Logger.debug(fn -> "Pushed to #{callback}, code #{code}" end)
138 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
142 def handle(type, _) do
143 Logger.debug(fn -> "Unknown task: #{type}" end)
144 {:error, "Don't know what to do with this"}
147 def enqueue(type, payload, priority \\ 1) do
149 if Mix.env() == :test do
150 handle(type, payload)
152 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
157 def maybe_start_job(running_jobs, queue) do
158 if :sets.size(running_jobs) < @max_jobs && queue != [] do
159 {{type, payload}, queue} = queue_pop(queue)
160 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
161 mref = Process.monitor(pid)
162 {:sets.add_element(mref, running_jobs), queue}
164 {running_jobs, queue}
168 def handle_cast({:enqueue, type, payload, _priority}, state)
169 when type in [:incoming_doc, :incoming_ap_doc] do
170 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
171 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
172 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
173 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
176 def handle_cast({:enqueue, type, payload, _priority}, state) do
177 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
178 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
179 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
180 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
183 def handle_cast(m, state) do
184 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
188 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
189 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
190 i_running_jobs = :sets.del_element(ref, i_running_jobs)
191 o_running_jobs = :sets.del_element(ref, o_running_jobs)
192 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
193 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
195 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
198 def enqueue_sorted(queue, element, priority) do
199 [%{item: element, priority: priority} | queue]
200 |> Enum.sort_by(fn %{priority: priority} -> priority end)
203 def queue_pop([%{item: element} | queue]) do
207 def ap_enabled_actor(id) do
208 user = User.get_by_ap_id(id)
210 if User.ap_enabled?(user) do
213 ActivityPub.make_user_from_ap_id(id)