From 10a11f083ca543022c61e24b1ec0cc83811d6d06 Mon Sep 17 00:00:00 2001 From: href Date: Tue, 19 Feb 2019 17:39:42 +0100 Subject: [PATCH] Embed player suitable for Twitter Cards --- lib/pleroma/web/metadata/player_view.ex | 21 +++++++++++++++++++ lib/pleroma/web/metadata/twitter_card.ex | 19 +++++++++++------ lib/pleroma/web/ostatus/ostatus_controller.ex | 20 ++++++++++++++++++ lib/pleroma/web/router.ex | 1 + .../templates/layout/metadata_player.html.eex | 16 ++++++++++++++ 5 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 lib/pleroma/web/metadata/player_view.ex create mode 100644 lib/pleroma/web/templates/layout/metadata_player.html.eex diff --git a/lib/pleroma/web/metadata/player_view.ex b/lib/pleroma/web/metadata/player_view.ex new file mode 100644 index 000000000..68b0a3507 --- /dev/null +++ b/lib/pleroma/web/metadata/player_view.ex @@ -0,0 +1,21 @@ +defmodule Pleroma.Web.Metadata.PlayerView do + use Pleroma.Web, :view + import Phoenix.HTML.Tag, only: [content_tag: 3, tag: 2] + + def render("player.html", %{"mediaType" => type, "href" => href}) do + tag_type = + case type do + "audio" <> _ -> :audio + "video" <> _ -> :video + end + + content_tag( + tag_type, + [ + tag(:source, src: href, type: type), + "Your browser does not support #{type} playback." + ], + controls: true + ) + end +end diff --git a/lib/pleroma/web/metadata/twitter_card.ex b/lib/pleroma/web/metadata/twitter_card.ex index 13fa22240..e7f5760a9 100644 --- a/lib/pleroma/web/metadata/twitter_card.ex +++ b/lib/pleroma/web/metadata/twitter_card.ex @@ -12,10 +12,11 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do @impl Provider def build_tags(%{ + activity_id: id, object: object, user: user }) do - attachments = build_attachments(object) + attachments = build_attachments(id, object) scrubbed_content = Utils.scrub_html_and_truncate(object) # Zero width space content = @@ -65,7 +66,9 @@ def build_tags(%{user: user}) do end end - defp build_attachments(%{data: %{"attachment" => attachments}}) do + defp build_attachments(id, z = %{data: %{"attachment" => attachments}}) do + IO.puts(inspect(z)) + Enum.reduce(attachments, [], fn attachment, acc -> rendered_tags = Enum.reduce(attachment["url"], [], fn url, acc -> @@ -79,8 +82,9 @@ defp build_attachments(%{data: %{"attachment" => attachments}}) do "audio" -> [ {:meta, [property: "twitter:card", content: "player"], []}, - {:meta, [property: "twitter:player", content: Utils.attachment_url(url["href"])], - []} + {:meta, [property: "twitter:player:width", content: "480"], []}, + {:meta, [property: "twitter:player:height", content: "80"], []}, + {:meta, [property: "twitter:player", content: player_url(id)], []} | acc ] @@ -99,8 +103,7 @@ defp build_attachments(%{data: %{"attachment" => attachments}}) do "video" -> [ {:meta, [property: "twitter:card", content: "player"], []}, - {:meta, [property: "twitter:player", content: Utils.attachment_url(url["href"])], - []}, + {:meta, [property: "twitter:player", content: player_url(id)], []}, {:meta, [property: "twitter:player:width", content: "1280"], []}, {:meta, [property: "twitter:player:height", content: "720"], []} | acc @@ -114,4 +117,8 @@ defp build_attachments(%{data: %{"attachment" => attachments}}) do acc ++ rendered_tags end) end + + defp player_url(id) do + Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id) + end end diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index db4c8f4da..e7bde28a6 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -153,6 +153,7 @@ def notice(conn, %{"id" => id}) do %Object{} = object = Object.normalize(activity.data["object"]) Fallback.RedirectController.redirector_with_meta(conn, %{ + activity_id: activity.id, object: object, url: Pleroma.Web.Router.Helpers.o_status_url( @@ -184,6 +185,25 @@ def notice(conn, %{"id" => id}) do end end + # Returns an HTML embedded