Add banner image to user json.
[akkoma] / lib / pleroma / web / federator / federator.ex
1 defmodule Pleroma.Web.Federator do
2 use GenServer
3 alias Pleroma.User
4 alias Pleroma.Activity
5 alias Pleroma.Web.{WebFinger, Websub}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.ActivityPub.Transmogrifier
8 require Logger
9
10 @websub Application.get_env(:pleroma, :websub)
11 @ostatus Application.get_env(:pleroma, :ostatus)
12 @httpoison Application.get_env(:pleroma, :httpoison)
13 @max_jobs 10
14
15 def start_link do
16 spawn(fn ->
17 Process.sleep(1000 * 60 * 1) # 1 minute
18 enqueue(:refresh_subscriptions, nil)
19 end)
20 GenServer.start_link(__MODULE__, %{
21 in: {:sets.new(), []},
22 out: {:sets.new(), []}
23 }, name: __MODULE__)
24 end
25
26 def handle(:refresh_subscriptions, _) do
27 Logger.debug("Federator running refresh subscriptions")
28 Websub.refresh_subscriptions()
29 spawn(fn ->
30 Process.sleep(1000 * 60 * 60 * 6) # 6 hours
31 enqueue(:refresh_subscriptions, nil)
32 end)
33 end
34
35 def handle(:request_subscription, websub) do
36 Logger.debug("Refreshing #{websub.topic}")
37 with {:ok, websub } <- Websub.request_subscription(websub) do
38 Logger.debug("Successfully refreshed #{websub.topic}")
39 else
40 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
41 end
42 end
43
44 def handle(:publish, activity) do
45 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
46 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
47 {:ok, actor} = WebFinger.ensure_keys_present(actor)
48 if ActivityPub.is_public?(activity) do
49 Logger.info(fn -> "Sending #{activity.data["id"]} out via websub" end)
50 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
51
52 Logger.info(fn -> "Sending #{activity.data["id"]} out via salmon" end)
53 Pleroma.Web.Salmon.publish(actor, activity)
54 end
55
56 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
57 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
58 end
59 end
60
61 def handle(:verify_websub, websub) do
62 Logger.debug(fn -> "Running websub verification for #{websub.id} (#{websub.topic}, #{websub.callback})" end)
63 @websub.verify(websub)
64 end
65
66 def handle(:incoming_doc, doc) do
67 Logger.info("Got document, trying to parse")
68 @ostatus.handle_incoming(doc)
69 end
70
71 def handle(:incoming_ap_doc, params) do
72 Logger.info("Handling incoming ap activity")
73 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
74 nil <- Activity.get_by_ap_id(params["id"]),
75 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
76 else
77 %Activity{} ->
78 Logger.info("Already had #{params["id"]}")
79 e ->
80 # Just drop those for now
81 Logger.info("Unhandled activity")
82 Logger.info(Poison.encode!(params, [pretty: 2]))
83 end
84 end
85
86 def handle(:publish_single_ap, params) do
87 ActivityPub.publish_one(params)
88 end
89
90 def handle(:publish_single_websub, %{xml: xml, topic: topic, callback: callback, secret: secret}) do
91 signature = @websub.sign(secret || "", xml)
92 Logger.debug(fn -> "Pushing #{topic} to #{callback}" end)
93
94 with {:ok, %{status_code: code}} <- @httpoison.post(callback, xml, [
95 {"Content-Type", "application/atom+xml"},
96 {"X-Hub-Signature", "sha1=#{signature}"}
97 ], timeout: 10000, recv_timeout: 20000) do
98 Logger.debug(fn -> "Pushed to #{callback}, code #{code}" end)
99 else e ->
100 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(e)}" end)
101 end
102 end
103
104 def handle(type, _) do
105 Logger.debug(fn -> "Unknown task: #{type}" end)
106 {:error, "Don't know what do do with this"}
107 end
108
109 def enqueue(type, payload, priority \\ 1) do
110 if Mix.env == :test do
111 handle(type, payload)
112 else
113 GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
114 end
115 end
116
117 def maybe_start_job(running_jobs, queue) do
118 if (:sets.size(running_jobs) < @max_jobs) && queue != [] do
119 {{type, payload}, queue} = queue_pop(queue)
120 {:ok, pid} = Task.start(fn -> handle(type, payload) end)
121 mref = Process.monitor(pid)
122 {:sets.add_element(mref, running_jobs), queue}
123 else
124 {running_jobs, queue}
125 end
126 end
127
128 def handle_cast({:enqueue, type, payload, priority}, state) when type in [:incoming_doc, :incoming_ap_doc] do
129 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
130 i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
131 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
132 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
133 end
134
135 def handle_cast({:enqueue, type, payload, priority}, state) do
136 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
137 o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
138 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
139 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
140 end
141
142 def handle_cast(m, state) do
143 IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
144 {:noreply, state}
145 end
146
147 def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do
148 %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
149 i_running_jobs = :sets.del_element(ref, i_running_jobs)
150 o_running_jobs = :sets.del_element(ref, o_running_jobs)
151 {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
152 {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)
153
154 {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
155 end
156
157 def enqueue_sorted(queue, element, priority) do
158 [%{item: element, priority: priority} | queue]
159 |> Enum.sort_by(fn (%{priority: priority}) -> priority end)
160 end
161
162 def queue_pop([%{item: element} | queue]) do
163 {element, queue}
164 end
165
166 def ap_enabled_actor(id) do
167 user = User.get_by_ap_id(id)
168 if User.ap_enabled?(user) do
169 {:ok, user}
170 else
171 ActivityPub.make_user_from_ap_id(id)
172 end
173 end
174 end