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