Replace map with reduce to remove nils
authorrinpatch <rinpatch@sdf.org>
Wed, 16 Jan 2019 13:52:01 +0000 (16:52 +0300)
committerrinpatch <rinpatch@sdf.org>
Wed, 16 Jan 2019 13:52:01 +0000 (16:52 +0300)
lib/pleroma/web/metadata/opengraph.ex

index b158569748d5559da9833e10201d59728a35f63f..f6a1f302316acd2a3cf0cf145d44f35def7c22a5 100644 (file)
@@ -50,23 +50,25 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
     end
   end
 
-  defp build_attachments(activity) do
-    Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc ->
+  defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do
+    Enum.reduce(attachments, [], fn attachment, acc ->
       rendered_tags =
-        Enum.map(attachment["url"], fn url ->
+        Enum.reduce(attachment["url"], [], fn url, acc ->
           media_type =
             Enum.find(["image", "audio", "video"], fn media_type ->
               String.starts_with?(url["mediaType"], media_type)
             end)
 
           if media_type do
-            {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
+            [
+              {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
+              | acc
+            ]
           else
-            nil
+            acc
           end
         end)
 
-      Enum.reject(rendered_tags, &is_nil/1)
       acc ++ rendered_tags
     end)
   end