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 incomming unfollows with an existing follow" do
692 File.read!("test/fixtures/mastodon-follow-activity.json")
694 |> Map.put("object", user.ap_id)
696 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
699 File.read!("test/fixtures/mastodon-unfollow-activity.json")
701 |> Map.put("object", follow_data)
703 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
705 assert data["type"] == "Undo"
706 assert data["object"]["type"] == "Follow"
707 assert data["object"]["object"] == user.ap_id
708 assert data["actor"] == "http://mastodon.example.org/users/admin"
710 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
713 test "it works for incoming follows to locked account" do
714 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
715 user = insert(:user, locked: true)
718 File.read!("test/fixtures/mastodon-follow-activity.json")
720 |> Map.put("object", user.ap_id)
722 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
724 assert data["type"] == "Follow"
725 assert data["object"] == user.ap_id
726 assert data["state"] == "pending"
727 assert data["actor"] == "http://mastodon.example.org/users/admin"
729 assert [^pending_follower] = User.get_follow_requests(user)
732 test "it works for incoming blocks" do
736 File.read!("test/fixtures/mastodon-block-activity.json")
738 |> Map.put("object", user.ap_id)
740 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
742 assert data["type"] == "Block"
743 assert data["object"] == user.ap_id
744 assert data["actor"] == "http://mastodon.example.org/users/admin"
746 blocker = User.get_cached_by_ap_id(data["actor"])
748 assert User.blocks?(blocker, user)
751 test "incoming blocks successfully tear down any follow relationship" do
752 blocker = insert(:user)
753 blocked = insert(:user)
756 File.read!("test/fixtures/mastodon-block-activity.json")
758 |> Map.put("object", blocked.ap_id)
759 |> Map.put("actor", blocker.ap_id)
761 {:ok, blocker} = User.follow(blocker, blocked)
762 {:ok, blocked} = User.follow(blocked, blocker)
764 assert User.following?(blocker, blocked)
765 assert User.following?(blocked, blocker)
767 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
769 assert data["type"] == "Block"
770 assert data["object"] == blocked.ap_id
771 assert data["actor"] == blocker.ap_id
773 blocker = User.get_cached_by_ap_id(data["actor"])
774 blocked = User.get_cached_by_ap_id(data["object"])
776 assert User.blocks?(blocker, blocked)
778 refute User.following?(blocker, blocked)
779 refute User.following?(blocked, blocker)
782 test "it works for incoming accepts which were pre-accepted" do
783 follower = insert(:user)
784 followed = insert(:user)
786 {:ok, follower} = User.follow(follower, followed)
787 assert User.following?(follower, followed) == true
789 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
792 File.read!("test/fixtures/mastodon-accept-activity.json")
794 |> Map.put("actor", followed.ap_id)
797 accept_data["object"]
798 |> Map.put("actor", follower.ap_id)
799 |> Map.put("id", follow_activity.data["id"])
801 accept_data = Map.put(accept_data, "object", object)
803 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
804 refute activity.local
806 assert activity.data["object"] == follow_activity.data["id"]
808 assert activity.data["id"] == accept_data["id"]
810 follower = User.get_cached_by_id(follower.id)
812 assert User.following?(follower, followed) == true
815 test "it works for incoming accepts which were orphaned" do
816 follower = insert(:user)
817 followed = insert(:user, locked: true)
819 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
822 File.read!("test/fixtures/mastodon-accept-activity.json")
824 |> Map.put("actor", followed.ap_id)
827 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
829 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
830 assert activity.data["object"] == follow_activity.data["id"]
832 follower = User.get_cached_by_id(follower.id)
834 assert User.following?(follower, followed) == true
837 test "it works for incoming accepts which are referenced by IRI only" do
838 follower = insert(:user)
839 followed = insert(:user, locked: true)
841 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
844 File.read!("test/fixtures/mastodon-accept-activity.json")
846 |> Map.put("actor", followed.ap_id)
847 |> Map.put("object", follow_activity.data["id"])
849 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
850 assert activity.data["object"] == follow_activity.data["id"]
852 follower = User.get_cached_by_id(follower.id)
854 assert User.following?(follower, followed) == true
857 test "it fails for incoming accepts which cannot be correlated" do
858 follower = insert(:user)
859 followed = insert(:user, locked: true)
862 File.read!("test/fixtures/mastodon-accept-activity.json")
864 |> Map.put("actor", followed.ap_id)
867 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
869 :error = Transmogrifier.handle_incoming(accept_data)
871 follower = User.get_cached_by_id(follower.id)
873 refute User.following?(follower, followed) == true
876 test "it fails for incoming rejects which cannot be correlated" do
877 follower = insert(:user)
878 followed = insert(:user, locked: true)
881 File.read!("test/fixtures/mastodon-reject-activity.json")
883 |> Map.put("actor", followed.ap_id)
886 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
888 :error = Transmogrifier.handle_incoming(accept_data)
890 follower = User.get_cached_by_id(follower.id)
892 refute User.following?(follower, followed) == true
895 test "it works for incoming rejects which are orphaned" do
896 follower = insert(:user)
897 followed = insert(:user, locked: true)
899 {:ok, follower} = User.follow(follower, followed)
900 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
902 assert User.following?(follower, followed) == true
905 File.read!("test/fixtures/mastodon-reject-activity.json")
907 |> Map.put("actor", followed.ap_id)
910 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
912 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
913 refute activity.local
914 assert activity.data["id"] == reject_data["id"]
916 follower = User.get_cached_by_id(follower.id)
918 assert User.following?(follower, followed) == false
921 test "it works for incoming rejects which are referenced by IRI only" do
922 follower = insert(:user)
923 followed = insert(:user, locked: true)
925 {:ok, follower} = User.follow(follower, followed)
926 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
928 assert User.following?(follower, followed) == true
931 File.read!("test/fixtures/mastodon-reject-activity.json")
933 |> Map.put("actor", followed.ap_id)
934 |> Map.put("object", follow_activity.data["id"])
936 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
938 follower = User.get_cached_by_id(follower.id)
940 assert User.following?(follower, followed) == false
943 test "it rejects activities without a valid ID" do
947 File.read!("test/fixtures/mastodon-follow-activity.json")
949 |> Map.put("object", user.ap_id)
952 :error = Transmogrifier.handle_incoming(data)
955 test "skip converting the content when it is nil" do
956 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
958 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
961 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
963 assert result["content"] == nil
966 test "it converts content of object to html" do
967 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
969 {:ok, %{"content" => content_markdown}} =
970 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
972 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
973 Fetcher.fetch_object_from_id(object_id)
975 assert content_markdown ==
976 "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..."
979 "<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>"
981 assert object.data["mediaType"] == "text/html"
984 test "it remaps video URLs as attachments if necessary" do
986 Fetcher.fetch_object_from_id(
987 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
992 "mediaType" => "video/mp4",
996 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
997 "mediaType" => "video/mp4"
1002 assert object.data["url"] ==
1003 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1005 assert object.data["attachment"] == [attachment]
1008 test "it accepts Flag activities" do
1009 user = insert(:user)
1010 other_user = insert(:user)
1012 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1013 object = Object.normalize(activity)
1017 "id" => activity.data["id"],
1018 "content" => "test post",
1019 "published" => object.data["published"],
1020 "actor" => AccountView.render("show.json", %{user: user})
1024 "@context" => "https://www.w3.org/ns/activitystreams",
1025 "cc" => [user.ap_id],
1026 "object" => [user.ap_id, activity.data["id"]],
1028 "content" => "blocked AND reported!!!",
1029 "actor" => other_user.ap_id
1032 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1034 assert activity.data["object"] == [user.ap_id, note_obj]
1035 assert activity.data["content"] == "blocked AND reported!!!"
1036 assert activity.data["actor"] == other_user.ap_id
1037 assert activity.data["cc"] == [user.ap_id]
1040 test "it correctly processes messages with non-array to field" do
1041 user = insert(:user)
1044 "@context" => "https://www.w3.org/ns/activitystreams",
1045 "to" => "https://www.w3.org/ns/activitystreams#Public",
1048 "content" => "blah blah blah",
1050 "attributedTo" => user.ap_id,
1053 "actor" => user.ap_id
1056 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1058 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1061 test "it correctly processes messages with non-array cc field" do
1062 user = insert(:user)
1065 "@context" => "https://www.w3.org/ns/activitystreams",
1066 "to" => user.follower_address,
1067 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1070 "content" => "blah blah blah",
1072 "attributedTo" => user.ap_id,
1075 "actor" => user.ap_id
1078 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1080 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1081 assert [user.follower_address] == activity.data["to"]
1084 test "it accepts Move activities" do
1085 old_user = insert(:user)
1086 new_user = insert(:user)
1089 "@context" => "https://www.w3.org/ns/activitystreams",
1091 "actor" => old_user.ap_id,
1092 "object" => old_user.ap_id,
1093 "target" => new_user.ap_id
1096 assert :error = Transmogrifier.handle_incoming(message)
1098 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1100 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1101 assert activity.actor == old_user.ap_id
1102 assert activity.data["actor"] == old_user.ap_id
1103 assert activity.data["object"] == old_user.ap_id
1104 assert activity.data["target"] == new_user.ap_id
1105 assert activity.data["type"] == "Move"
1109 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1110 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1111 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1115 "test/fixtures/mastodon-post-activity.json"
1119 items = get_in(data, ["object", "replies", "first", "items"])
1120 assert length(items) > 0
1122 %{data: data, items: items}
1125 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1129 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1131 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1134 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1135 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1139 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1141 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1143 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1145 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1149 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1150 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1151 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1154 user = insert(:user)
1156 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1159 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1162 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1164 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1166 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1168 Repo.delete(activity.object)
1169 Repo.delete(activity)
1171 %{federation_output: federation_output, replies_uris: replies_uris}
1174 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1175 federation_output: federation_output,
1176 replies_uris: replies_uris
1178 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1180 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1182 for id <- replies_uris do
1183 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1184 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1188 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1189 %{federation_output: federation_output} do
1190 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1192 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1194 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1198 describe "prepare outgoing" do
1199 test "it inlines private announced objects" do
1200 user = insert(:user)
1202 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1204 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1206 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1208 assert modified["object"]["content"] == "hey"
1209 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1212 test "it turns mentions into tags" do
1213 user = insert(:user)
1214 other_user = insert(:user)
1217 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1219 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1220 object = modified["object"]
1222 expected_mention = %{
1223 "href" => other_user.ap_id,
1224 "name" => "@#{other_user.nickname}",
1229 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1230 "type" => "Hashtag",
1234 assert Enum.member?(object["tag"], expected_tag)
1235 assert Enum.member?(object["tag"], expected_mention)
1238 test "it adds the sensitive property" do
1239 user = insert(:user)
1241 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1242 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1244 assert modified["object"]["sensitive"]
1247 test "it adds the json-ld context and the conversation property" do
1248 user = insert(:user)
1250 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1251 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1253 assert modified["@context"] ==
1254 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1256 assert modified["object"]["conversation"] == modified["context"]
1259 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1260 user = insert(:user)
1262 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1263 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1265 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1268 test "it strips internal hashtag data" do
1269 user = insert(:user)
1271 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1274 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1275 "type" => "Hashtag",
1279 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1281 assert modified["object"]["tag"] == [expected_tag]
1284 test "it strips internal fields" do
1285 user = insert(:user)
1287 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1289 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1291 assert length(modified["object"]["tag"]) == 2
1293 assert is_nil(modified["object"]["emoji"])
1294 assert is_nil(modified["object"]["like_count"])
1295 assert is_nil(modified["object"]["announcements"])
1296 assert is_nil(modified["object"]["announcement_count"])
1297 assert is_nil(modified["object"]["context_id"])
1300 test "it strips internal fields of article" do
1301 activity = insert(:article_activity)
1303 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1305 assert length(modified["object"]["tag"]) == 2
1307 assert is_nil(modified["object"]["emoji"])
1308 assert is_nil(modified["object"]["like_count"])
1309 assert is_nil(modified["object"]["announcements"])
1310 assert is_nil(modified["object"]["announcement_count"])
1311 assert is_nil(modified["object"]["context_id"])
1312 assert is_nil(modified["object"]["likes"])
1315 test "the directMessage flag is present" do
1316 user = insert(:user)
1317 other_user = insert(:user)
1319 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1321 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1323 assert modified["directMessage"] == false
1326 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1328 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1330 assert modified["directMessage"] == false
1333 CommonAPI.post(user, %{
1334 "status" => "@#{other_user.nickname} :moominmamma:",
1335 "visibility" => "direct"
1338 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1340 assert modified["directMessage"] == true
1343 test "it strips BCC field" do
1344 user = insert(:user)
1345 {:ok, list} = Pleroma.List.create("foo", user)
1348 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1350 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1352 assert is_nil(modified["bcc"])
1355 test "it can handle Listen activities" do
1356 listen_activity = insert(:listen)
1358 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1360 assert modified["type"] == "Listen"
1362 user = insert(:user)
1364 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1366 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1370 describe "user upgrade" do
1371 test "it upgrades a user to activitypub" do
1374 nickname: "rye@niu.moe",
1376 ap_id: "https://niu.moe/users/rye",
1377 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1380 user_two = insert(:user)
1381 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1383 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1384 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1385 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1387 user = User.get_cached_by_id(user.id)
1388 assert user.note_count == 1
1390 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1391 ObanHelpers.perform_all()
1393 assert user.ap_enabled
1394 assert user.note_count == 1
1395 assert user.follower_address == "https://niu.moe/users/rye/followers"
1396 assert user.following_address == "https://niu.moe/users/rye/following"
1398 user = User.get_cached_by_id(user.id)
1399 assert user.note_count == 1
1401 activity = Activity.get_by_id(activity.id)
1402 assert user.follower_address in activity.recipients
1408 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1417 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1422 refute "..." in activity.recipients
1424 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1425 refute user.follower_address in unrelated_activity.recipients
1427 user_two = User.get_cached_by_id(user_two.id)
1428 assert User.following?(user_two, user)
1429 refute "..." in User.following(user_two)
1433 describe "actor rewriting" do
1434 test "it fixes the actor URL property to be a proper URI" do
1436 "url" => %{"href" => "http://example.com"}
1439 rewritten = Transmogrifier.maybe_fix_user_object(data)
1440 assert rewritten["url"] == "http://example.com"
1444 describe "actor origin containment" do
1445 test "it rejects activities which reference objects with bogus origins" do
1447 "@context" => "https://www.w3.org/ns/activitystreams",
1448 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1449 "actor" => "http://mastodon.example.org/users/admin",
1450 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1451 "object" => "https://info.pleroma.site/activity.json",
1452 "type" => "Announce"
1455 assert capture_log(fn ->
1456 :error = Transmogrifier.handle_incoming(data)
1457 end) =~ "Object containment failed"
1460 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1462 "@context" => "https://www.w3.org/ns/activitystreams",
1463 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1464 "actor" => "http://mastodon.example.org/users/admin",
1465 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1466 "object" => "https://info.pleroma.site/activity2.json",
1467 "type" => "Announce"
1470 assert capture_log(fn ->
1471 :error = Transmogrifier.handle_incoming(data)
1472 end) =~ "Object containment failed"
1475 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1477 "@context" => "https://www.w3.org/ns/activitystreams",
1478 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1479 "actor" => "http://mastodon.example.org/users/admin",
1480 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1481 "object" => "https://info.pleroma.site/activity3.json",
1482 "type" => "Announce"
1485 assert capture_log(fn ->
1486 :error = Transmogrifier.handle_incoming(data)
1487 end) =~ "Object containment failed"
1491 describe "reserialization" do
1492 test "successfully reserializes a message with inReplyTo == nil" do
1493 user = insert(:user)
1496 "@context" => "https://www.w3.org/ns/activitystreams",
1497 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1501 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1506 "attributedTo" => user.ap_id
1508 "actor" => user.ap_id
1511 {:ok, activity} = Transmogrifier.handle_incoming(message)
1513 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1516 test "successfully reserializes a message with AS2 objects in IR" do
1517 user = insert(:user)
1520 "@context" => "https://www.w3.org/ns/activitystreams",
1521 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1525 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1530 "attributedTo" => user.ap_id,
1532 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1533 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1536 "actor" => user.ap_id
1539 {:ok, activity} = Transmogrifier.handle_incoming(message)
1541 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1545 test "Rewrites Answers to Notes" do
1546 user = insert(:user)
1548 {:ok, poll_activity} =
1549 CommonAPI.post(user, %{
1550 "status" => "suya...",
1551 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1554 poll_object = Object.normalize(poll_activity)
1555 # TODO: Replace with CommonAPI vote creation when implemented
1557 File.read!("test/fixtures/mastodon-vote.json")
1559 |> Kernel.put_in(["to"], user.ap_id)
1560 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1561 |> Kernel.put_in(["object", "to"], user.ap_id)
1563 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1564 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1566 assert data["object"]["type"] == "Note"
1569 describe "fix_explicit_addressing" do
1571 user = insert(:user)
1575 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1576 explicitly_mentioned_actors = [
1577 "https://pleroma.gold/users/user1",
1578 "https://pleroma.gold/user2"
1582 "actor" => user.ap_id,
1583 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1586 Enum.map(explicitly_mentioned_actors, fn href ->
1587 %{"type" => "Mention", "href" => href}
1591 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1592 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1593 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1594 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1597 test "does not move actor's follower collection to cc", %{user: user} do
1599 "actor" => user.ap_id,
1600 "to" => [user.follower_address],
1604 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1605 assert user.follower_address in fixed_object["to"]
1606 refute user.follower_address in fixed_object["cc"]
1609 test "removes recipient's follower collection from cc", %{user: user} do
1610 recipient = insert(:user)
1613 "actor" => user.ap_id,
1614 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1615 "cc" => [user.follower_address, recipient.follower_address]
1618 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1620 assert user.follower_address in fixed_object["cc"]
1621 refute recipient.follower_address in fixed_object["cc"]
1622 refute recipient.follower_address in fixed_object["to"]
1626 describe "fix_summary/1" do
1627 test "returns fixed object" do
1628 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1629 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1630 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1634 describe "fix_in_reply_to/2" do
1635 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1638 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1642 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1643 assert Transmogrifier.fix_in_reply_to(data) == data
1646 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1647 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1650 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1652 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1653 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1654 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1657 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1659 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1660 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1661 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1664 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1666 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1667 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1668 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1670 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1671 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1672 assert modified_object["inReplyTo"] == []
1673 assert modified_object["inReplyToAtomUri"] == ""
1676 @tag capture_log: true
1677 test "returns modified object when allowed incoming reply", %{data: data} do
1682 "https://shitposter.club/notice/2827873"
1685 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1686 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1688 assert modified_object["inReplyTo"] ==
1689 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1691 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1693 assert modified_object["conversation"] ==
1694 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1696 assert modified_object["context"] ==
1697 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1701 describe "fix_url/1" do
1702 test "fixes data for object when url is map" do
1706 "mimeType" => "video/mp4",
1707 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1711 assert Transmogrifier.fix_url(object) == %{
1712 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1716 test "fixes data for video object" do
1722 "mimeType" => "video/mp4",
1723 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1727 "mimeType" => "video/mp4",
1728 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1732 "mimeType" => "text/html",
1733 "href" => "https://peertube.-2d4c2d1630e3"
1737 "mimeType" => "text/html",
1738 "href" => "https://peertube.-2d4c2d16377-42"
1743 assert Transmogrifier.fix_url(object) == %{
1746 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1747 "mimeType" => "video/mp4",
1752 "url" => "https://peertube.-2d4c2d1630e3"
1756 test "fixes url for not Video object" do
1762 "mimeType" => "text/html",
1763 "href" => "https://peertube.-2d4c2d1630e3"
1767 "mimeType" => "text/html",
1768 "href" => "https://peertube.-2d4c2d16377-42"
1773 assert Transmogrifier.fix_url(object) == %{
1775 "url" => "https://peertube.-2d4c2d1630e3"
1778 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1784 test "retunrs not modified object" do
1785 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1789 describe "get_obj_helper/2" do
1790 test "returns nil when cannot normalize object" do
1791 assert capture_log(fn ->
1792 refute Transmogrifier.get_obj_helper("test-obj-id")
1793 end) =~ "Unsupported URI scheme"
1796 @tag capture_log: true
1797 test "returns {:ok, %Object{}} for success case" do
1798 assert {:ok, %Object{}} =
1799 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1803 describe "fix_attachments/1" do
1804 test "returns not modified object" do
1805 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1806 assert Transmogrifier.fix_attachments(data) == data
1809 test "returns modified object when attachment is map" do
1810 assert Transmogrifier.fix_attachments(%{
1812 "mediaType" => "video/mp4",
1813 "url" => "https://peertube.moe/stat-480.mp4"
1818 "mediaType" => "video/mp4",
1820 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1827 test "returns modified object when attachment is list" do
1828 assert Transmogrifier.fix_attachments(%{
1830 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1831 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1836 "mediaType" => "video/mp4",
1838 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1842 "mediaType" => "video/mp4",
1844 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1852 describe "fix_emoji/1" do
1853 test "returns not modified object when object not contains tags" do
1854 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1855 assert Transmogrifier.fix_emoji(data) == data
1858 test "returns object with emoji when object contains list tags" do
1859 assert Transmogrifier.fix_emoji(%{
1861 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1862 %{"type" => "Hashtag"}
1865 "emoji" => %{"bib" => "/test"},
1867 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1868 %{"type" => "Hashtag"}
1873 test "returns object with emoji when object contains map tag" do
1874 assert Transmogrifier.fix_emoji(%{
1875 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1877 "emoji" => %{"bib" => "/test"},
1878 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1883 describe "set_replies/1" do
1884 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1886 test "returns unmodified object if activity doesn't have self-replies" do
1887 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1888 assert Transmogrifier.set_replies(data) == data
1891 test "sets `replies` collection with a limited number of self-replies" do
1892 [user, another_user] = insert_list(2, :user)
1894 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
1896 {:ok, %{id: id2} = self_reply1} =
1897 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
1899 {:ok, self_reply2} =
1900 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
1902 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1904 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
1907 CommonAPI.post(user, %{
1908 "status" => "self-reply to self-reply",
1909 "in_reply_to_status_id" => id2
1913 CommonAPI.post(another_user, %{
1914 "status" => "another user's reply",
1915 "in_reply_to_status_id" => id1
1918 object = Object.normalize(activity)
1919 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1921 assert %{"type" => "Collection", "items" => ^replies_uris} =
1922 Transmogrifier.set_replies(object.data)["replies"]
1926 test "take_emoji_tags/1" do
1927 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1929 assert Transmogrifier.take_emoji_tags(user) == [
1931 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1932 "id" => "https://example.org/firefox.png",
1933 "name" => ":firefox:",
1935 "updated" => "1970-01-01T00:00:00Z"