Longer timeouts for outgoing federation.
[akkoma] / lib / pleroma / web / websub / websub.ex
1 defmodule Pleroma.Web.Websub do
2 alias Ecto.Changeset
3 alias Pleroma.Repo
4 alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription}
5 alias Pleroma.Web.OStatus.FeedRepresenter
6 alias Pleroma.Web.{XML, Endpoint, OStatus}
7 alias Pleroma.Web.Router.Helpers
8 require Logger
9
10 import Ecto.Query
11
12 @httpoison Application.get_env(:pleroma, :httpoison)
13
14 def verify(subscription, getter \\ &@httpoison.get/3) do
15 challenge = Base.encode16(:crypto.strong_rand_bytes(8))
16 lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.updated_at)
17 lease_seconds = lease_seconds |> to_string
18
19 params = %{
20 "hub.challenge": challenge,
21 "hub.lease_seconds": lease_seconds,
22 "hub.topic": subscription.topic,
23 "hub.mode": "subscribe"
24 }
25
26 url = hd(String.split(subscription.callback, "?"))
27 query = URI.parse(subscription.callback).query || ""
28 params = Map.merge(params, URI.decode_query(query))
29 with {:ok, response} <- getter.(url, [], [params: params]),
30 ^challenge <- response.body
31 do
32 changeset = Changeset.change(subscription, %{state: "active"})
33 Repo.update(changeset)
34 else _e ->
35 changeset = Changeset.change(subscription, %{state: "rejected"})
36 {:ok, subscription} = Repo.update(changeset)
37 {:error, subscription}
38 end
39 end
40
41 def publish(topic, user, activity) do
42 # TODO: Only send to still valid subscriptions.
43 query = from sub in WebsubServerSubscription,
44 where: sub.topic == ^topic and sub.state == "active"
45 subscriptions = Repo.all(query)
46 Enum.each(subscriptions, fn(sub) ->
47 response = user
48 |> FeedRepresenter.to_simple_form([activity], [user])
49 |> :xmerl.export_simple(:xmerl_xml)
50 |> to_string
51
52 signature = sign(sub.secret || "", response)
53 Logger.debug(fn -> "Pushing #{topic} to #{sub.callback}" end)
54
55 Task.start(fn ->
56 with {:ok, %{status_code: code}} <- @httpoison.post(sub.callback, response, [
57 {"Content-Type", "application/atom+xml"},
58 {"X-Hub-Signature", "sha1=#{signature}"}
59 ], timeout: 10000, recv_timeout: 20000) do
60 Logger.debug(fn -> "Pushed to #{sub.callback}, code #{code}" end)
61 else e ->
62 Logger.debug(fn -> "Couldn't push to #{sub.callback}, #{inspect(e)}" end)
63 end
64 end)
65 end)
66 end
67
68 def sign(secret, doc) do
69 :crypto.hmac(:sha, secret, to_string(doc)) |> Base.encode16 |> String.downcase
70 end
71
72 def incoming_subscription_request(user, %{"hub.mode" => "subscribe"} = params) do
73 with {:ok, topic} <- valid_topic(params, user),
74 {:ok, lease_time} <- lease_time(params),
75 secret <- params["hub.secret"],
76 callback <- params["hub.callback"]
77 do
78 subscription = get_subscription(topic, callback)
79 data = %{
80 state: subscription.state || "requested",
81 topic: topic,
82 secret: secret,
83 callback: callback
84 }
85
86 change = Changeset.change(subscription, data)
87 websub = Repo.insert_or_update!(change)
88
89 change = Changeset.change(websub, %{valid_until:
90 NaiveDateTime.add(websub.updated_at, lease_time)})
91 websub = Repo.update!(change)
92
93 Pleroma.Web.Federator.enqueue(:verify_websub, websub)
94
95 {:ok, websub}
96 else {:error, reason} ->
97 Logger.debug("Couldn't create subscription.")
98 Logger.debug(inspect(reason))
99
100 {:error, reason}
101 end
102 end
103
104 defp get_subscription(topic, callback) do
105 Repo.get_by(WebsubServerSubscription, topic: topic, callback: callback) ||
106 %WebsubServerSubscription{}
107 end
108
109 # Temp hack for mastodon.
110 defp lease_time(%{"hub.lease_seconds" => ""}) do
111 {:ok, 60 * 60 * 24 * 3} # three days
112 end
113
114 defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
115 {:ok, String.to_integer(lease_seconds)}
116 end
117
118 defp lease_time(_) do
119 {:ok, 60 * 60 * 24 * 3} # three days
120 end
121
122 defp valid_topic(%{"hub.topic" => topic}, user) do
123 if topic == OStatus.feed_path(user) do
124 {:ok, OStatus.feed_path(user)}
125 else
126 {:error, "Wrong topic requested, expected #{OStatus.feed_path(user)}, got #{topic}"}
127 end
128 end
129
130 def subscribe(subscriber, subscribed, requester \\ &request_subscription/1) do
131 topic = subscribed.info["topic"]
132 # FIXME: Race condition, use transactions
133 {:ok, subscription} = with subscription when not is_nil(subscription) <- Repo.get_by(WebsubClientSubscription, topic: topic) do
134 subscribers = [subscriber.ap_id | subscription.subscribers] |> Enum.uniq
135 change = Ecto.Changeset.change(subscription, %{subscribers: subscribers})
136 Repo.update(change)
137 else _e ->
138 subscription = %WebsubClientSubscription{
139 topic: topic,
140 hub: subscribed.info["hub"],
141 subscribers: [subscriber.ap_id],
142 state: "requested",
143 secret: :crypto.strong_rand_bytes(8) |> Base.url_encode64,
144 user: subscribed
145 }
146 Repo.insert(subscription)
147 end
148 requester.(subscription)
149 end
150
151 def gather_feed_data(topic, getter \\ &@httpoison.get/1) do
152 with {:ok, response} <- getter.(topic),
153 status_code when status_code in 200..299 <- response.status_code,
154 body <- response.body,
155 doc <- XML.parse_document(body),
156 uri when not is_nil(uri) <- XML.string_from_xpath("/feed/author[1]/uri", doc),
157 hub when not is_nil(hub) <- XML.string_from_xpath(~S{/feed/link[@rel="hub"]/@href}, doc) do
158
159 name = XML.string_from_xpath("/feed/author[1]/name", doc)
160 preferredUsername = XML.string_from_xpath("/feed/author[1]/poco:preferredUsername", doc)
161 displayName = XML.string_from_xpath("/feed/author[1]/poco:displayName", doc)
162 avatar = OStatus.make_avatar_object(doc)
163 bio = XML.string_from_xpath("/feed/author[1]/summary", doc)
164
165 {:ok, %{
166 "uri" => uri,
167 "hub" => hub,
168 "nickname" => preferredUsername || name,
169 "name" => displayName || name,
170 "host" => URI.parse(uri).host,
171 "avatar" => avatar,
172 "bio" => bio
173 }}
174 else e ->
175 {:error, e}
176 end
177 end
178
179 def request_subscription(websub, poster \\ &@httpoison.post/3, timeout \\ 10_000) do
180 data = [
181 "hub.mode": "subscribe",
182 "hub.topic": websub.topic,
183 "hub.secret": websub.secret,
184 "hub.callback": Helpers.websub_url(Endpoint, :websub_subscription_confirmation, websub.id)
185 ]
186
187 # This checks once a second if we are confirmed yet
188 websub_checker = fn ->
189 helper = fn (helper) ->
190 :timer.sleep(1000)
191 websub = Repo.get_by(WebsubClientSubscription, id: websub.id, state: "accepted")
192 if websub, do: websub, else: helper.(helper)
193 end
194 helper.(helper)
195 end
196
197 task = Task.async(websub_checker)
198
199 with {:ok, %{status_code: 202}} <- poster.(websub.hub, {:form, data}, ["Content-type": "application/x-www-form-urlencoded"]),
200 {:ok, websub} <- Task.yield(task, timeout) do
201 {:ok, websub}
202 else e ->
203 Task.shutdown(task)
204
205 change = Ecto.Changeset.change(websub, %{state: "rejected"})
206 {:ok, websub} = Repo.update(change)
207
208 Logger.debug(fn -> "Couldn't confirm subscription: #{inspect(websub)}" end)
209 Logger.debug(fn -> "error: #{inspect(e)}" end)
210
211 {:error, websub}
212 end
213 end
214
215 def refresh_subscriptions(delta \\ 60 * 60 * 24) do
216 Logger.debug("Refreshing subscriptions")
217
218 cut_off = NaiveDateTime.add(NaiveDateTime.utc_now, delta)
219
220 query = from sub in WebsubClientSubscription,
221 where: sub.valid_until < ^cut_off and sub.state == "accepted"
222
223 subs = Repo.all(query)
224
225 Enum.map(subs, fn (sub) ->
226 request_subscription(sub)
227 end)
228 end
229 end