fix mediaType of object
[akkoma] / test / web / activity_pub / transmogrifier_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.TransmogrifierTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.DataCase
8
9 alias Pleroma.Activity
10 alias Pleroma.Object
11 alias Pleroma.Object.Fetcher
12 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.User
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
18
19 import Mock
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
22
23 setup_all do
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 :ok
26 end
27
28 setup do: clear_config([:instance, :max_remote_account_fields])
29
30 describe "handle_incoming" do
31 test "it ignores an incoming notice if we already have it" do
32 activity = insert(:note_activity)
33
34 data =
35 File.read!("test/fixtures/mastodon-post-activity.json")
36 |> Poison.decode!()
37 |> Map.put("object", Object.normalize(activity).data)
38
39 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
40
41 assert activity == returned_activity
42 end
43
44 @tag capture_log: true
45 test "it fetches reply-to activities if we don't have them" do
46 data =
47 File.read!("test/fixtures/mastodon-post-activity.json")
48 |> Poison.decode!()
49
50 object =
51 data["object"]
52 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
53
54 data = Map.put(data, "object", object)
55 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
56 returned_object = Object.normalize(returned_activity, false)
57
58 assert activity =
59 Activity.get_create_by_object_ap_id(
60 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
61 )
62
63 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
64 end
65
66 test "it does not fetch reply-to activities beyond max replies depth limit" do
67 data =
68 File.read!("test/fixtures/mastodon-post-activity.json")
69 |> Poison.decode!()
70
71 object =
72 data["object"]
73 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
74
75 data = Map.put(data, "object", object)
76
77 with_mock Pleroma.Web.Federator,
78 allowed_thread_distance?: fn _ -> false end do
79 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
80
81 returned_object = Object.normalize(returned_activity, false)
82
83 refute Activity.get_create_by_object_ap_id(
84 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
85 )
86
87 assert returned_object.data["inReplyToAtomUri"] ==
88 "https://shitposter.club/notice/2827873"
89 end
90 end
91
92 test "it does not crash if the object in inReplyTo can't be fetched" do
93 data =
94 File.read!("test/fixtures/mastodon-post-activity.json")
95 |> Poison.decode!()
96
97 object =
98 data["object"]
99 |> Map.put("inReplyTo", "https://404.site/whatever")
100
101 data =
102 data
103 |> Map.put("object", object)
104
105 assert capture_log(fn ->
106 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
107 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
108 end
109
110 test "it works for incoming notices" do
111 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
112
113 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
114
115 assert data["id"] ==
116 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
117
118 assert data["context"] ==
119 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
120
121 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
122
123 assert data["cc"] == [
124 "http://mastodon.example.org/users/admin/followers",
125 "http://localtesting.pleroma.lol/users/lain"
126 ]
127
128 assert data["actor"] == "http://mastodon.example.org/users/admin"
129
130 object_data = Object.normalize(data["object"]).data
131
132 assert object_data["id"] ==
133 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
134
135 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
136
137 assert object_data["cc"] == [
138 "http://mastodon.example.org/users/admin/followers",
139 "http://localtesting.pleroma.lol/users/lain"
140 ]
141
142 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
143 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
144
145 assert object_data["context"] ==
146 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
147
148 assert object_data["sensitive"] == true
149
150 user = User.get_cached_by_ap_id(object_data["actor"])
151
152 assert user.note_count == 1
153 end
154
155 test "it works for incoming notices with hashtags" do
156 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
157
158 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
159 object = Object.normalize(data["object"])
160
161 assert Enum.at(object.data["tag"], 2) == "moo"
162 end
163
164 test "it works for incoming questions" do
165 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
166
167 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
168
169 object = Object.normalize(activity)
170
171 assert Enum.all?(object.data["oneOf"], fn choice ->
172 choice["name"] in [
173 "Dunno",
174 "Everyone knows that!",
175 "25 char limit is dumb",
176 "I can't even fit a funny"
177 ]
178 end)
179 end
180
181 test "it works for incoming listens" do
182 data = %{
183 "@context" => "https://www.w3.org/ns/activitystreams",
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
185 "cc" => [],
186 "type" => "Listen",
187 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
188 "actor" => "http://mastodon.example.org/users/admin",
189 "object" => %{
190 "type" => "Audio",
191 "id" => "http://mastodon.example.org/users/admin/listens/1234",
192 "attributedTo" => "http://mastodon.example.org/users/admin",
193 "title" => "lain radio episode 1",
194 "artist" => "lain",
195 "album" => "lain radio",
196 "length" => 180_000
197 }
198 }
199
200 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
201
202 object = Object.normalize(activity)
203
204 assert object.data["title"] == "lain radio episode 1"
205 assert object.data["artist"] == "lain"
206 assert object.data["album"] == "lain radio"
207 assert object.data["length"] == 180_000
208 end
209
210 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
211 user = insert(:user)
212
213 {:ok, activity} =
214 CommonAPI.post(user, %{
215 "status" => "suya...",
216 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
217 })
218
219 object = Object.normalize(activity)
220
221 data =
222 File.read!("test/fixtures/mastodon-vote.json")
223 |> Poison.decode!()
224 |> Kernel.put_in(["to"], user.ap_id)
225 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
226 |> Kernel.put_in(["object", "to"], user.ap_id)
227
228 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
229 answer_object = Object.normalize(activity)
230 assert answer_object.data["type"] == "Answer"
231 object = Object.get_by_ap_id(object.data["id"])
232
233 assert Enum.any?(
234 object.data["oneOf"],
235 fn
236 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
237 _ -> false
238 end
239 )
240 end
241
242 test "it works for incoming notices with contentMap" do
243 data =
244 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
245
246 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
247 object = Object.normalize(data["object"])
248
249 assert object.data["content"] ==
250 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
251 end
252
253 test "it works for incoming notices with to/cc not being an array (kroeg)" do
254 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
255
256 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
257 object = Object.normalize(data["object"])
258
259 assert object.data["content"] ==
260 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
261 end
262
263 test "it works for incoming announces with actor being inlined (kroeg)" do
264 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
265
266 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
267
268 assert data["actor"] == "https://puckipedia.com/"
269 end
270
271 test "it works for incoming notices with tag not being an array (kroeg)" do
272 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
273
274 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
275 object = Object.normalize(data["object"])
276
277 assert object.data["emoji"] == %{
278 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
279 }
280
281 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
282
283 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
284 object = Object.normalize(data["object"])
285
286 assert "test" in object.data["tag"]
287 end
288
289 test "it works for incoming notices with url not being a string (prismo)" do
290 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
291
292 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
293 object = Object.normalize(data["object"])
294
295 assert object.data["url"] == "https://prismo.news/posts/83"
296 end
297
298 test "it cleans up incoming notices which are not really DMs" do
299 user = insert(:user)
300 other_user = insert(:user)
301
302 to = [user.ap_id, other_user.ap_id]
303
304 data =
305 File.read!("test/fixtures/mastodon-post-activity.json")
306 |> Poison.decode!()
307 |> Map.put("to", to)
308 |> Map.put("cc", [])
309
310 object =
311 data["object"]
312 |> Map.put("to", to)
313 |> Map.put("cc", [])
314
315 data = Map.put(data, "object", object)
316
317 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
318
319 assert data["to"] == []
320 assert data["cc"] == to
321
322 object_data = Object.normalize(activity).data
323
324 assert object_data["to"] == []
325 assert object_data["cc"] == to
326 end
327
328 test "it works for incoming likes" do
329 user = insert(:user)
330 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
331
332 data =
333 File.read!("test/fixtures/mastodon-like.json")
334 |> Poison.decode!()
335 |> Map.put("object", activity.data["object"])
336
337 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
338
339 refute Enum.empty?(activity.recipients)
340
341 assert data["actor"] == "http://mastodon.example.org/users/admin"
342 assert data["type"] == "Like"
343 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
344 assert data["object"] == activity.data["object"]
345 end
346
347 test "it works for incoming misskey likes, turning them into EmojiReacts" do
348 user = insert(:user)
349 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
350
351 data =
352 File.read!("test/fixtures/misskey-like.json")
353 |> Poison.decode!()
354 |> Map.put("object", activity.data["object"])
355
356 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
357
358 assert data["actor"] == data["actor"]
359 assert data["type"] == "EmojiReact"
360 assert data["id"] == data["id"]
361 assert data["object"] == activity.data["object"]
362 assert data["content"] == "🍮"
363 end
364
365 test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReacts" do
366 user = insert(:user)
367 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
368
369 data =
370 File.read!("test/fixtures/misskey-like.json")
371 |> Poison.decode!()
372 |> Map.put("object", activity.data["object"])
373 |> Map.put("_misskey_reaction", "⭐")
374
375 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
376
377 assert data["actor"] == data["actor"]
378 assert data["type"] == "EmojiReact"
379 assert data["id"] == data["id"]
380 assert data["object"] == activity.data["object"]
381 assert data["content"] == "⭐"
382 end
383
384 test "it works for incoming emoji reactions" do
385 user = insert(:user)
386 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
387
388 data =
389 File.read!("test/fixtures/emoji-reaction.json")
390 |> Poison.decode!()
391 |> Map.put("object", activity.data["object"])
392
393 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
394
395 assert data["actor"] == "http://mastodon.example.org/users/admin"
396 assert data["type"] == "EmojiReact"
397 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
398 assert data["object"] == activity.data["object"]
399 assert data["content"] == "👌"
400 end
401
402 test "it reject invalid emoji reactions" do
403 user = insert(:user)
404 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
405
406 data =
407 File.read!("test/fixtures/emoji-reaction-too-long.json")
408 |> Poison.decode!()
409 |> Map.put("object", activity.data["object"])
410
411 assert :error = Transmogrifier.handle_incoming(data)
412
413 data =
414 File.read!("test/fixtures/emoji-reaction-no-emoji.json")
415 |> Poison.decode!()
416 |> Map.put("object", activity.data["object"])
417
418 assert :error = Transmogrifier.handle_incoming(data)
419 end
420
421 test "it works for incoming emoji reaction undos" do
422 user = insert(:user)
423
424 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
425 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
426
427 data =
428 File.read!("test/fixtures/mastodon-undo-like.json")
429 |> Poison.decode!()
430 |> Map.put("object", reaction_activity.data["id"])
431 |> Map.put("actor", user.ap_id)
432
433 {:ok, activity} = Transmogrifier.handle_incoming(data)
434
435 assert activity.actor == user.ap_id
436 assert activity.data["id"] == data["id"]
437 assert activity.data["type"] == "Undo"
438 end
439
440 test "it returns an error for incoming unlikes wihout a like activity" do
441 user = insert(:user)
442 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
443
444 data =
445 File.read!("test/fixtures/mastodon-undo-like.json")
446 |> Poison.decode!()
447 |> Map.put("object", activity.data["object"])
448
449 assert Transmogrifier.handle_incoming(data) == :error
450 end
451
452 test "it works for incoming unlikes with an existing like activity" do
453 user = insert(:user)
454 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
455
456 like_data =
457 File.read!("test/fixtures/mastodon-like.json")
458 |> Poison.decode!()
459 |> Map.put("object", activity.data["object"])
460
461 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
462
463 data =
464 File.read!("test/fixtures/mastodon-undo-like.json")
465 |> Poison.decode!()
466 |> Map.put("object", like_data)
467 |> Map.put("actor", like_data["actor"])
468
469 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
470
471 assert data["actor"] == "http://mastodon.example.org/users/admin"
472 assert data["type"] == "Undo"
473 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
474 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
475 end
476
477 test "it works for incoming unlikes with an existing like activity and a compact object" do
478 user = insert(:user)
479 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
480
481 like_data =
482 File.read!("test/fixtures/mastodon-like.json")
483 |> Poison.decode!()
484 |> Map.put("object", activity.data["object"])
485
486 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
487
488 data =
489 File.read!("test/fixtures/mastodon-undo-like.json")
490 |> Poison.decode!()
491 |> Map.put("object", like_data["id"])
492 |> Map.put("actor", like_data["actor"])
493
494 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
495
496 assert data["actor"] == "http://mastodon.example.org/users/admin"
497 assert data["type"] == "Undo"
498 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
499 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
500 end
501
502 test "it works for incoming announces" do
503 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
504
505 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
506
507 assert data["actor"] == "http://mastodon.example.org/users/admin"
508 assert data["type"] == "Announce"
509
510 assert data["id"] ==
511 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
512
513 assert data["object"] ==
514 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
515
516 assert Activity.get_create_by_object_ap_id(data["object"])
517 end
518
519 test "it works for incoming announces with an existing activity" do
520 user = insert(:user)
521 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
522
523 data =
524 File.read!("test/fixtures/mastodon-announce.json")
525 |> Poison.decode!()
526 |> Map.put("object", activity.data["object"])
527
528 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
529
530 assert data["actor"] == "http://mastodon.example.org/users/admin"
531 assert data["type"] == "Announce"
532
533 assert data["id"] ==
534 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
535
536 assert data["object"] == activity.data["object"]
537
538 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
539 end
540
541 test "it works for incoming announces with an inlined activity" do
542 data =
543 File.read!("test/fixtures/mastodon-announce-private.json")
544 |> Poison.decode!()
545
546 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
547
548 assert data["actor"] == "http://mastodon.example.org/users/admin"
549 assert data["type"] == "Announce"
550
551 assert data["id"] ==
552 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
553
554 object = Object.normalize(data["object"])
555
556 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
557 assert object.data["content"] == "this is a private toot"
558 end
559
560 @tag capture_log: true
561 test "it rejects incoming announces with an inlined activity from another origin" do
562 data =
563 File.read!("test/fixtures/bogus-mastodon-announce.json")
564 |> Poison.decode!()
565
566 assert :error = Transmogrifier.handle_incoming(data)
567 end
568
569 test "it does not clobber the addressing on announce activities" do
570 user = insert(:user)
571 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
572
573 data =
574 File.read!("test/fixtures/mastodon-announce.json")
575 |> Poison.decode!()
576 |> Map.put("object", Object.normalize(activity).data["id"])
577 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
578 |> Map.put("cc", [])
579
580 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
581
582 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
583 end
584
585 test "it ensures that as:Public activities make it to their followers collection" do
586 user = insert(:user)
587
588 data =
589 File.read!("test/fixtures/mastodon-post-activity.json")
590 |> Poison.decode!()
591 |> Map.put("actor", user.ap_id)
592 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
593 |> Map.put("cc", [])
594
595 object =
596 data["object"]
597 |> Map.put("attributedTo", user.ap_id)
598 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
599 |> Map.put("cc", [])
600 |> Map.put("id", user.ap_id <> "/activities/12345678")
601
602 data = Map.put(data, "object", object)
603
604 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
605
606 assert data["cc"] == [User.ap_followers(user)]
607 end
608
609 test "it ensures that address fields become lists" do
610 user = insert(:user)
611
612 data =
613 File.read!("test/fixtures/mastodon-post-activity.json")
614 |> Poison.decode!()
615 |> Map.put("actor", user.ap_id)
616 |> Map.put("to", nil)
617 |> Map.put("cc", nil)
618
619 object =
620 data["object"]
621 |> Map.put("attributedTo", user.ap_id)
622 |> Map.put("to", nil)
623 |> Map.put("cc", nil)
624 |> Map.put("id", user.ap_id <> "/activities/12345678")
625
626 data = Map.put(data, "object", object)
627
628 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
629
630 assert !is_nil(data["to"])
631 assert !is_nil(data["cc"])
632 end
633
634 test "it strips internal likes" do
635 data =
636 File.read!("test/fixtures/mastodon-post-activity.json")
637 |> Poison.decode!()
638
639 likes = %{
640 "first" =>
641 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
642 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
643 "totalItems" => 3,
644 "type" => "OrderedCollection"
645 }
646
647 object = Map.put(data["object"], "likes", likes)
648 data = Map.put(data, "object", object)
649
650 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
651
652 refute Map.has_key?(object.data, "likes")
653 end
654
655 test "it strips internal reactions" do
656 user = insert(:user)
657 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
658 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
659
660 %{object: object} = Activity.get_by_id_with_object(activity.id)
661 assert Map.has_key?(object.data, "reactions")
662 assert Map.has_key?(object.data, "reaction_count")
663
664 object_data = Transmogrifier.strip_internal_fields(object.data)
665 refute Map.has_key?(object_data, "reactions")
666 refute Map.has_key?(object_data, "reaction_count")
667 end
668
669 test "it works for incoming update activities" do
670 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
671
672 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
673 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
674
675 object =
676 update_data["object"]
677 |> Map.put("actor", data["actor"])
678 |> Map.put("id", data["actor"])
679
680 update_data =
681 update_data
682 |> Map.put("actor", data["actor"])
683 |> Map.put("object", object)
684
685 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
686
687 assert data["id"] == update_data["id"]
688
689 user = User.get_cached_by_ap_id(data["actor"])
690 assert user.name == "gargle"
691
692 assert user.avatar["url"] == [
693 %{
694 "href" =>
695 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
696 }
697 ]
698
699 assert user.banner["url"] == [
700 %{
701 "href" =>
702 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
703 }
704 ]
705
706 assert user.bio == "<p>Some bio</p>"
707 end
708
709 test "it works with alsoKnownAs" do
710 {:ok, %Activity{data: %{"actor" => actor}}} =
711 "test/fixtures/mastodon-post-activity.json"
712 |> File.read!()
713 |> Poison.decode!()
714 |> Transmogrifier.handle_incoming()
715
716 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
717
718 {:ok, _activity} =
719 "test/fixtures/mastodon-update.json"
720 |> File.read!()
721 |> Poison.decode!()
722 |> Map.put("actor", actor)
723 |> Map.update!("object", fn object ->
724 object
725 |> Map.put("actor", actor)
726 |> Map.put("id", actor)
727 |> Map.put("alsoKnownAs", [
728 "http://mastodon.example.org/users/foo",
729 "http://example.org/users/bar"
730 ])
731 end)
732 |> Transmogrifier.handle_incoming()
733
734 assert User.get_cached_by_ap_id(actor).also_known_as == [
735 "http://mastodon.example.org/users/foo",
736 "http://example.org/users/bar"
737 ]
738 end
739
740 test "it works with custom profile fields" do
741 {:ok, activity} =
742 "test/fixtures/mastodon-post-activity.json"
743 |> File.read!()
744 |> Poison.decode!()
745 |> Transmogrifier.handle_incoming()
746
747 user = User.get_cached_by_ap_id(activity.actor)
748
749 assert User.fields(user) == [
750 %{"name" => "foo", "value" => "bar"},
751 %{"name" => "foo1", "value" => "bar1"}
752 ]
753
754 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
755
756 object =
757 update_data["object"]
758 |> Map.put("actor", user.ap_id)
759 |> Map.put("id", user.ap_id)
760
761 update_data =
762 update_data
763 |> Map.put("actor", user.ap_id)
764 |> Map.put("object", object)
765
766 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
767
768 user = User.get_cached_by_ap_id(user.ap_id)
769
770 assert User.fields(user) == [
771 %{"name" => "foo", "value" => "updated"},
772 %{"name" => "foo1", "value" => "updated"}
773 ]
774
775 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
776
777 update_data =
778 put_in(update_data, ["object", "attachment"], [
779 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
780 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
781 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
782 ])
783
784 {:ok, _} = Transmogrifier.handle_incoming(update_data)
785
786 user = User.get_cached_by_ap_id(user.ap_id)
787
788 assert User.fields(user) == [
789 %{"name" => "foo", "value" => "updated"},
790 %{"name" => "foo1", "value" => "updated"}
791 ]
792
793 update_data = put_in(update_data, ["object", "attachment"], [])
794
795 {:ok, _} = Transmogrifier.handle_incoming(update_data)
796
797 user = User.get_cached_by_ap_id(user.ap_id)
798
799 assert User.fields(user) == []
800 end
801
802 test "it works for incoming update activities which lock the account" do
803 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
804
805 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
806 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
807
808 object =
809 update_data["object"]
810 |> Map.put("actor", data["actor"])
811 |> Map.put("id", data["actor"])
812 |> Map.put("manuallyApprovesFollowers", true)
813
814 update_data =
815 update_data
816 |> Map.put("actor", data["actor"])
817 |> Map.put("object", object)
818
819 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
820
821 user = User.get_cached_by_ap_id(data["actor"])
822 assert user.locked == true
823 end
824
825 test "it works for incoming deletes" do
826 activity = insert(:note_activity)
827 deleting_user = insert(:user)
828
829 data =
830 File.read!("test/fixtures/mastodon-delete.json")
831 |> Poison.decode!()
832
833 object =
834 data["object"]
835 |> Map.put("id", activity.data["object"])
836
837 data =
838 data
839 |> Map.put("object", object)
840 |> Map.put("actor", deleting_user.ap_id)
841
842 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
843 Transmogrifier.handle_incoming(data)
844
845 assert id == data["id"]
846 refute Activity.get_by_id(activity.id)
847 assert actor == deleting_user.ap_id
848 end
849
850 test "it fails for incoming deletes with spoofed origin" do
851 activity = insert(:note_activity)
852
853 data =
854 File.read!("test/fixtures/mastodon-delete.json")
855 |> Poison.decode!()
856
857 object =
858 data["object"]
859 |> Map.put("id", activity.data["object"])
860
861 data =
862 data
863 |> Map.put("object", object)
864
865 assert capture_log(fn ->
866 :error = Transmogrifier.handle_incoming(data)
867 end) =~
868 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
869
870 assert Activity.get_by_id(activity.id)
871 end
872
873 @tag capture_log: true
874 test "it works for incoming user deletes" do
875 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
876
877 data =
878 File.read!("test/fixtures/mastodon-delete-user.json")
879 |> Poison.decode!()
880
881 {:ok, _} = Transmogrifier.handle_incoming(data)
882 ObanHelpers.perform_all()
883
884 refute User.get_cached_by_ap_id(ap_id)
885 end
886
887 test "it fails for incoming user deletes with spoofed origin" do
888 %{ap_id: ap_id} = insert(:user)
889
890 data =
891 File.read!("test/fixtures/mastodon-delete-user.json")
892 |> Poison.decode!()
893 |> Map.put("actor", ap_id)
894
895 assert capture_log(fn ->
896 assert :error == Transmogrifier.handle_incoming(data)
897 end) =~ "Object containment failed"
898
899 assert User.get_cached_by_ap_id(ap_id)
900 end
901
902 test "it works for incoming unannounces with an existing notice" do
903 user = insert(:user)
904 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
905
906 announce_data =
907 File.read!("test/fixtures/mastodon-announce.json")
908 |> Poison.decode!()
909 |> Map.put("object", activity.data["object"])
910
911 {:ok, %Activity{data: announce_data, local: false}} =
912 Transmogrifier.handle_incoming(announce_data)
913
914 data =
915 File.read!("test/fixtures/mastodon-undo-announce.json")
916 |> Poison.decode!()
917 |> Map.put("object", announce_data)
918 |> Map.put("actor", announce_data["actor"])
919
920 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
921
922 assert data["type"] == "Undo"
923 assert object_data = data["object"]
924 assert object_data["type"] == "Announce"
925 assert object_data["object"] == activity.data["object"]
926
927 assert object_data["id"] ==
928 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
929 end
930
931 test "it works for incomming unfollows with an existing follow" do
932 user = insert(:user)
933
934 follow_data =
935 File.read!("test/fixtures/mastodon-follow-activity.json")
936 |> Poison.decode!()
937 |> Map.put("object", user.ap_id)
938
939 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
940
941 data =
942 File.read!("test/fixtures/mastodon-unfollow-activity.json")
943 |> Poison.decode!()
944 |> Map.put("object", follow_data)
945
946 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
947
948 assert data["type"] == "Undo"
949 assert data["object"]["type"] == "Follow"
950 assert data["object"]["object"] == user.ap_id
951 assert data["actor"] == "http://mastodon.example.org/users/admin"
952
953 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
954 end
955
956 test "it works for incoming follows to locked account" do
957 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
958 user = insert(:user, locked: true)
959
960 data =
961 File.read!("test/fixtures/mastodon-follow-activity.json")
962 |> Poison.decode!()
963 |> Map.put("object", user.ap_id)
964
965 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
966
967 assert data["type"] == "Follow"
968 assert data["object"] == user.ap_id
969 assert data["state"] == "pending"
970 assert data["actor"] == "http://mastodon.example.org/users/admin"
971
972 assert [^pending_follower] = User.get_follow_requests(user)
973 end
974
975 test "it works for incoming blocks" do
976 user = insert(:user)
977
978 data =
979 File.read!("test/fixtures/mastodon-block-activity.json")
980 |> Poison.decode!()
981 |> Map.put("object", user.ap_id)
982
983 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
984
985 assert data["type"] == "Block"
986 assert data["object"] == user.ap_id
987 assert data["actor"] == "http://mastodon.example.org/users/admin"
988
989 blocker = User.get_cached_by_ap_id(data["actor"])
990
991 assert User.blocks?(blocker, user)
992 end
993
994 test "incoming blocks successfully tear down any follow relationship" do
995 blocker = insert(:user)
996 blocked = insert(:user)
997
998 data =
999 File.read!("test/fixtures/mastodon-block-activity.json")
1000 |> Poison.decode!()
1001 |> Map.put("object", blocked.ap_id)
1002 |> Map.put("actor", blocker.ap_id)
1003
1004 {:ok, blocker} = User.follow(blocker, blocked)
1005 {:ok, blocked} = User.follow(blocked, blocker)
1006
1007 assert User.following?(blocker, blocked)
1008 assert User.following?(blocked, blocker)
1009
1010 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
1011
1012 assert data["type"] == "Block"
1013 assert data["object"] == blocked.ap_id
1014 assert data["actor"] == blocker.ap_id
1015
1016 blocker = User.get_cached_by_ap_id(data["actor"])
1017 blocked = User.get_cached_by_ap_id(data["object"])
1018
1019 assert User.blocks?(blocker, blocked)
1020
1021 refute User.following?(blocker, blocked)
1022 refute User.following?(blocked, blocker)
1023 end
1024
1025 test "it works for incoming unblocks with an existing block" do
1026 user = insert(:user)
1027
1028 block_data =
1029 File.read!("test/fixtures/mastodon-block-activity.json")
1030 |> Poison.decode!()
1031 |> Map.put("object", user.ap_id)
1032
1033 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
1034
1035 data =
1036 File.read!("test/fixtures/mastodon-unblock-activity.json")
1037 |> Poison.decode!()
1038 |> Map.put("object", block_data)
1039
1040 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
1041 assert data["type"] == "Undo"
1042 assert data["object"]["type"] == "Block"
1043 assert data["object"]["object"] == user.ap_id
1044 assert data["actor"] == "http://mastodon.example.org/users/admin"
1045
1046 blocker = User.get_cached_by_ap_id(data["actor"])
1047
1048 refute User.blocks?(blocker, user)
1049 end
1050
1051 test "it works for incoming accepts which were pre-accepted" do
1052 follower = insert(:user)
1053 followed = insert(:user)
1054
1055 {:ok, follower} = User.follow(follower, followed)
1056 assert User.following?(follower, followed) == true
1057
1058 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1059
1060 accept_data =
1061 File.read!("test/fixtures/mastodon-accept-activity.json")
1062 |> Poison.decode!()
1063 |> Map.put("actor", followed.ap_id)
1064
1065 object =
1066 accept_data["object"]
1067 |> Map.put("actor", follower.ap_id)
1068 |> Map.put("id", follow_activity.data["id"])
1069
1070 accept_data = Map.put(accept_data, "object", object)
1071
1072 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1073 refute activity.local
1074
1075 assert activity.data["object"] == follow_activity.data["id"]
1076
1077 assert activity.data["id"] == accept_data["id"]
1078
1079 follower = User.get_cached_by_id(follower.id)
1080
1081 assert User.following?(follower, followed) == true
1082 end
1083
1084 test "it works for incoming accepts which were orphaned" do
1085 follower = insert(:user)
1086 followed = insert(:user, locked: true)
1087
1088 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1089
1090 accept_data =
1091 File.read!("test/fixtures/mastodon-accept-activity.json")
1092 |> Poison.decode!()
1093 |> Map.put("actor", followed.ap_id)
1094
1095 accept_data =
1096 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1097
1098 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1099 assert activity.data["object"] == follow_activity.data["id"]
1100
1101 follower = User.get_cached_by_id(follower.id)
1102
1103 assert User.following?(follower, followed) == true
1104 end
1105
1106 test "it works for incoming accepts which are referenced by IRI only" do
1107 follower = insert(:user)
1108 followed = insert(:user, locked: true)
1109
1110 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1111
1112 accept_data =
1113 File.read!("test/fixtures/mastodon-accept-activity.json")
1114 |> Poison.decode!()
1115 |> Map.put("actor", followed.ap_id)
1116 |> Map.put("object", follow_activity.data["id"])
1117
1118 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1119 assert activity.data["object"] == follow_activity.data["id"]
1120
1121 follower = User.get_cached_by_id(follower.id)
1122
1123 assert User.following?(follower, followed) == true
1124 end
1125
1126 test "it fails for incoming accepts which cannot be correlated" do
1127 follower = insert(:user)
1128 followed = insert(:user, locked: true)
1129
1130 accept_data =
1131 File.read!("test/fixtures/mastodon-accept-activity.json")
1132 |> Poison.decode!()
1133 |> Map.put("actor", followed.ap_id)
1134
1135 accept_data =
1136 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1137
1138 :error = Transmogrifier.handle_incoming(accept_data)
1139
1140 follower = User.get_cached_by_id(follower.id)
1141
1142 refute User.following?(follower, followed) == true
1143 end
1144
1145 test "it fails for incoming rejects which cannot be correlated" do
1146 follower = insert(:user)
1147 followed = insert(:user, locked: true)
1148
1149 accept_data =
1150 File.read!("test/fixtures/mastodon-reject-activity.json")
1151 |> Poison.decode!()
1152 |> Map.put("actor", followed.ap_id)
1153
1154 accept_data =
1155 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1156
1157 :error = Transmogrifier.handle_incoming(accept_data)
1158
1159 follower = User.get_cached_by_id(follower.id)
1160
1161 refute User.following?(follower, followed) == true
1162 end
1163
1164 test "it works for incoming rejects which are orphaned" do
1165 follower = insert(:user)
1166 followed = insert(:user, locked: true)
1167
1168 {:ok, follower} = User.follow(follower, followed)
1169 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1170
1171 assert User.following?(follower, followed) == true
1172
1173 reject_data =
1174 File.read!("test/fixtures/mastodon-reject-activity.json")
1175 |> Poison.decode!()
1176 |> Map.put("actor", followed.ap_id)
1177
1178 reject_data =
1179 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1180
1181 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1182 refute activity.local
1183 assert activity.data["id"] == reject_data["id"]
1184
1185 follower = User.get_cached_by_id(follower.id)
1186
1187 assert User.following?(follower, followed) == false
1188 end
1189
1190 test "it works for incoming rejects which are referenced by IRI only" do
1191 follower = insert(:user)
1192 followed = insert(:user, locked: true)
1193
1194 {:ok, follower} = User.follow(follower, followed)
1195 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1196
1197 assert User.following?(follower, followed) == true
1198
1199 reject_data =
1200 File.read!("test/fixtures/mastodon-reject-activity.json")
1201 |> Poison.decode!()
1202 |> Map.put("actor", followed.ap_id)
1203 |> Map.put("object", follow_activity.data["id"])
1204
1205 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1206
1207 follower = User.get_cached_by_id(follower.id)
1208
1209 assert User.following?(follower, followed) == false
1210 end
1211
1212 test "it rejects activities without a valid ID" do
1213 user = insert(:user)
1214
1215 data =
1216 File.read!("test/fixtures/mastodon-follow-activity.json")
1217 |> Poison.decode!()
1218 |> Map.put("object", user.ap_id)
1219 |> Map.put("id", "")
1220
1221 :error = Transmogrifier.handle_incoming(data)
1222 end
1223
1224 test "it remaps mediaType of object" do
1225 {:ok, object} =
1226 Fetcher.fetch_object_from_id(
1227 "https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b"
1228 )
1229
1230 assert object.data["mediaType"] == "text/html"
1231 end
1232
1233 test "it remaps video URLs as attachments if necessary" do
1234 {:ok, object} =
1235 Fetcher.fetch_object_from_id(
1236 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1237 )
1238
1239 attachment = %{
1240 "type" => "Link",
1241 "mediaType" => "video/mp4",
1242 "url" => [
1243 %{
1244 "href" =>
1245 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1246 "mediaType" => "video/mp4"
1247 }
1248 ]
1249 }
1250
1251 assert object.data["url"] ==
1252 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1253
1254 assert object.data["attachment"] == [attachment]
1255 end
1256
1257 test "it accepts Flag activities" do
1258 user = insert(:user)
1259 other_user = insert(:user)
1260
1261 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1262 object = Object.normalize(activity)
1263
1264 note_obj = %{
1265 "type" => "Note",
1266 "id" => activity.data["id"],
1267 "content" => "test post",
1268 "published" => object.data["published"],
1269 "actor" => AccountView.render("show.json", %{user: user})
1270 }
1271
1272 message = %{
1273 "@context" => "https://www.w3.org/ns/activitystreams",
1274 "cc" => [user.ap_id],
1275 "object" => [user.ap_id, activity.data["id"]],
1276 "type" => "Flag",
1277 "content" => "blocked AND reported!!!",
1278 "actor" => other_user.ap_id
1279 }
1280
1281 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1282
1283 assert activity.data["object"] == [user.ap_id, note_obj]
1284 assert activity.data["content"] == "blocked AND reported!!!"
1285 assert activity.data["actor"] == other_user.ap_id
1286 assert activity.data["cc"] == [user.ap_id]
1287 end
1288
1289 test "it correctly processes messages with non-array to field" do
1290 user = insert(:user)
1291
1292 message = %{
1293 "@context" => "https://www.w3.org/ns/activitystreams",
1294 "to" => "https://www.w3.org/ns/activitystreams#Public",
1295 "type" => "Create",
1296 "object" => %{
1297 "content" => "blah blah blah",
1298 "type" => "Note",
1299 "attributedTo" => user.ap_id,
1300 "inReplyTo" => nil
1301 },
1302 "actor" => user.ap_id
1303 }
1304
1305 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1306
1307 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1308 end
1309
1310 test "it correctly processes messages with non-array cc field" do
1311 user = insert(:user)
1312
1313 message = %{
1314 "@context" => "https://www.w3.org/ns/activitystreams",
1315 "to" => user.follower_address,
1316 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1317 "type" => "Create",
1318 "object" => %{
1319 "content" => "blah blah blah",
1320 "type" => "Note",
1321 "attributedTo" => user.ap_id,
1322 "inReplyTo" => nil
1323 },
1324 "actor" => user.ap_id
1325 }
1326
1327 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1328
1329 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1330 assert [user.follower_address] == activity.data["to"]
1331 end
1332
1333 test "it accepts Move activities" do
1334 old_user = insert(:user)
1335 new_user = insert(:user)
1336
1337 message = %{
1338 "@context" => "https://www.w3.org/ns/activitystreams",
1339 "type" => "Move",
1340 "actor" => old_user.ap_id,
1341 "object" => old_user.ap_id,
1342 "target" => new_user.ap_id
1343 }
1344
1345 assert :error = Transmogrifier.handle_incoming(message)
1346
1347 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1348
1349 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1350 assert activity.actor == old_user.ap_id
1351 assert activity.data["actor"] == old_user.ap_id
1352 assert activity.data["object"] == old_user.ap_id
1353 assert activity.data["target"] == new_user.ap_id
1354 assert activity.data["type"] == "Move"
1355 end
1356 end
1357
1358 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1359 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1360 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1361
1362 setup do
1363 data =
1364 "test/fixtures/mastodon-post-activity.json"
1365 |> File.read!()
1366 |> Poison.decode!()
1367
1368 items = get_in(data, ["object", "replies", "first", "items"])
1369 assert length(items) > 0
1370
1371 %{data: data, items: items}
1372 end
1373
1374 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1375 data: data,
1376 items: items
1377 } do
1378 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1379
1380 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1381
1382 for id <- items do
1383 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1384 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1385 end
1386 end
1387
1388 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1389 %{data: data} do
1390 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1391
1392 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1393
1394 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1395 end
1396 end
1397
1398 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1399 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1400 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1401
1402 setup do
1403 user = insert(:user)
1404
1405 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1406
1407 {:ok, reply1} =
1408 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1409
1410 {:ok, reply2} =
1411 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1412
1413 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1414
1415 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1416
1417 Repo.delete(activity.object)
1418 Repo.delete(activity)
1419
1420 %{federation_output: federation_output, replies_uris: replies_uris}
1421 end
1422
1423 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1424 federation_output: federation_output,
1425 replies_uris: replies_uris
1426 } do
1427 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1428
1429 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1430
1431 for id <- replies_uris do
1432 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1433 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1434 end
1435 end
1436
1437 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1438 %{federation_output: federation_output} do
1439 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1440
1441 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1442
1443 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1444 end
1445 end
1446
1447 describe "prepare outgoing" do
1448 test "it inlines private announced objects" do
1449 user = insert(:user)
1450
1451 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1452
1453 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1454
1455 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1456
1457 assert modified["object"]["content"] == "hey"
1458 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1459 end
1460
1461 test "it turns mentions into tags" do
1462 user = insert(:user)
1463 other_user = insert(:user)
1464
1465 {:ok, activity} =
1466 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1467
1468 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1469 object = modified["object"]
1470
1471 expected_mention = %{
1472 "href" => other_user.ap_id,
1473 "name" => "@#{other_user.nickname}",
1474 "type" => "Mention"
1475 }
1476
1477 expected_tag = %{
1478 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1479 "type" => "Hashtag",
1480 "name" => "#2hu"
1481 }
1482
1483 assert Enum.member?(object["tag"], expected_tag)
1484 assert Enum.member?(object["tag"], expected_mention)
1485 end
1486
1487 test "it adds the sensitive property" do
1488 user = insert(:user)
1489
1490 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1491 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1492
1493 assert modified["object"]["sensitive"]
1494 end
1495
1496 test "it adds the json-ld context and the conversation property" do
1497 user = insert(:user)
1498
1499 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1500 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1501
1502 assert modified["@context"] ==
1503 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1504
1505 assert modified["object"]["conversation"] == modified["context"]
1506 end
1507
1508 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1509 user = insert(:user)
1510
1511 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1512 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1513
1514 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1515 end
1516
1517 test "it strips internal hashtag data" do
1518 user = insert(:user)
1519
1520 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1521
1522 expected_tag = %{
1523 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1524 "type" => "Hashtag",
1525 "name" => "#2hu"
1526 }
1527
1528 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1529
1530 assert modified["object"]["tag"] == [expected_tag]
1531 end
1532
1533 test "it strips internal fields" do
1534 user = insert(:user)
1535
1536 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1537
1538 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1539
1540 assert length(modified["object"]["tag"]) == 2
1541
1542 assert is_nil(modified["object"]["emoji"])
1543 assert is_nil(modified["object"]["like_count"])
1544 assert is_nil(modified["object"]["announcements"])
1545 assert is_nil(modified["object"]["announcement_count"])
1546 assert is_nil(modified["object"]["context_id"])
1547 end
1548
1549 test "it strips internal fields of article" do
1550 activity = insert(:article_activity)
1551
1552 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1553
1554 assert length(modified["object"]["tag"]) == 2
1555
1556 assert is_nil(modified["object"]["emoji"])
1557 assert is_nil(modified["object"]["like_count"])
1558 assert is_nil(modified["object"]["announcements"])
1559 assert is_nil(modified["object"]["announcement_count"])
1560 assert is_nil(modified["object"]["context_id"])
1561 assert is_nil(modified["object"]["likes"])
1562 end
1563
1564 test "the directMessage flag is present" do
1565 user = insert(:user)
1566 other_user = insert(:user)
1567
1568 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1569
1570 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1571
1572 assert modified["directMessage"] == false
1573
1574 {:ok, activity} =
1575 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1576
1577 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1578
1579 assert modified["directMessage"] == false
1580
1581 {:ok, activity} =
1582 CommonAPI.post(user, %{
1583 "status" => "@#{other_user.nickname} :moominmamma:",
1584 "visibility" => "direct"
1585 })
1586
1587 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1588
1589 assert modified["directMessage"] == true
1590 end
1591
1592 test "it strips BCC field" do
1593 user = insert(:user)
1594 {:ok, list} = Pleroma.List.create("foo", user)
1595
1596 {:ok, activity} =
1597 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1598
1599 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1600
1601 assert is_nil(modified["bcc"])
1602 end
1603
1604 test "it can handle Listen activities" do
1605 listen_activity = insert(:listen)
1606
1607 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1608
1609 assert modified["type"] == "Listen"
1610
1611 user = insert(:user)
1612
1613 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1614
1615 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1616 end
1617 end
1618
1619 describe "user upgrade" do
1620 test "it upgrades a user to activitypub" do
1621 user =
1622 insert(:user, %{
1623 nickname: "rye@niu.moe",
1624 local: false,
1625 ap_id: "https://niu.moe/users/rye",
1626 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1627 })
1628
1629 user_two = insert(:user)
1630 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1631
1632 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1633 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1634 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1635
1636 user = User.get_cached_by_id(user.id)
1637 assert user.note_count == 1
1638
1639 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1640 ObanHelpers.perform_all()
1641
1642 assert user.ap_enabled
1643 assert user.note_count == 1
1644 assert user.follower_address == "https://niu.moe/users/rye/followers"
1645 assert user.following_address == "https://niu.moe/users/rye/following"
1646
1647 user = User.get_cached_by_id(user.id)
1648 assert user.note_count == 1
1649
1650 activity = Activity.get_by_id(activity.id)
1651 assert user.follower_address in activity.recipients
1652
1653 assert %{
1654 "url" => [
1655 %{
1656 "href" =>
1657 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1658 }
1659 ]
1660 } = user.avatar
1661
1662 assert %{
1663 "url" => [
1664 %{
1665 "href" =>
1666 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1667 }
1668 ]
1669 } = user.banner
1670
1671 refute "..." in activity.recipients
1672
1673 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1674 refute user.follower_address in unrelated_activity.recipients
1675
1676 user_two = User.get_cached_by_id(user_two.id)
1677 assert User.following?(user_two, user)
1678 refute "..." in User.following(user_two)
1679 end
1680 end
1681
1682 describe "actor rewriting" do
1683 test "it fixes the actor URL property to be a proper URI" do
1684 data = %{
1685 "url" => %{"href" => "http://example.com"}
1686 }
1687
1688 rewritten = Transmogrifier.maybe_fix_user_object(data)
1689 assert rewritten["url"] == "http://example.com"
1690 end
1691 end
1692
1693 describe "actor origin containment" do
1694 test "it rejects activities which reference objects with bogus origins" do
1695 data = %{
1696 "@context" => "https://www.w3.org/ns/activitystreams",
1697 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1698 "actor" => "http://mastodon.example.org/users/admin",
1699 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1700 "object" => "https://info.pleroma.site/activity.json",
1701 "type" => "Announce"
1702 }
1703
1704 assert capture_log(fn ->
1705 :error = Transmogrifier.handle_incoming(data)
1706 end) =~ "Object containment failed"
1707 end
1708
1709 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1710 data = %{
1711 "@context" => "https://www.w3.org/ns/activitystreams",
1712 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1713 "actor" => "http://mastodon.example.org/users/admin",
1714 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1715 "object" => "https://info.pleroma.site/activity2.json",
1716 "type" => "Announce"
1717 }
1718
1719 assert capture_log(fn ->
1720 :error = Transmogrifier.handle_incoming(data)
1721 end) =~ "Object containment failed"
1722 end
1723
1724 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1725 data = %{
1726 "@context" => "https://www.w3.org/ns/activitystreams",
1727 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1728 "actor" => "http://mastodon.example.org/users/admin",
1729 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1730 "object" => "https://info.pleroma.site/activity3.json",
1731 "type" => "Announce"
1732 }
1733
1734 assert capture_log(fn ->
1735 :error = Transmogrifier.handle_incoming(data)
1736 end) =~ "Object containment failed"
1737 end
1738 end
1739
1740 describe "reserialization" do
1741 test "successfully reserializes a message with inReplyTo == nil" do
1742 user = insert(:user)
1743
1744 message = %{
1745 "@context" => "https://www.w3.org/ns/activitystreams",
1746 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1747 "cc" => [],
1748 "type" => "Create",
1749 "object" => %{
1750 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1751 "cc" => [],
1752 "type" => "Note",
1753 "content" => "Hi",
1754 "inReplyTo" => nil,
1755 "attributedTo" => user.ap_id
1756 },
1757 "actor" => user.ap_id
1758 }
1759
1760 {:ok, activity} = Transmogrifier.handle_incoming(message)
1761
1762 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1763 end
1764
1765 test "successfully reserializes a message with AS2 objects in IR" do
1766 user = insert(:user)
1767
1768 message = %{
1769 "@context" => "https://www.w3.org/ns/activitystreams",
1770 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1771 "cc" => [],
1772 "type" => "Create",
1773 "object" => %{
1774 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1775 "cc" => [],
1776 "type" => "Note",
1777 "content" => "Hi",
1778 "inReplyTo" => nil,
1779 "attributedTo" => user.ap_id,
1780 "tag" => [
1781 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1782 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1783 ]
1784 },
1785 "actor" => user.ap_id
1786 }
1787
1788 {:ok, activity} = Transmogrifier.handle_incoming(message)
1789
1790 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1791 end
1792 end
1793
1794 test "Rewrites Answers to Notes" do
1795 user = insert(:user)
1796
1797 {:ok, poll_activity} =
1798 CommonAPI.post(user, %{
1799 "status" => "suya...",
1800 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1801 })
1802
1803 poll_object = Object.normalize(poll_activity)
1804 # TODO: Replace with CommonAPI vote creation when implemented
1805 data =
1806 File.read!("test/fixtures/mastodon-vote.json")
1807 |> Poison.decode!()
1808 |> Kernel.put_in(["to"], user.ap_id)
1809 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1810 |> Kernel.put_in(["object", "to"], user.ap_id)
1811
1812 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1813 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1814
1815 assert data["object"]["type"] == "Note"
1816 end
1817
1818 describe "fix_explicit_addressing" do
1819 setup do
1820 user = insert(:user)
1821 [user: user]
1822 end
1823
1824 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1825 explicitly_mentioned_actors = [
1826 "https://pleroma.gold/users/user1",
1827 "https://pleroma.gold/user2"
1828 ]
1829
1830 object = %{
1831 "actor" => user.ap_id,
1832 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1833 "cc" => [],
1834 "tag" =>
1835 Enum.map(explicitly_mentioned_actors, fn href ->
1836 %{"type" => "Mention", "href" => href}
1837 end)
1838 }
1839
1840 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1841 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1842 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1843 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1844 end
1845
1846 test "does not move actor's follower collection to cc", %{user: user} do
1847 object = %{
1848 "actor" => user.ap_id,
1849 "to" => [user.follower_address],
1850 "cc" => []
1851 }
1852
1853 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1854 assert user.follower_address in fixed_object["to"]
1855 refute user.follower_address in fixed_object["cc"]
1856 end
1857
1858 test "removes recipient's follower collection from cc", %{user: user} do
1859 recipient = insert(:user)
1860
1861 object = %{
1862 "actor" => user.ap_id,
1863 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1864 "cc" => [user.follower_address, recipient.follower_address]
1865 }
1866
1867 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1868
1869 assert user.follower_address in fixed_object["cc"]
1870 refute recipient.follower_address in fixed_object["cc"]
1871 refute recipient.follower_address in fixed_object["to"]
1872 end
1873 end
1874
1875 describe "fix_summary/1" do
1876 test "returns fixed object" do
1877 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1878 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1879 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1880 end
1881 end
1882
1883 describe "fix_in_reply_to/2" do
1884 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1885
1886 setup do
1887 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1888 [data: data]
1889 end
1890
1891 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1892 assert Transmogrifier.fix_in_reply_to(data) == data
1893 end
1894
1895 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1896 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1897
1898 object_with_reply =
1899 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1900
1901 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1902 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1903 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1904
1905 object_with_reply =
1906 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1907
1908 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1909 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1910 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1911
1912 object_with_reply =
1913 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1914
1915 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1916 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1917 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1918
1919 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1920 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1921 assert modified_object["inReplyTo"] == []
1922 assert modified_object["inReplyToAtomUri"] == ""
1923 end
1924
1925 @tag capture_log: true
1926 test "returns modified object when allowed incoming reply", %{data: data} do
1927 object_with_reply =
1928 Map.put(
1929 data["object"],
1930 "inReplyTo",
1931 "https://shitposter.club/notice/2827873"
1932 )
1933
1934 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1935 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1936
1937 assert modified_object["inReplyTo"] ==
1938 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1939
1940 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1941
1942 assert modified_object["conversation"] ==
1943 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1944
1945 assert modified_object["context"] ==
1946 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1947 end
1948 end
1949
1950 describe "fix_url/1" do
1951 test "fixes data for object when url is map" do
1952 object = %{
1953 "url" => %{
1954 "type" => "Link",
1955 "mimeType" => "video/mp4",
1956 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1957 }
1958 }
1959
1960 assert Transmogrifier.fix_url(object) == %{
1961 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1962 }
1963 end
1964
1965 test "fixes data for video object" do
1966 object = %{
1967 "type" => "Video",
1968 "url" => [
1969 %{
1970 "type" => "Link",
1971 "mimeType" => "video/mp4",
1972 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1973 },
1974 %{
1975 "type" => "Link",
1976 "mimeType" => "video/mp4",
1977 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1978 },
1979 %{
1980 "type" => "Link",
1981 "mimeType" => "text/html",
1982 "href" => "https://peertube.-2d4c2d1630e3"
1983 },
1984 %{
1985 "type" => "Link",
1986 "mimeType" => "text/html",
1987 "href" => "https://peertube.-2d4c2d16377-42"
1988 }
1989 ]
1990 }
1991
1992 assert Transmogrifier.fix_url(object) == %{
1993 "attachment" => [
1994 %{
1995 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1996 "mimeType" => "video/mp4",
1997 "type" => "Link"
1998 }
1999 ],
2000 "type" => "Video",
2001 "url" => "https://peertube.-2d4c2d1630e3"
2002 }
2003 end
2004
2005 test "fixes url for not Video object" do
2006 object = %{
2007 "type" => "Text",
2008 "url" => [
2009 %{
2010 "type" => "Link",
2011 "mimeType" => "text/html",
2012 "href" => "https://peertube.-2d4c2d1630e3"
2013 },
2014 %{
2015 "type" => "Link",
2016 "mimeType" => "text/html",
2017 "href" => "https://peertube.-2d4c2d16377-42"
2018 }
2019 ]
2020 }
2021
2022 assert Transmogrifier.fix_url(object) == %{
2023 "type" => "Text",
2024 "url" => "https://peertube.-2d4c2d1630e3"
2025 }
2026
2027 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
2028 "type" => "Text",
2029 "url" => ""
2030 }
2031 end
2032
2033 test "retunrs not modified object" do
2034 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
2035 end
2036 end
2037
2038 describe "get_obj_helper/2" do
2039 test "returns nil when cannot normalize object" do
2040 assert capture_log(fn ->
2041 refute Transmogrifier.get_obj_helper("test-obj-id")
2042 end) =~ "Unsupported URI scheme"
2043 end
2044
2045 @tag capture_log: true
2046 test "returns {:ok, %Object{}} for success case" do
2047 assert {:ok, %Object{}} =
2048 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
2049 end
2050 end
2051
2052 describe "fix_attachments/1" do
2053 test "returns not modified object" do
2054 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2055 assert Transmogrifier.fix_attachments(data) == data
2056 end
2057
2058 test "returns modified object when attachment is map" do
2059 assert Transmogrifier.fix_attachments(%{
2060 "attachment" => %{
2061 "mediaType" => "video/mp4",
2062 "url" => "https://peertube.moe/stat-480.mp4"
2063 }
2064 }) == %{
2065 "attachment" => [
2066 %{
2067 "mediaType" => "video/mp4",
2068 "url" => [
2069 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
2070 ]
2071 }
2072 ]
2073 }
2074 end
2075
2076 test "returns modified object when attachment is list" do
2077 assert Transmogrifier.fix_attachments(%{
2078 "attachment" => [
2079 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
2080 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
2081 ]
2082 }) == %{
2083 "attachment" => [
2084 %{
2085 "mediaType" => "video/mp4",
2086 "url" => [
2087 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2088 ]
2089 },
2090 %{
2091 "mediaType" => "video/mp4",
2092 "url" => [
2093 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2094 ]
2095 }
2096 ]
2097 }
2098 end
2099 end
2100
2101 describe "fix_emoji/1" do
2102 test "returns not modified object when object not contains tags" do
2103 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2104 assert Transmogrifier.fix_emoji(data) == data
2105 end
2106
2107 test "returns object with emoji when object contains list tags" do
2108 assert Transmogrifier.fix_emoji(%{
2109 "tag" => [
2110 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
2111 %{"type" => "Hashtag"}
2112 ]
2113 }) == %{
2114 "emoji" => %{"bib" => "/test"},
2115 "tag" => [
2116 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
2117 %{"type" => "Hashtag"}
2118 ]
2119 }
2120 end
2121
2122 test "returns object with emoji when object contains map tag" do
2123 assert Transmogrifier.fix_emoji(%{
2124 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
2125 }) == %{
2126 "emoji" => %{"bib" => "/test"},
2127 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
2128 }
2129 end
2130 end
2131
2132 describe "set_replies/1" do
2133 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
2134
2135 test "returns unmodified object if activity doesn't have self-replies" do
2136 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2137 assert Transmogrifier.set_replies(data) == data
2138 end
2139
2140 test "sets `replies` collection with a limited number of self-replies" do
2141 [user, another_user] = insert_list(2, :user)
2142
2143 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
2144
2145 {:ok, %{id: id2} = self_reply1} =
2146 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
2147
2148 {:ok, self_reply2} =
2149 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
2150
2151 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
2152 {:ok, _} =
2153 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
2154
2155 {:ok, _} =
2156 CommonAPI.post(user, %{
2157 "status" => "self-reply to self-reply",
2158 "in_reply_to_status_id" => id2
2159 })
2160
2161 {:ok, _} =
2162 CommonAPI.post(another_user, %{
2163 "status" => "another user's reply",
2164 "in_reply_to_status_id" => id1
2165 })
2166
2167 object = Object.normalize(activity)
2168 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
2169
2170 assert %{"type" => "Collection", "items" => ^replies_uris} =
2171 Transmogrifier.set_replies(object.data)["replies"]
2172 end
2173 end
2174 end