logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

twitter_card.ex (3643B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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.Metadata
  7. alias Pleroma.Web.Metadata.Providers.Provider
  8. alias Pleroma.Web.Metadata.Utils
  9. @behaviour Provider
  10. @media_types ["image", "audio", "video"]
  11. @impl Provider
  12. def build_tags(%{activity_id: id, object: object, user: user}) do
  13. attachments = build_attachments(id, object)
  14. scrubbed_content = Utils.scrub_html_and_truncate(object)
  15. # Zero width space
  16. content =
  17. if scrubbed_content != "" and scrubbed_content != "\u200B" do
  18. "“" <> scrubbed_content <> "”"
  19. else
  20. ""
  21. end
  22. [
  23. title_tag(user),
  24. {:meta, [property: "twitter:description", content: content], []}
  25. ] ++
  26. if attachments == [] or Metadata.activity_nsfw?(object) do
  27. [
  28. image_tag(user),
  29. {:meta, [property: "twitter:card", content: "summary"], []}
  30. ]
  31. else
  32. attachments
  33. end
  34. end
  35. @impl Provider
  36. def build_tags(%{user: user}) do
  37. with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
  38. [
  39. title_tag(user),
  40. {:meta, [property: "twitter:description", content: truncated_bio], []},
  41. image_tag(user),
  42. {:meta, [property: "twitter:card", content: "summary"], []}
  43. ]
  44. end
  45. end
  46. defp title_tag(user) do
  47. {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
  48. end
  49. def image_tag(user) do
  50. {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []}
  51. end
  52. defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
  53. Enum.reduce(attachments, [], fn attachment, acc ->
  54. rendered_tags =
  55. Enum.reduce(attachment["url"], [], fn url, acc ->
  56. # TODO: Add additional properties to objects when we have the data available.
  57. case Utils.fetch_media_type(@media_types, url["mediaType"]) do
  58. "audio" ->
  59. [
  60. {:meta, [property: "twitter:card", content: "player"], []},
  61. {:meta, [property: "twitter:player:width", content: "480"], []},
  62. {:meta, [property: "twitter:player:height", content: "80"], []},
  63. {:meta, [property: "twitter:player", content: player_url(id)], []}
  64. | acc
  65. ]
  66. "image" ->
  67. [
  68. {:meta, [property: "twitter:card", content: "summary_large_image"], []},
  69. {:meta,
  70. [
  71. property: "twitter:player",
  72. content: Utils.attachment_url(url["href"])
  73. ], []}
  74. | acc
  75. ]
  76. # TODO: Need the true width and height values here or Twitter renders an iFrame with
  77. # a bad aspect ratio
  78. "video" ->
  79. [
  80. {:meta, [property: "twitter:card", content: "player"], []},
  81. {:meta, [property: "twitter:player", content: player_url(id)], []},
  82. {:meta, [property: "twitter:player:width", content: "480"], []},
  83. {:meta, [property: "twitter:player:height", content: "480"], []}
  84. | acc
  85. ]
  86. _ ->
  87. acc
  88. end
  89. end)
  90. acc ++ rendered_tags
  91. end)
  92. end
  93. defp build_attachments(_id, _object), do: []
  94. defp player_url(id) do
  95. Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
  96. end
  97. end