logo

pleroma

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

open_graph_test.exs (8580B)


  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. alias Pleroma.Web.Metadata.Utils
  11. setup do
  12. ConfigMock
  13. |> stub_with(Pleroma.Test.StaticConfig)
  14. :ok
  15. end
  16. setup do: clear_config([Pleroma.Web.Metadata, :unfurl_nsfw])
  17. test "it renders all supported types of attachments and skips unknown types" do
  18. user = insert(:user)
  19. note =
  20. insert(:note, %{
  21. data: %{
  22. "actor" => user.ap_id,
  23. "tag" => [],
  24. "id" => "https://pleroma.gov/objects/whatever",
  25. "content" => "pleroma in a nutshell",
  26. "attachment" => [
  27. %{
  28. "url" => [
  29. %{
  30. "mediaType" => "image/png",
  31. "href" => "https://pleroma.gov/tenshi.png",
  32. "height" => 1024,
  33. "width" => 1280
  34. }
  35. ]
  36. },
  37. %{
  38. "url" => [
  39. %{
  40. "mediaType" => "application/octet-stream",
  41. "href" => "https://pleroma.gov/fqa/badapple.sfc"
  42. }
  43. ]
  44. },
  45. %{
  46. "url" => [
  47. %{
  48. "mediaType" => "video/webm",
  49. "href" => "https://pleroma.gov/about/juche.webm",
  50. "height" => 600,
  51. "width" => 800
  52. }
  53. ]
  54. },
  55. %{
  56. "url" => [
  57. %{
  58. "mediaType" => "audio/basic",
  59. "href" => "http://www.gnu.org/music/free-software-song.au"
  60. }
  61. ]
  62. }
  63. ]
  64. }
  65. })
  66. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  67. assert Enum.all?(
  68. [
  69. {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []},
  70. {:meta, [property: "og:image:width", content: "1280"], []},
  71. {:meta, [property: "og:image:height", content: "1024"], []},
  72. {:meta,
  73. [property: "og:audio", content: "http://www.gnu.org/music/free-software-song.au"],
  74. []},
  75. {:meta, [property: "og:video", content: "https://pleroma.gov/about/juche.webm"],
  76. []},
  77. {:meta, [property: "og:video:width", content: "800"], []},
  78. {:meta, [property: "og:video:height", content: "600"], []}
  79. ],
  80. fn element -> element in result end
  81. )
  82. end
  83. test "it does not render attachments if post is nsfw" do
  84. clear_config([Pleroma.Web.Metadata, :unfurl_nsfw], false)
  85. user = insert(:user, avatar: %{"url" => [%{"href" => "https://pleroma.gov/tenshi.png"}]})
  86. note =
  87. insert(:note, %{
  88. data: %{
  89. "actor" => user.ap_id,
  90. "id" => "https://pleroma.gov/objects/whatever",
  91. "content" => "#cuteposting #nsfw #hambaga",
  92. "tag" => ["cuteposting", "nsfw", "hambaga"],
  93. "sensitive" => true,
  94. "attachment" => [
  95. %{
  96. "url" => [
  97. %{"mediaType" => "image/png", "href" => "https://misskey.microsoft/corndog.png"}
  98. ]
  99. }
  100. ]
  101. }
  102. })
  103. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  104. assert {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []} in result
  105. refute {:meta, [property: "og:image", content: "https://misskey.microsoft/corndog.png"], []} in result
  106. end
  107. test "video attachments have image thumbnail with WxH metadata with Preview Proxy enabled" do
  108. clear_config([:media_proxy, :enabled], true)
  109. clear_config([:media_preview_proxy, :enabled], true)
  110. user = insert(:user)
  111. note =
  112. insert(:note, %{
  113. data: %{
  114. "actor" => user.ap_id,
  115. "id" => "https://pleroma.gov/objects/whatever",
  116. "content" => "test video post",
  117. "sensitive" => false,
  118. "attachment" => [
  119. %{
  120. "url" => [
  121. %{
  122. "mediaType" => "video/webm",
  123. "href" => "https://pleroma.gov/about/juche.webm",
  124. "height" => 600,
  125. "width" => 800
  126. }
  127. ]
  128. }
  129. ]
  130. }
  131. })
  132. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  133. assert {:meta, [property: "og:image:width", content: "800"], []} in result
  134. assert {:meta, [property: "og:image:height", content: "600"], []} in result
  135. assert {:meta,
  136. [
  137. property: "og:image",
  138. content:
  139. "http://localhost:4001/proxy/preview/LzAnlke-l5oZbNzWsrHfprX1rGw/aHR0cHM6Ly9wbGVyb21hLmdvdi9hYm91dC9qdWNoZS53ZWJt/juche.webm"
  140. ], []} in result
  141. end
  142. test "video attachments have no image thumbnail with Preview Proxy disabled" do
  143. clear_config([:media_proxy, :enabled], true)
  144. clear_config([:media_preview_proxy, :enabled], false)
  145. user = insert(:user)
  146. note =
  147. insert(:note, %{
  148. data: %{
  149. "actor" => user.ap_id,
  150. "id" => "https://pleroma.gov/objects/whatever",
  151. "content" => "test video post",
  152. "sensitive" => false,
  153. "attachment" => [
  154. %{
  155. "url" => [
  156. %{
  157. "mediaType" => "video/webm",
  158. "href" => "https://pleroma.gov/about/juche.webm",
  159. "height" => 600,
  160. "width" => 800
  161. }
  162. ]
  163. }
  164. ]
  165. }
  166. })
  167. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  168. refute {:meta, [property: "og:image:width", content: "800"], []} in result
  169. refute {:meta, [property: "og:image:height", content: "600"], []} in result
  170. refute {:meta,
  171. [
  172. property: "og:image",
  173. content:
  174. "http://localhost:4001/proxy/preview/LzAnlke-l5oZbNzWsrHfprX1rGw/aHR0cHM6Ly9wbGVyb21hLmdvdi9hYm91dC9qdWNoZS53ZWJt/juche.webm"
  175. ], []} in result
  176. end
  177. test "meta tag ordering matches attachment order" do
  178. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  179. note =
  180. insert(:note, %{
  181. data: %{
  182. "actor" => user.ap_id,
  183. "tag" => [],
  184. "id" => "https://pleroma.gov/objects/whatever",
  185. "summary" => "",
  186. "content" => "pleroma in a nutshell",
  187. "attachment" => [
  188. %{
  189. "url" => [
  190. %{
  191. "mediaType" => "image/png",
  192. "href" => "https://example.com/first.png",
  193. "height" => 1024,
  194. "width" => 1280
  195. }
  196. ]
  197. },
  198. %{
  199. "url" => [
  200. %{
  201. "mediaType" => "image/png",
  202. "href" => "https://example.com/second.png",
  203. "height" => 1024,
  204. "width" => 1280
  205. }
  206. ]
  207. }
  208. ]
  209. }
  210. })
  211. result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
  212. assert [
  213. {:meta, [property: "og:title", content: Utils.user_name_string(user)], []},
  214. {:meta, [property: "og:url", content: "https://pleroma.gov/objects/whatever"], []},
  215. {:meta, [property: "og:description", content: "pleroma in a nutshell"], []},
  216. {:meta, [property: "og:type", content: "article"], []},
  217. {:meta, [property: "og:image", content: "https://example.com/first.png"], []},
  218. {:meta, [property: "og:image:alt", content: nil], []},
  219. {:meta, [property: "og:image:width", content: "1280"], []},
  220. {:meta, [property: "og:image:height", content: "1024"], []},
  221. {:meta, [property: "og:image", content: "https://example.com/second.png"], []},
  222. {:meta, [property: "og:image:alt", content: nil], []},
  223. {:meta, [property: "og:image:width", content: "1280"], []},
  224. {:meta, [property: "og:image:height", content: "1024"], []}
  225. ] == result
  226. end
  227. end