Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

133 lignes
4.5KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
  5. alias Pleroma.User
  6. alias Pleroma.Web.MediaProxy
  7. alias Pleroma.Web.Metadata
  8. alias Pleroma.Web.Metadata.Providers.Provider
  9. alias Pleroma.Web.Metadata.Utils
  10. @behaviour Provider
  11. @media_types ["image", "audio", "video"]
  12. @impl Provider
  13. def build_tags(%{activity_id: id, object: object, user: user}) do
  14. attachments = build_attachments(id, object)
  15. scrubbed_content = Utils.scrub_html_and_truncate(object)
  16. [
  17. title_tag(user),
  18. {:meta, [property: "twitter:description", content: scrubbed_content], []}
  19. ] ++
  20. if attachments == [] or Metadata.activity_nsfw?(object) do
  21. [
  22. image_tag(user),
  23. {:meta, [property: "twitter:card", content: "summary"], []}
  24. ]
  25. else
  26. attachments
  27. end
  28. end
  29. @impl Provider
  30. def build_tags(%{user: user}) do
  31. with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
  32. [
  33. title_tag(user),
  34. {:meta, [property: "twitter:description", content: truncated_bio], []},
  35. image_tag(user),
  36. {:meta, [property: "twitter:card", content: "summary"], []}
  37. ]
  38. end
  39. end
  40. defp title_tag(user) do
  41. {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
  42. end
  43. def image_tag(user) do
  44. {:meta, [property: "twitter:image", content: MediaProxy.preview_url(User.avatar_url(user))],
  45. []}
  46. end
  47. defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
  48. Enum.reduce(attachments, [], fn attachment, acc ->
  49. rendered_tags =
  50. Enum.reduce(attachment["url"], [], fn url, acc ->
  51. case Utils.fetch_media_type(@media_types, url["mediaType"]) do
  52. "audio" ->
  53. [
  54. {:meta, [property: "twitter:card", content: "player"], []},
  55. {:meta, [property: "twitter:player:width", content: "480"], []},
  56. {:meta, [property: "twitter:player:height", content: "80"], []},
  57. {:meta, [property: "twitter:player", content: player_url(id)], []}
  58. | acc
  59. ]
  60. # Not using preview_url for this. It saves bandwidth, but the image dimensions will be wrong.
  61. # We generate it on the fly and have no way to capture or analyze the image to get the dimensions.
  62. # This can be an issue for apps/FEs rendering images in timelines too, but you can get clever with
  63. # the aspect ratio metadata as a workaround.
  64. "image" ->
  65. [
  66. {:meta, [property: "twitter:card", content: "summary_large_image"], []},
  67. {:meta,
  68. [
  69. property: "twitter:player",
  70. content: MediaProxy.url(url["href"])
  71. ], []}
  72. | acc
  73. ]
  74. |> maybe_add_dimensions(url)
  75. "video" ->
  76. # fallback to old placeholder values
  77. height = url["height"] || 480
  78. width = url["width"] || 480
  79. [
  80. {:meta, [property: "twitter:card", content: "player"], []},
  81. {:meta, [property: "twitter:player", content: player_url(id)], []},
  82. {:meta, [property: "twitter:player:width", content: "#{width}"], []},
  83. {:meta, [property: "twitter:player:height", content: "#{height}"], []},
  84. {:meta, [property: "twitter:player:stream", content: MediaProxy.url(url["href"])], []},
  85. {:meta,
  86. [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
  87. | acc
  88. ]
  89. _ ->
  90. acc
  91. end
  92. end)
  93. acc ++ rendered_tags
  94. end)
  95. end
  96. defp build_attachments(_id, _object), do: []
  97. defp player_url(id) do
  98. Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
  99. end
  100. # Videos have problems without dimensions, but we used to not provide WxH for images.
  101. # A default (read: incorrect) fallback for images is likely to cause rendering bugs.
  102. defp maybe_add_dimensions(metadata, url) do
  103. cond do
  104. !is_nil(url["height"]) && !is_nil(url["width"]) ->
  105. metadata ++
  106. [
  107. {:meta, [property: "twitter:player:width", content: "#{url["width"]}"], []},
  108. {:meta, [property: "twitter:player:height", content: "#{url["height"]}"], []}
  109. ]
  110. true ->
  111. metadata
  112. end
  113. end
  114. end