logo

pleroma

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

open_graph_test.exs (6455B)


  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.OpenGraphTest do
  5. use Pleroma.DataCase
  6. import Mox
  7. import Pleroma.Factory
  8. alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  9. alias Pleroma.Web.Metadata.Providers.OpenGraph
  10. setup do
  11. ConfigMock
  12. |> stub_with(Pleroma.Test.StaticConfig)
  13. :ok
  14. end
  15. setup do: clear_config([Pleroma.Web.Metadata, :unfurl_nsfw])
  16. test "it renders all supported types of attachments and skips unknown types" do
  17. user = insert(:user)
  18. note =
  19. insert(:note, %{
  20. data: %{
  21. "actor" => user.ap_id,
  22. "tag" => [],
  23. "id" => "https://pleroma.gov/objects/whatever",
  24. "content" => "pleroma in a nutshell",
  25. "attachment" => [
  26. %{
  27. "url" => [
  28. %{
  29. "mediaType" => "image/png",
  30. "href" => "https://pleroma.gov/tenshi.png",
  31. "height" => 1024,
  32. "width" => 1280
  33. }
  34. ]
  35. },
  36. %{
  37. "url" => [
  38. %{
  39. "mediaType" => "application/octet-stream",
  40. "href" => "https://pleroma.gov/fqa/badapple.sfc"
  41. }
  42. ]
  43. },
  44. %{
  45. "url" => [
  46. %{
  47. "mediaType" => "video/webm",
  48. "href" => "https://pleroma.gov/about/juche.webm",
  49. "height" => 600,
  50. "width" => 800
  51. }
  52. ]
  53. },
  54. %{
  55. "url" => [
  56. %{
  57. "mediaType" => "audio/basic",
  58. "href" => "http://www.gnu.org/music/free-software-song.au"
  59. }
  60. ]
  61. }
  62. ]
  63. }
  64. })
  65. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  66. assert Enum.all?(
  67. [
  68. {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []},
  69. {:meta, [property: "og:image:width", content: "1280"], []},
  70. {:meta, [property: "og:image:height", content: "1024"], []},
  71. {:meta,
  72. [property: "og:audio", content: "http://www.gnu.org/music/free-software-song.au"],
  73. []},
  74. {:meta, [property: "og:video", content: "https://pleroma.gov/about/juche.webm"],
  75. []},
  76. {:meta, [property: "og:video:width", content: "800"], []},
  77. {:meta, [property: "og:video:height", content: "600"], []}
  78. ],
  79. fn element -> element in result end
  80. )
  81. end
  82. test "it does not render attachments if post is nsfw" do
  83. clear_config([Pleroma.Web.Metadata, :unfurl_nsfw], false)
  84. user = insert(:user, avatar: %{"url" => [%{"href" => "https://pleroma.gov/tenshi.png"}]})
  85. note =
  86. insert(:note, %{
  87. data: %{
  88. "actor" => user.ap_id,
  89. "id" => "https://pleroma.gov/objects/whatever",
  90. "content" => "#cuteposting #nsfw #hambaga",
  91. "tag" => ["cuteposting", "nsfw", "hambaga"],
  92. "sensitive" => true,
  93. "attachment" => [
  94. %{
  95. "url" => [
  96. %{"mediaType" => "image/png", "href" => "https://misskey.microsoft/corndog.png"}
  97. ]
  98. }
  99. ]
  100. }
  101. })
  102. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  103. assert {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []} in result
  104. refute {:meta, [property: "og:image", content: "https://misskey.microsoft/corndog.png"], []} in result
  105. end
  106. test "video attachments have image thumbnail with WxH metadata with Preview Proxy enabled" do
  107. clear_config([:media_proxy, :enabled], true)
  108. clear_config([:media_preview_proxy, :enabled], true)
  109. user = insert(:user)
  110. note =
  111. insert(:note, %{
  112. data: %{
  113. "actor" => user.ap_id,
  114. "id" => "https://pleroma.gov/objects/whatever",
  115. "content" => "test video post",
  116. "sensitive" => false,
  117. "attachment" => [
  118. %{
  119. "url" => [
  120. %{
  121. "mediaType" => "video/webm",
  122. "href" => "https://pleroma.gov/about/juche.webm",
  123. "height" => 600,
  124. "width" => 800
  125. }
  126. ]
  127. }
  128. ]
  129. }
  130. })
  131. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  132. assert {:meta, [property: "og:image:width", content: "800"], []} in result
  133. assert {:meta, [property: "og:image:height", content: "600"], []} in result
  134. assert {:meta,
  135. [
  136. property: "og:image",
  137. content:
  138. "http://localhost:4001/proxy/preview/LzAnlke-l5oZbNzWsrHfprX1rGw/aHR0cHM6Ly9wbGVyb21hLmdvdi9hYm91dC9qdWNoZS53ZWJt/juche.webm"
  139. ], []} in result
  140. end
  141. test "video attachments have no image thumbnail with Preview Proxy disabled" do
  142. clear_config([:media_proxy, :enabled], true)
  143. clear_config([:media_preview_proxy, :enabled], false)
  144. user = insert(:user)
  145. note =
  146. insert(:note, %{
  147. data: %{
  148. "actor" => user.ap_id,
  149. "id" => "https://pleroma.gov/objects/whatever",
  150. "content" => "test video post",
  151. "sensitive" => false,
  152. "attachment" => [
  153. %{
  154. "url" => [
  155. %{
  156. "mediaType" => "video/webm",
  157. "href" => "https://pleroma.gov/about/juche.webm",
  158. "height" => 600,
  159. "width" => 800
  160. }
  161. ]
  162. }
  163. ]
  164. }
  165. })
  166. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  167. refute {:meta, [property: "og:image:width", content: "800"], []} in result
  168. refute {:meta, [property: "og:image:height", content: "600"], []} in result
  169. refute {:meta,
  170. [
  171. property: "og:image",
  172. content:
  173. "http://localhost:4001/proxy/preview/LzAnlke-l5oZbNzWsrHfprX1rGw/aHR0cHM6Ly9wbGVyb21hLmdvdi9hYm91dC9qdWNoZS53ZWJt/juche.webm"
  174. ], []} in result
  175. end
  176. end