logo

pleroma

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

note_handling_test.exs (27359B)


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