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.Transmogrifier
15 alias Pleroma.Web.AdminAPI.AccountView
16 alias Pleroma.Web.CommonAPI
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 setup do: clear_config([:instance, :max_remote_account_fields])
29 describe "handle_incoming" do
30 test "it works for incoming notices with tag not being an array (kroeg)" do
31 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
33 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
34 object = Object.normalize(data["object"])
36 assert object.data["emoji"] == %{
37 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
40 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
42 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
43 object = Object.normalize(data["object"])
45 assert "test" in object.data["tag"]
48 test "it works for incoming notices with url not being a string (prismo)" do
49 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
51 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
52 object = Object.normalize(data["object"])
54 assert object.data["url"] == "https://prismo.news/posts/83"
57 test "it cleans up incoming notices which are not really DMs" do
59 other_user = insert(:user)
61 to = [user.ap_id, other_user.ap_id]
64 File.read!("test/fixtures/mastodon-post-activity.json")
74 data = Map.put(data, "object", object)
76 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
78 assert data["to"] == []
79 assert data["cc"] == to
81 object_data = Object.normalize(activity).data
83 assert object_data["to"] == []
84 assert object_data["cc"] == to
87 test "it ignores an incoming notice if we already have it" do
88 activity = insert(:note_activity)
91 File.read!("test/fixtures/mastodon-post-activity.json")
93 |> Map.put("object", Object.normalize(activity).data)
95 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
97 assert activity == returned_activity
100 @tag capture_log: true
101 test "it fetches reply-to activities if we don't have them" do
103 File.read!("test/fixtures/mastodon-post-activity.json")
108 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
110 data = Map.put(data, "object", object)
111 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
112 returned_object = Object.normalize(returned_activity, false)
115 Activity.get_create_by_object_ap_id(
116 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
119 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
122 test "it does not fetch reply-to activities beyond max replies depth limit" do
124 File.read!("test/fixtures/mastodon-post-activity.json")
129 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
131 data = Map.put(data, "object", object)
133 with_mock Pleroma.Web.Federator,
134 allowed_thread_distance?: fn _ -> false end do
135 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
137 returned_object = Object.normalize(returned_activity, false)
139 refute Activity.get_create_by_object_ap_id(
140 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
143 assert returned_object.data["inReplyToAtomUri"] ==
144 "https://shitposter.club/notice/2827873"
148 test "it does not crash if the object in inReplyTo can't be fetched" do
150 File.read!("test/fixtures/mastodon-post-activity.json")
155 |> Map.put("inReplyTo", "https://404.site/whatever")
159 |> Map.put("object", object)
161 assert capture_log(fn ->
162 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
163 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
166 test "it works for incoming notices" do
167 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
169 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
172 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
174 assert data["context"] ==
175 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
177 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
179 assert data["cc"] == [
180 "http://mastodon.example.org/users/admin/followers",
181 "http://localtesting.pleroma.lol/users/lain"
184 assert data["actor"] == "http://mastodon.example.org/users/admin"
186 object_data = Object.normalize(data["object"]).data
188 assert object_data["id"] ==
189 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
191 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
193 assert object_data["cc"] == [
194 "http://mastodon.example.org/users/admin/followers",
195 "http://localtesting.pleroma.lol/users/lain"
198 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
199 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
201 assert object_data["context"] ==
202 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
204 assert object_data["sensitive"] == true
206 user = User.get_cached_by_ap_id(object_data["actor"])
208 assert user.note_count == 1
211 test "it works for incoming notices with hashtags" do
212 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
214 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
215 object = Object.normalize(data["object"])
217 assert Enum.at(object.data["tag"], 2) == "moo"
220 test "it works for incoming listens" do
222 "@context" => "https://www.w3.org/ns/activitystreams",
223 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
226 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
227 "actor" => "http://mastodon.example.org/users/admin",
230 "id" => "http://mastodon.example.org/users/admin/listens/1234",
231 "attributedTo" => "http://mastodon.example.org/users/admin",
232 "title" => "lain radio episode 1",
234 "album" => "lain radio",
239 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
241 object = Object.normalize(activity)
243 assert object.data["title"] == "lain radio episode 1"
244 assert object.data["artist"] == "lain"
245 assert object.data["album"] == "lain radio"
246 assert object.data["length"] == 180_000
249 test "it works for incoming notices with contentMap" do
251 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
253 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
254 object = Object.normalize(data["object"])
256 assert object.data["content"] ==
257 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
260 test "it works for incoming notices with to/cc not being an array (kroeg)" do
261 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
263 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
264 object = Object.normalize(data["object"])
266 assert object.data["content"] ==
267 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
270 test "it ensures that as:Public activities make it to their followers collection" do
274 File.read!("test/fixtures/mastodon-post-activity.json")
276 |> Map.put("actor", user.ap_id)
277 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
282 |> Map.put("attributedTo", user.ap_id)
283 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
285 |> Map.put("id", user.ap_id <> "/activities/12345678")
287 data = Map.put(data, "object", object)
289 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
291 assert data["cc"] == [User.ap_followers(user)]
294 test "it ensures that address fields become lists" do
298 File.read!("test/fixtures/mastodon-post-activity.json")
300 |> Map.put("actor", user.ap_id)
301 |> Map.put("to", nil)
302 |> Map.put("cc", nil)
306 |> Map.put("attributedTo", user.ap_id)
307 |> Map.put("to", nil)
308 |> Map.put("cc", nil)
309 |> Map.put("id", user.ap_id <> "/activities/12345678")
311 data = Map.put(data, "object", object)
313 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
315 assert !is_nil(data["to"])
316 assert !is_nil(data["cc"])
319 test "it strips internal likes" do
321 File.read!("test/fixtures/mastodon-post-activity.json")
326 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
327 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
329 "type" => "OrderedCollection"
332 object = Map.put(data["object"], "likes", likes)
333 data = Map.put(data, "object", object)
335 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
337 refute Map.has_key?(object.data, "likes")
340 test "it strips internal reactions" do
342 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
343 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
345 %{object: object} = Activity.get_by_id_with_object(activity.id)
346 assert Map.has_key?(object.data, "reactions")
347 assert Map.has_key?(object.data, "reaction_count")
349 object_data = Transmogrifier.strip_internal_fields(object.data)
350 refute Map.has_key?(object_data, "reactions")
351 refute Map.has_key?(object_data, "reaction_count")
354 test "it works for incomming unfollows with an existing follow" do
358 File.read!("test/fixtures/mastodon-follow-activity.json")
360 |> Map.put("object", user.ap_id)
362 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
365 File.read!("test/fixtures/mastodon-unfollow-activity.json")
367 |> Map.put("object", follow_data)
369 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
371 assert data["type"] == "Undo"
372 assert data["object"]["type"] == "Follow"
373 assert data["object"]["object"] == user.ap_id
374 assert data["actor"] == "http://mastodon.example.org/users/admin"
376 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
379 test "it works for incoming follows to locked account" do
380 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
381 user = insert(:user, locked: true)
384 File.read!("test/fixtures/mastodon-follow-activity.json")
386 |> Map.put("object", user.ap_id)
388 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
390 assert data["type"] == "Follow"
391 assert data["object"] == user.ap_id
392 assert data["state"] == "pending"
393 assert data["actor"] == "http://mastodon.example.org/users/admin"
395 assert [^pending_follower] = User.get_follow_requests(user)
398 test "it works for incoming accepts which were pre-accepted" do
399 follower = insert(:user)
400 followed = insert(:user)
402 {:ok, follower} = User.follow(follower, followed)
403 assert User.following?(follower, followed) == true
405 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
408 File.read!("test/fixtures/mastodon-accept-activity.json")
410 |> Map.put("actor", followed.ap_id)
413 accept_data["object"]
414 |> Map.put("actor", follower.ap_id)
415 |> Map.put("id", follow_activity.data["id"])
417 accept_data = Map.put(accept_data, "object", object)
419 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
420 refute activity.local
422 assert activity.data["object"] == follow_activity.data["id"]
424 assert activity.data["id"] == accept_data["id"]
426 follower = User.get_cached_by_id(follower.id)
428 assert User.following?(follower, followed) == true
431 test "it works for incoming accepts which were orphaned" do
432 follower = insert(:user)
433 followed = insert(:user, locked: true)
435 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
438 File.read!("test/fixtures/mastodon-accept-activity.json")
440 |> Map.put("actor", followed.ap_id)
443 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
445 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
446 assert activity.data["object"] == follow_activity.data["id"]
448 follower = User.get_cached_by_id(follower.id)
450 assert User.following?(follower, followed) == true
453 test "it works for incoming accepts which are referenced by IRI only" do
454 follower = insert(:user)
455 followed = insert(:user, locked: true)
457 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
460 File.read!("test/fixtures/mastodon-accept-activity.json")
462 |> Map.put("actor", followed.ap_id)
463 |> Map.put("object", follow_activity.data["id"])
465 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
466 assert activity.data["object"] == follow_activity.data["id"]
468 follower = User.get_cached_by_id(follower.id)
470 assert User.following?(follower, followed) == true
472 follower = User.get_by_id(follower.id)
473 assert follower.following_count == 1
475 followed = User.get_by_id(followed.id)
476 assert followed.follower_count == 1
479 test "it fails for incoming accepts which cannot be correlated" do
480 follower = insert(:user)
481 followed = insert(:user, locked: true)
484 File.read!("test/fixtures/mastodon-accept-activity.json")
486 |> Map.put("actor", followed.ap_id)
489 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
491 :error = Transmogrifier.handle_incoming(accept_data)
493 follower = User.get_cached_by_id(follower.id)
495 refute User.following?(follower, followed) == true
498 test "it fails for incoming rejects which cannot be correlated" do
499 follower = insert(:user)
500 followed = insert(:user, locked: true)
503 File.read!("test/fixtures/mastodon-reject-activity.json")
505 |> Map.put("actor", followed.ap_id)
508 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
510 :error = Transmogrifier.handle_incoming(accept_data)
512 follower = User.get_cached_by_id(follower.id)
514 refute User.following?(follower, followed) == true
517 test "it works for incoming rejects which are orphaned" do
518 follower = insert(:user)
519 followed = insert(:user, locked: true)
521 {:ok, follower} = User.follow(follower, followed)
522 {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, followed)
524 assert User.following?(follower, followed) == true
527 File.read!("test/fixtures/mastodon-reject-activity.json")
529 |> Map.put("actor", followed.ap_id)
532 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
534 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
535 refute activity.local
536 assert activity.data["id"] == reject_data["id"]
538 follower = User.get_cached_by_id(follower.id)
540 assert User.following?(follower, followed) == false
543 test "it works for incoming rejects which are referenced by IRI only" do
544 follower = insert(:user)
545 followed = insert(:user, locked: true)
547 {:ok, follower} = User.follow(follower, followed)
548 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
550 assert User.following?(follower, followed) == true
553 File.read!("test/fixtures/mastodon-reject-activity.json")
555 |> Map.put("actor", followed.ap_id)
556 |> Map.put("object", follow_activity.data["id"])
558 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
560 follower = User.get_cached_by_id(follower.id)
562 assert User.following?(follower, followed) == false
565 test "it rejects activities without a valid ID" do
569 File.read!("test/fixtures/mastodon-follow-activity.json")
571 |> Map.put("object", user.ap_id)
574 :error = Transmogrifier.handle_incoming(data)
577 test "skip converting the content when it is nil" do
578 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
580 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
583 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
585 assert result["content"] == nil
588 test "it converts content of object to html" do
589 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
591 {:ok, %{"content" => content_markdown}} =
592 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
594 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
595 Fetcher.fetch_object_from_id(object_id)
597 assert content_markdown ==
598 "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..."
601 "<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>"
603 assert object.data["mediaType"] == "text/html"
606 test "it remaps video URLs as attachments if necessary" do
608 Fetcher.fetch_object_from_id(
609 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
612 assert object.data["url"] ==
613 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
615 assert object.data["attachment"] == [
618 "mediaType" => "video/mp4",
622 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
623 "mediaType" => "video/mp4"
630 Fetcher.fetch_object_from_id(
631 "https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206"
634 assert object.data["attachment"] == [
637 "mediaType" => "video/mp4",
641 "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4",
642 "mediaType" => "video/mp4"
648 assert object.data["url"] ==
649 "https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206"
652 test "it accepts Flag activities" do
654 other_user = insert(:user)
656 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
657 object = Object.normalize(activity)
661 "id" => activity.data["id"],
662 "content" => "test post",
663 "published" => object.data["published"],
664 "actor" => AccountView.render("show.json", %{user: user})
668 "@context" => "https://www.w3.org/ns/activitystreams",
669 "cc" => [user.ap_id],
670 "object" => [user.ap_id, activity.data["id"]],
672 "content" => "blocked AND reported!!!",
673 "actor" => other_user.ap_id
676 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
678 assert activity.data["object"] == [user.ap_id, note_obj]
679 assert activity.data["content"] == "blocked AND reported!!!"
680 assert activity.data["actor"] == other_user.ap_id
681 assert activity.data["cc"] == [user.ap_id]
684 test "it correctly processes messages with non-array to field" do
688 "@context" => "https://www.w3.org/ns/activitystreams",
689 "to" => "https://www.w3.org/ns/activitystreams#Public",
692 "content" => "blah blah blah",
694 "attributedTo" => user.ap_id,
697 "actor" => user.ap_id
700 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
702 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
705 test "it correctly processes messages with non-array cc field" do
709 "@context" => "https://www.w3.org/ns/activitystreams",
710 "to" => user.follower_address,
711 "cc" => "https://www.w3.org/ns/activitystreams#Public",
714 "content" => "blah blah blah",
716 "attributedTo" => user.ap_id,
719 "actor" => user.ap_id
722 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
724 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
725 assert [user.follower_address] == activity.data["to"]
728 test "it correctly processes messages with weirdness in address fields" do
732 "@context" => "https://www.w3.org/ns/activitystreams",
733 "to" => [nil, user.follower_address],
734 "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]],
739 "attributedTo" => user.ap_id,
742 "actor" => user.ap_id
745 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
747 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
748 assert [user.follower_address] == activity.data["to"]
751 test "it accepts Move activities" do
752 old_user = insert(:user)
753 new_user = insert(:user)
756 "@context" => "https://www.w3.org/ns/activitystreams",
758 "actor" => old_user.ap_id,
759 "object" => old_user.ap_id,
760 "target" => new_user.ap_id
763 assert :error = Transmogrifier.handle_incoming(message)
765 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
767 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
768 assert activity.actor == old_user.ap_id
769 assert activity.data["actor"] == old_user.ap_id
770 assert activity.data["object"] == old_user.ap_id
771 assert activity.data["target"] == new_user.ap_id
772 assert activity.data["type"] == "Move"
776 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
777 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
778 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
782 "test/fixtures/mastodon-post-activity.json"
786 items = get_in(data, ["object", "replies", "first", "items"])
787 assert length(items) > 0
789 %{data: data, items: items}
792 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
796 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
798 {:ok, _activity} = Transmogrifier.handle_incoming(data)
801 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
802 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
806 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
808 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
810 {:ok, _activity} = Transmogrifier.handle_incoming(data)
812 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
816 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
817 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
818 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
823 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
826 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
829 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
831 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
833 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
835 Repo.delete(activity.object)
836 Repo.delete(activity)
838 %{federation_output: federation_output, replies_uris: replies_uris}
841 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
842 federation_output: federation_output,
843 replies_uris: replies_uris
845 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
847 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
849 for id <- replies_uris do
850 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
851 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
855 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
856 %{federation_output: federation_output} do
857 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
859 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
861 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
865 describe "prepare outgoing" do
866 test "it inlines private announced objects" do
869 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
871 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
873 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
875 assert modified["object"]["content"] == "hey"
876 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
879 test "it turns mentions into tags" do
881 other_user = insert(:user)
884 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
886 with_mock Pleroma.Notification,
887 get_notified_from_activity: fn _, _ -> [] end do
888 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
890 object = modified["object"]
892 expected_mention = %{
893 "href" => other_user.ap_id,
894 "name" => "@#{other_user.nickname}",
899 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
904 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
905 assert Enum.member?(object["tag"], expected_tag)
906 assert Enum.member?(object["tag"], expected_mention)
910 test "it adds the sensitive property" do
913 {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
914 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
916 assert modified["object"]["sensitive"]
919 test "it adds the json-ld context and the conversation property" do
922 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
923 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
925 assert modified["@context"] ==
926 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
928 assert modified["object"]["conversation"] == modified["context"]
931 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
934 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
935 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
937 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
940 test "it strips internal hashtag data" do
943 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
946 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
951 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
953 assert modified["object"]["tag"] == [expected_tag]
956 test "it strips internal fields" do
959 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu :firefox:"})
961 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
963 assert length(modified["object"]["tag"]) == 2
965 assert is_nil(modified["object"]["emoji"])
966 assert is_nil(modified["object"]["like_count"])
967 assert is_nil(modified["object"]["announcements"])
968 assert is_nil(modified["object"]["announcement_count"])
969 assert is_nil(modified["object"]["context_id"])
972 test "it strips internal fields of article" do
973 activity = insert(:article_activity)
975 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
977 assert length(modified["object"]["tag"]) == 2
979 assert is_nil(modified["object"]["emoji"])
980 assert is_nil(modified["object"]["like_count"])
981 assert is_nil(modified["object"]["announcements"])
982 assert is_nil(modified["object"]["announcement_count"])
983 assert is_nil(modified["object"]["context_id"])
984 assert is_nil(modified["object"]["likes"])
987 test "the directMessage flag is present" do
989 other_user = insert(:user)
991 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
993 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
995 assert modified["directMessage"] == false
997 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
999 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1001 assert modified["directMessage"] == false
1004 CommonAPI.post(user, %{
1005 status: "@#{other_user.nickname} :moominmamma:",
1006 visibility: "direct"
1009 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1011 assert modified["directMessage"] == true
1014 test "it strips BCC field" do
1015 user = insert(:user)
1016 {:ok, list} = Pleroma.List.create("foo", user)
1018 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1020 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1022 assert is_nil(modified["bcc"])
1025 test "it can handle Listen activities" do
1026 listen_activity = insert(:listen)
1028 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1030 assert modified["type"] == "Listen"
1032 user = insert(:user)
1034 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1036 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1040 describe "user upgrade" do
1041 test "it upgrades a user to activitypub" do
1044 nickname: "rye@niu.moe",
1046 ap_id: "https://niu.moe/users/rye",
1047 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1050 user_two = insert(:user)
1051 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1053 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1054 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
1055 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1057 user = User.get_cached_by_id(user.id)
1058 assert user.note_count == 1
1060 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1061 ObanHelpers.perform_all()
1063 assert user.ap_enabled
1064 assert user.note_count == 1
1065 assert user.follower_address == "https://niu.moe/users/rye/followers"
1066 assert user.following_address == "https://niu.moe/users/rye/following"
1068 user = User.get_cached_by_id(user.id)
1069 assert user.note_count == 1
1071 activity = Activity.get_by_id(activity.id)
1072 assert user.follower_address in activity.recipients
1078 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1087 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1092 refute "..." in activity.recipients
1094 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1095 refute user.follower_address in unrelated_activity.recipients
1097 user_two = User.get_cached_by_id(user_two.id)
1098 assert User.following?(user_two, user)
1099 refute "..." in User.following(user_two)
1103 describe "actor rewriting" do
1104 test "it fixes the actor URL property to be a proper URI" do
1106 "url" => %{"href" => "http://example.com"}
1109 rewritten = Transmogrifier.maybe_fix_user_object(data)
1110 assert rewritten["url"] == "http://example.com"
1114 describe "actor origin containment" do
1115 test "it rejects activities which reference objects with bogus origins" do
1117 "@context" => "https://www.w3.org/ns/activitystreams",
1118 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1119 "actor" => "http://mastodon.example.org/users/admin",
1120 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1121 "object" => "https://info.pleroma.site/activity.json",
1122 "type" => "Announce"
1125 assert capture_log(fn ->
1126 {:error, _} = Transmogrifier.handle_incoming(data)
1127 end) =~ "Object containment failed"
1130 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1132 "@context" => "https://www.w3.org/ns/activitystreams",
1133 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1134 "actor" => "http://mastodon.example.org/users/admin",
1135 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1136 "object" => "https://info.pleroma.site/activity2.json",
1137 "type" => "Announce"
1140 assert capture_log(fn ->
1141 {:error, _} = Transmogrifier.handle_incoming(data)
1142 end) =~ "Object containment failed"
1145 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1147 "@context" => "https://www.w3.org/ns/activitystreams",
1148 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1149 "actor" => "http://mastodon.example.org/users/admin",
1150 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1151 "object" => "https://info.pleroma.site/activity3.json",
1152 "type" => "Announce"
1155 assert capture_log(fn ->
1156 {:error, _} = Transmogrifier.handle_incoming(data)
1157 end) =~ "Object containment failed"
1161 describe "reserialization" do
1162 test "successfully reserializes a message with inReplyTo == nil" do
1163 user = insert(:user)
1166 "@context" => "https://www.w3.org/ns/activitystreams",
1167 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1171 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1176 "attributedTo" => user.ap_id
1178 "actor" => user.ap_id
1181 {:ok, activity} = Transmogrifier.handle_incoming(message)
1183 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1186 test "successfully reserializes a message with AS2 objects in IR" do
1187 user = insert(:user)
1190 "@context" => "https://www.w3.org/ns/activitystreams",
1191 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1195 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1200 "attributedTo" => user.ap_id,
1202 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1203 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1206 "actor" => user.ap_id
1209 {:ok, activity} = Transmogrifier.handle_incoming(message)
1211 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1215 describe "fix_explicit_addressing" do
1217 user = insert(:user)
1221 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1222 explicitly_mentioned_actors = [
1223 "https://pleroma.gold/users/user1",
1224 "https://pleroma.gold/user2"
1228 "actor" => user.ap_id,
1229 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1232 Enum.map(explicitly_mentioned_actors, fn href ->
1233 %{"type" => "Mention", "href" => href}
1237 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1238 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1239 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1240 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1243 test "does not move actor's follower collection to cc", %{user: user} do
1245 "actor" => user.ap_id,
1246 "to" => [user.follower_address],
1250 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1251 assert user.follower_address in fixed_object["to"]
1252 refute user.follower_address in fixed_object["cc"]
1255 test "removes recipient's follower collection from cc", %{user: user} do
1256 recipient = insert(:user)
1259 "actor" => user.ap_id,
1260 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1261 "cc" => [user.follower_address, recipient.follower_address]
1264 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1266 assert user.follower_address in fixed_object["cc"]
1267 refute recipient.follower_address in fixed_object["cc"]
1268 refute recipient.follower_address in fixed_object["to"]
1272 describe "fix_summary/1" do
1273 test "returns fixed object" do
1274 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1275 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1276 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1280 describe "fix_in_reply_to/2" do
1281 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1284 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1288 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1289 assert Transmogrifier.fix_in_reply_to(data) == data
1292 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1293 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1296 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1298 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1299 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1300 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1303 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1305 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1306 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1307 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1310 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1312 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1313 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1314 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1316 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1317 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1318 assert modified_object["inReplyTo"] == []
1319 assert modified_object["inReplyToAtomUri"] == ""
1322 @tag capture_log: true
1323 test "returns modified object when allowed incoming reply", %{data: data} do
1328 "https://shitposter.club/notice/2827873"
1331 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1332 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1334 assert modified_object["inReplyTo"] ==
1335 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1337 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1339 assert modified_object["context"] ==
1340 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1344 describe "fix_url/1" do
1345 test "fixes data for object when url is map" do
1349 "mimeType" => "video/mp4",
1350 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1354 assert Transmogrifier.fix_url(object) == %{
1355 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1359 test "fixes data for video object" do
1365 "mimeType" => "video/mp4",
1366 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1370 "mimeType" => "video/mp4",
1371 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1375 "mimeType" => "text/html",
1376 "href" => "https://peertube.-2d4c2d1630e3"
1380 "mimeType" => "text/html",
1381 "href" => "https://peertube.-2d4c2d16377-42"
1386 assert Transmogrifier.fix_url(object) == %{
1389 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1390 "mimeType" => "video/mp4",
1395 "url" => "https://peertube.-2d4c2d1630e3"
1399 test "fixes url for not Video object" do
1405 "mimeType" => "text/html",
1406 "href" => "https://peertube.-2d4c2d1630e3"
1410 "mimeType" => "text/html",
1411 "href" => "https://peertube.-2d4c2d16377-42"
1416 assert Transmogrifier.fix_url(object) == %{
1418 "url" => "https://peertube.-2d4c2d1630e3"
1421 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1427 test "retunrs not modified object" do
1428 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1432 describe "get_obj_helper/2" do
1433 test "returns nil when cannot normalize object" do
1434 assert capture_log(fn ->
1435 refute Transmogrifier.get_obj_helper("test-obj-id")
1436 end) =~ "Unsupported URI scheme"
1439 @tag capture_log: true
1440 test "returns {:ok, %Object{}} for success case" do
1441 assert {:ok, %Object{}} =
1442 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1446 describe "fix_attachments/1" do
1447 test "returns not modified object" do
1448 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1449 assert Transmogrifier.fix_attachments(data) == data
1452 test "returns modified object when attachment is map" do
1453 assert Transmogrifier.fix_attachments(%{
1455 "mediaType" => "video/mp4",
1456 "url" => "https://peertube.moe/stat-480.mp4"
1461 "mediaType" => "video/mp4",
1463 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1470 test "returns modified object when attachment is list" do
1471 assert Transmogrifier.fix_attachments(%{
1473 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1474 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1479 "mediaType" => "video/mp4",
1481 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1485 "mediaType" => "video/mp4",
1487 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1495 describe "fix_emoji/1" do
1496 test "returns not modified object when object not contains tags" do
1497 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1498 assert Transmogrifier.fix_emoji(data) == data
1501 test "returns object with emoji when object contains list tags" do
1502 assert Transmogrifier.fix_emoji(%{
1504 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1505 %{"type" => "Hashtag"}
1508 "emoji" => %{"bib" => "/test"},
1510 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1511 %{"type" => "Hashtag"}
1516 test "returns object with emoji when object contains map tag" do
1517 assert Transmogrifier.fix_emoji(%{
1518 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1520 "emoji" => %{"bib" => "/test"},
1521 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1526 describe "set_replies/1" do
1527 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1529 test "returns unmodified object if activity doesn't have self-replies" do
1530 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1531 assert Transmogrifier.set_replies(data) == data
1534 test "sets `replies` collection with a limited number of self-replies" do
1535 [user, another_user] = insert_list(2, :user)
1537 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
1539 {:ok, %{id: id2} = self_reply1} =
1540 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
1542 {:ok, self_reply2} =
1543 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
1545 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1546 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
1549 CommonAPI.post(user, %{
1550 status: "self-reply to self-reply",
1551 in_reply_to_status_id: id2
1555 CommonAPI.post(another_user, %{
1556 status: "another user's reply",
1557 in_reply_to_status_id: id1
1560 object = Object.normalize(activity)
1561 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1563 assert %{"type" => "Collection", "items" => ^replies_uris} =
1564 Transmogrifier.set_replies(object.data)["replies"]
1568 test "take_emoji_tags/1" do
1569 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1571 assert Transmogrifier.take_emoji_tags(user) == [
1573 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1574 "id" => "https://example.org/firefox.png",
1575 "name" => ":firefox:",
1577 "updated" => "1970-01-01T00:00:00Z"