Merge branch 'features/mastoapi-multi-hashtag' into 'develop'
[akkoma] / lib / pleroma / web / rich_media / parser.ex
index b88ed537103493eaa79075ec5ce6408240034054..947dc0c3c2fef989d334f431cc4c9f29c2c9275a 100644 (file)
@@ -1,14 +1,29 @@
 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
-    Cachex.fetch!(:rich_media_cache, url, fn _ ->
-      {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
+  def parse(nil), do: {:error, "No URL provided"}
 
-      result = html |> maybe_parse() |> get_parsed_data()
+  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
 
-      {:commit, result}
-    end)
+  defp parse_url(url) do
+    {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
+
+    html |> maybe_parse() |> get_parsed_data()
   end
 
   defp maybe_parse(html) do