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 announces" do
329 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
331 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
333 assert data["actor"] == "http://mastodon.example.org/users/admin"
334 assert data["type"] == "Announce"
337 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
339 assert data["object"] ==
340 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
342 assert Activity.get_create_by_object_ap_id(data["object"])
345 test "it works for incoming announces with an existing activity" do
347 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
350 File.read!("test/fixtures/mastodon-announce.json")
352 |> Map.put("object", activity.data["object"])
354 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
356 assert data["actor"] == "http://mastodon.example.org/users/admin"
357 assert data["type"] == "Announce"
360 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
362 assert data["object"] == activity.data["object"]
364 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
367 test "it works for incoming announces with an inlined activity" do
369 File.read!("test/fixtures/mastodon-announce-private.json")
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 object = Object.normalize(data["object"])
382 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
383 assert object.data["content"] == "this is a private toot"
386 @tag capture_log: true
387 test "it rejects incoming announces with an inlined activity from another origin" do
389 File.read!("test/fixtures/bogus-mastodon-announce.json")
392 assert :error = Transmogrifier.handle_incoming(data)
395 test "it does not clobber the addressing on announce activities" do
397 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
400 File.read!("test/fixtures/mastodon-announce.json")
402 |> Map.put("object", Object.normalize(activity).data["id"])
403 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
406 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
408 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
411 test "it ensures that as:Public activities make it to their followers collection" do
415 File.read!("test/fixtures/mastodon-post-activity.json")
417 |> Map.put("actor", user.ap_id)
418 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
423 |> Map.put("attributedTo", user.ap_id)
424 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
426 |> Map.put("id", user.ap_id <> "/activities/12345678")
428 data = Map.put(data, "object", object)
430 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
432 assert data["cc"] == [User.ap_followers(user)]
435 test "it ensures that address fields become lists" do
439 File.read!("test/fixtures/mastodon-post-activity.json")
441 |> Map.put("actor", user.ap_id)
442 |> Map.put("to", nil)
443 |> Map.put("cc", nil)
447 |> Map.put("attributedTo", user.ap_id)
448 |> Map.put("to", nil)
449 |> Map.put("cc", nil)
450 |> Map.put("id", user.ap_id <> "/activities/12345678")
452 data = Map.put(data, "object", object)
454 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
456 assert !is_nil(data["to"])
457 assert !is_nil(data["cc"])
460 test "it strips internal likes" do
462 File.read!("test/fixtures/mastodon-post-activity.json")
467 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
468 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
470 "type" => "OrderedCollection"
473 object = Map.put(data["object"], "likes", likes)
474 data = Map.put(data, "object", object)
476 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
478 refute Map.has_key?(object.data, "likes")
481 test "it strips internal reactions" do
483 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
484 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
486 %{object: object} = Activity.get_by_id_with_object(activity.id)
487 assert Map.has_key?(object.data, "reactions")
488 assert Map.has_key?(object.data, "reaction_count")
490 object_data = Transmogrifier.strip_internal_fields(object.data)
491 refute Map.has_key?(object_data, "reactions")
492 refute Map.has_key?(object_data, "reaction_count")
495 test "it works for incoming update activities" do
496 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
498 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
499 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
502 update_data["object"]
503 |> Map.put("actor", data["actor"])
504 |> Map.put("id", data["actor"])
508 |> Map.put("actor", data["actor"])
509 |> Map.put("object", object)
511 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
513 assert data["id"] == update_data["id"]
515 user = User.get_cached_by_ap_id(data["actor"])
516 assert user.name == "gargle"
518 assert user.avatar["url"] == [
521 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
525 assert user.banner["url"] == [
528 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
532 assert user.bio == "<p>Some bio</p>"
535 test "it works with alsoKnownAs" do
536 {:ok, %Activity{data: %{"actor" => actor}}} =
537 "test/fixtures/mastodon-post-activity.json"
540 |> Transmogrifier.handle_incoming()
542 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
545 "test/fixtures/mastodon-update.json"
548 |> Map.put("actor", actor)
549 |> Map.update!("object", fn object ->
551 |> Map.put("actor", actor)
552 |> Map.put("id", actor)
553 |> Map.put("alsoKnownAs", [
554 "http://mastodon.example.org/users/foo",
555 "http://example.org/users/bar"
558 |> Transmogrifier.handle_incoming()
560 assert User.get_cached_by_ap_id(actor).also_known_as == [
561 "http://mastodon.example.org/users/foo",
562 "http://example.org/users/bar"
566 test "it works with custom profile fields" do
568 "test/fixtures/mastodon-post-activity.json"
571 |> Transmogrifier.handle_incoming()
573 user = User.get_cached_by_ap_id(activity.actor)
575 assert user.fields == [
576 %{"name" => "foo", "value" => "bar"},
577 %{"name" => "foo1", "value" => "bar1"}
580 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
583 update_data["object"]
584 |> Map.put("actor", user.ap_id)
585 |> Map.put("id", user.ap_id)
589 |> Map.put("actor", user.ap_id)
590 |> Map.put("object", object)
592 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
594 user = User.get_cached_by_ap_id(user.ap_id)
596 assert user.fields == [
597 %{"name" => "foo", "value" => "updated"},
598 %{"name" => "foo1", "value" => "updated"}
601 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
604 put_in(update_data, ["object", "attachment"], [
605 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
606 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
607 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
610 {:ok, _} = 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 update_data = put_in(update_data, ["object", "attachment"], [])
621 {:ok, _} = Transmogrifier.handle_incoming(update_data)
623 user = User.get_cached_by_ap_id(user.ap_id)
625 assert user.fields == []
628 test "it works for incoming update activities which lock the account" do
629 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
631 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
632 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
635 update_data["object"]
636 |> Map.put("actor", data["actor"])
637 |> Map.put("id", data["actor"])
638 |> Map.put("manuallyApprovesFollowers", true)
642 |> Map.put("actor", data["actor"])
643 |> Map.put("object", object)
645 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
647 user = User.get_cached_by_ap_id(data["actor"])
648 assert user.locked == true
651 test "it works for incomming unfollows with an existing follow" do
655 File.read!("test/fixtures/mastodon-follow-activity.json")
657 |> Map.put("object", user.ap_id)
659 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
662 File.read!("test/fixtures/mastodon-unfollow-activity.json")
664 |> Map.put("object", follow_data)
666 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
668 assert data["type"] == "Undo"
669 assert data["object"]["type"] == "Follow"
670 assert data["object"]["object"] == user.ap_id
671 assert data["actor"] == "http://mastodon.example.org/users/admin"
673 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
676 test "it works for incoming follows to locked account" do
677 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
678 user = insert(:user, locked: true)
681 File.read!("test/fixtures/mastodon-follow-activity.json")
683 |> Map.put("object", user.ap_id)
685 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
687 assert data["type"] == "Follow"
688 assert data["object"] == user.ap_id
689 assert data["state"] == "pending"
690 assert data["actor"] == "http://mastodon.example.org/users/admin"
692 assert [^pending_follower] = User.get_follow_requests(user)
695 test "it works for incoming blocks" do
699 File.read!("test/fixtures/mastodon-block-activity.json")
701 |> Map.put("object", user.ap_id)
703 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
705 assert data["type"] == "Block"
706 assert data["object"] == user.ap_id
707 assert data["actor"] == "http://mastodon.example.org/users/admin"
709 blocker = User.get_cached_by_ap_id(data["actor"])
711 assert User.blocks?(blocker, user)
714 test "incoming blocks successfully tear down any follow relationship" do
715 blocker = insert(:user)
716 blocked = insert(:user)
719 File.read!("test/fixtures/mastodon-block-activity.json")
721 |> Map.put("object", blocked.ap_id)
722 |> Map.put("actor", blocker.ap_id)
724 {:ok, blocker} = User.follow(blocker, blocked)
725 {:ok, blocked} = User.follow(blocked, blocker)
727 assert User.following?(blocker, blocked)
728 assert User.following?(blocked, blocker)
730 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
732 assert data["type"] == "Block"
733 assert data["object"] == blocked.ap_id
734 assert data["actor"] == blocker.ap_id
736 blocker = User.get_cached_by_ap_id(data["actor"])
737 blocked = User.get_cached_by_ap_id(data["object"])
739 assert User.blocks?(blocker, blocked)
741 refute User.following?(blocker, blocked)
742 refute User.following?(blocked, blocker)
745 test "it works for incoming accepts which were pre-accepted" do
746 follower = insert(:user)
747 followed = insert(:user)
749 {:ok, follower} = User.follow(follower, followed)
750 assert User.following?(follower, followed) == true
752 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
755 File.read!("test/fixtures/mastodon-accept-activity.json")
757 |> Map.put("actor", followed.ap_id)
760 accept_data["object"]
761 |> Map.put("actor", follower.ap_id)
762 |> Map.put("id", follow_activity.data["id"])
764 accept_data = Map.put(accept_data, "object", object)
766 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
767 refute activity.local
769 assert activity.data["object"] == follow_activity.data["id"]
771 assert activity.data["id"] == accept_data["id"]
773 follower = User.get_cached_by_id(follower.id)
775 assert User.following?(follower, followed) == true
778 test "it works for incoming accepts which were orphaned" do
779 follower = insert(:user)
780 followed = insert(:user, locked: true)
782 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
785 File.read!("test/fixtures/mastodon-accept-activity.json")
787 |> Map.put("actor", followed.ap_id)
790 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
792 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
793 assert activity.data["object"] == follow_activity.data["id"]
795 follower = User.get_cached_by_id(follower.id)
797 assert User.following?(follower, followed) == true
800 test "it works for incoming accepts which are referenced by IRI only" do
801 follower = insert(:user)
802 followed = insert(:user, locked: true)
804 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
807 File.read!("test/fixtures/mastodon-accept-activity.json")
809 |> Map.put("actor", followed.ap_id)
810 |> Map.put("object", follow_activity.data["id"])
812 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
813 assert activity.data["object"] == follow_activity.data["id"]
815 follower = User.get_cached_by_id(follower.id)
817 assert User.following?(follower, followed) == true
820 test "it fails for incoming accepts which cannot be correlated" do
821 follower = insert(:user)
822 followed = insert(:user, locked: true)
825 File.read!("test/fixtures/mastodon-accept-activity.json")
827 |> Map.put("actor", followed.ap_id)
830 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
832 :error = Transmogrifier.handle_incoming(accept_data)
834 follower = User.get_cached_by_id(follower.id)
836 refute User.following?(follower, followed) == true
839 test "it fails for incoming rejects which cannot be correlated" do
840 follower = insert(:user)
841 followed = insert(:user, locked: true)
844 File.read!("test/fixtures/mastodon-reject-activity.json")
846 |> Map.put("actor", followed.ap_id)
849 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
851 :error = Transmogrifier.handle_incoming(accept_data)
853 follower = User.get_cached_by_id(follower.id)
855 refute User.following?(follower, followed) == true
858 test "it works for incoming rejects which are orphaned" do
859 follower = insert(:user)
860 followed = insert(:user, locked: true)
862 {:ok, follower} = User.follow(follower, followed)
863 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
865 assert User.following?(follower, followed) == true
868 File.read!("test/fixtures/mastodon-reject-activity.json")
870 |> Map.put("actor", followed.ap_id)
873 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
875 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
876 refute activity.local
877 assert activity.data["id"] == reject_data["id"]
879 follower = User.get_cached_by_id(follower.id)
881 assert User.following?(follower, followed) == false
884 test "it works for incoming rejects which are referenced by IRI only" do
885 follower = insert(:user)
886 followed = insert(:user, locked: true)
888 {:ok, follower} = User.follow(follower, followed)
889 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
891 assert User.following?(follower, followed) == true
894 File.read!("test/fixtures/mastodon-reject-activity.json")
896 |> Map.put("actor", followed.ap_id)
897 |> Map.put("object", follow_activity.data["id"])
899 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
901 follower = User.get_cached_by_id(follower.id)
903 assert User.following?(follower, followed) == false
906 test "it rejects activities without a valid ID" do
910 File.read!("test/fixtures/mastodon-follow-activity.json")
912 |> Map.put("object", user.ap_id)
915 :error = Transmogrifier.handle_incoming(data)
918 test "skip converting the content when it is nil" do
919 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
921 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
924 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
926 assert result["content"] == nil
929 test "it converts content of object to html" do
930 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
932 {:ok, %{"content" => content_markdown}} =
933 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
935 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
936 Fetcher.fetch_object_from_id(object_id)
938 assert content_markdown ==
939 "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..."
942 "<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>"
944 assert object.data["mediaType"] == "text/html"
947 test "it remaps video URLs as attachments if necessary" do
949 Fetcher.fetch_object_from_id(
950 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
955 "mediaType" => "video/mp4",
959 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
960 "mediaType" => "video/mp4"
965 assert object.data["url"] ==
966 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
968 assert object.data["attachment"] == [attachment]
971 test "it accepts Flag activities" do
973 other_user = insert(:user)
975 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
976 object = Object.normalize(activity)
980 "id" => activity.data["id"],
981 "content" => "test post",
982 "published" => object.data["published"],
983 "actor" => AccountView.render("show.json", %{user: user})
987 "@context" => "https://www.w3.org/ns/activitystreams",
988 "cc" => [user.ap_id],
989 "object" => [user.ap_id, activity.data["id"]],
991 "content" => "blocked AND reported!!!",
992 "actor" => other_user.ap_id
995 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
997 assert activity.data["object"] == [user.ap_id, note_obj]
998 assert activity.data["content"] == "blocked AND reported!!!"
999 assert activity.data["actor"] == other_user.ap_id
1000 assert activity.data["cc"] == [user.ap_id]
1003 test "it correctly processes messages with non-array to field" do
1004 user = insert(:user)
1007 "@context" => "https://www.w3.org/ns/activitystreams",
1008 "to" => "https://www.w3.org/ns/activitystreams#Public",
1011 "content" => "blah blah blah",
1013 "attributedTo" => user.ap_id,
1016 "actor" => user.ap_id
1019 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1021 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1024 test "it correctly processes messages with non-array cc field" do
1025 user = insert(:user)
1028 "@context" => "https://www.w3.org/ns/activitystreams",
1029 "to" => user.follower_address,
1030 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1033 "content" => "blah blah blah",
1035 "attributedTo" => user.ap_id,
1038 "actor" => user.ap_id
1041 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1043 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1044 assert [user.follower_address] == activity.data["to"]
1047 test "it accepts Move activities" do
1048 old_user = insert(:user)
1049 new_user = insert(:user)
1052 "@context" => "https://www.w3.org/ns/activitystreams",
1054 "actor" => old_user.ap_id,
1055 "object" => old_user.ap_id,
1056 "target" => new_user.ap_id
1059 assert :error = Transmogrifier.handle_incoming(message)
1061 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1063 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1064 assert activity.actor == old_user.ap_id
1065 assert activity.data["actor"] == old_user.ap_id
1066 assert activity.data["object"] == old_user.ap_id
1067 assert activity.data["target"] == new_user.ap_id
1068 assert activity.data["type"] == "Move"
1072 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1073 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1074 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1078 "test/fixtures/mastodon-post-activity.json"
1082 items = get_in(data, ["object", "replies", "first", "items"])
1083 assert length(items) > 0
1085 %{data: data, items: items}
1088 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1092 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1094 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1097 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1098 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1102 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1104 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1106 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1108 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1112 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1113 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1114 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1117 user = insert(:user)
1119 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1122 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1125 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1127 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1129 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1131 Repo.delete(activity.object)
1132 Repo.delete(activity)
1134 %{federation_output: federation_output, replies_uris: replies_uris}
1137 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1138 federation_output: federation_output,
1139 replies_uris: replies_uris
1141 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1143 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1145 for id <- replies_uris do
1146 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1147 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1151 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1152 %{federation_output: federation_output} do
1153 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1155 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1157 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1161 describe "prepare outgoing" do
1162 test "it inlines private announced objects" do
1163 user = insert(:user)
1165 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1167 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1169 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1171 assert modified["object"]["content"] == "hey"
1172 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1175 test "it turns mentions into tags" do
1176 user = insert(:user)
1177 other_user = insert(:user)
1180 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1182 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1183 object = modified["object"]
1185 expected_mention = %{
1186 "href" => other_user.ap_id,
1187 "name" => "@#{other_user.nickname}",
1192 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1193 "type" => "Hashtag",
1197 assert Enum.member?(object["tag"], expected_tag)
1198 assert Enum.member?(object["tag"], expected_mention)
1201 test "it adds the sensitive property" do
1202 user = insert(:user)
1204 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1205 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1207 assert modified["object"]["sensitive"]
1210 test "it adds the json-ld context and the conversation property" do
1211 user = insert(:user)
1213 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1214 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1216 assert modified["@context"] ==
1217 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1219 assert modified["object"]["conversation"] == modified["context"]
1222 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1223 user = insert(:user)
1225 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1226 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1228 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1231 test "it strips internal hashtag data" do
1232 user = insert(:user)
1234 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1237 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1238 "type" => "Hashtag",
1242 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1244 assert modified["object"]["tag"] == [expected_tag]
1247 test "it strips internal fields" do
1248 user = insert(:user)
1250 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1252 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1254 assert length(modified["object"]["tag"]) == 2
1256 assert is_nil(modified["object"]["emoji"])
1257 assert is_nil(modified["object"]["like_count"])
1258 assert is_nil(modified["object"]["announcements"])
1259 assert is_nil(modified["object"]["announcement_count"])
1260 assert is_nil(modified["object"]["context_id"])
1263 test "it strips internal fields of article" do
1264 activity = insert(:article_activity)
1266 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1268 assert length(modified["object"]["tag"]) == 2
1270 assert is_nil(modified["object"]["emoji"])
1271 assert is_nil(modified["object"]["like_count"])
1272 assert is_nil(modified["object"]["announcements"])
1273 assert is_nil(modified["object"]["announcement_count"])
1274 assert is_nil(modified["object"]["context_id"])
1275 assert is_nil(modified["object"]["likes"])
1278 test "the directMessage flag is present" do
1279 user = insert(:user)
1280 other_user = insert(:user)
1282 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1284 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1286 assert modified["directMessage"] == false
1289 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1291 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1293 assert modified["directMessage"] == false
1296 CommonAPI.post(user, %{
1297 "status" => "@#{other_user.nickname} :moominmamma:",
1298 "visibility" => "direct"
1301 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1303 assert modified["directMessage"] == true
1306 test "it strips BCC field" do
1307 user = insert(:user)
1308 {:ok, list} = Pleroma.List.create("foo", user)
1311 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1313 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1315 assert is_nil(modified["bcc"])
1318 test "it can handle Listen activities" do
1319 listen_activity = insert(:listen)
1321 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1323 assert modified["type"] == "Listen"
1325 user = insert(:user)
1327 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1329 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1333 describe "user upgrade" do
1334 test "it upgrades a user to activitypub" do
1337 nickname: "rye@niu.moe",
1339 ap_id: "https://niu.moe/users/rye",
1340 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1343 user_two = insert(:user)
1344 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1346 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1347 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1348 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1350 user = User.get_cached_by_id(user.id)
1351 assert user.note_count == 1
1353 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1354 ObanHelpers.perform_all()
1356 assert user.ap_enabled
1357 assert user.note_count == 1
1358 assert user.follower_address == "https://niu.moe/users/rye/followers"
1359 assert user.following_address == "https://niu.moe/users/rye/following"
1361 user = User.get_cached_by_id(user.id)
1362 assert user.note_count == 1
1364 activity = Activity.get_by_id(activity.id)
1365 assert user.follower_address in activity.recipients
1371 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1380 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1385 refute "..." in activity.recipients
1387 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1388 refute user.follower_address in unrelated_activity.recipients
1390 user_two = User.get_cached_by_id(user_two.id)
1391 assert User.following?(user_two, user)
1392 refute "..." in User.following(user_two)
1396 describe "actor rewriting" do
1397 test "it fixes the actor URL property to be a proper URI" do
1399 "url" => %{"href" => "http://example.com"}
1402 rewritten = Transmogrifier.maybe_fix_user_object(data)
1403 assert rewritten["url"] == "http://example.com"
1407 describe "actor origin containment" do
1408 test "it rejects activities which reference objects with bogus origins" do
1410 "@context" => "https://www.w3.org/ns/activitystreams",
1411 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1412 "actor" => "http://mastodon.example.org/users/admin",
1413 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1414 "object" => "https://info.pleroma.site/activity.json",
1415 "type" => "Announce"
1418 assert capture_log(fn ->
1419 :error = Transmogrifier.handle_incoming(data)
1420 end) =~ "Object containment failed"
1423 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1425 "@context" => "https://www.w3.org/ns/activitystreams",
1426 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1427 "actor" => "http://mastodon.example.org/users/admin",
1428 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1429 "object" => "https://info.pleroma.site/activity2.json",
1430 "type" => "Announce"
1433 assert capture_log(fn ->
1434 :error = Transmogrifier.handle_incoming(data)
1435 end) =~ "Object containment failed"
1438 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1440 "@context" => "https://www.w3.org/ns/activitystreams",
1441 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1442 "actor" => "http://mastodon.example.org/users/admin",
1443 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1444 "object" => "https://info.pleroma.site/activity3.json",
1445 "type" => "Announce"
1448 assert capture_log(fn ->
1449 :error = Transmogrifier.handle_incoming(data)
1450 end) =~ "Object containment failed"
1454 describe "reserialization" do
1455 test "successfully reserializes a message with inReplyTo == nil" do
1456 user = insert(:user)
1459 "@context" => "https://www.w3.org/ns/activitystreams",
1460 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1464 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1469 "attributedTo" => user.ap_id
1471 "actor" => user.ap_id
1474 {:ok, activity} = Transmogrifier.handle_incoming(message)
1476 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1479 test "successfully reserializes a message with AS2 objects in IR" do
1480 user = insert(:user)
1483 "@context" => "https://www.w3.org/ns/activitystreams",
1484 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1488 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1493 "attributedTo" => user.ap_id,
1495 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1496 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1499 "actor" => user.ap_id
1502 {:ok, activity} = Transmogrifier.handle_incoming(message)
1504 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1508 test "Rewrites Answers to Notes" do
1509 user = insert(:user)
1511 {:ok, poll_activity} =
1512 CommonAPI.post(user, %{
1513 "status" => "suya...",
1514 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1517 poll_object = Object.normalize(poll_activity)
1518 # TODO: Replace with CommonAPI vote creation when implemented
1520 File.read!("test/fixtures/mastodon-vote.json")
1522 |> Kernel.put_in(["to"], user.ap_id)
1523 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1524 |> Kernel.put_in(["object", "to"], user.ap_id)
1526 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1527 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1529 assert data["object"]["type"] == "Note"
1532 describe "fix_explicit_addressing" do
1534 user = insert(:user)
1538 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1539 explicitly_mentioned_actors = [
1540 "https://pleroma.gold/users/user1",
1541 "https://pleroma.gold/user2"
1545 "actor" => user.ap_id,
1546 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1549 Enum.map(explicitly_mentioned_actors, fn href ->
1550 %{"type" => "Mention", "href" => href}
1554 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1555 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1556 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1557 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1560 test "does not move actor's follower collection to cc", %{user: user} do
1562 "actor" => user.ap_id,
1563 "to" => [user.follower_address],
1567 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1568 assert user.follower_address in fixed_object["to"]
1569 refute user.follower_address in fixed_object["cc"]
1572 test "removes recipient's follower collection from cc", %{user: user} do
1573 recipient = insert(:user)
1576 "actor" => user.ap_id,
1577 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1578 "cc" => [user.follower_address, recipient.follower_address]
1581 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1583 assert user.follower_address in fixed_object["cc"]
1584 refute recipient.follower_address in fixed_object["cc"]
1585 refute recipient.follower_address in fixed_object["to"]
1589 describe "fix_summary/1" do
1590 test "returns fixed object" do
1591 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1592 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1593 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1597 describe "fix_in_reply_to/2" do
1598 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1601 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1605 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1606 assert Transmogrifier.fix_in_reply_to(data) == data
1609 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1610 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1613 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1615 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1616 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1617 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1620 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1622 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1623 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1624 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1627 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1629 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1630 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1631 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1633 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1634 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1635 assert modified_object["inReplyTo"] == []
1636 assert modified_object["inReplyToAtomUri"] == ""
1639 @tag capture_log: true
1640 test "returns modified object when allowed incoming reply", %{data: data} do
1645 "https://shitposter.club/notice/2827873"
1648 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1649 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1651 assert modified_object["inReplyTo"] ==
1652 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1654 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1656 assert modified_object["conversation"] ==
1657 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1659 assert modified_object["context"] ==
1660 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1664 describe "fix_url/1" do
1665 test "fixes data for object when url is map" do
1669 "mimeType" => "video/mp4",
1670 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1674 assert Transmogrifier.fix_url(object) == %{
1675 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1679 test "fixes data for video object" do
1685 "mimeType" => "video/mp4",
1686 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1690 "mimeType" => "video/mp4",
1691 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1695 "mimeType" => "text/html",
1696 "href" => "https://peertube.-2d4c2d1630e3"
1700 "mimeType" => "text/html",
1701 "href" => "https://peertube.-2d4c2d16377-42"
1706 assert Transmogrifier.fix_url(object) == %{
1709 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1710 "mimeType" => "video/mp4",
1715 "url" => "https://peertube.-2d4c2d1630e3"
1719 test "fixes url for not Video object" do
1725 "mimeType" => "text/html",
1726 "href" => "https://peertube.-2d4c2d1630e3"
1730 "mimeType" => "text/html",
1731 "href" => "https://peertube.-2d4c2d16377-42"
1736 assert Transmogrifier.fix_url(object) == %{
1738 "url" => "https://peertube.-2d4c2d1630e3"
1741 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1747 test "retunrs not modified object" do
1748 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1752 describe "get_obj_helper/2" do
1753 test "returns nil when cannot normalize object" do
1754 assert capture_log(fn ->
1755 refute Transmogrifier.get_obj_helper("test-obj-id")
1756 end) =~ "Unsupported URI scheme"
1759 @tag capture_log: true
1760 test "returns {:ok, %Object{}} for success case" do
1761 assert {:ok, %Object{}} =
1762 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1766 describe "fix_attachments/1" do
1767 test "returns not modified object" do
1768 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1769 assert Transmogrifier.fix_attachments(data) == data
1772 test "returns modified object when attachment is map" do
1773 assert Transmogrifier.fix_attachments(%{
1775 "mediaType" => "video/mp4",
1776 "url" => "https://peertube.moe/stat-480.mp4"
1781 "mediaType" => "video/mp4",
1783 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1790 test "returns modified object when attachment is list" do
1791 assert Transmogrifier.fix_attachments(%{
1793 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1794 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1799 "mediaType" => "video/mp4",
1801 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1805 "mediaType" => "video/mp4",
1807 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1815 describe "fix_emoji/1" do
1816 test "returns not modified object when object not contains tags" do
1817 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1818 assert Transmogrifier.fix_emoji(data) == data
1821 test "returns object with emoji when object contains list tags" do
1822 assert Transmogrifier.fix_emoji(%{
1824 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1825 %{"type" => "Hashtag"}
1828 "emoji" => %{"bib" => "/test"},
1830 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1831 %{"type" => "Hashtag"}
1836 test "returns object with emoji when object contains map tag" do
1837 assert Transmogrifier.fix_emoji(%{
1838 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1840 "emoji" => %{"bib" => "/test"},
1841 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1846 describe "set_replies/1" do
1847 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1849 test "returns unmodified object if activity doesn't have self-replies" do
1850 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1851 assert Transmogrifier.set_replies(data) == data
1854 test "sets `replies` collection with a limited number of self-replies" do
1855 [user, another_user] = insert_list(2, :user)
1857 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
1859 {:ok, %{id: id2} = self_reply1} =
1860 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
1862 {:ok, self_reply2} =
1863 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
1865 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1867 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
1870 CommonAPI.post(user, %{
1871 "status" => "self-reply to self-reply",
1872 "in_reply_to_status_id" => id2
1876 CommonAPI.post(another_user, %{
1877 "status" => "another user's reply",
1878 "in_reply_to_status_id" => id1
1881 object = Object.normalize(activity)
1882 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1884 assert %{"type" => "Collection", "items" => ^replies_uris} =
1885 Transmogrifier.set_replies(object.data)["replies"]
1889 test "take_emoji_tags/1" do
1890 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1892 assert Transmogrifier.take_emoji_tags(user) == [
1894 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1895 "id" => "https://example.org/firefox.png",
1896 "name" => ":firefox:",
1898 "updated" => "1970-01-01T00:00:00Z"