mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-10 02:05:12 +00:00
Embed player suitable for Twitter Cards
This commit is contained in:
parent
c984e8272a
commit
10a11f083c
21
lib/pleroma/web/metadata/player_view.ex
Normal file
21
lib/pleroma/web/metadata/player_view.ex
Normal file
|
@ -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
|
|
@ -12,10 +12,11 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
|
||||||
|
|
||||||
@impl Provider
|
@impl Provider
|
||||||
def build_tags(%{
|
def build_tags(%{
|
||||||
|
activity_id: id,
|
||||||
object: object,
|
object: object,
|
||||||
user: user
|
user: user
|
||||||
}) do
|
}) do
|
||||||
attachments = build_attachments(object)
|
attachments = build_attachments(id, object)
|
||||||
scrubbed_content = Utils.scrub_html_and_truncate(object)
|
scrubbed_content = Utils.scrub_html_and_truncate(object)
|
||||||
# Zero width space
|
# Zero width space
|
||||||
content =
|
content =
|
||||||
|
@ -65,7 +66,9 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
|
||||||
end
|
end
|
||||||
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 ->
|
Enum.reduce(attachments, [], fn attachment, acc ->
|
||||||
rendered_tags =
|
rendered_tags =
|
||||||
Enum.reduce(attachment["url"], [], fn url, acc ->
|
Enum.reduce(attachment["url"], [], fn url, acc ->
|
||||||
|
@ -79,8 +82,9 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
|
||||||
"audio" ->
|
"audio" ->
|
||||||
[
|
[
|
||||||
{:meta, [property: "twitter:card", content: "player"], []},
|
{: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
|
| acc
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -99,8 +103,7 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
|
||||||
"video" ->
|
"video" ->
|
||||||
[
|
[
|
||||||
{:meta, [property: "twitter:card", content: "player"], []},
|
{: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:width", content: "1280"], []},
|
||||||
{:meta, [property: "twitter:player:height", content: "720"], []}
|
{:meta, [property: "twitter:player:height", content: "720"], []}
|
||||||
| acc
|
| acc
|
||||||
|
@ -114,4 +117,8 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
|
||||||
acc ++ rendered_tags
|
acc ++ rendered_tags
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp player_url(id) do
|
||||||
|
Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -153,6 +153,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|
||||||
%Object{} = object = Object.normalize(activity.data["object"])
|
%Object{} = object = Object.normalize(activity.data["object"])
|
||||||
|
|
||||||
Fallback.RedirectController.redirector_with_meta(conn, %{
|
Fallback.RedirectController.redirector_with_meta(conn, %{
|
||||||
|
activity_id: activity.id,
|
||||||
object: object,
|
object: object,
|
||||||
url:
|
url:
|
||||||
Pleroma.Web.Router.Helpers.o_status_url(
|
Pleroma.Web.Router.Helpers.o_status_url(
|
||||||
|
@ -184,6 +185,25 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns an HTML embedded <audio> or <video> player suitable for embed iframes.
|
||||||
|
def notice_player(conn, %{"id" => id}) do
|
||||||
|
with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(id),
|
||||||
|
true <- ActivityPub.is_public?(activity),
|
||||||
|
%Object{} = object <- Object.normalize(activity.data["object"]),
|
||||||
|
%{data: %{"attachment" => [%{"url" => [url | _]} | _]}} <- object,
|
||||||
|
true <- String.starts_with?(url["mediaType"], ["audio", "video"]) do
|
||||||
|
conn
|
||||||
|
|> put_layout(:metadata_player)
|
||||||
|
|> put_view(Pleroma.Web.Metadata.PlayerView)
|
||||||
|
|> render("player.html", url)
|
||||||
|
else
|
||||||
|
_error ->
|
||||||
|
conn
|
||||||
|
|> put_status(404)
|
||||||
|
|> Fallback.RedirectController.redirector(nil, 404)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp represent_activity(
|
defp represent_activity(
|
||||||
conn,
|
conn,
|
||||||
"activity+json",
|
"activity+json",
|
||||||
|
|
|
@ -409,6 +409,7 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/objects/:uuid", OStatus.OStatusController, :object)
|
get("/objects/:uuid", OStatus.OStatusController, :object)
|
||||||
get("/activities/:uuid", OStatus.OStatusController, :activity)
|
get("/activities/:uuid", OStatus.OStatusController, :activity)
|
||||||
get("/notice/:id", OStatus.OStatusController, :notice)
|
get("/notice/:id", OStatus.OStatusController, :notice)
|
||||||
|
get("/notice/:id/embed_player", OStatus.OStatusController, :notice_player)
|
||||||
get("/users/:nickname/feed", OStatus.OStatusController, :feed)
|
get("/users/:nickname/feed", OStatus.OStatusController, :feed)
|
||||||
get("/users/:nickname", OStatus.OStatusController, :feed_redirect)
|
get("/users/:nickname", OStatus.OStatusController, :feed_redirect)
|
||||||
|
|
||||||
|
|
16
lib/pleroma/web/templates/layout/metadata_player.html.eex
Normal file
16
lib/pleroma/web/templates/layout/metadata_player.html.eex
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<style type="text/css">
|
||||||
|
video {
|
||||||
|
width:100%;
|
||||||
|
max-width:600px;
|
||||||
|
height:auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<%= render @view_module, @view_template, assigns %>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue