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