Merge branch 'align-mastodon-conversations' into 'develop'
[akkoma] / lib / pleroma / web / rich_media / parser.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.RichMedia.Parser do
6 @parsers [
7 Pleroma.Web.RichMedia.Parsers.OGP,
8 Pleroma.Web.RichMedia.Parsers.TwitterCard,
9 Pleroma.Web.RichMedia.Parsers.OEmbed
10 ]
11
12 @hackney_options [
13 pool: :media,
14 recv_timeout: 2_000,
15 max_body: 2_000_000,
16 with_body: true
17 ]
18
19 def parse(nil), do: {:error, "No URL provided"}
20
21 if Mix.env() == :test do
22 def parse(url), do: parse_url(url)
23 else
24 def parse(url) do
25 try do
26 Cachex.fetch!(:rich_media_cache, url, fn _ ->
27 {:commit, parse_url(url)}
28 end)
29 rescue
30 e ->
31 {:error, "Cachex error: #{inspect(e)}"}
32 end
33 end
34 end
35
36 defp parse_url(url) do
37 try do
38 {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url, [], adapter: @hackney_options)
39
40 html
41 |> maybe_parse()
42 |> clean_parsed_data()
43 |> check_parsed_data()
44 rescue
45 e ->
46 {:error, "Parsing error: #{inspect(e)}"}
47 end
48 end
49
50 defp maybe_parse(html) do
51 Enum.reduce_while(@parsers, %{}, fn parser, acc ->
52 case parser.parse(html, acc) do
53 {:ok, data} -> {:halt, data}
54 {:error, _msg} -> {:cont, acc}
55 end
56 end)
57 end
58
59 defp check_parsed_data(%{title: title} = data) when is_binary(title) and byte_size(title) > 0 do
60 {:ok, data}
61 end
62
63 defp check_parsed_data(data) do
64 {:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
65 end
66
67 defp clean_parsed_data(data) do
68 data
69 |> Enum.reject(fn {key, val} ->
70 with {:ok, _} <- Jason.encode(%{key => val}) do
71 false
72 else
73 _ -> true
74 end
75 end)
76 |> Map.new()
77 end
78 end