e9aa2dd03d69e810caff67e85cfdbf879979991a
[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 Cachex.fetch!(:rich_media_cache, url, fn _ ->
21 with {:ok, data} <- parse_url(url) do
22 {:commit, {:ok, data}}
23 else
24 error -> {:ignore, error}
25 end
26 end)
27 |> set_ttl_based_on_image(url)
28 end
29 end
30
31 @doc """
32 Set the rich media cache based on the expiration time of image.
33
34 Adopt behaviour `Pleroma.Web.RichMedia.Parser.TTL`
35
36 ## Example
37
38 defmodule MyModule do
39 @behaviour Pleroma.Web.RichMedia.Parser.TTL
40 def ttl(data, url) do
41 image_url = Map.get(data, :image)
42 # do some parsing in the url and get the ttl of the image
43 # and return ttl is unix time
44 parse_ttl_from_url(image_url)
45 end
46 end
47
48 Define the module in the config
49
50 config :pleroma, :rich_media,
51 ttl_setters: [MyModule]
52 """
53 @spec set_ttl_based_on_image({:ok, map()} | {:error, any()}, String.t()) ::
54 {:ok, map()} | {:error, any()}
55 def set_ttl_based_on_image({:ok, data}, url) do
56 with {:ok, nil} <- Cachex.ttl(:rich_media_cache, url),
57 {:ok, ttl} when is_number(ttl) <- get_ttl_from_image(data, url) do
58 Cachex.expire_at(:rich_media_cache, url, ttl * 1000)
59 {:ok, data}
60 else
61 _ ->
62 {:ok, data}
63 end
64 end
65
66 def set_ttl_based_on_image({:error, _} = error, _) do
67 Logger.error("parsing error: #{inspect(error)}")
68 error
69 end
70
71 defp get_ttl_from_image(data, url) do
72 [:rich_media, :ttl_setters]
73 |> Pleroma.Config.get()
74 |> Enum.reduce({:ok, nil}, fn
75 module, {:ok, _ttl} ->
76 module.ttl(data, url)
77
78 _, error ->
79 error
80 end)
81 end
82
83 defp parse_url(url) do
84 with {:ok, %Tesla.Env{body: html}} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url),
85 {:ok, html} <- Floki.parse_document(html) do
86 html
87 |> maybe_parse()
88 |> Map.put("url", url)
89 |> clean_parsed_data()
90 |> check_parsed_data()
91 end
92 end
93
94 defp maybe_parse(html) do
95 Enum.reduce_while(parsers(), %{}, fn parser, acc ->
96 case parser.parse(html, acc) do
97 data when data != %{} -> {:halt, data}
98 _ -> {:cont, acc}
99 end
100 end)
101 end
102
103 defp check_parsed_data(%{"title" => title} = data)
104 when is_binary(title) and title != "" do
105 {:ok, data}
106 end
107
108 defp check_parsed_data(data) do
109 {:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
110 end
111
112 defp clean_parsed_data(data) do
113 data
114 |> Enum.reject(fn {key, val} ->
115 not match?({:ok, _}, Jason.encode(%{key => val}))
116 end)
117 |> Map.new()
118 end
119 end