1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Object.Fetcher
12 alias Pleroma.Tests.ObanHelpers
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 setup do: clear_config([:instance, :max_remote_account_fields])
30 describe "handle_incoming" do
31 test "it ignores an incoming notice if we already have it" do
32 activity = insert(:note_activity)
35 File.read!("test/fixtures/mastodon-post-activity.json")
37 |> Map.put("object", Object.normalize(activity).data)
39 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
41 assert activity == returned_activity
44 @tag capture_log: true
45 test "it fetches reply-to activities if we don't have them" do
47 File.read!("test/fixtures/mastodon-post-activity.json")
52 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
54 data = Map.put(data, "object", object)
55 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
56 returned_object = Object.normalize(returned_activity, false)
59 Activity.get_create_by_object_ap_id(
60 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
63 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
66 test "it does not fetch reply-to activities beyond max replies depth limit" do
68 File.read!("test/fixtures/mastodon-post-activity.json")
73 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
75 data = Map.put(data, "object", object)
77 with_mock Pleroma.Web.Federator,
78 allowed_thread_distance?: fn _ -> false end do
79 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
81 returned_object = Object.normalize(returned_activity, false)
83 refute Activity.get_create_by_object_ap_id(
84 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
87 assert returned_object.data["inReplyToAtomUri"] ==
88 "https://shitposter.club/notice/2827873"
92 test "it does not crash if the object in inReplyTo can't be fetched" do
94 File.read!("test/fixtures/mastodon-post-activity.json")
99 |> Map.put("inReplyTo", "https://404.site/whatever")
103 |> Map.put("object", object)
105 assert capture_log(fn ->
106 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
107 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
110 test "it works for incoming notices" do
111 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
113 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
116 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
118 assert data["context"] ==
119 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
121 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
123 assert data["cc"] == [
124 "http://mastodon.example.org/users/admin/followers",
125 "http://localtesting.pleroma.lol/users/lain"
128 assert data["actor"] == "http://mastodon.example.org/users/admin"
130 object_data = Object.normalize(data["object"]).data
132 assert object_data["id"] ==
133 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
135 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
137 assert object_data["cc"] == [
138 "http://mastodon.example.org/users/admin/followers",
139 "http://localtesting.pleroma.lol/users/lain"
142 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
143 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
145 assert object_data["context"] ==
146 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
148 assert object_data["sensitive"] == true
150 user = User.get_cached_by_ap_id(object_data["actor"])
152 assert user.note_count == 1
155 test "it works for incoming notices with hashtags" do
156 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
158 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
159 object = Object.normalize(data["object"])
161 assert Enum.at(object.data["tag"], 2) == "moo"
164 test "it works for incoming questions" do
165 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
167 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
169 object = Object.normalize(activity)
171 assert Enum.all?(object.data["oneOf"], fn choice ->
174 "Everyone knows that!",
175 "25 char limit is dumb",
176 "I can't even fit a funny"
181 test "it works for incoming listens" do
183 "@context" => "https://www.w3.org/ns/activitystreams",
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
187 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
188 "actor" => "http://mastodon.example.org/users/admin",
191 "id" => "http://mastodon.example.org/users/admin/listens/1234",
192 "attributedTo" => "http://mastodon.example.org/users/admin",
193 "title" => "lain radio episode 1",
195 "album" => "lain radio",
200 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
202 object = Object.normalize(activity)
204 assert object.data["title"] == "lain radio episode 1"
205 assert object.data["artist"] == "lain"
206 assert object.data["album"] == "lain radio"
207 assert object.data["length"] == 180_000
210 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
214 CommonAPI.post(user, %{
215 "status" => "suya...",
216 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
219 object = Object.normalize(activity)
222 File.read!("test/fixtures/mastodon-vote.json")
224 |> Kernel.put_in(["to"], user.ap_id)
225 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
226 |> Kernel.put_in(["object", "to"], user.ap_id)
228 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
229 answer_object = Object.normalize(activity)
230 assert answer_object.data["type"] == "Answer"
231 object = Object.get_by_ap_id(object.data["id"])
234 object.data["oneOf"],
236 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
242 test "it works for incoming notices with contentMap" do
244 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
246 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
247 object = Object.normalize(data["object"])
249 assert object.data["content"] ==
250 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
253 test "it works for incoming notices with to/cc not being an array (kroeg)" do
254 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
256 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
257 object = Object.normalize(data["object"])
259 assert object.data["content"] ==
260 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
263 test "it works for incoming announces with actor being inlined (kroeg)" do
264 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
266 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
268 assert data["actor"] == "https://puckipedia.com/"
271 test "it works for incoming notices with tag not being an array (kroeg)" do
272 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
274 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
275 object = Object.normalize(data["object"])
277 assert object.data["emoji"] == %{
278 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
281 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
283 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
284 object = Object.normalize(data["object"])
286 assert "test" in object.data["tag"]
289 test "it works for incoming notices with url not being a string (prismo)" do
290 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
292 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
293 object = Object.normalize(data["object"])
295 assert object.data["url"] == "https://prismo.news/posts/83"
298 test "it cleans up incoming notices which are not really DMs" do
300 other_user = insert(:user)
302 to = [user.ap_id, other_user.ap_id]
305 File.read!("test/fixtures/mastodon-post-activity.json")
315 data = Map.put(data, "object", object)
317 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
319 assert data["to"] == []
320 assert data["cc"] == to
322 object_data = Object.normalize(activity).data
324 assert object_data["to"] == []
325 assert object_data["cc"] == to
328 test "it works for incoming emoji reactions" do
330 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
333 File.read!("test/fixtures/emoji-reaction.json")
335 |> Map.put("object", activity.data["object"])
337 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
339 assert data["actor"] == "http://mastodon.example.org/users/admin"
340 assert data["type"] == "EmojiReact"
341 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
342 assert data["object"] == activity.data["object"]
343 assert data["content"] == "👌"
346 test "it reject invalid emoji reactions" do
348 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
351 File.read!("test/fixtures/emoji-reaction-too-long.json")
353 |> Map.put("object", activity.data["object"])
355 assert :error = Transmogrifier.handle_incoming(data)
358 File.read!("test/fixtures/emoji-reaction-no-emoji.json")
360 |> Map.put("object", activity.data["object"])
362 assert :error = Transmogrifier.handle_incoming(data)
365 test "it works for incoming emoji reaction undos" do
368 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
369 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
372 File.read!("test/fixtures/mastodon-undo-like.json")
374 |> Map.put("object", reaction_activity.data["id"])
375 |> Map.put("actor", user.ap_id)
377 {:ok, activity} = Transmogrifier.handle_incoming(data)
379 assert activity.actor == user.ap_id
380 assert activity.data["id"] == data["id"]
381 assert activity.data["type"] == "Undo"
384 test "it returns an error for incoming unlikes wihout a like activity" do
386 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
389 File.read!("test/fixtures/mastodon-undo-like.json")
391 |> Map.put("object", activity.data["object"])
393 assert Transmogrifier.handle_incoming(data) == :error
396 test "it works for incoming unlikes with an existing like activity" do
398 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
401 File.read!("test/fixtures/mastodon-like.json")
403 |> Map.put("object", activity.data["object"])
405 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
408 File.read!("test/fixtures/mastodon-undo-like.json")
410 |> Map.put("object", like_data)
411 |> Map.put("actor", like_data["actor"])
413 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
415 assert data["actor"] == "http://mastodon.example.org/users/admin"
416 assert data["type"] == "Undo"
417 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
418 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
421 test "it works for incoming unlikes with an existing like activity and a compact object" do
423 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
426 File.read!("test/fixtures/mastodon-like.json")
428 |> Map.put("object", activity.data["object"])
430 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
433 File.read!("test/fixtures/mastodon-undo-like.json")
435 |> Map.put("object", like_data["id"])
436 |> Map.put("actor", like_data["actor"])
438 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
440 assert data["actor"] == "http://mastodon.example.org/users/admin"
441 assert data["type"] == "Undo"
442 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
443 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
446 test "it works for incoming announces" do
447 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
449 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
451 assert data["actor"] == "http://mastodon.example.org/users/admin"
452 assert data["type"] == "Announce"
455 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
457 assert data["object"] ==
458 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
460 assert Activity.get_create_by_object_ap_id(data["object"])
463 test "it works for incoming announces with an existing activity" do
465 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
468 File.read!("test/fixtures/mastodon-announce.json")
470 |> Map.put("object", activity.data["object"])
472 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
474 assert data["actor"] == "http://mastodon.example.org/users/admin"
475 assert data["type"] == "Announce"
478 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
480 assert data["object"] == activity.data["object"]
482 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
485 test "it works for incoming announces with an inlined activity" do
487 File.read!("test/fixtures/mastodon-announce-private.json")
490 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
492 assert data["actor"] == "http://mastodon.example.org/users/admin"
493 assert data["type"] == "Announce"
496 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
498 object = Object.normalize(data["object"])
500 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
501 assert object.data["content"] == "this is a private toot"
504 @tag capture_log: true
505 test "it rejects incoming announces with an inlined activity from another origin" do
507 File.read!("test/fixtures/bogus-mastodon-announce.json")
510 assert :error = Transmogrifier.handle_incoming(data)
513 test "it does not clobber the addressing on announce activities" do
515 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
518 File.read!("test/fixtures/mastodon-announce.json")
520 |> Map.put("object", Object.normalize(activity).data["id"])
521 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
524 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
526 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
529 test "it ensures that as:Public activities make it to their followers collection" do
533 File.read!("test/fixtures/mastodon-post-activity.json")
535 |> Map.put("actor", user.ap_id)
536 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
541 |> Map.put("attributedTo", user.ap_id)
542 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
544 |> Map.put("id", user.ap_id <> "/activities/12345678")
546 data = Map.put(data, "object", object)
548 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
550 assert data["cc"] == [User.ap_followers(user)]
553 test "it ensures that address fields become lists" do
557 File.read!("test/fixtures/mastodon-post-activity.json")
559 |> Map.put("actor", user.ap_id)
560 |> Map.put("to", nil)
561 |> Map.put("cc", nil)
565 |> Map.put("attributedTo", user.ap_id)
566 |> Map.put("to", nil)
567 |> Map.put("cc", nil)
568 |> Map.put("id", user.ap_id <> "/activities/12345678")
570 data = Map.put(data, "object", object)
572 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
574 assert !is_nil(data["to"])
575 assert !is_nil(data["cc"])
578 test "it strips internal likes" do
580 File.read!("test/fixtures/mastodon-post-activity.json")
585 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
586 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
588 "type" => "OrderedCollection"
591 object = Map.put(data["object"], "likes", likes)
592 data = Map.put(data, "object", object)
594 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
596 refute Map.has_key?(object.data, "likes")
599 test "it strips internal reactions" do
601 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
602 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
604 %{object: object} = Activity.get_by_id_with_object(activity.id)
605 assert Map.has_key?(object.data, "reactions")
606 assert Map.has_key?(object.data, "reaction_count")
608 object_data = Transmogrifier.strip_internal_fields(object.data)
609 refute Map.has_key?(object_data, "reactions")
610 refute Map.has_key?(object_data, "reaction_count")
613 test "it works for incoming update activities" do
614 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
616 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
617 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
620 update_data["object"]
621 |> Map.put("actor", data["actor"])
622 |> Map.put("id", data["actor"])
626 |> Map.put("actor", data["actor"])
627 |> Map.put("object", object)
629 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
631 assert data["id"] == update_data["id"]
633 user = User.get_cached_by_ap_id(data["actor"])
634 assert user.name == "gargle"
636 assert user.avatar["url"] == [
639 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
643 assert user.banner["url"] == [
646 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
650 assert user.bio == "<p>Some bio</p>"
653 test "it works with alsoKnownAs" do
654 {:ok, %Activity{data: %{"actor" => actor}}} =
655 "test/fixtures/mastodon-post-activity.json"
658 |> Transmogrifier.handle_incoming()
660 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
663 "test/fixtures/mastodon-update.json"
666 |> Map.put("actor", actor)
667 |> Map.update!("object", fn object ->
669 |> Map.put("actor", actor)
670 |> Map.put("id", actor)
671 |> Map.put("alsoKnownAs", [
672 "http://mastodon.example.org/users/foo",
673 "http://example.org/users/bar"
676 |> Transmogrifier.handle_incoming()
678 assert User.get_cached_by_ap_id(actor).also_known_as == [
679 "http://mastodon.example.org/users/foo",
680 "http://example.org/users/bar"
684 test "it works with custom profile fields" do
686 "test/fixtures/mastodon-post-activity.json"
689 |> Transmogrifier.handle_incoming()
691 user = User.get_cached_by_ap_id(activity.actor)
693 assert user.fields == [
694 %{"name" => "foo", "value" => "bar"},
695 %{"name" => "foo1", "value" => "bar1"}
698 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
701 update_data["object"]
702 |> Map.put("actor", user.ap_id)
703 |> Map.put("id", user.ap_id)
707 |> Map.put("actor", user.ap_id)
708 |> Map.put("object", object)
710 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
712 user = User.get_cached_by_ap_id(user.ap_id)
714 assert user.fields == [
715 %{"name" => "foo", "value" => "updated"},
716 %{"name" => "foo1", "value" => "updated"}
719 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
722 put_in(update_data, ["object", "attachment"], [
723 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
724 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
725 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
728 {:ok, _} = Transmogrifier.handle_incoming(update_data)
730 user = User.get_cached_by_ap_id(user.ap_id)
732 assert user.fields == [
733 %{"name" => "foo", "value" => "updated"},
734 %{"name" => "foo1", "value" => "updated"}
737 update_data = put_in(update_data, ["object", "attachment"], [])
739 {:ok, _} = Transmogrifier.handle_incoming(update_data)
741 user = User.get_cached_by_ap_id(user.ap_id)
743 assert user.fields == []
746 test "it works for incoming update activities which lock the account" do
747 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
749 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
750 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
753 update_data["object"]
754 |> Map.put("actor", data["actor"])
755 |> Map.put("id", data["actor"])
756 |> Map.put("manuallyApprovesFollowers", true)
760 |> Map.put("actor", data["actor"])
761 |> Map.put("object", object)
763 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
765 user = User.get_cached_by_ap_id(data["actor"])
766 assert user.locked == true
769 test "it works for incoming unannounces with an existing notice" do
771 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
774 File.read!("test/fixtures/mastodon-announce.json")
776 |> Map.put("object", activity.data["object"])
778 {:ok, %Activity{data: announce_data, local: false}} =
779 Transmogrifier.handle_incoming(announce_data)
782 File.read!("test/fixtures/mastodon-undo-announce.json")
784 |> Map.put("object", announce_data)
785 |> Map.put("actor", announce_data["actor"])
787 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
789 assert data["type"] == "Undo"
790 assert object_data = data["object"]
791 assert object_data["type"] == "Announce"
792 assert object_data["object"] == activity.data["object"]
794 assert object_data["id"] ==
795 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
798 test "it works for incomming unfollows with an existing follow" do
802 File.read!("test/fixtures/mastodon-follow-activity.json")
804 |> Map.put("object", user.ap_id)
806 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
809 File.read!("test/fixtures/mastodon-unfollow-activity.json")
811 |> Map.put("object", follow_data)
813 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
815 assert data["type"] == "Undo"
816 assert data["object"]["type"] == "Follow"
817 assert data["object"]["object"] == user.ap_id
818 assert data["actor"] == "http://mastodon.example.org/users/admin"
820 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
823 test "it works for incoming follows to locked account" do
824 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
825 user = insert(:user, locked: true)
828 File.read!("test/fixtures/mastodon-follow-activity.json")
830 |> Map.put("object", user.ap_id)
832 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
834 assert data["type"] == "Follow"
835 assert data["object"] == user.ap_id
836 assert data["state"] == "pending"
837 assert data["actor"] == "http://mastodon.example.org/users/admin"
839 assert [^pending_follower] = User.get_follow_requests(user)
842 test "it works for incoming blocks" do
846 File.read!("test/fixtures/mastodon-block-activity.json")
848 |> Map.put("object", user.ap_id)
850 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
852 assert data["type"] == "Block"
853 assert data["object"] == user.ap_id
854 assert data["actor"] == "http://mastodon.example.org/users/admin"
856 blocker = User.get_cached_by_ap_id(data["actor"])
858 assert User.blocks?(blocker, user)
861 test "incoming blocks successfully tear down any follow relationship" do
862 blocker = insert(:user)
863 blocked = insert(:user)
866 File.read!("test/fixtures/mastodon-block-activity.json")
868 |> Map.put("object", blocked.ap_id)
869 |> Map.put("actor", blocker.ap_id)
871 {:ok, blocker} = User.follow(blocker, blocked)
872 {:ok, blocked} = User.follow(blocked, blocker)
874 assert User.following?(blocker, blocked)
875 assert User.following?(blocked, blocker)
877 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
879 assert data["type"] == "Block"
880 assert data["object"] == blocked.ap_id
881 assert data["actor"] == blocker.ap_id
883 blocker = User.get_cached_by_ap_id(data["actor"])
884 blocked = User.get_cached_by_ap_id(data["object"])
886 assert User.blocks?(blocker, blocked)
888 refute User.following?(blocker, blocked)
889 refute User.following?(blocked, blocker)
892 test "it works for incoming unblocks with an existing block" do
896 File.read!("test/fixtures/mastodon-block-activity.json")
898 |> Map.put("object", user.ap_id)
900 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
903 File.read!("test/fixtures/mastodon-unblock-activity.json")
905 |> Map.put("object", block_data)
907 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
908 assert data["type"] == "Undo"
909 assert data["object"]["type"] == "Block"
910 assert data["object"]["object"] == user.ap_id
911 assert data["actor"] == "http://mastodon.example.org/users/admin"
913 blocker = User.get_cached_by_ap_id(data["actor"])
915 refute User.blocks?(blocker, user)
918 test "it works for incoming accepts which were pre-accepted" do
919 follower = insert(:user)
920 followed = insert(:user)
922 {:ok, follower} = User.follow(follower, followed)
923 assert User.following?(follower, followed) == true
925 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
928 File.read!("test/fixtures/mastodon-accept-activity.json")
930 |> Map.put("actor", followed.ap_id)
933 accept_data["object"]
934 |> Map.put("actor", follower.ap_id)
935 |> Map.put("id", follow_activity.data["id"])
937 accept_data = Map.put(accept_data, "object", object)
939 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
940 refute activity.local
942 assert activity.data["object"] == follow_activity.data["id"]
944 assert activity.data["id"] == accept_data["id"]
946 follower = User.get_cached_by_id(follower.id)
948 assert User.following?(follower, followed) == true
951 test "it works for incoming accepts which were orphaned" do
952 follower = insert(:user)
953 followed = insert(:user, locked: true)
955 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
958 File.read!("test/fixtures/mastodon-accept-activity.json")
960 |> Map.put("actor", followed.ap_id)
963 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
965 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
966 assert activity.data["object"] == follow_activity.data["id"]
968 follower = User.get_cached_by_id(follower.id)
970 assert User.following?(follower, followed) == true
973 test "it works for incoming accepts which are referenced by IRI only" do
974 follower = insert(:user)
975 followed = insert(:user, locked: true)
977 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
980 File.read!("test/fixtures/mastodon-accept-activity.json")
982 |> Map.put("actor", followed.ap_id)
983 |> Map.put("object", follow_activity.data["id"])
985 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
986 assert activity.data["object"] == follow_activity.data["id"]
988 follower = User.get_cached_by_id(follower.id)
990 assert User.following?(follower, followed) == true
993 test "it fails for incoming accepts which cannot be correlated" do
994 follower = insert(:user)
995 followed = insert(:user, locked: true)
998 File.read!("test/fixtures/mastodon-accept-activity.json")
1000 |> Map.put("actor", followed.ap_id)
1003 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1005 :error = Transmogrifier.handle_incoming(accept_data)
1007 follower = User.get_cached_by_id(follower.id)
1009 refute User.following?(follower, followed) == true
1012 test "it fails for incoming rejects which cannot be correlated" do
1013 follower = insert(:user)
1014 followed = insert(:user, locked: true)
1017 File.read!("test/fixtures/mastodon-reject-activity.json")
1019 |> Map.put("actor", followed.ap_id)
1022 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1024 :error = Transmogrifier.handle_incoming(accept_data)
1026 follower = User.get_cached_by_id(follower.id)
1028 refute User.following?(follower, followed) == true
1031 test "it works for incoming rejects which are orphaned" do
1032 follower = insert(:user)
1033 followed = insert(:user, locked: true)
1035 {:ok, follower} = User.follow(follower, followed)
1036 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1038 assert User.following?(follower, followed) == true
1041 File.read!("test/fixtures/mastodon-reject-activity.json")
1043 |> Map.put("actor", followed.ap_id)
1046 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1048 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1049 refute activity.local
1050 assert activity.data["id"] == reject_data["id"]
1052 follower = User.get_cached_by_id(follower.id)
1054 assert User.following?(follower, followed) == false
1057 test "it works for incoming rejects which are referenced by IRI only" do
1058 follower = insert(:user)
1059 followed = insert(:user, locked: true)
1061 {:ok, follower} = User.follow(follower, followed)
1062 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1064 assert User.following?(follower, followed) == true
1067 File.read!("test/fixtures/mastodon-reject-activity.json")
1069 |> Map.put("actor", followed.ap_id)
1070 |> Map.put("object", follow_activity.data["id"])
1072 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1074 follower = User.get_cached_by_id(follower.id)
1076 assert User.following?(follower, followed) == false
1079 test "it rejects activities without a valid ID" do
1080 user = insert(:user)
1083 File.read!("test/fixtures/mastodon-follow-activity.json")
1085 |> Map.put("object", user.ap_id)
1086 |> Map.put("id", "")
1088 :error = Transmogrifier.handle_incoming(data)
1091 test "skip converting the content when it is nil" do
1092 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1094 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1097 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
1099 assert result["content"] == nil
1102 test "it converts content of object to html" do
1103 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1105 {:ok, %{"content" => content_markdown}} =
1106 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1108 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
1109 Fetcher.fetch_object_from_id(object_id)
1111 assert content_markdown ==
1112 "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..."
1115 "<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>"
1117 assert object.data["mediaType"] == "text/html"
1120 test "it remaps video URLs as attachments if necessary" do
1122 Fetcher.fetch_object_from_id(
1123 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1128 "mediaType" => "video/mp4",
1132 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1133 "mediaType" => "video/mp4"
1138 assert object.data["url"] ==
1139 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1141 assert object.data["attachment"] == [attachment]
1144 test "it accepts Flag activities" do
1145 user = insert(:user)
1146 other_user = insert(:user)
1148 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1149 object = Object.normalize(activity)
1153 "id" => activity.data["id"],
1154 "content" => "test post",
1155 "published" => object.data["published"],
1156 "actor" => AccountView.render("show.json", %{user: user})
1160 "@context" => "https://www.w3.org/ns/activitystreams",
1161 "cc" => [user.ap_id],
1162 "object" => [user.ap_id, activity.data["id"]],
1164 "content" => "blocked AND reported!!!",
1165 "actor" => other_user.ap_id
1168 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1170 assert activity.data["object"] == [user.ap_id, note_obj]
1171 assert activity.data["content"] == "blocked AND reported!!!"
1172 assert activity.data["actor"] == other_user.ap_id
1173 assert activity.data["cc"] == [user.ap_id]
1176 test "it correctly processes messages with non-array to field" do
1177 user = insert(:user)
1180 "@context" => "https://www.w3.org/ns/activitystreams",
1181 "to" => "https://www.w3.org/ns/activitystreams#Public",
1184 "content" => "blah blah blah",
1186 "attributedTo" => user.ap_id,
1189 "actor" => user.ap_id
1192 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1194 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1197 test "it correctly processes messages with non-array cc field" do
1198 user = insert(:user)
1201 "@context" => "https://www.w3.org/ns/activitystreams",
1202 "to" => user.follower_address,
1203 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1206 "content" => "blah blah blah",
1208 "attributedTo" => user.ap_id,
1211 "actor" => user.ap_id
1214 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1216 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1217 assert [user.follower_address] == activity.data["to"]
1220 test "it accepts Move activities" do
1221 old_user = insert(:user)
1222 new_user = insert(:user)
1225 "@context" => "https://www.w3.org/ns/activitystreams",
1227 "actor" => old_user.ap_id,
1228 "object" => old_user.ap_id,
1229 "target" => new_user.ap_id
1232 assert :error = Transmogrifier.handle_incoming(message)
1234 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1236 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1237 assert activity.actor == old_user.ap_id
1238 assert activity.data["actor"] == old_user.ap_id
1239 assert activity.data["object"] == old_user.ap_id
1240 assert activity.data["target"] == new_user.ap_id
1241 assert activity.data["type"] == "Move"
1245 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1246 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1247 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1251 "test/fixtures/mastodon-post-activity.json"
1255 items = get_in(data, ["object", "replies", "first", "items"])
1256 assert length(items) > 0
1258 %{data: data, items: items}
1261 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1265 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1267 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1270 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1271 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1275 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1277 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1279 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1281 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1285 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1286 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1287 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1290 user = insert(:user)
1292 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1295 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1298 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1300 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1302 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1304 Repo.delete(activity.object)
1305 Repo.delete(activity)
1307 %{federation_output: federation_output, replies_uris: replies_uris}
1310 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1311 federation_output: federation_output,
1312 replies_uris: replies_uris
1314 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1316 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1318 for id <- replies_uris do
1319 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1320 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1324 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1325 %{federation_output: federation_output} do
1326 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1328 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1330 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1334 describe "prepare outgoing" do
1335 test "it inlines private announced objects" do
1336 user = insert(:user)
1338 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1340 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1342 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1344 assert modified["object"]["content"] == "hey"
1345 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1348 test "it turns mentions into tags" do
1349 user = insert(:user)
1350 other_user = insert(:user)
1353 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1355 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1356 object = modified["object"]
1358 expected_mention = %{
1359 "href" => other_user.ap_id,
1360 "name" => "@#{other_user.nickname}",
1365 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1366 "type" => "Hashtag",
1370 assert Enum.member?(object["tag"], expected_tag)
1371 assert Enum.member?(object["tag"], expected_mention)
1374 test "it adds the sensitive property" do
1375 user = insert(:user)
1377 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1378 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1380 assert modified["object"]["sensitive"]
1383 test "it adds the json-ld context and the conversation property" do
1384 user = insert(:user)
1386 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1387 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1389 assert modified["@context"] ==
1390 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1392 assert modified["object"]["conversation"] == modified["context"]
1395 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1396 user = insert(:user)
1398 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1399 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1401 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1404 test "it strips internal hashtag data" do
1405 user = insert(:user)
1407 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1410 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1411 "type" => "Hashtag",
1415 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1417 assert modified["object"]["tag"] == [expected_tag]
1420 test "it strips internal fields" do
1421 user = insert(:user)
1423 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1425 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1427 assert length(modified["object"]["tag"]) == 2
1429 assert is_nil(modified["object"]["emoji"])
1430 assert is_nil(modified["object"]["like_count"])
1431 assert is_nil(modified["object"]["announcements"])
1432 assert is_nil(modified["object"]["announcement_count"])
1433 assert is_nil(modified["object"]["context_id"])
1436 test "it strips internal fields of article" do
1437 activity = insert(:article_activity)
1439 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1441 assert length(modified["object"]["tag"]) == 2
1443 assert is_nil(modified["object"]["emoji"])
1444 assert is_nil(modified["object"]["like_count"])
1445 assert is_nil(modified["object"]["announcements"])
1446 assert is_nil(modified["object"]["announcement_count"])
1447 assert is_nil(modified["object"]["context_id"])
1448 assert is_nil(modified["object"]["likes"])
1451 test "the directMessage flag is present" do
1452 user = insert(:user)
1453 other_user = insert(:user)
1455 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1457 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1459 assert modified["directMessage"] == false
1462 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1464 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1466 assert modified["directMessage"] == false
1469 CommonAPI.post(user, %{
1470 "status" => "@#{other_user.nickname} :moominmamma:",
1471 "visibility" => "direct"
1474 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1476 assert modified["directMessage"] == true
1479 test "it strips BCC field" do
1480 user = insert(:user)
1481 {:ok, list} = Pleroma.List.create("foo", user)
1484 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1486 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1488 assert is_nil(modified["bcc"])
1491 test "it can handle Listen activities" do
1492 listen_activity = insert(:listen)
1494 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1496 assert modified["type"] == "Listen"
1498 user = insert(:user)
1500 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1502 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1506 describe "user upgrade" do
1507 test "it upgrades a user to activitypub" do
1510 nickname: "rye@niu.moe",
1512 ap_id: "https://niu.moe/users/rye",
1513 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1516 user_two = insert(:user)
1517 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1519 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1520 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1521 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1523 user = User.get_cached_by_id(user.id)
1524 assert user.note_count == 1
1526 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1527 ObanHelpers.perform_all()
1529 assert user.ap_enabled
1530 assert user.note_count == 1
1531 assert user.follower_address == "https://niu.moe/users/rye/followers"
1532 assert user.following_address == "https://niu.moe/users/rye/following"
1534 user = User.get_cached_by_id(user.id)
1535 assert user.note_count == 1
1537 activity = Activity.get_by_id(activity.id)
1538 assert user.follower_address in activity.recipients
1544 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1553 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1558 refute "..." in activity.recipients
1560 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1561 refute user.follower_address in unrelated_activity.recipients
1563 user_two = User.get_cached_by_id(user_two.id)
1564 assert User.following?(user_two, user)
1565 refute "..." in User.following(user_two)
1569 describe "actor rewriting" do
1570 test "it fixes the actor URL property to be a proper URI" do
1572 "url" => %{"href" => "http://example.com"}
1575 rewritten = Transmogrifier.maybe_fix_user_object(data)
1576 assert rewritten["url"] == "http://example.com"
1580 describe "actor origin containment" do
1581 test "it rejects activities which reference objects with bogus origins" do
1583 "@context" => "https://www.w3.org/ns/activitystreams",
1584 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1585 "actor" => "http://mastodon.example.org/users/admin",
1586 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1587 "object" => "https://info.pleroma.site/activity.json",
1588 "type" => "Announce"
1591 assert capture_log(fn ->
1592 :error = Transmogrifier.handle_incoming(data)
1593 end) =~ "Object containment failed"
1596 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1598 "@context" => "https://www.w3.org/ns/activitystreams",
1599 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1600 "actor" => "http://mastodon.example.org/users/admin",
1601 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1602 "object" => "https://info.pleroma.site/activity2.json",
1603 "type" => "Announce"
1606 assert capture_log(fn ->
1607 :error = Transmogrifier.handle_incoming(data)
1608 end) =~ "Object containment failed"
1611 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1613 "@context" => "https://www.w3.org/ns/activitystreams",
1614 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1615 "actor" => "http://mastodon.example.org/users/admin",
1616 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1617 "object" => "https://info.pleroma.site/activity3.json",
1618 "type" => "Announce"
1621 assert capture_log(fn ->
1622 :error = Transmogrifier.handle_incoming(data)
1623 end) =~ "Object containment failed"
1627 describe "reserialization" do
1628 test "successfully reserializes a message with inReplyTo == nil" do
1629 user = insert(:user)
1632 "@context" => "https://www.w3.org/ns/activitystreams",
1633 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1637 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1642 "attributedTo" => user.ap_id
1644 "actor" => user.ap_id
1647 {:ok, activity} = Transmogrifier.handle_incoming(message)
1649 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1652 test "successfully reserializes a message with AS2 objects in IR" do
1653 user = insert(:user)
1656 "@context" => "https://www.w3.org/ns/activitystreams",
1657 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1661 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1666 "attributedTo" => user.ap_id,
1668 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1669 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1672 "actor" => user.ap_id
1675 {:ok, activity} = Transmogrifier.handle_incoming(message)
1677 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1681 test "Rewrites Answers to Notes" do
1682 user = insert(:user)
1684 {:ok, poll_activity} =
1685 CommonAPI.post(user, %{
1686 "status" => "suya...",
1687 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1690 poll_object = Object.normalize(poll_activity)
1691 # TODO: Replace with CommonAPI vote creation when implemented
1693 File.read!("test/fixtures/mastodon-vote.json")
1695 |> Kernel.put_in(["to"], user.ap_id)
1696 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1697 |> Kernel.put_in(["object", "to"], user.ap_id)
1699 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1700 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1702 assert data["object"]["type"] == "Note"
1705 describe "fix_explicit_addressing" do
1707 user = insert(:user)
1711 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1712 explicitly_mentioned_actors = [
1713 "https://pleroma.gold/users/user1",
1714 "https://pleroma.gold/user2"
1718 "actor" => user.ap_id,
1719 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1722 Enum.map(explicitly_mentioned_actors, fn href ->
1723 %{"type" => "Mention", "href" => href}
1727 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1728 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1729 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1730 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1733 test "does not move actor's follower collection to cc", %{user: user} do
1735 "actor" => user.ap_id,
1736 "to" => [user.follower_address],
1740 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1741 assert user.follower_address in fixed_object["to"]
1742 refute user.follower_address in fixed_object["cc"]
1745 test "removes recipient's follower collection from cc", %{user: user} do
1746 recipient = insert(:user)
1749 "actor" => user.ap_id,
1750 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1751 "cc" => [user.follower_address, recipient.follower_address]
1754 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1756 assert user.follower_address in fixed_object["cc"]
1757 refute recipient.follower_address in fixed_object["cc"]
1758 refute recipient.follower_address in fixed_object["to"]
1762 describe "fix_summary/1" do
1763 test "returns fixed object" do
1764 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1765 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1766 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1770 describe "fix_in_reply_to/2" do
1771 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1774 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1778 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1779 assert Transmogrifier.fix_in_reply_to(data) == data
1782 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1783 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1786 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1788 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1789 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1790 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1793 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1795 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1796 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1797 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1800 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1802 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1803 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1804 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1806 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1807 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1808 assert modified_object["inReplyTo"] == []
1809 assert modified_object["inReplyToAtomUri"] == ""
1812 @tag capture_log: true
1813 test "returns modified object when allowed incoming reply", %{data: data} do
1818 "https://shitposter.club/notice/2827873"
1821 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1822 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1824 assert modified_object["inReplyTo"] ==
1825 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1827 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1829 assert modified_object["conversation"] ==
1830 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1832 assert modified_object["context"] ==
1833 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1837 describe "fix_url/1" do
1838 test "fixes data for object when url is map" do
1842 "mimeType" => "video/mp4",
1843 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1847 assert Transmogrifier.fix_url(object) == %{
1848 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1852 test "fixes data for video object" do
1858 "mimeType" => "video/mp4",
1859 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1863 "mimeType" => "video/mp4",
1864 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1868 "mimeType" => "text/html",
1869 "href" => "https://peertube.-2d4c2d1630e3"
1873 "mimeType" => "text/html",
1874 "href" => "https://peertube.-2d4c2d16377-42"
1879 assert Transmogrifier.fix_url(object) == %{
1882 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1883 "mimeType" => "video/mp4",
1888 "url" => "https://peertube.-2d4c2d1630e3"
1892 test "fixes url for not Video object" do
1898 "mimeType" => "text/html",
1899 "href" => "https://peertube.-2d4c2d1630e3"
1903 "mimeType" => "text/html",
1904 "href" => "https://peertube.-2d4c2d16377-42"
1909 assert Transmogrifier.fix_url(object) == %{
1911 "url" => "https://peertube.-2d4c2d1630e3"
1914 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1920 test "retunrs not modified object" do
1921 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1925 describe "get_obj_helper/2" do
1926 test "returns nil when cannot normalize object" do
1927 assert capture_log(fn ->
1928 refute Transmogrifier.get_obj_helper("test-obj-id")
1929 end) =~ "Unsupported URI scheme"
1932 @tag capture_log: true
1933 test "returns {:ok, %Object{}} for success case" do
1934 assert {:ok, %Object{}} =
1935 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1939 describe "fix_attachments/1" do
1940 test "returns not modified object" do
1941 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1942 assert Transmogrifier.fix_attachments(data) == data
1945 test "returns modified object when attachment is map" do
1946 assert Transmogrifier.fix_attachments(%{
1948 "mediaType" => "video/mp4",
1949 "url" => "https://peertube.moe/stat-480.mp4"
1954 "mediaType" => "video/mp4",
1956 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1963 test "returns modified object when attachment is list" do
1964 assert Transmogrifier.fix_attachments(%{
1966 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1967 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1972 "mediaType" => "video/mp4",
1974 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1978 "mediaType" => "video/mp4",
1980 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1988 describe "fix_emoji/1" do
1989 test "returns not modified object when object not contains tags" do
1990 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1991 assert Transmogrifier.fix_emoji(data) == data
1994 test "returns object with emoji when object contains list tags" do
1995 assert Transmogrifier.fix_emoji(%{
1997 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1998 %{"type" => "Hashtag"}
2001 "emoji" => %{"bib" => "/test"},
2003 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
2004 %{"type" => "Hashtag"}
2009 test "returns object with emoji when object contains map tag" do
2010 assert Transmogrifier.fix_emoji(%{
2011 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
2013 "emoji" => %{"bib" => "/test"},
2014 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
2019 describe "set_replies/1" do
2020 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
2022 test "returns unmodified object if activity doesn't have self-replies" do
2023 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2024 assert Transmogrifier.set_replies(data) == data
2027 test "sets `replies` collection with a limited number of self-replies" do
2028 [user, another_user] = insert_list(2, :user)
2030 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
2032 {:ok, %{id: id2} = self_reply1} =
2033 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
2035 {:ok, self_reply2} =
2036 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
2038 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
2040 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
2043 CommonAPI.post(user, %{
2044 "status" => "self-reply to self-reply",
2045 "in_reply_to_status_id" => id2
2049 CommonAPI.post(another_user, %{
2050 "status" => "another user's reply",
2051 "in_reply_to_status_id" => id1
2054 object = Object.normalize(activity)
2055 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
2057 assert %{"type" => "Collection", "items" => ^replies_uris} =
2058 Transmogrifier.set_replies(object.data)["replies"]
2062 test "take_emoji_tags/1" do
2063 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
2065 assert Transmogrifier.take_emoji_tags(user) == [
2067 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
2068 "id" => "https://example.org/firefox.png",
2069 "name" => ":firefox:",
2071 "updated" => "1970-01-01T00:00:00Z"