1 defmodule Pleroma.Web.Federator do
5 alias Pleroma.Web.{WebFinger, Websub}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.ActivityPub.Transmogrifier
10 @websub Application.get_env(:pleroma, :websub)
11 @ostatus Application.get_env(:pleroma, :ostatus)
12 @httpoison Application.get_env(:pleroma, :httpoison)
13 @instance Application.get_env(:pleroma, :instance)
14 @federating Keyword.get(@instance, :federating)
20 Process.sleep(1000 * 60 * 1)
21 enqueue(:refresh_subscriptions, nil)
27 in: {:sets.new(), []},
28 out: {:sets.new(), []}
34 def handle(:refresh_subscriptions, _) do
35 Logger.debug("Federator running refresh subscriptions")
36 Websub.refresh_subscriptions()
40 Process.sleep(1000 * 60 * 60 * 6)
41 enqueue(:refresh_subscriptions, nil)
45 def handle(:request_subscription, websub) do
46 Logger.debug("Refreshing #{websub.topic}")
48 with {:ok, websub} <- Websub.request_subscription(websub) do
49 Logger.debug("Successfully refreshed #{websub.topic}")
51 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
55 def handle(:publish, activity) do
56 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
58 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
59 {:ok, actor} = WebFinger.ensure_keys_present(actor)
61 if ActivityPub.is_public?(activity) do
62 Logger.info(fn -> "Sending #{activity.data["id"]} out via WebSub" end)
63 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
65 Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
66 Pleroma.Web.Salmon.publish(actor, activity)
69 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
70 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
74 def handle(:verify_websub, websub) do
76 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
79 @websub.verify(websub)
82 def handle(:incoming_doc, doc) do
83 Logger.info("Got document, trying to parse")
84 @ostatus.handle_incoming(doc)
87 def handle(:incoming_ap_doc, params) do
88 Logger.info("Handling incoming AP activity")
90 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
91 nil <- Activity.get_by_ap_id(params["id"]),
92 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
95 Logger.info("Already had #{params["id"]}")
98 # Just drop those for now
99 Logger.info("Unhandled activity")
100 Logger.info(Poison.encode!(params, pretty: 2))
104 def handle(:publish_single_ap, params) do
105 ActivityPub.publish_one(params)
108 def handle(:publish_single_websub, %{xml: xml, topic: topic, callback: callback, secret: secret}) do
109 signature = @websub.sign(secret || "", xml)
110 Logger.debug(fn -> "Pushing #{topic} to #{callback}" end)
112 with {:ok, %{status_code: code}} <-
117 {"Content-Type", "application/atom+xml"},
118 {"X-Hub-Signature", "sha1=#{signature}"}
122 hackney: [pool: :default]
124 Logger.debug(fn -> "Pushed to #{callback}, code #{code}" end)
127 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
131 def handle(type, _) do
132 Logger.debug(fn -> "Unknown task: #{type}" end)
133 {:error, "Don't know what to do with this"}
136 def enqueue(type, payload, priority \\ 1) do
138 if Mix.env() == :test do
139 handle(type, payload)
141 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
146 def maybe_start_job(running_jobs, queue) do
147 if :sets.size(running_jobs) < @max_jobs && queue != [] do
148 {{type, payload}, queue} = queue_pop(queue)
149 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
150 mref = Process.monitor(pid)
151 {:sets.add_element(mref, running_jobs), queue}
153 {running_jobs, queue}
157 def handle_cast({:enqueue, type, payload, priority}, state)
158 when type in [:incoming_doc, :incoming_ap_doc] do
159 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
160 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
161 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
162 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
165 def handle_cast({:enqueue, type, payload, priority}, state) do
166 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
167 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
168 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
169 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
172 def handle_cast(m, state) do
173 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
177 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
178 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
179 i_running_jobs = :sets.del_element(ref, i_running_jobs)
180 o_running_jobs = :sets.del_element(ref, o_running_jobs)
181 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
182 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
184 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
187 def enqueue_sorted(queue, element, priority) do
188 [%{item: element, priority: priority} | queue]
189 |> Enum.sort_by(fn %{priority: priority} -> priority end)
192 def queue_pop([%{item: element} | queue]) do
196 def ap_enabled_actor(id) do
197 user = User.get_by_ap_id(id)
199 if User.ap_enabled?(user) do
202 ActivityPub.make_user_from_ap_id(id)