Merge branch 'mastofe-content-types' 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 ]
17
18 def parse(nil), do: {:error, "No URL provided"}
19
20 if Mix.env() == :test do
21 def parse(url), do: parse_url(url)
22 else
23 def parse(url) do
24 try do
25 Cachex.fetch!(:rich_media_cache, url, fn _ ->
26 {:commit, parse_url(url)}
27 end)
28 rescue
29 e ->
30 {:error, "Cachex error: #{inspect(e)}"}
31 end
32 end
33 end
34
35 defp parse_url(url) do
36 try do
37 {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url, [], adapter: @hackney_options)
38
39 html |> maybe_parse() |> clean_parsed_data() |> check_parsed_data()
40 rescue
41 e ->
42 {:error, "Parsing error: #{inspect(e)}"}
43 end
44 end
45
46 defp maybe_parse(html) do
47 Enum.reduce_while(@parsers, %{}, fn parser, acc ->
48 case parser.parse(html, acc) do
49 {:ok, data} -> {:halt, data}
50 {:error, _msg} -> {:cont, acc}
51 end
52 end)
53 end
54
55 defp check_parsed_data(%{title: title} = data) when is_binary(title) and byte_size(title) > 0 do
56 {:ok, data}
57 end
58
59 defp check_parsed_data(data) do
60 {:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
61 end
62
63 defp clean_parsed_data(data) do
64 data
65 |> Enum.reject(fn {key, val} ->
66 with {:ok, _} <- Jason.encode(%{key => val}) do
67 false
68 else
69 _ -> true
70 end
71 end)
72 |> Map.new()
73 end
74 end