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, %{
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 honk announces" do
264 _user = insert(:user, ap_id: "https://honktest/u/test", local: false)
265 other_user = insert(:user)
266 {:ok, post} = CommonAPI.post(other_user, %{status: "bonkeronk"})
269 "@context" => "https://www.w3.org/ns/activitystreams",
270 "actor" => "https://honktest/u/test",
271 "id" => "https://honktest/u/test/bonk/1793M7B9MQ48847vdx",
272 "object" => post.data["object"],
273 "published" => "2019-06-25T19:33:58Z",
274 "to" => "https://www.w3.org/ns/activitystreams#Public",
278 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(announce)
281 test "it works for incoming announces with actor being inlined (kroeg)" do
282 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
284 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
286 assert data["actor"] == "https://puckipedia.com/"
289 test "it works for incoming notices with tag not being an array (kroeg)" do
290 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
292 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
293 object = Object.normalize(data["object"])
295 assert object.data["emoji"] == %{
296 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
299 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
301 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
302 object = Object.normalize(data["object"])
304 assert "test" in object.data["tag"]
307 test "it works for incoming notices with url not being a string (prismo)" do
308 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
310 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
311 object = Object.normalize(data["object"])
313 assert object.data["url"] == "https://prismo.news/posts/83"
316 test "it cleans up incoming notices which are not really DMs" do
318 other_user = insert(:user)
320 to = [user.ap_id, other_user.ap_id]
323 File.read!("test/fixtures/mastodon-post-activity.json")
333 data = Map.put(data, "object", object)
335 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
337 assert data["to"] == []
338 assert data["cc"] == to
340 object_data = Object.normalize(activity).data
342 assert object_data["to"] == []
343 assert object_data["cc"] == to
346 test "it works for incoming announces" do
347 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
349 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
351 assert data["actor"] == "http://mastodon.example.org/users/admin"
352 assert data["type"] == "Announce"
355 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
357 assert data["object"] ==
358 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
360 assert Activity.get_create_by_object_ap_id(data["object"])
363 test "it works for incoming announces with an existing activity" do
365 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
368 File.read!("test/fixtures/mastodon-announce.json")
370 |> Map.put("object", activity.data["object"])
372 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
374 assert data["actor"] == "http://mastodon.example.org/users/admin"
375 assert data["type"] == "Announce"
378 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
380 assert data["object"] == activity.data["object"]
382 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
385 test "it works for incoming announces with an inlined activity" do
387 File.read!("test/fixtures/mastodon-announce-private.json")
390 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
392 assert data["actor"] == "http://mastodon.example.org/users/admin"
393 assert data["type"] == "Announce"
396 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
398 object = Object.normalize(data["object"])
400 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
401 assert object.data["content"] == "this is a private toot"
404 @tag capture_log: true
405 test "it rejects incoming announces with an inlined activity from another origin" do
407 File.read!("test/fixtures/bogus-mastodon-announce.json")
410 assert :error = Transmogrifier.handle_incoming(data)
413 test "it does not clobber the addressing on announce activities" do
415 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
418 File.read!("test/fixtures/mastodon-announce.json")
420 |> Map.put("object", Object.normalize(activity).data["id"])
421 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
424 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
426 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
429 test "it ensures that as:Public activities make it to their followers collection" do
433 File.read!("test/fixtures/mastodon-post-activity.json")
435 |> Map.put("actor", user.ap_id)
436 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
441 |> Map.put("attributedTo", user.ap_id)
442 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
444 |> Map.put("id", user.ap_id <> "/activities/12345678")
446 data = Map.put(data, "object", object)
448 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
450 assert data["cc"] == [User.ap_followers(user)]
453 test "it ensures that address fields become lists" do
457 File.read!("test/fixtures/mastodon-post-activity.json")
459 |> Map.put("actor", user.ap_id)
460 |> Map.put("to", nil)
461 |> Map.put("cc", nil)
465 |> Map.put("attributedTo", user.ap_id)
466 |> Map.put("to", nil)
467 |> Map.put("cc", nil)
468 |> Map.put("id", user.ap_id <> "/activities/12345678")
470 data = Map.put(data, "object", object)
472 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
474 assert !is_nil(data["to"])
475 assert !is_nil(data["cc"])
478 test "it strips internal likes" do
480 File.read!("test/fixtures/mastodon-post-activity.json")
485 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
486 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
488 "type" => "OrderedCollection"
491 object = Map.put(data["object"], "likes", likes)
492 data = Map.put(data, "object", object)
494 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
496 refute Map.has_key?(object.data, "likes")
499 test "it strips internal reactions" do
501 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
502 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
504 %{object: object} = Activity.get_by_id_with_object(activity.id)
505 assert Map.has_key?(object.data, "reactions")
506 assert Map.has_key?(object.data, "reaction_count")
508 object_data = Transmogrifier.strip_internal_fields(object.data)
509 refute Map.has_key?(object_data, "reactions")
510 refute Map.has_key?(object_data, "reaction_count")
513 test "it works for incoming update activities" do
514 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
516 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
517 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
520 update_data["object"]
521 |> Map.put("actor", data["actor"])
522 |> Map.put("id", data["actor"])
526 |> Map.put("actor", data["actor"])
527 |> Map.put("object", object)
529 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
531 assert data["id"] == update_data["id"]
533 user = User.get_cached_by_ap_id(data["actor"])
534 assert user.name == "gargle"
536 assert user.avatar["url"] == [
539 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
543 assert user.banner["url"] == [
546 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
550 assert user.bio == "<p>Some bio</p>"
553 test "it works with alsoKnownAs" do
554 {:ok, %Activity{data: %{"actor" => actor}}} =
555 "test/fixtures/mastodon-post-activity.json"
558 |> Transmogrifier.handle_incoming()
560 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
563 "test/fixtures/mastodon-update.json"
566 |> Map.put("actor", actor)
567 |> Map.update!("object", fn object ->
569 |> Map.put("actor", actor)
570 |> Map.put("id", actor)
571 |> Map.put("alsoKnownAs", [
572 "http://mastodon.example.org/users/foo",
573 "http://example.org/users/bar"
576 |> Transmogrifier.handle_incoming()
578 assert User.get_cached_by_ap_id(actor).also_known_as == [
579 "http://mastodon.example.org/users/foo",
580 "http://example.org/users/bar"
584 test "it works with custom profile fields" do
586 "test/fixtures/mastodon-post-activity.json"
589 |> Transmogrifier.handle_incoming()
591 user = User.get_cached_by_ap_id(activity.actor)
593 assert user.fields == [
594 %{"name" => "foo", "value" => "bar"},
595 %{"name" => "foo1", "value" => "bar1"}
598 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
601 update_data["object"]
602 |> Map.put("actor", user.ap_id)
603 |> Map.put("id", user.ap_id)
607 |> Map.put("actor", user.ap_id)
608 |> Map.put("object", object)
610 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
612 user = User.get_cached_by_ap_id(user.ap_id)
614 assert user.fields == [
615 %{"name" => "foo", "value" => "updated"},
616 %{"name" => "foo1", "value" => "updated"}
619 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
622 put_in(update_data, ["object", "attachment"], [
623 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
624 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
625 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
628 {:ok, _} = Transmogrifier.handle_incoming(update_data)
630 user = User.get_cached_by_ap_id(user.ap_id)
632 assert user.fields == [
633 %{"name" => "foo", "value" => "updated"},
634 %{"name" => "foo1", "value" => "updated"}
637 update_data = put_in(update_data, ["object", "attachment"], [])
639 {:ok, _} = Transmogrifier.handle_incoming(update_data)
641 user = User.get_cached_by_ap_id(user.ap_id)
643 assert user.fields == []
646 test "it works for incoming update activities which lock the account" do
647 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
649 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
650 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
653 update_data["object"]
654 |> Map.put("actor", data["actor"])
655 |> Map.put("id", data["actor"])
656 |> Map.put("manuallyApprovesFollowers", true)
660 |> Map.put("actor", data["actor"])
661 |> Map.put("object", object)
663 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
665 user = User.get_cached_by_ap_id(data["actor"])
666 assert user.locked == true
669 test "it works for incomming unfollows with an existing follow" do
673 File.read!("test/fixtures/mastodon-follow-activity.json")
675 |> Map.put("object", user.ap_id)
677 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
680 File.read!("test/fixtures/mastodon-unfollow-activity.json")
682 |> Map.put("object", follow_data)
684 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
686 assert data["type"] == "Undo"
687 assert data["object"]["type"] == "Follow"
688 assert data["object"]["object"] == user.ap_id
689 assert data["actor"] == "http://mastodon.example.org/users/admin"
691 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
694 test "it works for incoming follows to locked account" do
695 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
696 user = insert(:user, locked: true)
699 File.read!("test/fixtures/mastodon-follow-activity.json")
701 |> Map.put("object", user.ap_id)
703 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
705 assert data["type"] == "Follow"
706 assert data["object"] == user.ap_id
707 assert data["state"] == "pending"
708 assert data["actor"] == "http://mastodon.example.org/users/admin"
710 assert [^pending_follower] = User.get_follow_requests(user)
713 test "it works for incoming blocks" do
717 File.read!("test/fixtures/mastodon-block-activity.json")
719 |> Map.put("object", user.ap_id)
721 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
723 assert data["type"] == "Block"
724 assert data["object"] == user.ap_id
725 assert data["actor"] == "http://mastodon.example.org/users/admin"
727 blocker = User.get_cached_by_ap_id(data["actor"])
729 assert User.blocks?(blocker, user)
732 test "incoming blocks successfully tear down any follow relationship" do
733 blocker = insert(:user)
734 blocked = insert(:user)
737 File.read!("test/fixtures/mastodon-block-activity.json")
739 |> Map.put("object", blocked.ap_id)
740 |> Map.put("actor", blocker.ap_id)
742 {:ok, blocker} = User.follow(blocker, blocked)
743 {:ok, blocked} = User.follow(blocked, blocker)
745 assert User.following?(blocker, blocked)
746 assert User.following?(blocked, blocker)
748 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
750 assert data["type"] == "Block"
751 assert data["object"] == blocked.ap_id
752 assert data["actor"] == blocker.ap_id
754 blocker = User.get_cached_by_ap_id(data["actor"])
755 blocked = User.get_cached_by_ap_id(data["object"])
757 assert User.blocks?(blocker, blocked)
759 refute User.following?(blocker, blocked)
760 refute User.following?(blocked, blocker)
763 test "it works for incoming accepts which were pre-accepted" do
764 follower = insert(:user)
765 followed = insert(:user)
767 {:ok, follower} = User.follow(follower, followed)
768 assert User.following?(follower, followed) == true
770 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
773 File.read!("test/fixtures/mastodon-accept-activity.json")
775 |> Map.put("actor", followed.ap_id)
778 accept_data["object"]
779 |> Map.put("actor", follower.ap_id)
780 |> Map.put("id", follow_activity.data["id"])
782 accept_data = Map.put(accept_data, "object", object)
784 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
785 refute activity.local
787 assert activity.data["object"] == follow_activity.data["id"]
789 assert activity.data["id"] == accept_data["id"]
791 follower = User.get_cached_by_id(follower.id)
793 assert User.following?(follower, followed) == true
796 test "it works for incoming accepts which were orphaned" do
797 follower = insert(:user)
798 followed = insert(:user, locked: true)
800 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
803 File.read!("test/fixtures/mastodon-accept-activity.json")
805 |> Map.put("actor", followed.ap_id)
808 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
810 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
811 assert activity.data["object"] == follow_activity.data["id"]
813 follower = User.get_cached_by_id(follower.id)
815 assert User.following?(follower, followed) == true
818 test "it works for incoming accepts which are referenced by IRI only" do
819 follower = insert(:user)
820 followed = insert(:user, locked: true)
822 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
825 File.read!("test/fixtures/mastodon-accept-activity.json")
827 |> Map.put("actor", followed.ap_id)
828 |> Map.put("object", follow_activity.data["id"])
830 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
831 assert activity.data["object"] == follow_activity.data["id"]
833 follower = User.get_cached_by_id(follower.id)
835 assert User.following?(follower, followed) == true
837 follower = User.get_by_id(follower.id)
838 assert follower.following_count == 1
840 followed = User.get_by_id(followed.id)
841 assert followed.follower_count == 1
844 test "it fails for incoming accepts which cannot be correlated" do
845 follower = insert(:user)
846 followed = insert(:user, locked: true)
849 File.read!("test/fixtures/mastodon-accept-activity.json")
851 |> Map.put("actor", followed.ap_id)
854 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
856 :error = Transmogrifier.handle_incoming(accept_data)
858 follower = User.get_cached_by_id(follower.id)
860 refute User.following?(follower, followed) == true
863 test "it fails for incoming rejects which cannot be correlated" do
864 follower = insert(:user)
865 followed = insert(:user, locked: true)
868 File.read!("test/fixtures/mastodon-reject-activity.json")
870 |> Map.put("actor", followed.ap_id)
873 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
875 :error = Transmogrifier.handle_incoming(accept_data)
877 follower = User.get_cached_by_id(follower.id)
879 refute User.following?(follower, followed) == true
882 test "it works for incoming rejects which are orphaned" do
883 follower = insert(:user)
884 followed = insert(:user, locked: true)
886 {:ok, follower} = User.follow(follower, followed)
887 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
889 assert User.following?(follower, followed) == true
892 File.read!("test/fixtures/mastodon-reject-activity.json")
894 |> Map.put("actor", followed.ap_id)
897 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
899 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
900 refute activity.local
901 assert activity.data["id"] == reject_data["id"]
903 follower = User.get_cached_by_id(follower.id)
905 assert User.following?(follower, followed) == false
908 test "it works for incoming rejects which are referenced by IRI only" do
909 follower = insert(:user)
910 followed = insert(:user, locked: true)
912 {:ok, follower} = User.follow(follower, followed)
913 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
915 assert User.following?(follower, followed) == true
918 File.read!("test/fixtures/mastodon-reject-activity.json")
920 |> Map.put("actor", followed.ap_id)
921 |> Map.put("object", follow_activity.data["id"])
923 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
925 follower = User.get_cached_by_id(follower.id)
927 assert User.following?(follower, followed) == false
930 test "it rejects activities without a valid ID" do
934 File.read!("test/fixtures/mastodon-follow-activity.json")
936 |> Map.put("object", user.ap_id)
939 :error = Transmogrifier.handle_incoming(data)
942 test "skip converting the content when it is nil" do
943 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
945 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
948 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
950 assert result["content"] == nil
953 test "it converts content of object to html" do
954 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
956 {:ok, %{"content" => content_markdown}} =
957 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
959 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
960 Fetcher.fetch_object_from_id(object_id)
962 assert content_markdown ==
963 "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..."
966 "<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>"
968 assert object.data["mediaType"] == "text/html"
971 test "it remaps video URLs as attachments if necessary" do
973 Fetcher.fetch_object_from_id(
974 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
979 "mediaType" => "video/mp4",
983 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
984 "mediaType" => "video/mp4"
989 assert object.data["url"] ==
990 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
992 assert object.data["attachment"] == [attachment]
995 test "it accepts Flag activities" do
997 other_user = insert(:user)
999 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
1000 object = Object.normalize(activity)
1004 "id" => activity.data["id"],
1005 "content" => "test post",
1006 "published" => object.data["published"],
1007 "actor" => AccountView.render("show.json", %{user: user})
1011 "@context" => "https://www.w3.org/ns/activitystreams",
1012 "cc" => [user.ap_id],
1013 "object" => [user.ap_id, activity.data["id"]],
1015 "content" => "blocked AND reported!!!",
1016 "actor" => other_user.ap_id
1019 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1021 assert activity.data["object"] == [user.ap_id, note_obj]
1022 assert activity.data["content"] == "blocked AND reported!!!"
1023 assert activity.data["actor"] == other_user.ap_id
1024 assert activity.data["cc"] == [user.ap_id]
1027 test "it correctly processes messages with non-array to field" do
1028 user = insert(:user)
1031 "@context" => "https://www.w3.org/ns/activitystreams",
1032 "to" => "https://www.w3.org/ns/activitystreams#Public",
1035 "content" => "blah blah blah",
1037 "attributedTo" => user.ap_id,
1040 "actor" => user.ap_id
1043 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1045 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1048 test "it correctly processes messages with non-array cc field" do
1049 user = insert(:user)
1052 "@context" => "https://www.w3.org/ns/activitystreams",
1053 "to" => user.follower_address,
1054 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1057 "content" => "blah blah blah",
1059 "attributedTo" => user.ap_id,
1062 "actor" => user.ap_id
1065 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1067 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1068 assert [user.follower_address] == activity.data["to"]
1071 test "it accepts Move activities" do
1072 old_user = insert(:user)
1073 new_user = insert(:user)
1076 "@context" => "https://www.w3.org/ns/activitystreams",
1078 "actor" => old_user.ap_id,
1079 "object" => old_user.ap_id,
1080 "target" => new_user.ap_id
1083 assert :error = Transmogrifier.handle_incoming(message)
1085 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1087 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1088 assert activity.actor == old_user.ap_id
1089 assert activity.data["actor"] == old_user.ap_id
1090 assert activity.data["object"] == old_user.ap_id
1091 assert activity.data["target"] == new_user.ap_id
1092 assert activity.data["type"] == "Move"
1096 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1097 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1098 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1102 "test/fixtures/mastodon-post-activity.json"
1106 items = get_in(data, ["object", "replies", "first", "items"])
1107 assert length(items) > 0
1109 %{data: data, items: items}
1112 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1116 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1118 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1121 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1122 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1126 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1128 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1130 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1132 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1136 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1137 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1138 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1141 user = insert(:user)
1143 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
1146 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
1149 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
1151 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1153 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1155 Repo.delete(activity.object)
1156 Repo.delete(activity)
1158 %{federation_output: federation_output, replies_uris: replies_uris}
1161 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1162 federation_output: federation_output,
1163 replies_uris: replies_uris
1165 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1167 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1169 for id <- replies_uris do
1170 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1171 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1175 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1176 %{federation_output: federation_output} do
1177 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1179 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1181 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1185 describe "prepare outgoing" do
1186 test "it inlines private announced objects" do
1187 user = insert(:user)
1189 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
1191 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1193 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1195 assert modified["object"]["content"] == "hey"
1196 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1199 test "it turns mentions into tags" do
1200 user = insert(:user)
1201 other_user = insert(:user)
1204 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
1206 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1207 object = modified["object"]
1209 expected_mention = %{
1210 "href" => other_user.ap_id,
1211 "name" => "@#{other_user.nickname}",
1216 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1217 "type" => "Hashtag",
1221 assert Enum.member?(object["tag"], expected_tag)
1222 assert Enum.member?(object["tag"], expected_mention)
1225 test "it adds the sensitive property" do
1226 user = insert(:user)
1228 {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
1229 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1231 assert modified["object"]["sensitive"]
1234 test "it adds the json-ld context and the conversation property" do
1235 user = insert(:user)
1237 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
1238 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1240 assert modified["@context"] ==
1241 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1243 assert modified["object"]["conversation"] == modified["context"]
1246 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1247 user = insert(:user)
1249 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
1250 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1252 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1255 test "it strips internal hashtag data" do
1256 user = insert(:user)
1258 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
1261 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1262 "type" => "Hashtag",
1266 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1268 assert modified["object"]["tag"] == [expected_tag]
1271 test "it strips internal fields" do
1272 user = insert(:user)
1274 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu :firefox:"})
1276 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1278 assert length(modified["object"]["tag"]) == 2
1280 assert is_nil(modified["object"]["emoji"])
1281 assert is_nil(modified["object"]["like_count"])
1282 assert is_nil(modified["object"]["announcements"])
1283 assert is_nil(modified["object"]["announcement_count"])
1284 assert is_nil(modified["object"]["context_id"])
1287 test "it strips internal fields of article" do
1288 activity = insert(:article_activity)
1290 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1292 assert length(modified["object"]["tag"]) == 2
1294 assert is_nil(modified["object"]["emoji"])
1295 assert is_nil(modified["object"]["like_count"])
1296 assert is_nil(modified["object"]["announcements"])
1297 assert is_nil(modified["object"]["announcement_count"])
1298 assert is_nil(modified["object"]["context_id"])
1299 assert is_nil(modified["object"]["likes"])
1302 test "the directMessage flag is present" do
1303 user = insert(:user)
1304 other_user = insert(:user)
1306 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
1308 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1310 assert modified["directMessage"] == false
1312 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
1314 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1316 assert modified["directMessage"] == false
1319 CommonAPI.post(user, %{
1320 status: "@#{other_user.nickname} :moominmamma:",
1321 visibility: "direct"
1324 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1326 assert modified["directMessage"] == true
1329 test "it strips BCC field" do
1330 user = insert(:user)
1331 {:ok, list} = Pleroma.List.create("foo", user)
1333 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1335 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1337 assert is_nil(modified["bcc"])
1340 test "it can handle Listen activities" do
1341 listen_activity = insert(:listen)
1343 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1345 assert modified["type"] == "Listen"
1347 user = insert(:user)
1349 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1351 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1355 describe "user upgrade" do
1356 test "it upgrades a user to activitypub" do
1359 nickname: "rye@niu.moe",
1361 ap_id: "https://niu.moe/users/rye",
1362 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1365 user_two = insert(:user)
1366 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1368 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1369 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
1370 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1372 user = User.get_cached_by_id(user.id)
1373 assert user.note_count == 1
1375 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1376 ObanHelpers.perform_all()
1378 assert user.ap_enabled
1379 assert user.note_count == 1
1380 assert user.follower_address == "https://niu.moe/users/rye/followers"
1381 assert user.following_address == "https://niu.moe/users/rye/following"
1383 user = User.get_cached_by_id(user.id)
1384 assert user.note_count == 1
1386 activity = Activity.get_by_id(activity.id)
1387 assert user.follower_address in activity.recipients
1393 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1402 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1407 refute "..." in activity.recipients
1409 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1410 refute user.follower_address in unrelated_activity.recipients
1412 user_two = User.get_cached_by_id(user_two.id)
1413 assert User.following?(user_two, user)
1414 refute "..." in User.following(user_two)
1418 describe "actor rewriting" do
1419 test "it fixes the actor URL property to be a proper URI" do
1421 "url" => %{"href" => "http://example.com"}
1424 rewritten = Transmogrifier.maybe_fix_user_object(data)
1425 assert rewritten["url"] == "http://example.com"
1429 describe "actor origin containment" do
1430 test "it rejects activities which reference objects with bogus origins" do
1432 "@context" => "https://www.w3.org/ns/activitystreams",
1433 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1434 "actor" => "http://mastodon.example.org/users/admin",
1435 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1436 "object" => "https://info.pleroma.site/activity.json",
1437 "type" => "Announce"
1440 assert capture_log(fn ->
1441 :error = Transmogrifier.handle_incoming(data)
1442 end) =~ "Object containment failed"
1445 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" 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/activity2.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 2)" 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/activity3.json",
1467 "type" => "Announce"
1470 assert capture_log(fn ->
1471 :error = Transmogrifier.handle_incoming(data)
1472 end) =~ "Object containment failed"
1476 describe "reserialization" do
1477 test "successfully reserializes a message with inReplyTo == nil" do
1478 user = insert(:user)
1481 "@context" => "https://www.w3.org/ns/activitystreams",
1482 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1486 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1491 "attributedTo" => user.ap_id
1493 "actor" => user.ap_id
1496 {:ok, activity} = Transmogrifier.handle_incoming(message)
1498 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1501 test "successfully reserializes a message with AS2 objects in IR" do
1502 user = insert(:user)
1505 "@context" => "https://www.w3.org/ns/activitystreams",
1506 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1510 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1515 "attributedTo" => user.ap_id,
1517 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1518 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1521 "actor" => user.ap_id
1524 {:ok, activity} = Transmogrifier.handle_incoming(message)
1526 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1530 test "Rewrites Answers to Notes" do
1531 user = insert(:user)
1533 {:ok, poll_activity} =
1534 CommonAPI.post(user, %{
1536 poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
1539 poll_object = Object.normalize(poll_activity)
1540 # TODO: Replace with CommonAPI vote creation when implemented
1542 File.read!("test/fixtures/mastodon-vote.json")
1544 |> Kernel.put_in(["to"], user.ap_id)
1545 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1546 |> Kernel.put_in(["object", "to"], user.ap_id)
1548 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1549 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1551 assert data["object"]["type"] == "Note"
1554 describe "fix_explicit_addressing" do
1556 user = insert(:user)
1560 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1561 explicitly_mentioned_actors = [
1562 "https://pleroma.gold/users/user1",
1563 "https://pleroma.gold/user2"
1567 "actor" => user.ap_id,
1568 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1571 Enum.map(explicitly_mentioned_actors, fn href ->
1572 %{"type" => "Mention", "href" => href}
1576 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1577 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1578 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1579 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1582 test "does not move actor's follower collection to cc", %{user: user} do
1584 "actor" => user.ap_id,
1585 "to" => [user.follower_address],
1589 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1590 assert user.follower_address in fixed_object["to"]
1591 refute user.follower_address in fixed_object["cc"]
1594 test "removes recipient's follower collection from cc", %{user: user} do
1595 recipient = insert(:user)
1598 "actor" => user.ap_id,
1599 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1600 "cc" => [user.follower_address, recipient.follower_address]
1603 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1605 assert user.follower_address in fixed_object["cc"]
1606 refute recipient.follower_address in fixed_object["cc"]
1607 refute recipient.follower_address in fixed_object["to"]
1611 describe "fix_summary/1" do
1612 test "returns fixed object" do
1613 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1614 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1615 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1619 describe "fix_in_reply_to/2" do
1620 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1623 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1627 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1628 assert Transmogrifier.fix_in_reply_to(data) == data
1631 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1632 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1635 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1637 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1638 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1639 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1642 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1644 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1645 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1646 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1649 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1651 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1652 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1653 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1655 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1656 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1657 assert modified_object["inReplyTo"] == []
1658 assert modified_object["inReplyToAtomUri"] == ""
1661 @tag capture_log: true
1662 test "returns modified object when allowed incoming reply", %{data: data} do
1667 "https://shitposter.club/notice/2827873"
1670 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1671 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1673 assert modified_object["inReplyTo"] ==
1674 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1676 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1678 assert modified_object["conversation"] ==
1679 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1681 assert modified_object["context"] ==
1682 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1686 describe "fix_url/1" do
1687 test "fixes data for object when url is map" do
1691 "mimeType" => "video/mp4",
1692 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1696 assert Transmogrifier.fix_url(object) == %{
1697 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1701 test "fixes data for video object" do
1707 "mimeType" => "video/mp4",
1708 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1712 "mimeType" => "video/mp4",
1713 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1717 "mimeType" => "text/html",
1718 "href" => "https://peertube.-2d4c2d1630e3"
1722 "mimeType" => "text/html",
1723 "href" => "https://peertube.-2d4c2d16377-42"
1728 assert Transmogrifier.fix_url(object) == %{
1731 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1732 "mimeType" => "video/mp4",
1737 "url" => "https://peertube.-2d4c2d1630e3"
1741 test "fixes url for not Video object" do
1747 "mimeType" => "text/html",
1748 "href" => "https://peertube.-2d4c2d1630e3"
1752 "mimeType" => "text/html",
1753 "href" => "https://peertube.-2d4c2d16377-42"
1758 assert Transmogrifier.fix_url(object) == %{
1760 "url" => "https://peertube.-2d4c2d1630e3"
1763 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1769 test "retunrs not modified object" do
1770 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1774 describe "get_obj_helper/2" do
1775 test "returns nil when cannot normalize object" do
1776 assert capture_log(fn ->
1777 refute Transmogrifier.get_obj_helper("test-obj-id")
1778 end) =~ "Unsupported URI scheme"
1781 @tag capture_log: true
1782 test "returns {:ok, %Object{}} for success case" do
1783 assert {:ok, %Object{}} =
1784 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1788 describe "fix_attachments/1" do
1789 test "returns not modified object" do
1790 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1791 assert Transmogrifier.fix_attachments(data) == data
1794 test "returns modified object when attachment is map" do
1795 assert Transmogrifier.fix_attachments(%{
1797 "mediaType" => "video/mp4",
1798 "url" => "https://peertube.moe/stat-480.mp4"
1803 "mediaType" => "video/mp4",
1805 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1812 test "returns modified object when attachment is list" do
1813 assert Transmogrifier.fix_attachments(%{
1815 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1816 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1821 "mediaType" => "video/mp4",
1823 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1827 "mediaType" => "video/mp4",
1829 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1837 describe "fix_emoji/1" do
1838 test "returns not modified object when object not contains tags" do
1839 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1840 assert Transmogrifier.fix_emoji(data) == data
1843 test "returns object with emoji when object contains list tags" do
1844 assert Transmogrifier.fix_emoji(%{
1846 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1847 %{"type" => "Hashtag"}
1850 "emoji" => %{"bib" => "/test"},
1852 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1853 %{"type" => "Hashtag"}
1858 test "returns object with emoji when object contains map tag" do
1859 assert Transmogrifier.fix_emoji(%{
1860 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1862 "emoji" => %{"bib" => "/test"},
1863 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1868 describe "set_replies/1" do
1869 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1871 test "returns unmodified object if activity doesn't have self-replies" do
1872 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1873 assert Transmogrifier.set_replies(data) == data
1876 test "sets `replies` collection with a limited number of self-replies" do
1877 [user, another_user] = insert_list(2, :user)
1879 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
1881 {:ok, %{id: id2} = self_reply1} =
1882 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
1884 {:ok, self_reply2} =
1885 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
1887 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1888 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
1891 CommonAPI.post(user, %{
1892 status: "self-reply to self-reply",
1893 in_reply_to_status_id: id2
1897 CommonAPI.post(another_user, %{
1898 status: "another user's reply",
1899 in_reply_to_status_id: id1
1902 object = Object.normalize(activity)
1903 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1905 assert %{"type" => "Collection", "items" => ^replies_uris} =
1906 Transmogrifier.set_replies(object.data)["replies"]
1910 test "take_emoji_tags/1" do
1911 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1913 assert Transmogrifier.take_emoji_tags(user) == [
1915 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1916 "id" => "https://example.org/firefox.png",
1917 "name" => ":firefox:",
1919 "updated" => "1970-01-01T00:00:00Z"