[#534] Various tweaks. Tests for Instances and Instance.
[akkoma] / lib / pleroma / web / websub / websub.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Websub do
6 alias Ecto.Changeset
7 alias Pleroma.Repo
8 alias Pleroma.Instances
9 alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription}
10 alias Pleroma.Web.OStatus.FeedRepresenter
11 alias Pleroma.Web.{XML, Endpoint, OStatus}
12 alias Pleroma.Web.Router.Helpers
13 require Logger
14
15 import Ecto.Query
16
17 @httpoison Application.get_env(:pleroma, :httpoison)
18
19 def verify(subscription, getter \\ &@httpoison.get/3) do
20 challenge = Base.encode16(:crypto.strong_rand_bytes(8))
21 lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.updated_at)
22 lease_seconds = lease_seconds |> to_string
23
24 params = %{
25 "hub.challenge": challenge,
26 "hub.lease_seconds": lease_seconds,
27 "hub.topic": subscription.topic,
28 "hub.mode": "subscribe"
29 }
30
31 url = hd(String.split(subscription.callback, "?"))
32 query = URI.parse(subscription.callback).query || ""
33 params = Map.merge(params, URI.decode_query(query))
34
35 with {:ok, response} <- getter.(url, [], params: params),
36 ^challenge <- response.body do
37 changeset = Changeset.change(subscription, %{state: "active"})
38 Repo.update(changeset)
39 else
40 e ->
41 Logger.debug("Couldn't verify subscription")
42 Logger.debug(inspect(e))
43 {:error, subscription}
44 end
45 end
46
47 @supported_activities [
48 "Create",
49 "Follow",
50 "Like",
51 "Announce",
52 "Undo",
53 "Delete"
54 ]
55 def publish(topic, user, %{data: %{"type" => type}} = activity)
56 when type in @supported_activities do
57 response =
58 user
59 |> FeedRepresenter.to_simple_form([activity], [user])
60 |> :xmerl.export_simple(:xmerl_xml)
61 |> to_string
62
63 query =
64 from(
65 sub in WebsubServerSubscription,
66 where: sub.topic == ^topic and sub.state == "active",
67 where: fragment("? > (NOW() at time zone 'UTC')", sub.valid_until)
68 )
69
70 subscriptions = Repo.all(query)
71
72 callbacks = Enum.map(subscriptions, & &1.callback)
73 reachable_callbacks = Instances.filter_reachable(callbacks)
74
75 subscriptions
76 |> Enum.filter(&(&1.callback in reachable_callbacks))
77 |> Enum.each(fn sub ->
78 data = %{
79 xml: response,
80 topic: topic,
81 callback: sub.callback,
82 secret: sub.secret
83 }
84
85 Pleroma.Web.Federator.enqueue(:publish_single_websub, data)
86 end)
87 end
88
89 def publish(_, _, _), do: ""
90
91 def sign(secret, doc) do
92 :crypto.hmac(:sha, secret, to_string(doc)) |> Base.encode16() |> String.downcase()
93 end
94
95 def incoming_subscription_request(user, %{"hub.mode" => "subscribe"} = params) do
96 with {:ok, topic} <- valid_topic(params, user),
97 {:ok, lease_time} <- lease_time(params),
98 secret <- params["hub.secret"],
99 callback <- params["hub.callback"] do
100 subscription = get_subscription(topic, callback)
101
102 data = %{
103 state: subscription.state || "requested",
104 topic: topic,
105 secret: secret,
106 callback: callback
107 }
108
109 change = Changeset.change(subscription, data)
110 websub = Repo.insert_or_update!(change)
111
112 change =
113 Changeset.change(websub, %{valid_until: NaiveDateTime.add(websub.updated_at, lease_time)})
114
115 websub = Repo.update!(change)
116
117 Pleroma.Web.Federator.enqueue(:verify_websub, websub)
118
119 {:ok, websub}
120 else
121 {:error, reason} ->
122 Logger.debug("Couldn't create subscription")
123 Logger.debug(inspect(reason))
124
125 {:error, reason}
126 end
127 end
128
129 def incoming_subscription_request(user, params) do
130 Logger.info("Unhandled WebSub request for #{user.nickname}: #{inspect(params)}")
131
132 {:error, "Invalid WebSub request"}
133 end
134
135 defp get_subscription(topic, callback) do
136 Repo.get_by(WebsubServerSubscription, topic: topic, callback: callback) ||
137 %WebsubServerSubscription{}
138 end
139
140 # Temp hack for mastodon.
141 defp lease_time(%{"hub.lease_seconds" => ""}) do
142 # three days
143 {:ok, 60 * 60 * 24 * 3}
144 end
145
146 defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
147 {:ok, String.to_integer(lease_seconds)}
148 end
149
150 defp lease_time(_) do
151 # three days
152 {:ok, 60 * 60 * 24 * 3}
153 end
154
155 defp valid_topic(%{"hub.topic" => topic}, user) do
156 if topic == OStatus.feed_path(user) do
157 {:ok, OStatus.feed_path(user)}
158 else
159 {:error, "Wrong topic requested, expected #{OStatus.feed_path(user)}, got #{topic}"}
160 end
161 end
162
163 def subscribe(subscriber, subscribed, requester \\ &request_subscription/1) do
164 topic = subscribed.info.topic
165 # FIXME: Race condition, use transactions
166 {:ok, subscription} =
167 with subscription when not is_nil(subscription) <-
168 Repo.get_by(WebsubClientSubscription, topic: topic) do
169 subscribers = [subscriber.ap_id | subscription.subscribers] |> Enum.uniq()
170 change = Ecto.Changeset.change(subscription, %{subscribers: subscribers})
171 Repo.update(change)
172 else
173 _e ->
174 subscription = %WebsubClientSubscription{
175 topic: topic,
176 hub: subscribed.info.hub,
177 subscribers: [subscriber.ap_id],
178 state: "requested",
179 secret: :crypto.strong_rand_bytes(8) |> Base.url_encode64(),
180 user: subscribed
181 }
182
183 Repo.insert(subscription)
184 end
185
186 requester.(subscription)
187 end
188
189 def gather_feed_data(topic, getter \\ &@httpoison.get/1) do
190 with {:ok, response} <- getter.(topic),
191 status when status in 200..299 <- response.status,
192 body <- response.body,
193 doc <- XML.parse_document(body),
194 uri when not is_nil(uri) <- XML.string_from_xpath("/feed/author[1]/uri", doc),
195 hub when not is_nil(hub) <- XML.string_from_xpath(~S{/feed/link[@rel="hub"]/@href}, doc) do
196 name = XML.string_from_xpath("/feed/author[1]/name", doc)
197 preferredUsername = XML.string_from_xpath("/feed/author[1]/poco:preferredUsername", doc)
198 displayName = XML.string_from_xpath("/feed/author[1]/poco:displayName", doc)
199 avatar = OStatus.make_avatar_object(doc)
200 bio = XML.string_from_xpath("/feed/author[1]/summary", doc)
201
202 {:ok,
203 %{
204 "uri" => uri,
205 "hub" => hub,
206 "nickname" => preferredUsername || name,
207 "name" => displayName || name,
208 "host" => URI.parse(uri).host,
209 "avatar" => avatar,
210 "bio" => bio
211 }}
212 else
213 e ->
214 {:error, e}
215 end
216 end
217
218 def request_subscription(websub, poster \\ &@httpoison.post/3, timeout \\ 10_000) do
219 data = [
220 "hub.mode": "subscribe",
221 "hub.topic": websub.topic,
222 "hub.secret": websub.secret,
223 "hub.callback": Helpers.websub_url(Endpoint, :websub_subscription_confirmation, websub.id)
224 ]
225
226 # This checks once a second if we are confirmed yet
227 websub_checker = fn ->
228 helper = fn helper ->
229 :timer.sleep(1000)
230 websub = Repo.get_by(WebsubClientSubscription, id: websub.id, state: "accepted")
231 if websub, do: websub, else: helper.(helper)
232 end
233
234 helper.(helper)
235 end
236
237 task = Task.async(websub_checker)
238
239 with {:ok, %{status: 202}} <-
240 poster.(websub.hub, {:form, data}, "Content-type": "application/x-www-form-urlencoded"),
241 {:ok, websub} <- Task.yield(task, timeout) do
242 {:ok, websub}
243 else
244 e ->
245 Task.shutdown(task)
246
247 change = Ecto.Changeset.change(websub, %{state: "rejected"})
248 {:ok, websub} = Repo.update(change)
249
250 Logger.debug(fn -> "Couldn't confirm subscription: #{inspect(websub)}" end)
251 Logger.debug(fn -> "error: #{inspect(e)}" end)
252
253 {:error, websub}
254 end
255 end
256
257 def refresh_subscriptions(delta \\ 60 * 60 * 24) do
258 Logger.debug("Refreshing subscriptions")
259
260 cut_off = NaiveDateTime.add(NaiveDateTime.utc_now(), delta)
261
262 query = from(sub in WebsubClientSubscription, where: sub.valid_until < ^cut_off)
263
264 subs = Repo.all(query)
265
266 Enum.each(subs, fn sub ->
267 Pleroma.Web.Federator.enqueue(:request_subscription, sub)
268 end)
269 end
270
271 def publish_one(%{xml: xml, topic: topic, callback: callback, secret: secret}) do
272 signature = sign(secret || "", xml)
273 Logger.info(fn -> "Pushing #{topic} to #{callback}" end)
274
275 with {:ok, %{status: code}} when code in 200..299 <-
276 @httpoison.post(
277 callback,
278 xml,
279 [
280 {"Content-Type", "application/atom+xml"},
281 {"X-Hub-Signature", "sha1=#{signature}"},
282 {"referer", Pleroma.Web.Endpoint.url()}
283 ]
284 ) do
285 Instances.set_reachable(callback)
286 Logger.info(fn -> "Pushed to #{callback}, code #{code}" end)
287 {:ok, code}
288 else
289 {_post_result, response} ->
290 Instances.set_unreachable(callback)
291 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(response)}" end)
292 {:error, response}
293 end
294 end
295 end