Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / rich_media / parser.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.RichMedia.Parser do
6 require Logger
7
8 defp parsers do
9 Pleroma.Config.get([:rich_media, :parsers])
10 end
11
12 def parse(nil), do: {:error, "No URL provided"}
13
14 if Pleroma.Config.get(:env) == :test do
15 @spec parse(String.t()) :: {:ok, map()} | {:error, any()}
16 def parse(url), do: parse_url(url)
17 else
18 @spec parse(String.t()) :: {:ok, map()} | {:error, any()}
19 def parse(url) do
20 with {:ok, data} <- get_cached_or_parse(url),
21 {:ok, _} <- set_ttl_based_on_image(data, url) do
22 {:ok, data}
23 else
24 {:error, {:invalid_metadata, data}} = e ->
25 Logger.debug(fn -> "Incomplete or invalid metadata for #{url}: #{inspect(data)}" end)
26 e
27
28 error ->
29 Logger.error(fn -> "Rich media error for #{url}: #{inspect(error)}" end)
30 error
31 end
32 end
33
34 defp get_cached_or_parse(url) do
35 case Cachex.fetch!(:rich_media_cache, url, fn _ -> {:commit, parse_url(url)} end) do
36 {:ok, _data} = res ->
37 res
38
39 {:error, _} = e ->
40 ttl = Pleroma.Config.get([:rich_media, :failure_backoff], 60_000)
41 Cachex.expire(:rich_media_cache, url, ttl)
42 e
43 end
44 end
45 end
46
47 @doc """
48 Set the rich media cache based on the expiration time of image.
49
50 Adopt behaviour `Pleroma.Web.RichMedia.Parser.TTL`
51
52 ## Example
53
54 defmodule MyModule do
55 @behaviour Pleroma.Web.RichMedia.Parser.TTL
56 def ttl(data, url) do
57 image_url = Map.get(data, :image)
58 # do some parsing in the url and get the ttl of the image
59 # and return ttl is unix time
60 parse_ttl_from_url(image_url)
61 end
62 end
63
64 Define the module in the config
65
66 config :pleroma, :rich_media,
67 ttl_setters: [MyModule]
68 """
69 @spec set_ttl_based_on_image(map(), String.t()) ::
70 {:ok, Integer.t() | :noop} | {:error, :no_key}
71 def set_ttl_based_on_image(data, url) do
72 case get_ttl_from_image(data, url) do
73 {:ok, ttl} when is_number(ttl) ->
74 ttl = ttl * 1000
75
76 case Cachex.expire_at(:rich_media_cache, url, ttl) do
77 {:ok, true} -> {:ok, ttl}
78 {:ok, false} -> {:error, :no_key}
79 end
80
81 _ ->
82 {:ok, :noop}
83 end
84 end
85
86 defp get_ttl_from_image(data, url) do
87 [:rich_media, :ttl_setters]
88 |> Pleroma.Config.get()
89 |> Enum.reduce({:ok, nil}, fn
90 module, {:ok, _ttl} ->
91 module.ttl(data, url)
92
93 _, error ->
94 error
95 end)
96 end
97
98 def parse_url(url) do
99 with {:ok, %Tesla.Env{body: html}} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url),
100 {:ok, html} <- Floki.parse_document(html) do
101 html
102 |> maybe_parse()
103 |> Map.put("url", url)
104 |> clean_parsed_data()
105 |> check_parsed_data()
106 end
107 end
108
109 defp maybe_parse(html) do
110 Enum.reduce_while(parsers(), %{}, fn parser, acc ->
111 case parser.parse(html, acc) do
112 data when data != %{} -> {:halt, data}
113 _ -> {:cont, acc}
114 end
115 end)
116 end
117
118 defp check_parsed_data(%{"title" => title} = data)
119 when is_binary(title) and title != "" do
120 {:ok, data}
121 end
122
123 defp check_parsed_data(data) do
124 {:error, {:invalid_metadata, data}}
125 end
126
127 defp clean_parsed_data(data) do
128 data
129 |> Enum.reject(fn {key, val} ->
130 not match?({:ok, _}, Jason.encode(%{key => val}))
131 end)
132 |> Map.new()
133 end
134 end