1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Object.Fetcher
12 alias Pleroma.Tests.ObanHelpers
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 setup do: clear_config([:instance, :max_remote_account_fields])
30 describe "handle_incoming" do
31 test "it ignores an incoming notice if we already have it" do
32 activity = insert(:note_activity)
35 File.read!("test/fixtures/mastodon-post-activity.json")
37 |> Map.put("object", Object.normalize(activity).data)
39 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
41 assert activity == returned_activity
44 @tag capture_log: true
45 test "it fetches reply-to activities if we don't have them" do
47 File.read!("test/fixtures/mastodon-post-activity.json")
52 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
54 data = Map.put(data, "object", object)
55 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
56 returned_object = Object.normalize(returned_activity, false)
59 Activity.get_create_by_object_ap_id(
60 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
63 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
66 test "it does not fetch reply-to activities beyond max replies depth limit" do
68 File.read!("test/fixtures/mastodon-post-activity.json")
73 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
75 data = Map.put(data, "object", object)
77 with_mock Pleroma.Web.Federator,
78 allowed_thread_distance?: fn _ -> false end do
79 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
81 returned_object = Object.normalize(returned_activity, false)
83 refute Activity.get_create_by_object_ap_id(
84 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
87 assert returned_object.data["inReplyToAtomUri"] ==
88 "https://shitposter.club/notice/2827873"
92 test "it does not crash if the object in inReplyTo can't be fetched" do
94 File.read!("test/fixtures/mastodon-post-activity.json")
99 |> Map.put("inReplyTo", "https://404.site/whatever")
103 |> Map.put("object", object)
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"
110 test "it works for incoming notices" do
111 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
113 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
116 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
118 assert data["context"] ==
119 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
121 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
123 assert data["cc"] == [
124 "http://mastodon.example.org/users/admin/followers",
125 "http://localtesting.pleroma.lol/users/lain"
128 assert data["actor"] == "http://mastodon.example.org/users/admin"
130 object_data = Object.normalize(data["object"]).data
132 assert object_data["id"] ==
133 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
135 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
137 assert object_data["cc"] == [
138 "http://mastodon.example.org/users/admin/followers",
139 "http://localtesting.pleroma.lol/users/lain"
142 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
143 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
145 assert object_data["context"] ==
146 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
148 assert object_data["sensitive"] == true
150 user = User.get_cached_by_ap_id(object_data["actor"])
152 assert user.note_count == 1
155 test "it works for incoming notices with hashtags" do
156 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
158 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
159 object = Object.normalize(data["object"])
161 assert Enum.at(object.data["tag"], 2) == "moo"
164 test "it works for incoming questions" do
165 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
167 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
169 object = Object.normalize(activity)
171 assert Enum.all?(object.data["oneOf"], fn choice ->
174 "Everyone knows that!",
175 "25 char limit is dumb",
176 "I can't even fit a funny"
181 test "it works for incoming listens" do
183 "@context" => "https://www.w3.org/ns/activitystreams",
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
187 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
188 "actor" => "http://mastodon.example.org/users/admin",
191 "id" => "http://mastodon.example.org/users/admin/listens/1234",
192 "attributedTo" => "http://mastodon.example.org/users/admin",
193 "title" => "lain radio episode 1",
195 "album" => "lain radio",
200 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
202 object = Object.normalize(activity)
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
210 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
214 CommonAPI.post(user, %{
215 "status" => "suya...",
216 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
219 object = Object.normalize(activity)
222 File.read!("test/fixtures/mastodon-vote.json")
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)
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"])
234 object.data["oneOf"],
236 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
242 test "it works for incoming notices with contentMap" do
244 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
246 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
247 object = Object.normalize(data["object"])
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>"
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!()
256 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
257 object = Object.normalize(data["object"])
259 assert object.data["content"] ==
260 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
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!()
266 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
268 assert data["actor"] == "https://puckipedia.com/"
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!()
274 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
275 object = Object.normalize(data["object"])
277 assert object.data["emoji"] == %{
278 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
281 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
283 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
284 object = Object.normalize(data["object"])
286 assert "test" in object.data["tag"]
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!()
292 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
293 object = Object.normalize(data["object"])
295 assert object.data["url"] == "https://prismo.news/posts/83"
298 test "it cleans up incoming notices which are not really DMs" do
300 other_user = insert(:user)
302 to = [user.ap_id, other_user.ap_id]
305 File.read!("test/fixtures/mastodon-post-activity.json")
315 data = Map.put(data, "object", object)
317 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
319 assert data["to"] == []
320 assert data["cc"] == to
322 object_data = Object.normalize(activity).data
324 assert object_data["to"] == []
325 assert object_data["cc"] == to
328 test "it works for incoming emoji reactions" do
330 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
333 File.read!("test/fixtures/emoji-reaction.json")
335 |> Map.put("object", activity.data["object"])
337 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
339 assert data["actor"] == "http://mastodon.example.org/users/admin"
340 assert data["type"] == "EmojiReact"
341 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
342 assert data["object"] == activity.data["object"]
343 assert data["content"] == "👌"
346 test "it reject invalid emoji reactions" do
348 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
351 File.read!("test/fixtures/emoji-reaction-too-long.json")
353 |> Map.put("object", activity.data["object"])
355 assert :error = Transmogrifier.handle_incoming(data)
358 File.read!("test/fixtures/emoji-reaction-no-emoji.json")
360 |> Map.put("object", activity.data["object"])
362 assert :error = Transmogrifier.handle_incoming(data)
365 test "it works for incoming announces" do
366 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
368 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
370 assert data["actor"] == "http://mastodon.example.org/users/admin"
371 assert data["type"] == "Announce"
374 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
376 assert data["object"] ==
377 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
379 assert Activity.get_create_by_object_ap_id(data["object"])
382 test "it works for incoming announces with an existing activity" do
384 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
387 File.read!("test/fixtures/mastodon-announce.json")
389 |> Map.put("object", activity.data["object"])
391 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
393 assert data["actor"] == "http://mastodon.example.org/users/admin"
394 assert data["type"] == "Announce"
397 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
399 assert data["object"] == activity.data["object"]
401 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
404 test "it works for incoming announces with an inlined activity" do
406 File.read!("test/fixtures/mastodon-announce-private.json")
409 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
411 assert data["actor"] == "http://mastodon.example.org/users/admin"
412 assert data["type"] == "Announce"
415 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
417 object = Object.normalize(data["object"])
419 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
420 assert object.data["content"] == "this is a private toot"
423 @tag capture_log: true
424 test "it rejects incoming announces with an inlined activity from another origin" do
426 File.read!("test/fixtures/bogus-mastodon-announce.json")
429 assert :error = Transmogrifier.handle_incoming(data)
432 test "it does not clobber the addressing on announce activities" do
434 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
437 File.read!("test/fixtures/mastodon-announce.json")
439 |> Map.put("object", Object.normalize(activity).data["id"])
440 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
443 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
445 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
448 test "it ensures that as:Public activities make it to their followers collection" do
452 File.read!("test/fixtures/mastodon-post-activity.json")
454 |> Map.put("actor", user.ap_id)
455 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
460 |> Map.put("attributedTo", user.ap_id)
461 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
463 |> Map.put("id", user.ap_id <> "/activities/12345678")
465 data = Map.put(data, "object", object)
467 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
469 assert data["cc"] == [User.ap_followers(user)]
472 test "it ensures that address fields become lists" do
476 File.read!("test/fixtures/mastodon-post-activity.json")
478 |> Map.put("actor", user.ap_id)
479 |> Map.put("to", nil)
480 |> Map.put("cc", nil)
484 |> Map.put("attributedTo", user.ap_id)
485 |> Map.put("to", nil)
486 |> Map.put("cc", nil)
487 |> Map.put("id", user.ap_id <> "/activities/12345678")
489 data = Map.put(data, "object", object)
491 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
493 assert !is_nil(data["to"])
494 assert !is_nil(data["cc"])
497 test "it strips internal likes" do
499 File.read!("test/fixtures/mastodon-post-activity.json")
504 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
505 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
507 "type" => "OrderedCollection"
510 object = Map.put(data["object"], "likes", likes)
511 data = Map.put(data, "object", object)
513 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
515 refute Map.has_key?(object.data, "likes")
518 test "it strips internal reactions" do
520 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
521 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
523 %{object: object} = Activity.get_by_id_with_object(activity.id)
524 assert Map.has_key?(object.data, "reactions")
525 assert Map.has_key?(object.data, "reaction_count")
527 object_data = Transmogrifier.strip_internal_fields(object.data)
528 refute Map.has_key?(object_data, "reactions")
529 refute Map.has_key?(object_data, "reaction_count")
532 test "it works for incoming update activities" do
533 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
535 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
536 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
539 update_data["object"]
540 |> Map.put("actor", data["actor"])
541 |> Map.put("id", data["actor"])
545 |> Map.put("actor", data["actor"])
546 |> Map.put("object", object)
548 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
550 assert data["id"] == update_data["id"]
552 user = User.get_cached_by_ap_id(data["actor"])
553 assert user.name == "gargle"
555 assert user.avatar["url"] == [
558 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
562 assert user.banner["url"] == [
565 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
569 assert user.bio == "<p>Some bio</p>"
572 test "it works with alsoKnownAs" do
573 {:ok, %Activity{data: %{"actor" => actor}}} =
574 "test/fixtures/mastodon-post-activity.json"
577 |> Transmogrifier.handle_incoming()
579 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
582 "test/fixtures/mastodon-update.json"
585 |> Map.put("actor", actor)
586 |> Map.update!("object", fn object ->
588 |> Map.put("actor", actor)
589 |> Map.put("id", actor)
590 |> Map.put("alsoKnownAs", [
591 "http://mastodon.example.org/users/foo",
592 "http://example.org/users/bar"
595 |> Transmogrifier.handle_incoming()
597 assert User.get_cached_by_ap_id(actor).also_known_as == [
598 "http://mastodon.example.org/users/foo",
599 "http://example.org/users/bar"
603 test "it works with custom profile fields" do
605 "test/fixtures/mastodon-post-activity.json"
608 |> Transmogrifier.handle_incoming()
610 user = User.get_cached_by_ap_id(activity.actor)
612 assert user.fields == [
613 %{"name" => "foo", "value" => "bar"},
614 %{"name" => "foo1", "value" => "bar1"}
617 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
620 update_data["object"]
621 |> Map.put("actor", user.ap_id)
622 |> Map.put("id", user.ap_id)
626 |> Map.put("actor", user.ap_id)
627 |> Map.put("object", object)
629 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
631 user = User.get_cached_by_ap_id(user.ap_id)
633 assert user.fields == [
634 %{"name" => "foo", "value" => "updated"},
635 %{"name" => "foo1", "value" => "updated"}
638 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
641 put_in(update_data, ["object", "attachment"], [
642 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
643 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
644 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
647 {:ok, _} = Transmogrifier.handle_incoming(update_data)
649 user = User.get_cached_by_ap_id(user.ap_id)
651 assert user.fields == [
652 %{"name" => "foo", "value" => "updated"},
653 %{"name" => "foo1", "value" => "updated"}
656 update_data = put_in(update_data, ["object", "attachment"], [])
658 {:ok, _} = Transmogrifier.handle_incoming(update_data)
660 user = User.get_cached_by_ap_id(user.ap_id)
662 assert user.fields == []
665 test "it works for incoming update activities which lock the account" do
666 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
668 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
669 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
672 update_data["object"]
673 |> Map.put("actor", data["actor"])
674 |> Map.put("id", data["actor"])
675 |> Map.put("manuallyApprovesFollowers", true)
679 |> Map.put("actor", data["actor"])
680 |> Map.put("object", object)
682 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
684 user = User.get_cached_by_ap_id(data["actor"])
685 assert user.locked == true
688 test "it works for incoming deletes" do
689 activity = insert(:note_activity)
690 deleting_user = insert(:user)
693 File.read!("test/fixtures/mastodon-delete.json")
698 |> Map.put("id", activity.data["object"])
702 |> Map.put("object", object)
703 |> Map.put("actor", deleting_user.ap_id)
705 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
706 Transmogrifier.handle_incoming(data)
708 assert id == data["id"]
709 refute Activity.get_by_id(activity.id)
710 assert actor == deleting_user.ap_id
713 test "it fails for incoming deletes with spoofed origin" do
714 activity = insert(:note_activity)
717 File.read!("test/fixtures/mastodon-delete.json")
722 |> Map.put("id", activity.data["object"])
726 |> Map.put("object", object)
728 assert capture_log(fn ->
729 :error = Transmogrifier.handle_incoming(data)
731 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
733 assert Activity.get_by_id(activity.id)
736 @tag capture_log: true
737 test "it works for incoming user deletes" do
739 insert(:user, ap_id: "http://mastodon.example.org/users/admin", local: false)
742 File.read!("test/fixtures/mastodon-delete-user.json")
745 {:ok, _} = Transmogrifier.handle_incoming(data)
746 ObanHelpers.perform_all()
748 refute User.get_cached_by_ap_id(ap_id)
751 test "it fails for incoming user deletes with spoofed origin" do
752 %{ap_id: ap_id} = insert(:user)
755 File.read!("test/fixtures/mastodon-delete-user.json")
757 |> Map.put("actor", ap_id)
759 assert capture_log(fn ->
760 assert :error == Transmogrifier.handle_incoming(data)
761 end) =~ "Object containment failed"
763 assert User.get_cached_by_ap_id(ap_id)
766 test "it works for incoming follows to locked account" do
767 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
768 user = insert(:user, locked: true)
771 File.read!("test/fixtures/mastodon-follow-activity.json")
773 |> Map.put("object", user.ap_id)
775 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
777 assert data["type"] == "Follow"
778 assert data["object"] == user.ap_id
779 assert data["state"] == "pending"
780 assert data["actor"] == "http://mastodon.example.org/users/admin"
782 assert [^pending_follower] = User.get_follow_requests(user)
785 test "it works for incoming blocks" do
789 File.read!("test/fixtures/mastodon-block-activity.json")
791 |> Map.put("object", user.ap_id)
793 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
795 assert data["type"] == "Block"
796 assert data["object"] == user.ap_id
797 assert data["actor"] == "http://mastodon.example.org/users/admin"
799 blocker = User.get_cached_by_ap_id(data["actor"])
801 assert User.blocks?(blocker, user)
804 test "incoming blocks successfully tear down any follow relationship" do
805 blocker = insert(:user)
806 blocked = insert(:user)
809 File.read!("test/fixtures/mastodon-block-activity.json")
811 |> Map.put("object", blocked.ap_id)
812 |> Map.put("actor", blocker.ap_id)
814 {:ok, blocker} = User.follow(blocker, blocked)
815 {:ok, blocked} = User.follow(blocked, blocker)
817 assert User.following?(blocker, blocked)
818 assert User.following?(blocked, blocker)
820 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
822 assert data["type"] == "Block"
823 assert data["object"] == blocked.ap_id
824 assert data["actor"] == blocker.ap_id
826 blocker = User.get_cached_by_ap_id(data["actor"])
827 blocked = User.get_cached_by_ap_id(data["object"])
829 assert User.blocks?(blocker, blocked)
831 refute User.following?(blocker, blocked)
832 refute User.following?(blocked, blocker)
835 test "it works for incoming accepts which were pre-accepted" do
836 follower = insert(:user)
837 followed = insert(:user)
839 {:ok, follower} = User.follow(follower, followed)
840 assert User.following?(follower, followed) == true
842 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
845 File.read!("test/fixtures/mastodon-accept-activity.json")
847 |> Map.put("actor", followed.ap_id)
850 accept_data["object"]
851 |> Map.put("actor", follower.ap_id)
852 |> Map.put("id", follow_activity.data["id"])
854 accept_data = Map.put(accept_data, "object", object)
856 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
857 refute activity.local
859 assert activity.data["object"] == follow_activity.data["id"]
861 assert activity.data["id"] == accept_data["id"]
863 follower = User.get_cached_by_id(follower.id)
865 assert User.following?(follower, followed) == true
868 test "it works for incoming accepts which were orphaned" do
869 follower = insert(:user)
870 followed = insert(:user, locked: true)
872 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
875 File.read!("test/fixtures/mastodon-accept-activity.json")
877 |> Map.put("actor", followed.ap_id)
880 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
882 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
883 assert activity.data["object"] == follow_activity.data["id"]
885 follower = User.get_cached_by_id(follower.id)
887 assert User.following?(follower, followed) == true
890 test "it works for incoming accepts which are referenced by IRI only" do
891 follower = insert(:user)
892 followed = insert(:user, locked: true)
894 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
897 File.read!("test/fixtures/mastodon-accept-activity.json")
899 |> Map.put("actor", followed.ap_id)
900 |> Map.put("object", follow_activity.data["id"])
902 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
903 assert activity.data["object"] == follow_activity.data["id"]
905 follower = User.get_cached_by_id(follower.id)
907 assert User.following?(follower, followed) == true
910 test "it fails for incoming accepts which cannot be correlated" do
911 follower = insert(:user)
912 followed = insert(:user, locked: true)
915 File.read!("test/fixtures/mastodon-accept-activity.json")
917 |> Map.put("actor", followed.ap_id)
920 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
922 :error = Transmogrifier.handle_incoming(accept_data)
924 follower = User.get_cached_by_id(follower.id)
926 refute User.following?(follower, followed) == true
929 test "it fails for incoming rejects which cannot be correlated" do
930 follower = insert(:user)
931 followed = insert(:user, locked: true)
934 File.read!("test/fixtures/mastodon-reject-activity.json")
936 |> Map.put("actor", followed.ap_id)
939 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
941 :error = Transmogrifier.handle_incoming(accept_data)
943 follower = User.get_cached_by_id(follower.id)
945 refute User.following?(follower, followed) == true
948 test "it works for incoming rejects which are orphaned" do
949 follower = insert(:user)
950 followed = insert(:user, locked: true)
952 {:ok, follower} = User.follow(follower, followed)
953 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
955 assert User.following?(follower, followed) == true
958 File.read!("test/fixtures/mastodon-reject-activity.json")
960 |> Map.put("actor", followed.ap_id)
963 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
965 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
966 refute activity.local
967 assert activity.data["id"] == reject_data["id"]
969 follower = User.get_cached_by_id(follower.id)
971 assert User.following?(follower, followed) == false
974 test "it works for incoming rejects which are referenced by IRI only" do
975 follower = insert(:user)
976 followed = insert(:user, locked: true)
978 {:ok, follower} = User.follow(follower, followed)
979 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
981 assert User.following?(follower, followed) == true
984 File.read!("test/fixtures/mastodon-reject-activity.json")
986 |> Map.put("actor", followed.ap_id)
987 |> Map.put("object", follow_activity.data["id"])
989 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
991 follower = User.get_cached_by_id(follower.id)
993 assert User.following?(follower, followed) == false
996 test "it rejects activities without a valid ID" do
1000 File.read!("test/fixtures/mastodon-follow-activity.json")
1002 |> Map.put("object", user.ap_id)
1003 |> Map.put("id", "")
1005 :error = Transmogrifier.handle_incoming(data)
1008 test "skip converting the content when it is nil" do
1009 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1011 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1014 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
1016 assert result["content"] == nil
1019 test "it converts content of object to html" do
1020 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1022 {:ok, %{"content" => content_markdown}} =
1023 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1025 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
1026 Fetcher.fetch_object_from_id(object_id)
1028 assert content_markdown ==
1029 "Support this and our other Michigan!/usr/group videos and meetings. Learn more at http://mug.org/membership\n\nTwenty Years in Jail: FreeBSD's Jails, Then and Now\n\nJails started as a limited virtualization system, but over the last two years they've..."
1032 "<p>Support this and our other Michigan!/usr/group videos and meetings. Learn more at <a href=\"http://mug.org/membership\">http://mug.org/membership</a></p><p>Twenty Years in Jail: FreeBSD’s Jails, Then and Now</p><p>Jails started as a limited virtualization system, but over the last two years they’ve…</p>"
1034 assert object.data["mediaType"] == "text/html"
1037 test "it remaps video URLs as attachments if necessary" do
1039 Fetcher.fetch_object_from_id(
1040 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1045 "mediaType" => "video/mp4",
1049 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1050 "mediaType" => "video/mp4"
1055 assert object.data["url"] ==
1056 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1058 assert object.data["attachment"] == [attachment]
1061 test "it accepts Flag activities" do
1062 user = insert(:user)
1063 other_user = insert(:user)
1065 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1066 object = Object.normalize(activity)
1070 "id" => activity.data["id"],
1071 "content" => "test post",
1072 "published" => object.data["published"],
1073 "actor" => AccountView.render("show.json", %{user: user})
1077 "@context" => "https://www.w3.org/ns/activitystreams",
1078 "cc" => [user.ap_id],
1079 "object" => [user.ap_id, activity.data["id"]],
1081 "content" => "blocked AND reported!!!",
1082 "actor" => other_user.ap_id
1085 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1087 assert activity.data["object"] == [user.ap_id, note_obj]
1088 assert activity.data["content"] == "blocked AND reported!!!"
1089 assert activity.data["actor"] == other_user.ap_id
1090 assert activity.data["cc"] == [user.ap_id]
1093 test "it correctly processes messages with non-array to field" do
1094 user = insert(:user)
1097 "@context" => "https://www.w3.org/ns/activitystreams",
1098 "to" => "https://www.w3.org/ns/activitystreams#Public",
1101 "content" => "blah blah blah",
1103 "attributedTo" => user.ap_id,
1106 "actor" => user.ap_id
1109 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1111 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1114 test "it correctly processes messages with non-array cc field" do
1115 user = insert(:user)
1118 "@context" => "https://www.w3.org/ns/activitystreams",
1119 "to" => user.follower_address,
1120 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1123 "content" => "blah blah blah",
1125 "attributedTo" => user.ap_id,
1128 "actor" => user.ap_id
1131 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1133 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1134 assert [user.follower_address] == activity.data["to"]
1137 test "it accepts Move activities" do
1138 old_user = insert(:user)
1139 new_user = insert(:user)
1142 "@context" => "https://www.w3.org/ns/activitystreams",
1144 "actor" => old_user.ap_id,
1145 "object" => old_user.ap_id,
1146 "target" => new_user.ap_id
1149 assert :error = Transmogrifier.handle_incoming(message)
1151 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1153 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1154 assert activity.actor == old_user.ap_id
1155 assert activity.data["actor"] == old_user.ap_id
1156 assert activity.data["object"] == old_user.ap_id
1157 assert activity.data["target"] == new_user.ap_id
1158 assert activity.data["type"] == "Move"
1162 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1163 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1164 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1168 "test/fixtures/mastodon-post-activity.json"
1172 items = get_in(data, ["object", "replies", "first", "items"])
1173 assert length(items) > 0
1175 %{data: data, items: items}
1178 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1182 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1184 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1187 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1188 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1192 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1194 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1196 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1198 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1202 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1203 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1204 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1207 user = insert(:user)
1209 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1212 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1215 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1217 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1219 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1221 Repo.delete(activity.object)
1222 Repo.delete(activity)
1224 %{federation_output: federation_output, replies_uris: replies_uris}
1227 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1228 federation_output: federation_output,
1229 replies_uris: replies_uris
1231 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1233 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1235 for id <- replies_uris do
1236 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1237 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1241 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1242 %{federation_output: federation_output} do
1243 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1245 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1247 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1251 describe "prepare outgoing" do
1252 test "it inlines private announced objects" do
1253 user = insert(:user)
1255 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1257 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1259 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1261 assert modified["object"]["content"] == "hey"
1262 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1265 test "it turns mentions into tags" do
1266 user = insert(:user)
1267 other_user = insert(:user)
1270 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1272 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1273 object = modified["object"]
1275 expected_mention = %{
1276 "href" => other_user.ap_id,
1277 "name" => "@#{other_user.nickname}",
1282 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1283 "type" => "Hashtag",
1287 assert Enum.member?(object["tag"], expected_tag)
1288 assert Enum.member?(object["tag"], expected_mention)
1291 test "it adds the sensitive property" do
1292 user = insert(:user)
1294 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1295 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1297 assert modified["object"]["sensitive"]
1300 test "it adds the json-ld context and the conversation property" do
1301 user = insert(:user)
1303 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1304 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1306 assert modified["@context"] ==
1307 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1309 assert modified["object"]["conversation"] == modified["context"]
1312 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1313 user = insert(:user)
1315 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1316 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1318 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1321 test "it strips internal hashtag data" do
1322 user = insert(:user)
1324 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1327 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1328 "type" => "Hashtag",
1332 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1334 assert modified["object"]["tag"] == [expected_tag]
1337 test "it strips internal fields" do
1338 user = insert(:user)
1340 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1342 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1344 assert length(modified["object"]["tag"]) == 2
1346 assert is_nil(modified["object"]["emoji"])
1347 assert is_nil(modified["object"]["like_count"])
1348 assert is_nil(modified["object"]["announcements"])
1349 assert is_nil(modified["object"]["announcement_count"])
1350 assert is_nil(modified["object"]["context_id"])
1353 test "it strips internal fields of article" do
1354 activity = insert(:article_activity)
1356 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1358 assert length(modified["object"]["tag"]) == 2
1360 assert is_nil(modified["object"]["emoji"])
1361 assert is_nil(modified["object"]["like_count"])
1362 assert is_nil(modified["object"]["announcements"])
1363 assert is_nil(modified["object"]["announcement_count"])
1364 assert is_nil(modified["object"]["context_id"])
1365 assert is_nil(modified["object"]["likes"])
1368 test "the directMessage flag is present" do
1369 user = insert(:user)
1370 other_user = insert(:user)
1372 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1374 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1376 assert modified["directMessage"] == false
1379 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1381 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1383 assert modified["directMessage"] == false
1386 CommonAPI.post(user, %{
1387 "status" => "@#{other_user.nickname} :moominmamma:",
1388 "visibility" => "direct"
1391 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1393 assert modified["directMessage"] == true
1396 test "it strips BCC field" do
1397 user = insert(:user)
1398 {:ok, list} = Pleroma.List.create("foo", user)
1401 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1403 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1405 assert is_nil(modified["bcc"])
1408 test "it can handle Listen activities" do
1409 listen_activity = insert(:listen)
1411 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1413 assert modified["type"] == "Listen"
1415 user = insert(:user)
1417 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1419 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1423 describe "user upgrade" do
1424 test "it upgrades a user to activitypub" do
1427 nickname: "rye@niu.moe",
1429 ap_id: "https://niu.moe/users/rye",
1430 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1433 user_two = insert(:user)
1434 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1436 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1437 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1438 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1440 user = User.get_cached_by_id(user.id)
1441 assert user.note_count == 1
1443 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1444 ObanHelpers.perform_all()
1446 assert user.ap_enabled
1447 assert user.note_count == 1
1448 assert user.follower_address == "https://niu.moe/users/rye/followers"
1449 assert user.following_address == "https://niu.moe/users/rye/following"
1451 user = User.get_cached_by_id(user.id)
1452 assert user.note_count == 1
1454 activity = Activity.get_by_id(activity.id)
1455 assert user.follower_address in activity.recipients
1461 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1470 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1475 refute "..." in activity.recipients
1477 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1478 refute user.follower_address in unrelated_activity.recipients
1480 user_two = User.get_cached_by_id(user_two.id)
1481 assert User.following?(user_two, user)
1482 refute "..." in User.following(user_two)
1486 describe "actor rewriting" do
1487 test "it fixes the actor URL property to be a proper URI" do
1489 "url" => %{"href" => "http://example.com"}
1492 rewritten = Transmogrifier.maybe_fix_user_object(data)
1493 assert rewritten["url"] == "http://example.com"
1497 describe "actor origin containment" do
1498 test "it rejects activities which reference objects with bogus origins" do
1500 "@context" => "https://www.w3.org/ns/activitystreams",
1501 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1502 "actor" => "http://mastodon.example.org/users/admin",
1503 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1504 "object" => "https://info.pleroma.site/activity.json",
1505 "type" => "Announce"
1508 assert capture_log(fn ->
1509 :error = Transmogrifier.handle_incoming(data)
1510 end) =~ "Object containment failed"
1513 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1515 "@context" => "https://www.w3.org/ns/activitystreams",
1516 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1517 "actor" => "http://mastodon.example.org/users/admin",
1518 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1519 "object" => "https://info.pleroma.site/activity2.json",
1520 "type" => "Announce"
1523 assert capture_log(fn ->
1524 :error = Transmogrifier.handle_incoming(data)
1525 end) =~ "Object containment failed"
1528 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1530 "@context" => "https://www.w3.org/ns/activitystreams",
1531 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1532 "actor" => "http://mastodon.example.org/users/admin",
1533 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1534 "object" => "https://info.pleroma.site/activity3.json",
1535 "type" => "Announce"
1538 assert capture_log(fn ->
1539 :error = Transmogrifier.handle_incoming(data)
1540 end) =~ "Object containment failed"
1544 describe "reserialization" do
1545 test "successfully reserializes a message with inReplyTo == nil" do
1546 user = insert(:user)
1549 "@context" => "https://www.w3.org/ns/activitystreams",
1550 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1554 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1559 "attributedTo" => user.ap_id
1561 "actor" => user.ap_id
1564 {:ok, activity} = Transmogrifier.handle_incoming(message)
1566 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1569 test "successfully reserializes a message with AS2 objects in IR" do
1570 user = insert(:user)
1573 "@context" => "https://www.w3.org/ns/activitystreams",
1574 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1578 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1583 "attributedTo" => user.ap_id,
1585 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1586 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1589 "actor" => user.ap_id
1592 {:ok, activity} = Transmogrifier.handle_incoming(message)
1594 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1598 test "Rewrites Answers to Notes" do
1599 user = insert(:user)
1601 {:ok, poll_activity} =
1602 CommonAPI.post(user, %{
1603 "status" => "suya...",
1604 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1607 poll_object = Object.normalize(poll_activity)
1608 # TODO: Replace with CommonAPI vote creation when implemented
1610 File.read!("test/fixtures/mastodon-vote.json")
1612 |> Kernel.put_in(["to"], user.ap_id)
1613 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1614 |> Kernel.put_in(["object", "to"], user.ap_id)
1616 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1617 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1619 assert data["object"]["type"] == "Note"
1622 describe "fix_explicit_addressing" do
1624 user = insert(:user)
1628 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1629 explicitly_mentioned_actors = [
1630 "https://pleroma.gold/users/user1",
1631 "https://pleroma.gold/user2"
1635 "actor" => user.ap_id,
1636 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1639 Enum.map(explicitly_mentioned_actors, fn href ->
1640 %{"type" => "Mention", "href" => href}
1644 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1645 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1646 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1647 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1650 test "does not move actor's follower collection to cc", %{user: user} do
1652 "actor" => user.ap_id,
1653 "to" => [user.follower_address],
1657 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1658 assert user.follower_address in fixed_object["to"]
1659 refute user.follower_address in fixed_object["cc"]
1662 test "removes recipient's follower collection from cc", %{user: user} do
1663 recipient = insert(:user)
1666 "actor" => user.ap_id,
1667 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1668 "cc" => [user.follower_address, recipient.follower_address]
1671 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1673 assert user.follower_address in fixed_object["cc"]
1674 refute recipient.follower_address in fixed_object["cc"]
1675 refute recipient.follower_address in fixed_object["to"]
1679 describe "fix_summary/1" do
1680 test "returns fixed object" do
1681 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1682 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1683 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1687 describe "fix_in_reply_to/2" do
1688 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1691 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1695 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1696 assert Transmogrifier.fix_in_reply_to(data) == data
1699 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1700 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1703 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1705 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1706 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1707 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1710 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1712 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1713 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1714 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1717 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1719 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1720 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1721 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1723 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1724 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1725 assert modified_object["inReplyTo"] == []
1726 assert modified_object["inReplyToAtomUri"] == ""
1729 @tag capture_log: true
1730 test "returns modified object when allowed incoming reply", %{data: data} do
1735 "https://shitposter.club/notice/2827873"
1738 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1739 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1741 assert modified_object["inReplyTo"] ==
1742 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1744 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1746 assert modified_object["conversation"] ==
1747 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1749 assert modified_object["context"] ==
1750 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1754 describe "fix_url/1" do
1755 test "fixes data for object when url is map" do
1759 "mimeType" => "video/mp4",
1760 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1764 assert Transmogrifier.fix_url(object) == %{
1765 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1769 test "fixes data for video object" do
1775 "mimeType" => "video/mp4",
1776 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1780 "mimeType" => "video/mp4",
1781 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1785 "mimeType" => "text/html",
1786 "href" => "https://peertube.-2d4c2d1630e3"
1790 "mimeType" => "text/html",
1791 "href" => "https://peertube.-2d4c2d16377-42"
1796 assert Transmogrifier.fix_url(object) == %{
1799 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1800 "mimeType" => "video/mp4",
1805 "url" => "https://peertube.-2d4c2d1630e3"
1809 test "fixes url for not Video object" do
1815 "mimeType" => "text/html",
1816 "href" => "https://peertube.-2d4c2d1630e3"
1820 "mimeType" => "text/html",
1821 "href" => "https://peertube.-2d4c2d16377-42"
1826 assert Transmogrifier.fix_url(object) == %{
1828 "url" => "https://peertube.-2d4c2d1630e3"
1831 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1837 test "retunrs not modified object" do
1838 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1842 describe "get_obj_helper/2" do
1843 test "returns nil when cannot normalize object" do
1844 assert capture_log(fn ->
1845 refute Transmogrifier.get_obj_helper("test-obj-id")
1846 end) =~ "Unsupported URI scheme"
1849 @tag capture_log: true
1850 test "returns {:ok, %Object{}} for success case" do
1851 assert {:ok, %Object{}} =
1852 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1856 describe "fix_attachments/1" do
1857 test "returns not modified object" do
1858 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1859 assert Transmogrifier.fix_attachments(data) == data
1862 test "returns modified object when attachment is map" do
1863 assert Transmogrifier.fix_attachments(%{
1865 "mediaType" => "video/mp4",
1866 "url" => "https://peertube.moe/stat-480.mp4"
1871 "mediaType" => "video/mp4",
1873 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1880 test "returns modified object when attachment is list" do
1881 assert Transmogrifier.fix_attachments(%{
1883 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1884 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1889 "mediaType" => "video/mp4",
1891 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1895 "mediaType" => "video/mp4",
1897 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1905 describe "fix_emoji/1" do
1906 test "returns not modified object when object not contains tags" do
1907 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1908 assert Transmogrifier.fix_emoji(data) == data
1911 test "returns object with emoji when object contains list tags" do
1912 assert Transmogrifier.fix_emoji(%{
1914 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1915 %{"type" => "Hashtag"}
1918 "emoji" => %{"bib" => "/test"},
1920 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1921 %{"type" => "Hashtag"}
1926 test "returns object with emoji when object contains map tag" do
1927 assert Transmogrifier.fix_emoji(%{
1928 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1930 "emoji" => %{"bib" => "/test"},
1931 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1936 describe "set_replies/1" do
1937 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1939 test "returns unmodified object if activity doesn't have self-replies" do
1940 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1941 assert Transmogrifier.set_replies(data) == data
1944 test "sets `replies` collection with a limited number of self-replies" do
1945 [user, another_user] = insert_list(2, :user)
1947 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
1949 {:ok, %{id: id2} = self_reply1} =
1950 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
1952 {:ok, self_reply2} =
1953 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
1955 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1957 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
1960 CommonAPI.post(user, %{
1961 "status" => "self-reply to self-reply",
1962 "in_reply_to_status_id" => id2
1966 CommonAPI.post(another_user, %{
1967 "status" => "another user's reply",
1968 "in_reply_to_status_id" => id1
1971 object = Object.normalize(activity)
1972 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1974 assert %{"type" => "Collection", "items" => ^replies_uris} =
1975 Transmogrifier.set_replies(object.data)["replies"]
1979 test "take_emoji_tags/1" do
1980 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1982 assert Transmogrifier.take_emoji_tags(user) == [
1984 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1985 "id" => "https://example.org/firefox.png",
1986 "name" => ":firefox:",
1988 "updated" => "1970-01-01T00:00:00Z"