From 410fd9d774fb3437a38adfe405ff45d4950b51a9 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Tue, 15 Jan 2019 21:17:56 +0300 Subject: [PATCH] Attach attachments --- lib/pleroma/activity.ex | 3 ++- lib/pleroma/web/metadata/opengraph.ex | 36 ++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 47562306d..8fd0311d2 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -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 diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex index 6d86c0ee6..2eac04ae7 100644 --- a/lib/pleroma/web/metadata/opengraph.ex +++ b/lib/pleroma/web/metadata/opengraph.ex @@ -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