[#534] Made federation push sender be determined basing on content instead of `refere...
[akkoma] / lib / pleroma / web / ostatus / ostatus.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.OStatus do
6 @httpoison Application.get_env(:pleroma, :httpoison)
7
8 import Ecto.Query
9 import Pleroma.Web.XML
10 require Logger
11
12 alias Pleroma.{Repo, User, Web, Object, Activity}
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.{WebFinger, Websub}
15 alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler}
16 alias Pleroma.Web.ActivityPub.Transmogrifier
17
18 def is_representable?(%Activity{data: data}) do
19 object = Object.normalize(data["object"])
20
21 cond do
22 is_nil(object) ->
23 false
24
25 object.data["type"] == "Note" ->
26 true
27
28 true ->
29 false
30 end
31 end
32
33 def feed_path(user) do
34 "#{user.ap_id}/feed.atom"
35 end
36
37 def pubsub_path(user) do
38 "#{Web.base_url()}/push/hub/#{user.nickname}"
39 end
40
41 def salmon_path(user) do
42 "#{user.ap_id}/salmon"
43 end
44
45 def remote_follow_path do
46 "#{Web.base_url()}/ostatus_subscribe?acct={uri}"
47 end
48
49 def handle_incoming(xml_string) do
50 with doc when doc != :error <- parse_document(xml_string) do
51 with {:ok, actor_user} <- find_make_or_update_user(doc),
52 do: Pleroma.Instances.set_reachable(actor_user.ap_id)
53
54 entries = :xmerl_xpath.string('//entry', doc)
55
56 activities =
57 Enum.map(entries, fn entry ->
58 {:xmlObj, :string, object_type} =
59 :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
60
61 {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
62 Logger.debug("Handling #{verb}")
63
64 try do
65 case verb do
66 'http://activitystrea.ms/schema/1.0/delete' ->
67 with {:ok, activity} <- DeleteHandler.handle_delete(entry, doc), do: activity
68
69 'http://activitystrea.ms/schema/1.0/follow' ->
70 with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
71
72 'http://activitystrea.ms/schema/1.0/unfollow' ->
73 with {:ok, activity} <- UnfollowHandler.handle(entry, doc), do: activity
74
75 'http://activitystrea.ms/schema/1.0/share' ->
76 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc),
77 do: [activity, retweeted_activity]
78
79 'http://activitystrea.ms/schema/1.0/favorite' ->
80 with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc),
81 do: [activity, favorited_activity]
82
83 _ ->
84 case object_type do
85 'http://activitystrea.ms/schema/1.0/note' ->
86 with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
87
88 'http://activitystrea.ms/schema/1.0/comment' ->
89 with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
90
91 _ ->
92 Logger.error("Couldn't parse incoming document")
93 nil
94 end
95 end
96 rescue
97 e ->
98 Logger.error("Error occured while handling activity")
99 Logger.error(xml_string)
100 Logger.error(inspect(e))
101 nil
102 end
103 end)
104 |> Enum.filter(& &1)
105
106 {:ok, activities}
107 else
108 _e -> {:error, []}
109 end
110 end
111
112 def make_share(entry, doc, retweeted_activity) do
113 with {:ok, actor} <- find_make_or_update_user(doc),
114 %Object{} = object <- Object.normalize(retweeted_activity.data["object"]),
115 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
116 {:ok, activity, _object} = ActivityPub.announce(actor, object, id, false) do
117 {:ok, activity}
118 end
119 end
120
121 def handle_share(entry, doc) do
122 with {:ok, retweeted_activity} <- get_or_build_object(entry),
123 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
124 {:ok, activity, retweeted_activity}
125 else
126 e -> {:error, e}
127 end
128 end
129
130 def make_favorite(entry, doc, favorited_activity) do
131 with {:ok, actor} <- find_make_or_update_user(doc),
132 %Object{} = object <- Object.normalize(favorited_activity.data["object"]),
133 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
134 {:ok, activity, _object} = ActivityPub.like(actor, object, id, false) do
135 {:ok, activity}
136 end
137 end
138
139 def get_or_build_object(entry) do
140 with {:ok, activity} <- get_or_try_fetching(entry) do
141 {:ok, activity}
142 else
143 _e ->
144 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry) do
145 NoteHandler.handle_note(object, object)
146 end
147 end
148 end
149
150 def get_or_try_fetching(entry) do
151 Logger.debug("Trying to get entry from db")
152
153 with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
154 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
155 {:ok, activity}
156 else
157 _ ->
158 Logger.debug("Couldn't get, will try to fetch")
159
160 with href when not is_nil(href) <-
161 string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry),
162 {:ok, [favorited_activity]} <- fetch_activity_from_url(href) do
163 {:ok, favorited_activity}
164 else
165 e -> Logger.debug("Couldn't find href: #{inspect(e)}")
166 end
167 end
168 end
169
170 def handle_favorite(entry, doc) do
171 with {:ok, favorited_activity} <- get_or_try_fetching(entry),
172 {:ok, activity} <- make_favorite(entry, doc, favorited_activity) do
173 {:ok, activity, favorited_activity}
174 else
175 e -> {:error, e}
176 end
177 end
178
179 def get_attachments(entry) do
180 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
181 |> Enum.map(fn enclosure ->
182 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
183 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
184 %{
185 "type" => "Attachment",
186 "url" => [
187 %{
188 "type" => "Link",
189 "mediaType" => type,
190 "href" => href
191 }
192 ]
193 }
194 end
195 end)
196 |> Enum.filter(& &1)
197 end
198
199 @doc """
200 Gets the content from a an entry.
201 """
202 def get_content(entry) do
203 string_from_xpath("//content", entry)
204 end
205
206 @doc """
207 Get the cw that mastodon uses.
208 """
209 def get_cw(entry) do
210 with cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do
211 cw
212 else
213 _e -> nil
214 end
215 end
216
217 def get_tags(entry) do
218 :xmerl_xpath.string('//category', entry)
219 |> Enum.map(fn category -> string_from_xpath("/category/@term", category) end)
220 |> Enum.filter(& &1)
221 |> Enum.map(&String.downcase/1)
222 end
223
224 def maybe_update(doc, user) do
225 if "true" == string_from_xpath("//author[1]/ap_enabled", doc) do
226 Transmogrifier.upgrade_user_from_ap_id(user.ap_id)
227 else
228 maybe_update_ostatus(doc, user)
229 end
230 end
231
232 def maybe_update_ostatus(doc, user) do
233 old_data = %{
234 avatar: user.avatar,
235 bio: user.bio,
236 name: user.name
237 }
238
239 with false <- user.local,
240 avatar <- make_avatar_object(doc),
241 bio <- string_from_xpath("//author[1]/summary", doc),
242 name <- string_from_xpath("//author[1]/poco:displayName", doc),
243 new_data <- %{
244 avatar: avatar || old_data.avatar,
245 name: name || old_data.name,
246 bio: bio || old_data.bio
247 },
248 false <- new_data == old_data do
249 change = Ecto.Changeset.change(user, new_data)
250 User.update_and_set_cache(change)
251 else
252 _ ->
253 {:ok, user}
254 end
255 end
256
257 def find_make_or_update_user(doc) do
258 uri = string_from_xpath("//author/uri[1]", doc)
259
260 with {:ok, user} <- find_or_make_user(uri) do
261 maybe_update(doc, user)
262 end
263 end
264
265 def find_or_make_user(uri) do
266 query = from(user in User, where: user.ap_id == ^uri)
267
268 user = Repo.one(query)
269
270 if is_nil(user) do
271 make_user(uri)
272 else
273 {:ok, user}
274 end
275 end
276
277 def make_user(uri, update \\ false) do
278 with {:ok, info} <- gather_user_info(uri) do
279 data = %{
280 name: info["name"],
281 nickname: info["nickname"] <> "@" <> info["host"],
282 ap_id: info["uri"],
283 info: info,
284 avatar: info["avatar"],
285 bio: info["bio"]
286 }
287
288 with false <- update,
289 %User{} = user <- User.get_by_ap_id(data.ap_id) do
290 {:ok, user}
291 else
292 _e -> User.insert_or_update_user(data)
293 end
294 end
295 end
296
297 # TODO: Just takes the first one for now.
298 def make_avatar_object(author_doc, rel \\ "avatar") do
299 href = string_from_xpath("//author[1]/link[@rel=\"#{rel}\"]/@href", author_doc)
300 type = string_from_xpath("//author[1]/link[@rel=\"#{rel}\"]/@type", author_doc)
301
302 if href do
303 %{
304 "type" => "Image",
305 "url" => [
306 %{
307 "type" => "Link",
308 "mediaType" => type,
309 "href" => href
310 }
311 ]
312 }
313 else
314 nil
315 end
316 end
317
318 def gather_user_info(username) do
319 with {:ok, webfinger_data} <- WebFinger.finger(username),
320 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
321 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
322 else
323 e ->
324 Logger.debug(fn -> "Couldn't gather info for #{username}" end)
325 {:error, e}
326 end
327 end
328
329 # Regex-based 'parsing' so we don't have to pull in a full html parser
330 # It's a hack anyway. Maybe revisit this in the future
331 @mastodon_regex ~r/<link href='(.*)' rel='alternate' type='application\/atom\+xml'>/
332 @gs_regex ~r/<link title=.* href="(.*)" type="application\/atom\+xml" rel="alternate">/
333 @gs_classic_regex ~r/<link rel="alternate" href="(.*)" type="application\/atom\+xml" title=.*>/
334 def get_atom_url(body) do
335 cond do
336 Regex.match?(@mastodon_regex, body) ->
337 [[_, match]] = Regex.scan(@mastodon_regex, body)
338 {:ok, match}
339
340 Regex.match?(@gs_regex, body) ->
341 [[_, match]] = Regex.scan(@gs_regex, body)
342 {:ok, match}
343
344 Regex.match?(@gs_classic_regex, body) ->
345 [[_, match]] = Regex.scan(@gs_classic_regex, body)
346 {:ok, match}
347
348 true ->
349 Logger.debug(fn -> "Couldn't find Atom link in #{inspect(body)}" end)
350 {:error, "Couldn't find the Atom link"}
351 end
352 end
353
354 def fetch_activity_from_atom_url(url) do
355 with true <- String.starts_with?(url, "http"),
356 {:ok, %{body: body, status: code}} when code in 200..299 <-
357 @httpoison.get(
358 url,
359 [{:Accept, "application/atom+xml"}]
360 ) do
361 Logger.debug("Got document from #{url}, handling...")
362 handle_incoming(body)
363 else
364 e ->
365 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
366 e
367 end
368 end
369
370 def fetch_activity_from_html_url(url) do
371 Logger.debug("Trying to fetch #{url}")
372
373 with true <- String.starts_with?(url, "http"),
374 {:ok, %{body: body}} <- @httpoison.get(url, []),
375 {:ok, atom_url} <- get_atom_url(body) do
376 fetch_activity_from_atom_url(atom_url)
377 else
378 e ->
379 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
380 e
381 end
382 end
383
384 def fetch_activity_from_url(url) do
385 with {:ok, [_ | _] = activities} <- fetch_activity_from_atom_url(url) do
386 {:ok, activities}
387 else
388 _e -> fetch_activity_from_html_url(url)
389 end
390 rescue
391 e ->
392 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
393 {:error, "Couldn't get #{url}: #{inspect(e)}"}
394 end
395 end