logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

open_graph.ex (4992B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Metadata.Providers.OpenGraph 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(%{
  14. object: object,
  15. url: url,
  16. user: user
  17. }) do
  18. attachments = build_attachments(object)
  19. scrubbed_content = Utils.scrub_html_and_truncate(object)
  20. [
  21. {:meta,
  22. [
  23. property: "og:title",
  24. content: Utils.user_name_string(user)
  25. ], []},
  26. {:meta, [property: "og:url", content: url], []},
  27. {:meta,
  28. [
  29. property: "og:description",
  30. content: scrubbed_content
  31. ], []},
  32. {:meta, [property: "og:type", content: "article"], []}
  33. ] ++
  34. if attachments == [] or Metadata.activity_nsfw?(object) do
  35. [
  36. {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
  37. []},
  38. {:meta, [property: "og:image:width", content: 150], []},
  39. {:meta, [property: "og:image:height", content: 150], []}
  40. ]
  41. else
  42. attachments
  43. end
  44. end
  45. @impl Provider
  46. def build_tags(%{user: user}) do
  47. with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
  48. [
  49. {:meta,
  50. [
  51. property: "og:title",
  52. content: Utils.user_name_string(user)
  53. ], []},
  54. {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
  55. {:meta, [property: "og:description", content: truncated_bio], []},
  56. {:meta, [property: "og:type", content: "article"], []},
  57. {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
  58. []},
  59. {:meta, [property: "og:image:width", content: 150], []},
  60. {:meta, [property: "og:image:height", content: 150], []}
  61. ]
  62. end
  63. end
  64. defp build_attachments(%{data: %{"attachment" => attachments}}) do
  65. Enum.reduce(attachments, [], fn attachment, acc ->
  66. rendered_tags =
  67. Enum.reduce(attachment["url"], [], fn url, acc ->
  68. # TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
  69. # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
  70. case Utils.fetch_media_type(@media_types, url["mediaType"]) do
  71. "audio" ->
  72. [
  73. {:meta, [property: "og:audio", content: MediaProxy.url(url["href"])], []}
  74. | acc
  75. ]
  76. # Not using preview_url for this. It saves bandwidth, but the image dimensions will
  77. # be wrong. We generate it on the fly and have no way to capture or analyze the
  78. # image to get the dimensions. This can be an issue for apps/FEs rendering images
  79. # in timelines too, but you can get clever with the aspect ratio metadata as a
  80. # workaround.
  81. "image" ->
  82. [
  83. {:meta, [property: "og:image", content: MediaProxy.url(url["href"])], []},
  84. {:meta, [property: "og:image:alt", content: attachment["name"]], []}
  85. | acc
  86. ]
  87. |> maybe_add_dimensions(url)
  88. "video" ->
  89. [
  90. {:meta, [property: "og:video", content: MediaProxy.url(url["href"])], []}
  91. | acc
  92. ]
  93. |> maybe_add_dimensions(url)
  94. |> maybe_add_video_thumbnail(url)
  95. _ ->
  96. acc
  97. end
  98. end)
  99. acc ++ rendered_tags
  100. end)
  101. end
  102. defp build_attachments(_), do: []
  103. # We can use url["mediaType"] to dynamically fill the metadata
  104. defp maybe_add_dimensions(metadata, url) do
  105. type = url["mediaType"] |> String.split("/") |> List.first()
  106. cond do
  107. !is_nil(url["height"]) && !is_nil(url["width"]) ->
  108. metadata ++
  109. [
  110. {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
  111. {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
  112. ]
  113. true ->
  114. metadata
  115. end
  116. end
  117. # Media Preview Proxy makes thumbnails of videos without resizing, so we can trust the
  118. # width and height of the source video.
  119. defp maybe_add_video_thumbnail(metadata, url) do
  120. cond do
  121. Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
  122. metadata ++
  123. [
  124. {:meta, [property: "og:image:width", content: "#{url["width"]}"], []},
  125. {:meta, [property: "og:image:height", content: "#{url["height"]}"], []},
  126. {:meta, [property: "og:image", content: MediaProxy.preview_url(url["href"])], []}
  127. ]
  128. true ->
  129. metadata
  130. end
  131. end
  132. end