logo

pleroma

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

note_handling_test.exs (28189B)


  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.ActivityPub.Transmogrifier.NoteHandlingTest do
  5. use Oban.Testing, repo: Pleroma.Repo
  6. use Pleroma.DataCase
  7. alias Pleroma.Activity
  8. alias Pleroma.Object
  9. alias Pleroma.User
  10. alias Pleroma.Web.ActivityPub.Transmogrifier
  11. alias Pleroma.Web.ActivityPub.Utils
  12. alias Pleroma.Web.CommonAPI
  13. import Mock
  14. import Pleroma.Factory
  15. setup_all do
  16. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  17. :ok
  18. end
  19. setup do: clear_config([:instance, :max_remote_account_fields])
  20. describe "handle_incoming" do
  21. test "it works for incoming notices with tag not being an array (kroeg)" do
  22. data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Jason.decode!()
  23. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  24. object = Object.normalize(data["object"], fetch: false)
  25. assert object.data["emoji"] == %{
  26. "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
  27. }
  28. data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Jason.decode!()
  29. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  30. object = Object.normalize(data["object"], fetch: false)
  31. assert "test" in Object.tags(object)
  32. assert Object.hashtags(object) == ["test"]
  33. end
  34. test "it ignores an incoming notice if we already have it" do
  35. activity = insert(:note_activity)
  36. data =
  37. File.read!("test/fixtures/mastodon-post-activity.json")
  38. |> Jason.decode!()
  39. |> Map.put("object", Object.normalize(activity, fetch: false).data)
  40. {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
  41. assert activity == returned_activity
  42. end
  43. test "it fetches reply-to activities if we don't have them" do
  44. data =
  45. File.read!("test/fixtures/mastodon-post-activity.json")
  46. |> Jason.decode!()
  47. object =
  48. data["object"]
  49. |> Map.put("inReplyTo", "https://mstdn.io/users/mayuutann/statuses/99568293732299394")
  50. data = Map.put(data, "object", object)
  51. {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
  52. returned_object = Object.normalize(returned_activity, fetch: false)
  53. assert %Activity{} =
  54. Activity.get_create_by_object_ap_id(
  55. "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
  56. )
  57. assert returned_object.data["inReplyTo"] ==
  58. "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
  59. end
  60. test "it does not fetch reply-to activities beyond max replies depth limit" do
  61. data =
  62. File.read!("test/fixtures/mastodon-post-activity.json")
  63. |> Jason.decode!()
  64. object =
  65. data["object"]
  66. |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
  67. data = Map.put(data, "object", object)
  68. with_mock Pleroma.Web.Federator,
  69. allowed_thread_distance?: fn _ -> false end do
  70. {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
  71. returned_object = Object.normalize(returned_activity, fetch: false)
  72. refute Activity.get_create_by_object_ap_id(
  73. "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
  74. )
  75. assert returned_object.data["inReplyTo"] == "https://shitposter.club/notice/2827873"
  76. end
  77. end
  78. test "it does not crash if the object in inReplyTo can't be fetched" do
  79. data =
  80. File.read!("test/fixtures/mastodon-post-activity.json")
  81. |> Jason.decode!()
  82. object =
  83. data["object"]
  84. |> Map.put("inReplyTo", "https://404.site/whatever")
  85. data =
  86. data
  87. |> Map.put("object", object)
  88. assert {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
  89. end
  90. test "it does not work for deactivated users" do
  91. data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
  92. insert(:user, ap_id: data["actor"], is_active: false)
  93. assert {:error, _} = Transmogrifier.handle_incoming(data)
  94. end
  95. test "it works for incoming notices" do
  96. data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
  97. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  98. assert data["id"] ==
  99. "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
  100. assert data["context"] ==
  101. "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
  102. assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
  103. assert data["cc"] == [
  104. "http://localtesting.pleroma.lol/users/lain",
  105. "http://mastodon.example.org/users/admin/followers"
  106. ]
  107. assert data["actor"] == "http://mastodon.example.org/users/admin"
  108. object_data = Object.normalize(data["object"], fetch: false).data
  109. assert object_data["id"] ==
  110. "http://mastodon.example.org/users/admin/statuses/99512778738411822"
  111. assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
  112. assert object_data["cc"] == [
  113. "http://localtesting.pleroma.lol/users/lain",
  114. "http://mastodon.example.org/users/admin/followers"
  115. ]
  116. assert object_data["actor"] == "http://mastodon.example.org/users/admin"
  117. assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
  118. assert object_data["context"] ==
  119. "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
  120. assert object_data["sensitive"] == true
  121. user = User.get_cached_by_ap_id(object_data["actor"])
  122. assert user.note_count == 1
  123. end
  124. test "it works for incoming notices without the sensitive property but an nsfw hashtag" do
  125. data = File.read!("test/fixtures/mastodon-post-activity-nsfw.json") |> Jason.decode!()
  126. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  127. object_data = Object.normalize(data["object"], fetch: false).data
  128. assert object_data["sensitive"] == true
  129. end
  130. test "it works for incoming notices with hashtags" do
  131. data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Jason.decode!()
  132. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  133. object = Object.normalize(data["object"], fetch: false)
  134. assert match?(
  135. %{
  136. "href" => "http://localtesting.pleroma.lol/users/lain",
  137. "name" => "@lain@localtesting.pleroma.lol",
  138. "type" => "Mention"
  139. },
  140. Enum.at(object.data["tag"], 0)
  141. )
  142. assert match?(
  143. %{
  144. "href" => "http://mastodon.example.org/tags/moo",
  145. "name" => "#moo",
  146. "type" => "Hashtag"
  147. },
  148. Enum.at(object.data["tag"], 1)
  149. )
  150. assert "moo" == Enum.at(object.data["tag"], 2)
  151. end
  152. test "it works for incoming notices with contentMap" do
  153. data = File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Jason.decode!()
  154. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  155. object = Object.normalize(data["object"], fetch: false)
  156. assert object.data["content"] ==
  157. "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
  158. end
  159. test "it only uses contentMap if content is not present" do
  160. user = insert(:user)
  161. message = %{
  162. "@context" => "https://www.w3.org/ns/activitystreams",
  163. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  164. "cc" => [],
  165. "type" => "Create",
  166. "object" => %{
  167. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  168. "cc" => [],
  169. "id" => Utils.generate_object_id(),
  170. "type" => "Note",
  171. "content" => "Hi",
  172. "contentMap" => %{
  173. "de" => "Hallo",
  174. "uk" => "Привіт"
  175. },
  176. "inReplyTo" => nil,
  177. "attributedTo" => user.ap_id
  178. },
  179. "actor" => user.ap_id
  180. }
  181. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message)
  182. object = Object.normalize(data["object"], fetch: false)
  183. assert object.data["content"] == "Hi"
  184. end
  185. test "it works for incoming notices with a nil contentMap (firefish)" do
  186. data =
  187. File.read!("test/fixtures/mastodon-post-activity-contentmap.json")
  188. |> Jason.decode!()
  189. |> Map.put("contentMap", nil)
  190. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  191. object = Object.normalize(data["object"], fetch: false)
  192. assert object.data["content"] ==
  193. "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
  194. end
  195. test "it works for incoming notices with to/cc not being an array (kroeg)" do
  196. data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
  197. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  198. object = Object.normalize(data["object"], fetch: false)
  199. assert object.data["content"] ==
  200. "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
  201. end
  202. test "it ensures that as:Public activities make it to their followers collection" do
  203. user = insert(:user)
  204. data =
  205. File.read!("test/fixtures/mastodon-post-activity.json")
  206. |> Jason.decode!()
  207. |> Map.put("actor", user.ap_id)
  208. |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
  209. |> Map.put("cc", [])
  210. object =
  211. data["object"]
  212. |> Map.put("attributedTo", user.ap_id)
  213. |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
  214. |> Map.put("cc", [])
  215. |> Map.put("id", user.ap_id <> "/activities/12345678")
  216. data = Map.put(data, "object", object)
  217. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  218. assert data["cc"] == [User.ap_followers(user)]
  219. end
  220. test "it ensures that address fields become lists" do
  221. user = insert(:user)
  222. data =
  223. File.read!("test/fixtures/mastodon-post-activity.json")
  224. |> Jason.decode!()
  225. |> Map.put("actor", user.ap_id)
  226. |> Map.put("cc", nil)
  227. object =
  228. data["object"]
  229. |> Map.put("attributedTo", user.ap_id)
  230. |> Map.put("cc", nil)
  231. |> Map.put("id", user.ap_id <> "/activities/12345678")
  232. data = Map.put(data, "object", object)
  233. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  234. refute is_nil(data["cc"])
  235. end
  236. test "it strips internal likes" do
  237. data =
  238. File.read!("test/fixtures/mastodon-post-activity.json")
  239. |> Jason.decode!()
  240. likes = %{
  241. "first" =>
  242. "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
  243. "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
  244. "totalItems" => 3,
  245. "type" => "OrderedCollection"
  246. }
  247. object = Map.put(data["object"], "likes", likes)
  248. data = Map.put(data, "object", object)
  249. {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
  250. object = Object.normalize(activity)
  251. assert object.data["likes"] == []
  252. end
  253. test "it strips internal reactions" do
  254. user = insert(:user)
  255. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  256. {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
  257. %{object: object} = Activity.get_by_id_with_object(activity.id)
  258. assert Map.has_key?(object.data, "reactions")
  259. assert Map.has_key?(object.data, "reaction_count")
  260. object_data = Transmogrifier.strip_internal_fields(object.data)
  261. refute Map.has_key?(object_data, "reactions")
  262. refute Map.has_key?(object_data, "reaction_count")
  263. end
  264. test "it correctly processes messages with non-array to field" do
  265. data =
  266. File.read!("test/fixtures/mastodon-post-activity.json")
  267. |> Poison.decode!()
  268. |> Map.put("to", "https://www.w3.org/ns/activitystreams#Public")
  269. |> put_in(["object", "to"], "https://www.w3.org/ns/activitystreams#Public")
  270. assert {:ok, activity} = Transmogrifier.handle_incoming(data)
  271. assert [
  272. "http://localtesting.pleroma.lol/users/lain",
  273. "http://mastodon.example.org/users/admin/followers"
  274. ] == activity.data["cc"]
  275. assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
  276. end
  277. test "it correctly processes messages with non-array cc field" do
  278. data =
  279. File.read!("test/fixtures/mastodon-post-activity.json")
  280. |> Poison.decode!()
  281. |> Map.put("cc", "http://mastodon.example.org/users/admin/followers")
  282. |> put_in(["object", "cc"], "http://mastodon.example.org/users/admin/followers")
  283. assert {:ok, activity} = Transmogrifier.handle_incoming(data)
  284. assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
  285. assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
  286. end
  287. test "it correctly processes messages with weirdness in address fields" do
  288. data =
  289. File.read!("test/fixtures/mastodon-post-activity.json")
  290. |> Poison.decode!()
  291. |> Map.put("cc", ["http://mastodon.example.org/users/admin/followers", ["¿"]])
  292. |> put_in(["object", "cc"], ["http://mastodon.example.org/users/admin/followers", ["¿"]])
  293. assert {:ok, activity} = Transmogrifier.handle_incoming(data)
  294. assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
  295. assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
  296. end
  297. end
  298. describe "`handle_incoming/2`, Mastodon format `replies` handling" do
  299. setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
  300. setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
  301. setup do
  302. data =
  303. "test/fixtures/mastodon-post-activity.json"
  304. |> File.read!()
  305. |> Jason.decode!()
  306. items = get_in(data, ["object", "replies", "first", "items"])
  307. assert length(items) > 0
  308. %{data: data, items: items}
  309. end
  310. test "schedules background fetching of `replies` items if max thread depth limit allows", %{
  311. data: data,
  312. items: items
  313. } do
  314. clear_config([:instance, :federation_incoming_replies_max_depth], 10)
  315. {:ok, activity} = Transmogrifier.handle_incoming(data)
  316. object = Object.normalize(activity.data["object"])
  317. assert object.data["replies"] == items
  318. for id <- items do
  319. job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
  320. assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
  321. end
  322. end
  323. test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
  324. %{data: data} do
  325. clear_config([:instance, :federation_incoming_replies_max_depth], 0)
  326. {:ok, _activity} = Transmogrifier.handle_incoming(data)
  327. assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
  328. end
  329. end
  330. describe "`handle_incoming/2`, Pleroma format `replies` handling" do
  331. setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
  332. setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
  333. setup do
  334. replies = %{
  335. "type" => "Collection",
  336. "items" => [Utils.generate_object_id(), Utils.generate_object_id()]
  337. }
  338. activity =
  339. File.read!("test/fixtures/mastodon-post-activity.json")
  340. |> Poison.decode!()
  341. |> Kernel.put_in(["object", "replies"], replies)
  342. %{activity: activity}
  343. end
  344. test "schedules background fetching of `replies` items if max thread depth limit allows", %{
  345. activity: activity
  346. } do
  347. clear_config([:instance, :federation_incoming_replies_max_depth], 1)
  348. assert {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(activity)
  349. object = Object.normalize(data["object"])
  350. for id <- object.data["replies"] do
  351. job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
  352. assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
  353. end
  354. end
  355. test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
  356. %{activity: activity} do
  357. clear_config([:instance, :federation_incoming_replies_max_depth], 0)
  358. {:ok, _activity} = Transmogrifier.handle_incoming(activity)
  359. assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
  360. end
  361. end
  362. describe "reserialization" do
  363. test "successfully reserializes a message with inReplyTo == nil" do
  364. user = insert(:user)
  365. message = %{
  366. "@context" => "https://www.w3.org/ns/activitystreams",
  367. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  368. "cc" => [],
  369. "type" => "Create",
  370. "object" => %{
  371. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  372. "cc" => [],
  373. "id" => Utils.generate_object_id(),
  374. "type" => "Note",
  375. "content" => "Hi",
  376. "inReplyTo" => nil,
  377. "attributedTo" => user.ap_id
  378. },
  379. "actor" => user.ap_id
  380. }
  381. {:ok, activity} = Transmogrifier.handle_incoming(message)
  382. {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
  383. end
  384. test "successfully reserializes a message with AS2 objects in IR" do
  385. user = insert(:user)
  386. message = %{
  387. "@context" => "https://www.w3.org/ns/activitystreams",
  388. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  389. "cc" => [],
  390. "type" => "Create",
  391. "object" => %{
  392. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  393. "cc" => [],
  394. "id" => Utils.generate_object_id(),
  395. "type" => "Note",
  396. "content" => "Hi",
  397. "inReplyTo" => nil,
  398. "attributedTo" => user.ap_id,
  399. "tag" => [
  400. %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
  401. %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
  402. ]
  403. },
  404. "actor" => user.ap_id
  405. }
  406. {:ok, activity} = Transmogrifier.handle_incoming(message)
  407. {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
  408. end
  409. end
  410. describe "fix_in_reply_to/2" do
  411. setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
  412. setup do
  413. data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
  414. [data: data]
  415. end
  416. test "returns not modified object when has no inReplyTo field", %{data: data} do
  417. assert Transmogrifier.fix_in_reply_to(data) == data
  418. end
  419. test "returns object with inReplyTo when denied incoming reply", %{data: data} do
  420. clear_config([:instance, :federation_incoming_replies_max_depth], 0)
  421. object_with_reply =
  422. Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
  423. modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
  424. assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
  425. object_with_reply =
  426. Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
  427. modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
  428. assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
  429. object_with_reply =
  430. Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
  431. modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
  432. assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
  433. object_with_reply = Map.put(data["object"], "inReplyTo", [])
  434. modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
  435. assert modified_object["inReplyTo"] == []
  436. end
  437. test "returns modified object when allowed incoming reply", %{data: data} do
  438. object_with_reply =
  439. Map.put(
  440. data["object"],
  441. "inReplyTo",
  442. "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
  443. )
  444. clear_config([:instance, :federation_incoming_replies_max_depth], 5)
  445. modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
  446. assert modified_object["inReplyTo"] ==
  447. "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
  448. assert modified_object["context"] ==
  449. "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
  450. end
  451. end
  452. describe "fix_attachments/1" do
  453. test "returns not modified object" do
  454. data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
  455. assert Transmogrifier.fix_attachments(data) == data
  456. end
  457. test "returns modified object when attachment is map" do
  458. assert Transmogrifier.fix_attachments(%{
  459. "attachment" => %{
  460. "mediaType" => "video/mp4",
  461. "url" => "https://peertube.moe/stat-480.mp4"
  462. }
  463. }) == %{
  464. "attachment" => [
  465. %{
  466. "mediaType" => "video/mp4",
  467. "type" => "Document",
  468. "url" => [
  469. %{
  470. "href" => "https://peertube.moe/stat-480.mp4",
  471. "mediaType" => "video/mp4",
  472. "type" => "Link"
  473. }
  474. ]
  475. }
  476. ]
  477. }
  478. end
  479. test "returns modified object when attachment is list" do
  480. assert Transmogrifier.fix_attachments(%{
  481. "attachment" => [
  482. %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
  483. %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
  484. ]
  485. }) == %{
  486. "attachment" => [
  487. %{
  488. "mediaType" => "video/mp4",
  489. "type" => "Document",
  490. "url" => [
  491. %{
  492. "href" => "https://pe.er/stat-480.mp4",
  493. "mediaType" => "video/mp4",
  494. "type" => "Link"
  495. }
  496. ]
  497. },
  498. %{
  499. "mediaType" => "video/mp4",
  500. "type" => "Document",
  501. "url" => [
  502. %{
  503. "href" => "https://pe.er/stat-480.mp4",
  504. "mediaType" => "video/mp4",
  505. "type" => "Link"
  506. }
  507. ]
  508. }
  509. ]
  510. }
  511. end
  512. end
  513. describe "fix_emoji/1" do
  514. test "returns not modified object when object not contains tags" do
  515. data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
  516. assert Transmogrifier.fix_emoji(data) == data
  517. end
  518. test "returns object with emoji when object contains list tags" do
  519. assert Transmogrifier.fix_emoji(%{
  520. "tag" => [
  521. %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
  522. %{"type" => "Hashtag"}
  523. ]
  524. }) == %{
  525. "emoji" => %{"bib" => "/test"},
  526. "tag" => [
  527. %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
  528. %{"type" => "Hashtag"}
  529. ]
  530. }
  531. end
  532. test "returns object with emoji when object contains map tag" do
  533. assert Transmogrifier.fix_emoji(%{
  534. "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
  535. }) == %{
  536. "emoji" => %{"bib" => "/test"},
  537. "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
  538. }
  539. end
  540. end
  541. describe "set_replies/1" do
  542. setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
  543. test "returns unmodified object if activity doesn't have self-replies" do
  544. data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
  545. assert Transmogrifier.set_replies(data) == data
  546. end
  547. test "sets `replies` collection with a limited number of self-replies" do
  548. [user, another_user] = insert_list(2, :user)
  549. {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
  550. {:ok, %{id: id2} = self_reply1} =
  551. CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
  552. {:ok, self_reply2} =
  553. CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
  554. # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
  555. {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
  556. {:ok, _} =
  557. CommonAPI.post(user, %{
  558. status: "self-reply to self-reply",
  559. in_reply_to_status_id: id2
  560. })
  561. {:ok, _} =
  562. CommonAPI.post(another_user, %{
  563. status: "another user's reply",
  564. in_reply_to_status_id: id1
  565. })
  566. object = Object.normalize(activity, fetch: false)
  567. replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
  568. assert %{"type" => "Collection", "items" => ^replies_uris} =
  569. Transmogrifier.set_replies(object.data)["replies"]
  570. end
  571. end
  572. test "take_emoji_tags/1" do
  573. user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
  574. assert Transmogrifier.take_emoji_tags(user) == [
  575. %{
  576. "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
  577. "id" => "https://example.org/firefox.png",
  578. "name" => ":firefox:",
  579. "type" => "Emoji",
  580. "updated" => "1970-01-01T00:00:00Z"
  581. }
  582. ]
  583. end
  584. test "the standalone note uses its own ID when context is missing" do
  585. insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
  586. activity =
  587. "test/fixtures/tesla_mock/mk.absturztau.be-93e7nm8wqg-activity.json"
  588. |> File.read!()
  589. |> Jason.decode!()
  590. {:ok, %Activity{} = modified} = Transmogrifier.handle_incoming(activity)
  591. object = Object.normalize(modified, fetch: false)
  592. assert object.data["context"] == object.data["id"]
  593. assert modified.data["context"] == object.data["id"]
  594. end
  595. test "the reply note uses its parent's ID when context is missing and reply is unreachable" do
  596. insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
  597. activity =
  598. "test/fixtures/tesla_mock/mk.absturztau.be-93e7nm8wqg-activity.json"
  599. |> File.read!()
  600. |> Jason.decode!()
  601. object =
  602. activity["object"]
  603. |> Map.put("inReplyTo", "https://404.site/object/went-to-buy-milk")
  604. activity =
  605. activity
  606. |> Map.put("object", object)
  607. {:ok, %Activity{} = modified} = Transmogrifier.handle_incoming(activity)
  608. object = Object.normalize(modified, fetch: false)
  609. assert object.data["context"] == object.data["inReplyTo"]
  610. assert modified.data["context"] == object.data["inReplyTo"]
  611. end
  612. end