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