Attach attachments
authorrinpatch <rinpatch@sdf.org>
Tue, 15 Jan 2019 18:17:56 +0000 (21:17 +0300)
committerrinpatch <rinpatch@sdf.org>
Tue, 15 Jan 2019 18:17:56 +0000 (21:17 +0300)
lib/pleroma/activity.ex
lib/pleroma/web/metadata/opengraph.ex

index 47562306d136a76fc10917926025ac4c015fbee5..8fd0311d2c25a78fd1259c58e17acbbff64c90bc 100644 (file)
@@ -35,10 +35,11 @@ defmodule Pleroma.Activity do
       )
     )
   end
-  
+
   def get_by_id(id) do
     Repo.get(Activity, id)
   end
+
   # TODO:
   # Go through these and fix them everywhere.
   # Wrong name, only returns create activities
index 6d86c0ee6d5720d7b89ada56bf4b4eac9cd4954b..2eac04ae77699a0421742e4841ec0dbca43d11b1 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
   @impl Provider
   def build_tags(%{activity: activity, user: user}) do
     with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
+      attachments = build_attachments(activity)
       [
         {:meta,
          [
@@ -16,11 +17,11 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
          ], []},
         {:meta, [property: "og:url", content: activity.data["id"]], []},
         {:meta, [property: "og:description", content: truncated_content], []},
-        {:meta, [property: "og:image", content: user_avatar_url(user)], []},
-        {:meta, [property: "og:image:width", content: 120], []},
-        {:meta, [property: "og:image:height", content: 120], []},
         {:meta, [property: "twitter:card", content: "summary"], []}
-      ]
+      ] ++ if attachments == [] do [
+        {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
+        {:meta, [property: "og:image:width", content: 120], []},
+        {:meta, [property: "og:image:height", content: 120], []} ] else attachments end
     end
   end
 
@@ -35,7 +36,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
          ], []},
         {:meta, [property: "og:url", content: User.profile_url(user)], []},
         {:meta, [property: "og:description", content: truncated_bio], []},
-        {:meta, [property: "og:image", content: user_avatar_url(user)], []},
+        {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
         {:meta, [property: "og:image:width", content: 120], []},
         {:meta, [property: "og:image:height", content: 120], []},
         {:meta, [property: "twitter:card", content: "summary"], []}
@@ -43,6 +44,27 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
     end
   end
 
+  defp build_attachments(activity) do
+    Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc ->
+      rendered_tags =
+        Enum.map(attachment["url"], fn url ->
+          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"])], []}
+          else
+            nil
+          end
+        end)
+
+      Enum.reject(rendered_tags, &is_nil/1)
+      acc ++ rendered_tags
+    end)
+  end
+
   defp scrub_html_and_truncate(content) do
     content
     # html content comes from DB already encoded, decode first and scrub after
@@ -52,8 +74,8 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
     |> Formatter.truncate()
   end
 
-  defp user_avatar_url(user) do
-    User.avatar_url(user) |> MediaProxy.url()
+  defp attachment_url(url) do
+    MediaProxy.url(url)
   end
 
   defp user_name_string(user) do