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