logo

pleroma

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

open_graph.ex (5066B)


  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. @impl Provider
  65. def build_tags(_), do: []
  66. defp build_attachments(%{data: %{"attachment" => attachments}}) do
  67. Enum.reduce(attachments, [], fn attachment, acc ->
  68. rendered_tags =
  69. Enum.reduce(attachment["url"], [], fn url, acc ->
  70. # TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
  71. # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
  72. case Utils.fetch_media_type(@media_types, url["mediaType"]) do
  73. "audio" ->
  74. acc ++
  75. [
  76. {:meta, [property: "og:audio", content: MediaProxy.url(url["href"])], []}
  77. ]
  78. # Not using preview_url for this. It saves bandwidth, but the image dimensions will
  79. # be wrong. We generate it on the fly and have no way to capture or analyze the
  80. # image to get the dimensions. This can be an issue for apps/FEs rendering images
  81. # in timelines too, but you can get clever with the aspect ratio metadata as a
  82. # workaround.
  83. "image" ->
  84. (acc ++
  85. [
  86. {:meta, [property: "og:image", content: MediaProxy.url(url["href"])], []},
  87. {:meta, [property: "og:image:alt", content: attachment["name"]], []}
  88. ])
  89. |> maybe_add_dimensions(url)
  90. "video" ->
  91. (acc ++
  92. [
  93. {:meta, [property: "og:video", content: MediaProxy.url(url["href"])], []}
  94. ])
  95. |> maybe_add_dimensions(url)
  96. |> maybe_add_video_thumbnail(url)
  97. _ ->
  98. acc
  99. end
  100. end)
  101. acc ++ rendered_tags
  102. end)
  103. end
  104. defp build_attachments(_), do: []
  105. # We can use url["mediaType"] to dynamically fill the metadata
  106. defp maybe_add_dimensions(metadata, url) do
  107. type = url["mediaType"] |> String.split("/") |> List.first()
  108. cond do
  109. !is_nil(url["height"]) && !is_nil(url["width"]) ->
  110. metadata ++
  111. [
  112. {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
  113. {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
  114. ]
  115. true ->
  116. metadata
  117. end
  118. end
  119. # Media Preview Proxy makes thumbnails of videos without resizing, so we can trust the
  120. # width and height of the source video.
  121. defp maybe_add_video_thumbnail(metadata, url) do
  122. cond do
  123. Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
  124. metadata ++
  125. [
  126. {:meta, [property: "og:image:width", content: "#{url["width"]}"], []},
  127. {:meta, [property: "og:image:height", content: "#{url["height"]}"], []},
  128. {:meta, [property: "og:image", content: MediaProxy.preview_url(url["href"])], []}
  129. ]
  130. true ->
  131. metadata
  132. end
  133. end
  134. end