Make timeouts longer, some servers are very slow.
[akkoma] / lib / pleroma / web / ostatus / ostatus.ex
1 defmodule Pleroma.Web.OStatus do
2 @httpoison Application.get_env(:pleroma, :httpoison)
3
4 import Ecto.Query
5 import Pleroma.Web.XML
6 require Logger
7
8 alias Pleroma.{Repo, User, Web, Object, Activity}
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.{WebFinger, Websub}
12 alias Pleroma.Web.OStatus.FollowHandler
13
14 def feed_path(user) do
15 "#{user.ap_id}/feed.atom"
16 end
17
18 def pubsub_path(user) do
19 "#{Web.base_url}/push/hub/#{user.nickname}"
20 end
21
22 def salmon_path(user) do
23 "#{user.ap_id}/salmon"
24 end
25
26 def handle_incoming(xml_string) do
27 doc = parse_document(xml_string)
28 entries = :xmerl_xpath.string('//entry', doc)
29
30 activities = Enum.map(entries, fn (entry) ->
31 {:xmlObj, :string, object_type} = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
32 {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
33
34 case verb do
35 'http://activitystrea.ms/schema/1.0/follow' ->
36 with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
37 'http://activitystrea.ms/schema/1.0/share' ->
38 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
39 'http://activitystrea.ms/schema/1.0/favorite' ->
40 with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc), do: [activity, favorited_activity]
41 _ ->
42 case object_type do
43 'http://activitystrea.ms/schema/1.0/note' ->
44 with {:ok, activity} <- handle_note(entry, doc), do: activity
45 'http://activitystrea.ms/schema/1.0/comment' ->
46 with {:ok, activity} <- handle_note(entry, doc), do: activity
47 _ ->
48 Logger.error("Couldn't parse incoming document")
49 nil
50 end
51 end
52 end)
53 {:ok, activities}
54 end
55
56 def make_share(entry, doc, retweeted_activity) do
57 with {:ok, actor} <- find_make_or_update_user(doc),
58 %Object{} = object <- Object.get_by_ap_id(retweeted_activity.data["object"]["id"]),
59 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
60 {:ok, activity, _object} = ActivityPub.announce(actor, object, id, false) do
61 {:ok, activity}
62 end
63 end
64
65 def handle_share(entry, doc) do
66 with {:ok, retweeted_activity} <- get_or_build_object(entry),
67 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
68 {:ok, activity, retweeted_activity}
69 else
70 e -> {:error, e}
71 end
72 end
73
74 def make_favorite(entry, doc, favorited_activity) do
75 with {:ok, actor} <- find_make_or_update_user(doc),
76 %Object{} = object <- Object.get_by_ap_id(favorited_activity.data["object"]["id"]),
77 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
78 {:ok, activity, _object} = ActivityPub.like(actor, object, id, false) do
79 {:ok, activity}
80 end
81 end
82
83 def get_or_build_object(entry) do
84 with {:ok, activity} <- get_or_try_fetching(entry) do
85 {:ok, activity}
86 else
87 _e ->
88 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry) do
89 handle_note(object, object)
90 end
91 end
92 end
93
94
95 def get_or_try_fetching(entry) do
96 Logger.debug("Trying to get entry from db")
97 with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
98 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
99 {:ok, activity}
100 else e ->
101 Logger.debug("Couldn't get, will try to fetch")
102 with href when not is_nil(href) <- string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry),
103 {:ok, [favorited_activity]} <- fetch_activity_from_html_url(href) do
104 {:ok, favorited_activity}
105 else e -> Logger.debug("Couldn't find href: #{inspect(e)}")
106 end
107 end
108 end
109
110 def handle_favorite(entry, doc) do
111 with {:ok, favorited_activity} <- get_or_try_fetching(entry),
112 {:ok, activity} <- make_favorite(entry, doc, favorited_activity) do
113 {:ok, activity, favorited_activity}
114 else
115 e -> {:error, e}
116 end
117 end
118
119 def get_attachments(entry) do
120 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
121 |> Enum.map(fn (enclosure) ->
122 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
123 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
124 %{
125 "type" => "Attachment",
126 "url" => [%{
127 "type" => "Link",
128 "mediaType" => type,
129 "href" => href
130 }]
131 }
132 end
133 end)
134 |> Enum.filter(&(&1))
135 end
136
137 def get_content(entry) do
138 base_content = string_from_xpath("//content", entry)
139
140 with scope when not is_nil(scope) <- string_from_xpath("//mastodon:scope", entry),
141 cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do
142 "<span class='mastodon-cw'>#{cw}</span><br>#{base_content}"
143 else _e -> base_content
144 end
145 end
146
147 def get_tags(entry) do
148 :xmerl_xpath.string('//category', entry)
149 |> Enum.map(fn (category) -> string_from_xpath("/category/@term", category) end)
150 end
151
152 def handle_note(entry, doc \\ nil) do
153 content_html = get_content(entry)
154
155 [author] = :xmerl_xpath.string('//author[1]', doc)
156 {:ok, actor} = find_make_or_update_user(author)
157 inReplyTo = string_from_xpath("//thr:in-reply-to[1]/@ref", entry)
158
159 if inReplyTo && !Object.get_cached_by_ap_id(inReplyTo) do
160 inReplyToHref = string_from_xpath("//thr:in-reply-to[1]/@href", entry)
161 if inReplyToHref do
162 fetch_activity_from_html_url(inReplyToHref)
163 else
164 Logger.debug("Couldn't find a href link to #{inReplyTo}")
165 end
166 end
167
168 context = (string_from_xpath("//ostatus:conversation[1]", entry) || "") |> String.trim
169
170 attachments = get_attachments(entry)
171
172 context = with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
173 context
174 else _e ->
175 if String.length(context) > 0 do
176 context
177 else
178 Utils.generate_context_id
179 end
180 end
181
182 tags = get_tags(entry)
183
184 to = [
185 "https://www.w3.org/ns/activitystreams#Public",
186 User.ap_followers(actor)
187 ]
188
189 mentions = :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
190 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
191
192 to = to ++ mentions
193
194 date = string_from_xpath("//published", entry)
195 id = string_from_xpath("//id", entry)
196
197 object = %{
198 "id" => id,
199 "type" => "Note",
200 "to" => to,
201 "content" => content_html,
202 "published" => date,
203 "context" => context,
204 "actor" => actor.ap_id,
205 "attachment" => attachments,
206 "tag" => tags
207 }
208
209 object = if inReplyTo do
210 replied_to_activity = Activity.get_create_activity_by_object_ap_id(inReplyTo)
211 if replied_to_activity do
212 object
213 |> Map.put("inReplyTo", inReplyTo)
214 |> Map.put("inReplyToStatusId", replied_to_activity.id)
215 else
216 object
217 |> Map.put("inReplyTo", inReplyTo)
218 end
219 else
220 object
221 end
222
223 # TODO: Bail out sooner and use transaction.
224 if Object.get_by_ap_id(id) do
225 {:ok, Activity.get_create_activity_by_object_ap_id(id)}
226 else
227 ActivityPub.create(to, actor, context, object, %{}, date, false)
228 end
229 end
230
231 def maybe_update(doc, user) do
232 old_data = %{
233 avatar: user.avatar,
234 bio: user.bio,
235 name: user.name
236 }
237
238 with false <- user.local,
239 avatar <- make_avatar_object(doc),
240 bio when not is_nil(bio) <- string_from_xpath("//author[1]/summary", doc),
241 name when not is_nil(name) <- string_from_xpath("//author[1]/poco:displayName", doc),
242 new_data <- %{avatar: avatar, name: name, bio: bio},
243 false <- new_data == old_data do
244 change = Ecto.Changeset.change(user, new_data)
245 Repo.update(change)
246 else e ->
247 {:ok, user}
248 end
249 end
250
251 def find_make_or_update_user(doc) do
252 uri = string_from_xpath("//author/uri[1]", doc)
253 with {:ok, user} <- find_or_make_user(uri) do
254 maybe_update(doc, user)
255 end
256 end
257
258 def find_or_make_user(uri) do
259 query = from user in User,
260 where: user.ap_id == ^uri
261
262 user = Repo.one(query)
263
264 if is_nil(user) do
265 make_user(uri)
266 else
267 {:ok, user}
268 end
269 end
270
271 def make_user(uri) do
272 with {:ok, info} <- gather_user_info(uri) do
273 data = %{
274 name: info["name"],
275 nickname: info["nickname"] <> "@" <> info["host"],
276 ap_id: info["uri"],
277 info: info,
278 avatar: info["avatar"],
279 bio: info["bio"]
280 }
281 with %User{} = user <- User.get_by_ap_id(data.ap_id) do
282 {:ok, user}
283 else _e ->
284 cs = User.remote_user_creation(data)
285 Repo.insert(cs)
286 end
287 end
288 end
289
290 # TODO: Just takes the first one for now.
291 def make_avatar_object(author_doc) do
292 href = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@href", author_doc)
293 type = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@type", author_doc)
294
295 if href do
296 %{
297 "type" => "Image",
298 "url" =>
299 [%{
300 "type" => "Link",
301 "mediaType" => type,
302 "href" => href
303 }]
304 }
305 else
306 nil
307 end
308 end
309
310 def gather_user_info(username) do
311 with {:ok, webfinger_data} <- WebFinger.finger(username),
312 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
313 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
314 else e ->
315 Logger.debug(fn -> "Couldn't gather info for #{username}" end)
316 {:error, e}
317 end
318 end
319
320 # Regex-based 'parsing' so we don't have to pull in a full html parser
321 # It's a hack anyway. Maybe revisit this in the future
322 @mastodon_regex ~r/<link href='(.*)' rel='alternate' type='application\/atom\+xml'>/
323 @gs_regex ~r/<link title=.* href="(.*)" type="application\/atom\+xml" rel="alternate">/
324 @gs_classic_regex ~r/<link rel="alternate" href="(.*)" type="application\/atom\+xml" title=.*>/
325 def get_atom_url(body) do
326 cond do
327 Regex.match?(@mastodon_regex, body) ->
328 [[_, match]] = Regex.scan(@mastodon_regex, body)
329 {:ok, match}
330 Regex.match?(@gs_regex, body) ->
331 [[_, match]] = Regex.scan(@gs_regex, body)
332 {:ok, match}
333 Regex.match?(@gs_classic_regex, body) ->
334 [[_, match]] = Regex.scan(@gs_classic_regex, body)
335 {:ok, match}
336 true ->
337 Logger.debug(fn -> "Couldn't find atom link in #{inspect(body)}" end)
338 {:error, "Couldn't find the atom link"}
339 end
340 end
341
342 def fetch_activity_from_html_url(url) do
343 Logger.debug("Trying to fetch #{url}")
344 with {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000),
345 {:ok, atom_url} <- get_atom_url(body),
346 {:ok, %{status_code: code, body: body}} when code in 200..299 <- @httpoison.get(atom_url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do
347 Logger.debug("Got document from #{url}, handling...")
348 handle_incoming(body)
349 else e -> Logger.debug("Couldn't get #{url}: #{inspect(e)}")
350 end
351 end
352 end