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 reaction undos" do
331 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
332 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
335 File.read!("test/fixtures/mastodon-undo-like.json")
337 |> Map.put("object", reaction_activity.data["id"])
338 |> Map.put("actor", user.ap_id)
340 {:ok, activity} = Transmogrifier.handle_incoming(data)
342 assert activity.actor == user.ap_id
343 assert activity.data["id"] == data["id"]
344 assert activity.data["type"] == "Undo"
347 test "it returns an error for incoming unlikes wihout a like activity" do
349 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
352 File.read!("test/fixtures/mastodon-undo-like.json")
354 |> Map.put("object", activity.data["object"])
356 assert Transmogrifier.handle_incoming(data) == :error
359 test "it works for incoming unlikes with an existing like activity" do
361 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
364 File.read!("test/fixtures/mastodon-like.json")
366 |> Map.put("object", activity.data["object"])
368 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
371 File.read!("test/fixtures/mastodon-undo-like.json")
373 |> Map.put("object", like_data)
374 |> Map.put("actor", like_data["actor"])
376 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
378 assert data["actor"] == "http://mastodon.example.org/users/admin"
379 assert data["type"] == "Undo"
380 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
381 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
384 test "it works for incoming unlikes with an existing like activity and a compact object" do
386 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
389 File.read!("test/fixtures/mastodon-like.json")
391 |> Map.put("object", activity.data["object"])
393 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
396 File.read!("test/fixtures/mastodon-undo-like.json")
398 |> Map.put("object", like_data["id"])
399 |> Map.put("actor", like_data["actor"])
401 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
403 assert data["actor"] == "http://mastodon.example.org/users/admin"
404 assert data["type"] == "Undo"
405 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
406 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
409 test "it works for incoming announces" do
410 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
412 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
414 assert data["actor"] == "http://mastodon.example.org/users/admin"
415 assert data["type"] == "Announce"
418 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
420 assert data["object"] ==
421 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
423 assert Activity.get_create_by_object_ap_id(data["object"])
426 test "it works for incoming announces with an existing activity" do
428 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
431 File.read!("test/fixtures/mastodon-announce.json")
433 |> Map.put("object", activity.data["object"])
435 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
437 assert data["actor"] == "http://mastodon.example.org/users/admin"
438 assert data["type"] == "Announce"
441 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
443 assert data["object"] == activity.data["object"]
445 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
448 test "it works for incoming announces with an inlined activity" do
450 File.read!("test/fixtures/mastodon-announce-private.json")
453 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
455 assert data["actor"] == "http://mastodon.example.org/users/admin"
456 assert data["type"] == "Announce"
459 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
461 object = Object.normalize(data["object"])
463 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
464 assert object.data["content"] == "this is a private toot"
467 @tag capture_log: true
468 test "it rejects incoming announces with an inlined activity from another origin" do
470 File.read!("test/fixtures/bogus-mastodon-announce.json")
473 assert :error = Transmogrifier.handle_incoming(data)
476 test "it does not clobber the addressing on announce activities" do
478 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
481 File.read!("test/fixtures/mastodon-announce.json")
483 |> Map.put("object", Object.normalize(activity).data["id"])
484 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
487 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
489 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
492 test "it ensures that as:Public activities make it to their followers collection" do
496 File.read!("test/fixtures/mastodon-post-activity.json")
498 |> Map.put("actor", user.ap_id)
499 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
504 |> Map.put("attributedTo", user.ap_id)
505 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
507 |> Map.put("id", user.ap_id <> "/activities/12345678")
509 data = Map.put(data, "object", object)
511 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
513 assert data["cc"] == [User.ap_followers(user)]
516 test "it ensures that address fields become lists" do
520 File.read!("test/fixtures/mastodon-post-activity.json")
522 |> Map.put("actor", user.ap_id)
523 |> Map.put("to", nil)
524 |> Map.put("cc", nil)
528 |> Map.put("attributedTo", user.ap_id)
529 |> Map.put("to", nil)
530 |> Map.put("cc", nil)
531 |> Map.put("id", user.ap_id <> "/activities/12345678")
533 data = Map.put(data, "object", object)
535 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
537 assert !is_nil(data["to"])
538 assert !is_nil(data["cc"])
541 test "it strips internal likes" do
543 File.read!("test/fixtures/mastodon-post-activity.json")
548 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
549 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
551 "type" => "OrderedCollection"
554 object = Map.put(data["object"], "likes", likes)
555 data = Map.put(data, "object", object)
557 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
559 refute Map.has_key?(object.data, "likes")
562 test "it strips internal reactions" do
564 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
565 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
567 %{object: object} = Activity.get_by_id_with_object(activity.id)
568 assert Map.has_key?(object.data, "reactions")
569 assert Map.has_key?(object.data, "reaction_count")
571 object_data = Transmogrifier.strip_internal_fields(object.data)
572 refute Map.has_key?(object_data, "reactions")
573 refute Map.has_key?(object_data, "reaction_count")
576 test "it works for incoming update activities" do
577 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
579 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
580 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
583 update_data["object"]
584 |> Map.put("actor", data["actor"])
585 |> Map.put("id", data["actor"])
589 |> Map.put("actor", data["actor"])
590 |> Map.put("object", object)
592 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
594 assert data["id"] == update_data["id"]
596 user = User.get_cached_by_ap_id(data["actor"])
597 assert user.name == "gargle"
599 assert user.avatar["url"] == [
602 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
606 assert user.banner["url"] == [
609 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
613 assert user.bio == "<p>Some bio</p>"
616 test "it works with alsoKnownAs" do
617 {:ok, %Activity{data: %{"actor" => actor}}} =
618 "test/fixtures/mastodon-post-activity.json"
621 |> Transmogrifier.handle_incoming()
623 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
626 "test/fixtures/mastodon-update.json"
629 |> Map.put("actor", actor)
630 |> Map.update!("object", fn object ->
632 |> Map.put("actor", actor)
633 |> Map.put("id", actor)
634 |> Map.put("alsoKnownAs", [
635 "http://mastodon.example.org/users/foo",
636 "http://example.org/users/bar"
639 |> Transmogrifier.handle_incoming()
641 assert User.get_cached_by_ap_id(actor).also_known_as == [
642 "http://mastodon.example.org/users/foo",
643 "http://example.org/users/bar"
647 test "it works with custom profile fields" do
649 "test/fixtures/mastodon-post-activity.json"
652 |> Transmogrifier.handle_incoming()
654 user = User.get_cached_by_ap_id(activity.actor)
656 assert user.fields == [
657 %{"name" => "foo", "value" => "bar"},
658 %{"name" => "foo1", "value" => "bar1"}
661 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
664 update_data["object"]
665 |> Map.put("actor", user.ap_id)
666 |> Map.put("id", user.ap_id)
670 |> Map.put("actor", user.ap_id)
671 |> Map.put("object", object)
673 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
675 user = User.get_cached_by_ap_id(user.ap_id)
677 assert user.fields == [
678 %{"name" => "foo", "value" => "updated"},
679 %{"name" => "foo1", "value" => "updated"}
682 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
685 put_in(update_data, ["object", "attachment"], [
686 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
687 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
688 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
691 {:ok, _} = Transmogrifier.handle_incoming(update_data)
693 user = User.get_cached_by_ap_id(user.ap_id)
695 assert user.fields == [
696 %{"name" => "foo", "value" => "updated"},
697 %{"name" => "foo1", "value" => "updated"}
700 update_data = put_in(update_data, ["object", "attachment"], [])
702 {:ok, _} = Transmogrifier.handle_incoming(update_data)
704 user = User.get_cached_by_ap_id(user.ap_id)
706 assert user.fields == []
709 test "it works for incoming update activities which lock the account" do
710 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
712 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
713 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
716 update_data["object"]
717 |> Map.put("actor", data["actor"])
718 |> Map.put("id", data["actor"])
719 |> Map.put("manuallyApprovesFollowers", true)
723 |> Map.put("actor", data["actor"])
724 |> Map.put("object", object)
726 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
728 user = User.get_cached_by_ap_id(data["actor"])
729 assert user.locked == true
732 test "it works for incoming deletes" do
733 activity = insert(:note_activity)
734 deleting_user = insert(:user)
737 File.read!("test/fixtures/mastodon-delete.json")
742 |> Map.put("id", activity.data["object"])
746 |> Map.put("object", object)
747 |> Map.put("actor", deleting_user.ap_id)
749 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
750 Transmogrifier.handle_incoming(data)
752 assert id == data["id"]
753 refute Activity.get_by_id(activity.id)
754 assert actor == deleting_user.ap_id
757 test "it fails for incoming deletes with spoofed origin" do
758 activity = insert(:note_activity)
761 File.read!("test/fixtures/mastodon-delete.json")
766 |> Map.put("id", activity.data["object"])
770 |> Map.put("object", object)
772 assert capture_log(fn ->
773 :error = Transmogrifier.handle_incoming(data)
775 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
777 assert Activity.get_by_id(activity.id)
780 @tag capture_log: true
781 test "it works for incoming user deletes" do
783 insert(:user, ap_id: "http://mastodon.example.org/users/admin", local: false)
786 File.read!("test/fixtures/mastodon-delete-user.json")
789 {:ok, _} = Transmogrifier.handle_incoming(data)
790 ObanHelpers.perform_all()
792 refute User.get_cached_by_ap_id(ap_id)
795 test "it fails for incoming user deletes with spoofed origin" do
796 %{ap_id: ap_id} = insert(:user)
799 File.read!("test/fixtures/mastodon-delete-user.json")
801 |> Map.put("actor", ap_id)
803 assert capture_log(fn ->
804 assert :error == Transmogrifier.handle_incoming(data)
805 end) =~ "Object containment failed"
807 assert User.get_cached_by_ap_id(ap_id)
810 test "it works for incoming unannounces with an existing notice" do
812 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
815 File.read!("test/fixtures/mastodon-announce.json")
817 |> Map.put("object", activity.data["object"])
819 {:ok, %Activity{data: announce_data, local: false}} =
820 Transmogrifier.handle_incoming(announce_data)
823 File.read!("test/fixtures/mastodon-undo-announce.json")
825 |> Map.put("object", announce_data)
826 |> Map.put("actor", announce_data["actor"])
828 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
830 assert data["type"] == "Undo"
831 assert object_data = data["object"]
832 assert object_data["type"] == "Announce"
833 assert object_data["object"] == activity.data["object"]
835 assert object_data["id"] ==
836 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
839 test "it works for incomming unfollows with an existing follow" do
843 File.read!("test/fixtures/mastodon-follow-activity.json")
845 |> Map.put("object", user.ap_id)
847 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
850 File.read!("test/fixtures/mastodon-unfollow-activity.json")
852 |> Map.put("object", follow_data)
854 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
856 assert data["type"] == "Undo"
857 assert data["object"]["type"] == "Follow"
858 assert data["object"]["object"] == user.ap_id
859 assert data["actor"] == "http://mastodon.example.org/users/admin"
861 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
864 test "it works for incoming follows to locked account" do
865 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
866 user = insert(:user, locked: true)
869 File.read!("test/fixtures/mastodon-follow-activity.json")
871 |> Map.put("object", user.ap_id)
873 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
875 assert data["type"] == "Follow"
876 assert data["object"] == user.ap_id
877 assert data["state"] == "pending"
878 assert data["actor"] == "http://mastodon.example.org/users/admin"
880 assert [^pending_follower] = User.get_follow_requests(user)
883 test "it works for incoming blocks" do
887 File.read!("test/fixtures/mastodon-block-activity.json")
889 |> Map.put("object", user.ap_id)
891 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
893 assert data["type"] == "Block"
894 assert data["object"] == user.ap_id
895 assert data["actor"] == "http://mastodon.example.org/users/admin"
897 blocker = User.get_cached_by_ap_id(data["actor"])
899 assert User.blocks?(blocker, user)
902 test "incoming blocks successfully tear down any follow relationship" do
903 blocker = insert(:user)
904 blocked = insert(:user)
907 File.read!("test/fixtures/mastodon-block-activity.json")
909 |> Map.put("object", blocked.ap_id)
910 |> Map.put("actor", blocker.ap_id)
912 {:ok, blocker} = User.follow(blocker, blocked)
913 {:ok, blocked} = User.follow(blocked, blocker)
915 assert User.following?(blocker, blocked)
916 assert User.following?(blocked, blocker)
918 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
920 assert data["type"] == "Block"
921 assert data["object"] == blocked.ap_id
922 assert data["actor"] == blocker.ap_id
924 blocker = User.get_cached_by_ap_id(data["actor"])
925 blocked = User.get_cached_by_ap_id(data["object"])
927 assert User.blocks?(blocker, blocked)
929 refute User.following?(blocker, blocked)
930 refute User.following?(blocked, blocker)
933 test "it works for incoming unblocks with an existing block" do
937 File.read!("test/fixtures/mastodon-block-activity.json")
939 |> Map.put("object", user.ap_id)
941 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
944 File.read!("test/fixtures/mastodon-unblock-activity.json")
946 |> Map.put("object", block_data)
948 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
949 assert data["type"] == "Undo"
950 assert data["object"]["type"] == "Block"
951 assert data["object"]["object"] == user.ap_id
952 assert data["actor"] == "http://mastodon.example.org/users/admin"
954 blocker = User.get_cached_by_ap_id(data["actor"])
956 refute User.blocks?(blocker, user)
959 test "it works for incoming accepts which were pre-accepted" do
960 follower = insert(:user)
961 followed = insert(:user)
963 {:ok, follower} = User.follow(follower, followed)
964 assert User.following?(follower, followed) == true
966 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
969 File.read!("test/fixtures/mastodon-accept-activity.json")
971 |> Map.put("actor", followed.ap_id)
974 accept_data["object"]
975 |> Map.put("actor", follower.ap_id)
976 |> Map.put("id", follow_activity.data["id"])
978 accept_data = Map.put(accept_data, "object", object)
980 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
981 refute activity.local
983 assert activity.data["object"] == follow_activity.data["id"]
985 assert activity.data["id"] == accept_data["id"]
987 follower = User.get_cached_by_id(follower.id)
989 assert User.following?(follower, followed) == true
992 test "it works for incoming accepts which were orphaned" do
993 follower = insert(:user)
994 followed = insert(:user, locked: true)
996 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
999 File.read!("test/fixtures/mastodon-accept-activity.json")
1001 |> Map.put("actor", followed.ap_id)
1004 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1006 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1007 assert activity.data["object"] == follow_activity.data["id"]
1009 follower = User.get_cached_by_id(follower.id)
1011 assert User.following?(follower, followed) == true
1014 test "it works for incoming accepts which are referenced by IRI only" do
1015 follower = insert(:user)
1016 followed = insert(:user, locked: true)
1018 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1021 File.read!("test/fixtures/mastodon-accept-activity.json")
1023 |> Map.put("actor", followed.ap_id)
1024 |> Map.put("object", follow_activity.data["id"])
1026 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1027 assert activity.data["object"] == follow_activity.data["id"]
1029 follower = User.get_cached_by_id(follower.id)
1031 assert User.following?(follower, followed) == true
1034 test "it fails for incoming accepts which cannot be correlated" do
1035 follower = insert(:user)
1036 followed = insert(:user, locked: true)
1039 File.read!("test/fixtures/mastodon-accept-activity.json")
1041 |> Map.put("actor", followed.ap_id)
1044 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1046 :error = Transmogrifier.handle_incoming(accept_data)
1048 follower = User.get_cached_by_id(follower.id)
1050 refute User.following?(follower, followed) == true
1053 test "it fails for incoming rejects which cannot be correlated" do
1054 follower = insert(:user)
1055 followed = insert(:user, locked: true)
1058 File.read!("test/fixtures/mastodon-reject-activity.json")
1060 |> Map.put("actor", followed.ap_id)
1063 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1065 :error = Transmogrifier.handle_incoming(accept_data)
1067 follower = User.get_cached_by_id(follower.id)
1069 refute User.following?(follower, followed) == true
1072 test "it works for incoming rejects which are orphaned" do
1073 follower = insert(:user)
1074 followed = insert(:user, locked: true)
1076 {:ok, follower} = User.follow(follower, followed)
1077 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1079 assert User.following?(follower, followed) == true
1082 File.read!("test/fixtures/mastodon-reject-activity.json")
1084 |> Map.put("actor", followed.ap_id)
1087 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1089 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1090 refute activity.local
1091 assert activity.data["id"] == reject_data["id"]
1093 follower = User.get_cached_by_id(follower.id)
1095 assert User.following?(follower, followed) == false
1098 test "it works for incoming rejects which are referenced by IRI only" do
1099 follower = insert(:user)
1100 followed = insert(:user, locked: true)
1102 {:ok, follower} = User.follow(follower, followed)
1103 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1105 assert User.following?(follower, followed) == true
1108 File.read!("test/fixtures/mastodon-reject-activity.json")
1110 |> Map.put("actor", followed.ap_id)
1111 |> Map.put("object", follow_activity.data["id"])
1113 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1115 follower = User.get_cached_by_id(follower.id)
1117 assert User.following?(follower, followed) == false
1120 test "it rejects activities without a valid ID" do
1121 user = insert(:user)
1124 File.read!("test/fixtures/mastodon-follow-activity.json")
1126 |> Map.put("object", user.ap_id)
1127 |> Map.put("id", "")
1129 :error = Transmogrifier.handle_incoming(data)
1132 test "skip converting the content when it is nil" do
1133 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1135 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1138 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
1140 assert result["content"] == nil
1143 test "it converts content of object to html" do
1144 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1146 {:ok, %{"content" => content_markdown}} =
1147 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1149 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
1150 Fetcher.fetch_object_from_id(object_id)
1152 assert content_markdown ==
1153 "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..."
1156 "<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>"
1158 assert object.data["mediaType"] == "text/html"
1161 test "it remaps video URLs as attachments if necessary" do
1163 Fetcher.fetch_object_from_id(
1164 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1169 "mediaType" => "video/mp4",
1173 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1174 "mediaType" => "video/mp4"
1179 assert object.data["url"] ==
1180 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1182 assert object.data["attachment"] == [attachment]
1185 test "it accepts Flag activities" do
1186 user = insert(:user)
1187 other_user = insert(:user)
1189 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1190 object = Object.normalize(activity)
1194 "id" => activity.data["id"],
1195 "content" => "test post",
1196 "published" => object.data["published"],
1197 "actor" => AccountView.render("show.json", %{user: user})
1201 "@context" => "https://www.w3.org/ns/activitystreams",
1202 "cc" => [user.ap_id],
1203 "object" => [user.ap_id, activity.data["id"]],
1205 "content" => "blocked AND reported!!!",
1206 "actor" => other_user.ap_id
1209 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1211 assert activity.data["object"] == [user.ap_id, note_obj]
1212 assert activity.data["content"] == "blocked AND reported!!!"
1213 assert activity.data["actor"] == other_user.ap_id
1214 assert activity.data["cc"] == [user.ap_id]
1217 test "it correctly processes messages with non-array to field" do
1218 user = insert(:user)
1221 "@context" => "https://www.w3.org/ns/activitystreams",
1222 "to" => "https://www.w3.org/ns/activitystreams#Public",
1225 "content" => "blah blah blah",
1227 "attributedTo" => user.ap_id,
1230 "actor" => user.ap_id
1233 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1235 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1238 test "it correctly processes messages with non-array cc field" do
1239 user = insert(:user)
1242 "@context" => "https://www.w3.org/ns/activitystreams",
1243 "to" => user.follower_address,
1244 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1247 "content" => "blah blah blah",
1249 "attributedTo" => user.ap_id,
1252 "actor" => user.ap_id
1255 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1257 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1258 assert [user.follower_address] == activity.data["to"]
1261 test "it accepts Move activities" do
1262 old_user = insert(:user)
1263 new_user = insert(:user)
1266 "@context" => "https://www.w3.org/ns/activitystreams",
1268 "actor" => old_user.ap_id,
1269 "object" => old_user.ap_id,
1270 "target" => new_user.ap_id
1273 assert :error = Transmogrifier.handle_incoming(message)
1275 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1277 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1278 assert activity.actor == old_user.ap_id
1279 assert activity.data["actor"] == old_user.ap_id
1280 assert activity.data["object"] == old_user.ap_id
1281 assert activity.data["target"] == new_user.ap_id
1282 assert activity.data["type"] == "Move"
1286 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1287 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1288 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1292 "test/fixtures/mastodon-post-activity.json"
1296 items = get_in(data, ["object", "replies", "first", "items"])
1297 assert length(items) > 0
1299 %{data: data, items: items}
1302 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1306 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1308 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1311 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1312 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1316 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1318 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1320 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1322 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1326 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1327 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1328 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1331 user = insert(:user)
1333 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1336 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1339 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1341 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1343 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1345 Repo.delete(activity.object)
1346 Repo.delete(activity)
1348 %{federation_output: federation_output, replies_uris: replies_uris}
1351 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1352 federation_output: federation_output,
1353 replies_uris: replies_uris
1355 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1357 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1359 for id <- replies_uris do
1360 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1361 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1365 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1366 %{federation_output: federation_output} do
1367 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1369 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1371 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1375 describe "prepare outgoing" do
1376 test "it inlines private announced objects" do
1377 user = insert(:user)
1379 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1381 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1383 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1385 assert modified["object"]["content"] == "hey"
1386 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1389 test "it turns mentions into tags" do
1390 user = insert(:user)
1391 other_user = insert(:user)
1394 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1396 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1397 object = modified["object"]
1399 expected_mention = %{
1400 "href" => other_user.ap_id,
1401 "name" => "@#{other_user.nickname}",
1406 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1407 "type" => "Hashtag",
1411 assert Enum.member?(object["tag"], expected_tag)
1412 assert Enum.member?(object["tag"], expected_mention)
1415 test "it adds the sensitive property" do
1416 user = insert(:user)
1418 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1419 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1421 assert modified["object"]["sensitive"]
1424 test "it adds the json-ld context and the conversation property" do
1425 user = insert(:user)
1427 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1428 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1430 assert modified["@context"] ==
1431 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1433 assert modified["object"]["conversation"] == modified["context"]
1436 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1437 user = insert(:user)
1439 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1440 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1442 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1445 test "it strips internal hashtag data" do
1446 user = insert(:user)
1448 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1451 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1452 "type" => "Hashtag",
1456 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1458 assert modified["object"]["tag"] == [expected_tag]
1461 test "it strips internal fields" do
1462 user = insert(:user)
1464 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1466 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1468 assert length(modified["object"]["tag"]) == 2
1470 assert is_nil(modified["object"]["emoji"])
1471 assert is_nil(modified["object"]["like_count"])
1472 assert is_nil(modified["object"]["announcements"])
1473 assert is_nil(modified["object"]["announcement_count"])
1474 assert is_nil(modified["object"]["context_id"])
1477 test "it strips internal fields of article" do
1478 activity = insert(:article_activity)
1480 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1482 assert length(modified["object"]["tag"]) == 2
1484 assert is_nil(modified["object"]["emoji"])
1485 assert is_nil(modified["object"]["like_count"])
1486 assert is_nil(modified["object"]["announcements"])
1487 assert is_nil(modified["object"]["announcement_count"])
1488 assert is_nil(modified["object"]["context_id"])
1489 assert is_nil(modified["object"]["likes"])
1492 test "the directMessage flag is present" do
1493 user = insert(:user)
1494 other_user = insert(:user)
1496 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1498 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1500 assert modified["directMessage"] == false
1503 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1505 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1507 assert modified["directMessage"] == false
1510 CommonAPI.post(user, %{
1511 "status" => "@#{other_user.nickname} :moominmamma:",
1512 "visibility" => "direct"
1515 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1517 assert modified["directMessage"] == true
1520 test "it strips BCC field" do
1521 user = insert(:user)
1522 {:ok, list} = Pleroma.List.create("foo", user)
1525 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1527 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1529 assert is_nil(modified["bcc"])
1532 test "it can handle Listen activities" do
1533 listen_activity = insert(:listen)
1535 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1537 assert modified["type"] == "Listen"
1539 user = insert(:user)
1541 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1543 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1547 describe "user upgrade" do
1548 test "it upgrades a user to activitypub" do
1551 nickname: "rye@niu.moe",
1553 ap_id: "https://niu.moe/users/rye",
1554 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1557 user_two = insert(:user)
1558 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1560 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1561 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1562 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1564 user = User.get_cached_by_id(user.id)
1565 assert user.note_count == 1
1567 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1568 ObanHelpers.perform_all()
1570 assert user.ap_enabled
1571 assert user.note_count == 1
1572 assert user.follower_address == "https://niu.moe/users/rye/followers"
1573 assert user.following_address == "https://niu.moe/users/rye/following"
1575 user = User.get_cached_by_id(user.id)
1576 assert user.note_count == 1
1578 activity = Activity.get_by_id(activity.id)
1579 assert user.follower_address in activity.recipients
1585 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1594 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1599 refute "..." in activity.recipients
1601 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1602 refute user.follower_address in unrelated_activity.recipients
1604 user_two = User.get_cached_by_id(user_two.id)
1605 assert User.following?(user_two, user)
1606 refute "..." in User.following(user_two)
1610 describe "actor rewriting" do
1611 test "it fixes the actor URL property to be a proper URI" do
1613 "url" => %{"href" => "http://example.com"}
1616 rewritten = Transmogrifier.maybe_fix_user_object(data)
1617 assert rewritten["url"] == "http://example.com"
1621 describe "actor origin containment" do
1622 test "it rejects activities which reference objects with bogus origins" do
1624 "@context" => "https://www.w3.org/ns/activitystreams",
1625 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1626 "actor" => "http://mastodon.example.org/users/admin",
1627 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1628 "object" => "https://info.pleroma.site/activity.json",
1629 "type" => "Announce"
1632 assert capture_log(fn ->
1633 :error = Transmogrifier.handle_incoming(data)
1634 end) =~ "Object containment failed"
1637 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1639 "@context" => "https://www.w3.org/ns/activitystreams",
1640 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1641 "actor" => "http://mastodon.example.org/users/admin",
1642 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1643 "object" => "https://info.pleroma.site/activity2.json",
1644 "type" => "Announce"
1647 assert capture_log(fn ->
1648 :error = Transmogrifier.handle_incoming(data)
1649 end) =~ "Object containment failed"
1652 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1654 "@context" => "https://www.w3.org/ns/activitystreams",
1655 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1656 "actor" => "http://mastodon.example.org/users/admin",
1657 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1658 "object" => "https://info.pleroma.site/activity3.json",
1659 "type" => "Announce"
1662 assert capture_log(fn ->
1663 :error = Transmogrifier.handle_incoming(data)
1664 end) =~ "Object containment failed"
1668 describe "reserialization" do
1669 test "successfully reserializes a message with inReplyTo == nil" do
1670 user = insert(:user)
1673 "@context" => "https://www.w3.org/ns/activitystreams",
1674 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1678 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1683 "attributedTo" => user.ap_id
1685 "actor" => user.ap_id
1688 {:ok, activity} = Transmogrifier.handle_incoming(message)
1690 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1693 test "successfully reserializes a message with AS2 objects in IR" do
1694 user = insert(:user)
1697 "@context" => "https://www.w3.org/ns/activitystreams",
1698 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1702 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1707 "attributedTo" => user.ap_id,
1709 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1710 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1713 "actor" => user.ap_id
1716 {:ok, activity} = Transmogrifier.handle_incoming(message)
1718 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1722 test "Rewrites Answers to Notes" do
1723 user = insert(:user)
1725 {:ok, poll_activity} =
1726 CommonAPI.post(user, %{
1727 "status" => "suya...",
1728 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1731 poll_object = Object.normalize(poll_activity)
1732 # TODO: Replace with CommonAPI vote creation when implemented
1734 File.read!("test/fixtures/mastodon-vote.json")
1736 |> Kernel.put_in(["to"], user.ap_id)
1737 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1738 |> Kernel.put_in(["object", "to"], user.ap_id)
1740 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1741 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1743 assert data["object"]["type"] == "Note"
1746 describe "fix_explicit_addressing" do
1748 user = insert(:user)
1752 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1753 explicitly_mentioned_actors = [
1754 "https://pleroma.gold/users/user1",
1755 "https://pleroma.gold/user2"
1759 "actor" => user.ap_id,
1760 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1763 Enum.map(explicitly_mentioned_actors, fn href ->
1764 %{"type" => "Mention", "href" => href}
1768 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1769 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1770 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1771 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1774 test "does not move actor's follower collection to cc", %{user: user} do
1776 "actor" => user.ap_id,
1777 "to" => [user.follower_address],
1781 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1782 assert user.follower_address in fixed_object["to"]
1783 refute user.follower_address in fixed_object["cc"]
1786 test "removes recipient's follower collection from cc", %{user: user} do
1787 recipient = insert(:user)
1790 "actor" => user.ap_id,
1791 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1792 "cc" => [user.follower_address, recipient.follower_address]
1795 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1797 assert user.follower_address in fixed_object["cc"]
1798 refute recipient.follower_address in fixed_object["cc"]
1799 refute recipient.follower_address in fixed_object["to"]
1803 describe "fix_summary/1" do
1804 test "returns fixed object" do
1805 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1806 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1807 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1811 describe "fix_in_reply_to/2" do
1812 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1815 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1819 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1820 assert Transmogrifier.fix_in_reply_to(data) == data
1823 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1824 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1827 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1829 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1830 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1831 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1834 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1836 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1837 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1838 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1841 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1843 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1844 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1845 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1847 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1848 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1849 assert modified_object["inReplyTo"] == []
1850 assert modified_object["inReplyToAtomUri"] == ""
1853 @tag capture_log: true
1854 test "returns modified object when allowed incoming reply", %{data: data} do
1859 "https://shitposter.club/notice/2827873"
1862 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1863 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1865 assert modified_object["inReplyTo"] ==
1866 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1868 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1870 assert modified_object["conversation"] ==
1871 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1873 assert modified_object["context"] ==
1874 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1878 describe "fix_url/1" do
1879 test "fixes data for object when url is map" do
1883 "mimeType" => "video/mp4",
1884 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1888 assert Transmogrifier.fix_url(object) == %{
1889 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1893 test "fixes data for video object" do
1899 "mimeType" => "video/mp4",
1900 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1904 "mimeType" => "video/mp4",
1905 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1909 "mimeType" => "text/html",
1910 "href" => "https://peertube.-2d4c2d1630e3"
1914 "mimeType" => "text/html",
1915 "href" => "https://peertube.-2d4c2d16377-42"
1920 assert Transmogrifier.fix_url(object) == %{
1923 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1924 "mimeType" => "video/mp4",
1929 "url" => "https://peertube.-2d4c2d1630e3"
1933 test "fixes url for not Video object" do
1939 "mimeType" => "text/html",
1940 "href" => "https://peertube.-2d4c2d1630e3"
1944 "mimeType" => "text/html",
1945 "href" => "https://peertube.-2d4c2d16377-42"
1950 assert Transmogrifier.fix_url(object) == %{
1952 "url" => "https://peertube.-2d4c2d1630e3"
1955 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1961 test "retunrs not modified object" do
1962 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1966 describe "get_obj_helper/2" do
1967 test "returns nil when cannot normalize object" do
1968 assert capture_log(fn ->
1969 refute Transmogrifier.get_obj_helper("test-obj-id")
1970 end) =~ "Unsupported URI scheme"
1973 @tag capture_log: true
1974 test "returns {:ok, %Object{}} for success case" do
1975 assert {:ok, %Object{}} =
1976 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1980 describe "fix_attachments/1" do
1981 test "returns not modified object" do
1982 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1983 assert Transmogrifier.fix_attachments(data) == data
1986 test "returns modified object when attachment is map" do
1987 assert Transmogrifier.fix_attachments(%{
1989 "mediaType" => "video/mp4",
1990 "url" => "https://peertube.moe/stat-480.mp4"
1995 "mediaType" => "video/mp4",
1997 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
2004 test "returns modified object when attachment is list" do
2005 assert Transmogrifier.fix_attachments(%{
2007 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
2008 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
2013 "mediaType" => "video/mp4",
2015 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2019 "mediaType" => "video/mp4",
2021 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2029 describe "fix_emoji/1" do
2030 test "returns not modified object when object not contains tags" do
2031 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2032 assert Transmogrifier.fix_emoji(data) == data
2035 test "returns object with emoji when object contains list tags" do
2036 assert Transmogrifier.fix_emoji(%{
2038 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
2039 %{"type" => "Hashtag"}
2042 "emoji" => %{"bib" => "/test"},
2044 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
2045 %{"type" => "Hashtag"}
2050 test "returns object with emoji when object contains map tag" do
2051 assert Transmogrifier.fix_emoji(%{
2052 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
2054 "emoji" => %{"bib" => "/test"},
2055 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
2060 describe "set_replies/1" do
2061 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
2063 test "returns unmodified object if activity doesn't have self-replies" do
2064 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2065 assert Transmogrifier.set_replies(data) == data
2068 test "sets `replies` collection with a limited number of self-replies" do
2069 [user, another_user] = insert_list(2, :user)
2071 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
2073 {:ok, %{id: id2} = self_reply1} =
2074 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
2076 {:ok, self_reply2} =
2077 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
2079 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
2081 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
2084 CommonAPI.post(user, %{
2085 "status" => "self-reply to self-reply",
2086 "in_reply_to_status_id" => id2
2090 CommonAPI.post(another_user, %{
2091 "status" => "another user's reply",
2092 "in_reply_to_status_id" => id1
2095 object = Object.normalize(activity)
2096 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
2098 assert %{"type" => "Collection", "items" => ^replies_uris} =
2099 Transmogrifier.set_replies(object.data)["replies"]
2103 test "take_emoji_tags/1" do
2104 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
2106 assert Transmogrifier.take_emoji_tags(user) == [
2108 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
2109 "id" => "https://example.org/firefox.png",
2110 "name" => ":firefox:",
2112 "updated" => "1970-01-01T00:00:00Z"