1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.Parser do
8 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
11 Pleroma.Config.get([:rich_media, :parsers])
14 def parse(nil), do: {:error, "No URL provided"}
16 if Pleroma.Config.get(:env) == :test do
17 @spec parse(String.t()) :: {:ok, map()} | {:error, any()}
18 def parse(url), do: parse_url(url)
20 @spec parse(String.t()) :: {:ok, map()} | {:error, any()}
22 with {:ok, data} <- get_cached_or_parse(url),
23 {:ok, _} <- set_ttl_based_on_image(data, url) do
28 defp get_cached_or_parse(url) do
29 case @cachex.fetch(:rich_media_cache, url, fn ->
30 case parse_url(url) do
34 {:error, reason} = e ->
35 # Unfortunately we have to log errors here, instead of doing that
36 # along with ttl setting at the bottom. Otherwise we can get log spam
37 # if more than one process was waiting for the rich media card
38 # while it was generated. Ideally we would set ttl here as well,
39 # so we don't override it number_of_waiters_on_generation
40 # times, but one, obviously, can't set ttl for not-yet-created entry
41 # and Cachex doesn't support returning ttl from the fetch callback.
42 log_error(url, reason)
46 {action, res} when action in [:commit, :ok] ->
51 {:error, reason} = e ->
52 if action == :commit, do: set_error_ttl(url, reason)
57 {:error, {:cachex_error, e}}
61 defp set_error_ttl(_url, :body_too_large), do: :ok
62 defp set_error_ttl(_url, {:content_type, _}), do: :ok
64 # The TTL is not set for the errors above, since they are unlikely to change
67 defp set_error_ttl(url, _reason) do
68 ttl = Pleroma.Config.get([:rich_media, :failure_backoff], 60_000)
69 @cachex.expire(:rich_media_cache, url, ttl)
73 defp log_error(url, {:invalid_metadata, data}) do
74 Logger.debug(fn -> "Incomplete or invalid metadata for #{url}: #{inspect(data)}" end)
77 defp log_error(url, reason) do
78 Logger.warn(fn -> "Rich media error for #{url}: #{inspect(reason)}" end)
83 Set the rich media cache based on the expiration time of image.
85 Adopt behaviour `Pleroma.Web.RichMedia.Parser.TTL`
90 @behaviour Pleroma.Web.RichMedia.Parser.TTL
92 image_url = Map.get(data, :image)
93 # do some parsing in the url and get the ttl of the image
94 # and return ttl is unix time
95 parse_ttl_from_url(image_url)
99 Define the module in the config
101 config :pleroma, :rich_media,
102 ttl_setters: [MyModule]
104 @spec set_ttl_based_on_image(map(), String.t()) ::
105 {:ok, Integer.t() | :noop} | {:error, :no_key}
106 def set_ttl_based_on_image(data, url) do
107 case get_ttl_from_image(data, url) do
108 {:ok, ttl} when is_number(ttl) ->
111 case @cachex.expire_at(:rich_media_cache, url, ttl) do
112 {:ok, true} -> {:ok, ttl}
113 {:ok, false} -> {:error, :no_key}
121 defp get_ttl_from_image(data, url) do
122 [:rich_media, :ttl_setters]
123 |> Pleroma.Config.get()
124 |> Enum.reduce({:ok, nil}, fn
125 module, {:ok, _ttl} ->
126 module.ttl(data, url)
133 def parse_url(url) do
134 with {:ok, %Tesla.Env{body: html}} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url),
135 {:ok, html} <- Floki.parse_document(html) do
138 |> Map.put("url", url)
139 |> clean_parsed_data()
140 |> check_parsed_data()
144 defp maybe_parse(html) do
145 Enum.reduce_while(parsers(), %{}, fn parser, acc ->
146 case parser.parse(html, acc) do
147 data when data != %{} -> {:halt, data}
153 defp check_parsed_data(%{"title" => title} = data)
154 when is_binary(title) and title != "" do
158 defp check_parsed_data(data) do
159 {:error, {:invalid_metadata, data}}
162 defp clean_parsed_data(data) do
164 |> Enum.reject(fn {key, val} ->
165 not match?({:ok, _}, Jason.encode(%{key => val}))