Merge branch 'features/mastoapi-multi-hashtag' into 'develop'
[akkoma] / lib / pleroma / web / rich_media / parser.ex
index 477a381964b0a6f6da1dc776af119a97997f4a72..947dc0c3c2fef989d334f431cc4c9f29c2c9275a 100644 (file)
@@ -1,7 +1,26 @@
 defmodule Pleroma.Web.RichMedia.Parser do
-  @parsers [Pleroma.Web.RichMedia.Parsers.OGP]
+  @parsers [
+    Pleroma.Web.RichMedia.Parsers.OGP,
+    Pleroma.Web.RichMedia.Parsers.TwitterCard,
+    Pleroma.Web.RichMedia.Parsers.OEmbed
+  ]
 
-  def parse(url) do
+  def parse(nil), do: {:error, "No URL provided"}
+
+  if Mix.env() == :test do
+    def parse(url), do: parse_url(url)
+  else
+    def parse(url) do
+      with {:ok, data} <- Cachex.fetch(:rich_media_cache, url, fn _ -> parse_url(url) end) do
+        data
+      else
+        _e ->
+          {:error, "Parsing error"}
+      end
+    end
+  end
+
+  defp parse_url(url) do
     {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
 
     html |> maybe_parse() |> get_parsed_data()