1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
9 alias Pleroma.Object.Fetcher
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 describe "handle_incoming" do
28 test "it ignores an incoming notice if we already have it" do
29 activity = insert(:note_activity)
32 File.read!("test/fixtures/mastodon-post-activity.json")
34 |> Map.put("object", Object.normalize(activity).data)
36 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
38 assert activity == returned_activity
41 test "it fetches replied-to activities if we don't have them" do
43 File.read!("test/fixtures/mastodon-post-activity.json")
48 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
50 data = Map.put(data, "object", object)
51 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
52 returned_object = Object.normalize(returned_activity, false)
55 Activity.get_create_by_object_ap_id(
56 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
59 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
62 test "it does not fetch replied-to activities beyond max_replies_depth" do
64 File.read!("test/fixtures/mastodon-post-activity.json")
69 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
71 data = Map.put(data, "object", object)
73 with_mock Pleroma.Web.Federator,
74 allowed_incoming_reply_depth?: fn _ -> false end do
75 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
77 returned_object = Object.normalize(returned_activity, false)
79 refute Activity.get_create_by_object_ap_id(
80 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
83 assert returned_object.data["inReplyToAtomUri"] ==
84 "https://shitposter.club/notice/2827873"
88 test "it does not crash if the object in inReplyTo can't be fetched" do
90 File.read!("test/fixtures/mastodon-post-activity.json")
95 |> Map.put("inReplyTo", "https://404.site/whatever")
99 |> Map.put("object", object)
101 assert capture_log(fn ->
102 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
103 end) =~ "[error] Couldn't fetch \"\"https://404.site/whatever\"\", error: nil"
106 test "it works for incoming notices" do
107 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
109 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
112 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
114 assert data["context"] ==
115 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
117 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
119 assert data["cc"] == [
120 "http://mastodon.example.org/users/admin/followers",
121 "http://localtesting.pleroma.lol/users/lain"
124 assert data["actor"] == "http://mastodon.example.org/users/admin"
126 object_data = Object.normalize(data["object"]).data
128 assert object_data["id"] ==
129 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
131 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
133 assert object_data["cc"] == [
134 "http://mastodon.example.org/users/admin/followers",
135 "http://localtesting.pleroma.lol/users/lain"
138 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
139 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
141 assert object_data["context"] ==
142 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
144 assert object_data["sensitive"] == true
146 user = User.get_cached_by_ap_id(object_data["actor"])
148 assert user.info.note_count == 1
151 test "it works for incoming notices with hashtags" do
152 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
154 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
155 object = Object.normalize(data["object"])
157 assert Enum.at(object.data["tag"], 2) == "moo"
160 test "it works for incoming questions" do
161 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
163 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
165 object = Object.normalize(activity)
167 assert Enum.all?(object.data["oneOf"], fn choice ->
170 "Everyone knows that!",
171 "25 char limit is dumb",
172 "I can't even fit a funny"
177 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
181 CommonAPI.post(user, %{
182 "status" => "suya...",
183 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
186 object = Object.normalize(activity)
189 File.read!("test/fixtures/mastodon-vote.json")
191 |> Kernel.put_in(["to"], user.ap_id)
192 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
193 |> Kernel.put_in(["object", "to"], user.ap_id)
195 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
196 answer_object = Object.normalize(activity)
197 assert answer_object.data["type"] == "Answer"
198 object = Object.get_by_ap_id(object.data["id"])
201 object.data["oneOf"],
203 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
209 test "it works for incoming notices with contentMap" do
211 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
213 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
214 object = Object.normalize(data["object"])
216 assert object.data["content"] ==
217 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
220 test "it works for incoming notices with to/cc not being an array (kroeg)" do
221 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
223 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
224 object = Object.normalize(data["object"])
226 assert object.data["content"] ==
227 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
230 test "it works for incoming announces with actor being inlined (kroeg)" do
231 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
233 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
235 assert data["actor"] == "https://puckipedia.com/"
238 test "it works for incoming notices with tag not being an array (kroeg)" do
239 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
241 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
242 object = Object.normalize(data["object"])
244 assert object.data["emoji"] == %{
245 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
248 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
250 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
251 object = Object.normalize(data["object"])
253 assert "test" in object.data["tag"]
256 test "it works for incoming notices with url not being a string (prismo)" do
257 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
259 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
260 object = Object.normalize(data["object"])
262 assert object.data["url"] == "https://prismo.news/posts/83"
265 test "it cleans up incoming notices which are not really DMs" do
267 other_user = insert(:user)
269 to = [user.ap_id, other_user.ap_id]
272 File.read!("test/fixtures/mastodon-post-activity.json")
282 data = Map.put(data, "object", object)
284 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
286 assert data["to"] == []
287 assert data["cc"] == to
289 object_data = Object.normalize(activity).data
291 assert object_data["to"] == []
292 assert object_data["cc"] == to
295 test "it works for incoming likes" do
297 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
300 File.read!("test/fixtures/mastodon-like.json")
302 |> Map.put("object", activity.data["object"])
304 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
306 assert data["actor"] == "http://mastodon.example.org/users/admin"
307 assert data["type"] == "Like"
308 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
309 assert data["object"] == activity.data["object"]
312 test "it returns an error for incoming unlikes wihout a like activity" do
314 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
317 File.read!("test/fixtures/mastodon-undo-like.json")
319 |> Map.put("object", activity.data["object"])
321 assert Transmogrifier.handle_incoming(data) == :error
324 test "it works for incoming unlikes with an existing like activity" do
326 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
329 File.read!("test/fixtures/mastodon-like.json")
331 |> Map.put("object", activity.data["object"])
333 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
336 File.read!("test/fixtures/mastodon-undo-like.json")
338 |> Map.put("object", like_data)
339 |> Map.put("actor", like_data["actor"])
341 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
343 assert data["actor"] == "http://mastodon.example.org/users/admin"
344 assert data["type"] == "Undo"
345 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
346 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
349 test "it works for incoming announces" do
350 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
352 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
354 assert data["actor"] == "http://mastodon.example.org/users/admin"
355 assert data["type"] == "Announce"
358 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
360 assert data["object"] ==
361 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
363 assert Activity.get_create_by_object_ap_id(data["object"])
366 test "it works for incoming announces with an existing activity" do
368 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
371 File.read!("test/fixtures/mastodon-announce.json")
373 |> Map.put("object", activity.data["object"])
375 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
377 assert data["actor"] == "http://mastodon.example.org/users/admin"
378 assert data["type"] == "Announce"
381 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
383 assert data["object"] == activity.data["object"]
385 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
388 test "it does not clobber the addressing on announce activities" do
390 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
393 File.read!("test/fixtures/mastodon-announce.json")
395 |> Map.put("object", Object.normalize(activity).data["id"])
396 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
399 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
401 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
404 test "it ensures that as:Public activities make it to their followers collection" do
408 File.read!("test/fixtures/mastodon-post-activity.json")
410 |> Map.put("actor", user.ap_id)
411 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
416 |> Map.put("attributedTo", user.ap_id)
417 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
419 |> Map.put("id", user.ap_id <> "/activities/12345678")
421 data = Map.put(data, "object", object)
423 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
425 assert data["cc"] == [User.ap_followers(user)]
428 test "it ensures that address fields become lists" do
432 File.read!("test/fixtures/mastodon-post-activity.json")
434 |> Map.put("actor", user.ap_id)
435 |> Map.put("to", nil)
436 |> Map.put("cc", nil)
440 |> Map.put("attributedTo", user.ap_id)
441 |> Map.put("to", nil)
442 |> Map.put("cc", nil)
443 |> Map.put("id", user.ap_id <> "/activities/12345678")
445 data = Map.put(data, "object", object)
447 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
449 assert !is_nil(data["to"])
450 assert !is_nil(data["cc"])
453 test "it works for incoming update activities" do
454 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
456 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
457 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
460 update_data["object"]
461 |> Map.put("actor", data["actor"])
462 |> Map.put("id", data["actor"])
466 |> Map.put("actor", data["actor"])
467 |> Map.put("object", object)
469 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
471 user = User.get_cached_by_ap_id(data["actor"])
472 assert user.name == "gargle"
474 assert user.avatar["url"] == [
477 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
481 assert user.info.banner["url"] == [
484 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
488 assert user.bio == "<p>Some bio</p>"
491 test "it works for incoming update activities which lock the account" do
492 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
494 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
495 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
498 update_data["object"]
499 |> Map.put("actor", data["actor"])
500 |> Map.put("id", data["actor"])
501 |> Map.put("manuallyApprovesFollowers", true)
505 |> Map.put("actor", data["actor"])
506 |> Map.put("object", object)
508 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
510 user = User.get_cached_by_ap_id(data["actor"])
511 assert user.info.locked == true
514 test "it works for incoming deletes" do
515 activity = insert(:note_activity)
518 File.read!("test/fixtures/mastodon-delete.json")
523 |> Map.put("id", activity.data["object"])
527 |> Map.put("object", object)
528 |> Map.put("actor", activity.data["actor"])
530 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
532 refute Activity.get_by_id(activity.id)
535 test "it fails for incoming deletes with spoofed origin" do
536 activity = insert(:note_activity)
539 File.read!("test/fixtures/mastodon-delete.json")
544 |> Map.put("id", activity.data["object"])
548 |> Map.put("object", object)
550 assert capture_log(fn ->
551 :error = Transmogrifier.handle_incoming(data)
553 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
555 assert Activity.get_by_id(activity.id)
558 test "it works for incoming user deletes" do
559 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
562 File.read!("test/fixtures/mastodon-delete-user.json")
565 {:ok, _} = Transmogrifier.handle_incoming(data)
567 refute User.get_cached_by_ap_id(ap_id)
570 test "it fails for incoming user deletes with spoofed origin" do
571 %{ap_id: ap_id} = insert(:user)
574 File.read!("test/fixtures/mastodon-delete-user.json")
576 |> Map.put("actor", ap_id)
578 assert :error == Transmogrifier.handle_incoming(data)
579 assert User.get_cached_by_ap_id(ap_id)
582 test "it works for incoming unannounces with an existing notice" do
584 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
587 File.read!("test/fixtures/mastodon-announce.json")
589 |> Map.put("object", activity.data["object"])
591 {:ok, %Activity{data: announce_data, local: false}} =
592 Transmogrifier.handle_incoming(announce_data)
595 File.read!("test/fixtures/mastodon-undo-announce.json")
597 |> Map.put("object", announce_data)
598 |> Map.put("actor", announce_data["actor"])
600 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
602 assert data["type"] == "Undo"
603 assert object_data = data["object"]
604 assert object_data["type"] == "Announce"
605 assert object_data["object"] == activity.data["object"]
607 assert object_data["id"] ==
608 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
611 test "it works for incomming unfollows with an existing follow" do
615 File.read!("test/fixtures/mastodon-follow-activity.json")
617 |> Map.put("object", user.ap_id)
619 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
622 File.read!("test/fixtures/mastodon-unfollow-activity.json")
624 |> Map.put("object", follow_data)
626 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
628 assert data["type"] == "Undo"
629 assert data["object"]["type"] == "Follow"
630 assert data["object"]["object"] == user.ap_id
631 assert data["actor"] == "http://mastodon.example.org/users/admin"
633 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
636 test "it works for incoming blocks" do
640 File.read!("test/fixtures/mastodon-block-activity.json")
642 |> Map.put("object", user.ap_id)
644 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
646 assert data["type"] == "Block"
647 assert data["object"] == user.ap_id
648 assert data["actor"] == "http://mastodon.example.org/users/admin"
650 blocker = User.get_cached_by_ap_id(data["actor"])
652 assert User.blocks?(blocker, user)
655 test "incoming blocks successfully tear down any follow relationship" do
656 blocker = insert(:user)
657 blocked = insert(:user)
660 File.read!("test/fixtures/mastodon-block-activity.json")
662 |> Map.put("object", blocked.ap_id)
663 |> Map.put("actor", blocker.ap_id)
665 {:ok, blocker} = User.follow(blocker, blocked)
666 {:ok, blocked} = User.follow(blocked, blocker)
668 assert User.following?(blocker, blocked)
669 assert User.following?(blocked, blocker)
671 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
673 assert data["type"] == "Block"
674 assert data["object"] == blocked.ap_id
675 assert data["actor"] == blocker.ap_id
677 blocker = User.get_cached_by_ap_id(data["actor"])
678 blocked = User.get_cached_by_ap_id(data["object"])
680 assert User.blocks?(blocker, blocked)
682 refute User.following?(blocker, blocked)
683 refute User.following?(blocked, blocker)
686 test "it works for incoming unblocks with an existing block" do
690 File.read!("test/fixtures/mastodon-block-activity.json")
692 |> Map.put("object", user.ap_id)
694 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
697 File.read!("test/fixtures/mastodon-unblock-activity.json")
699 |> Map.put("object", block_data)
701 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
702 assert data["type"] == "Undo"
703 assert data["object"]["type"] == "Block"
704 assert data["object"]["object"] == user.ap_id
705 assert data["actor"] == "http://mastodon.example.org/users/admin"
707 blocker = User.get_cached_by_ap_id(data["actor"])
709 refute User.blocks?(blocker, user)
712 test "it works for incoming accepts which were pre-accepted" do
713 follower = insert(:user)
714 followed = insert(:user)
716 {:ok, follower} = User.follow(follower, followed)
717 assert User.following?(follower, followed) == true
719 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
722 File.read!("test/fixtures/mastodon-accept-activity.json")
724 |> Map.put("actor", followed.ap_id)
727 accept_data["object"]
728 |> Map.put("actor", follower.ap_id)
729 |> Map.put("id", follow_activity.data["id"])
731 accept_data = Map.put(accept_data, "object", object)
733 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
734 refute activity.local
736 assert activity.data["object"] == follow_activity.data["id"]
738 follower = User.get_cached_by_id(follower.id)
740 assert User.following?(follower, followed) == true
743 test "it works for incoming accepts which were orphaned" do
744 follower = insert(:user)
745 followed = insert(:user, %{info: %User.Info{locked: true}})
747 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
750 File.read!("test/fixtures/mastodon-accept-activity.json")
752 |> Map.put("actor", followed.ap_id)
755 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
757 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
758 assert activity.data["object"] == follow_activity.data["id"]
760 follower = User.get_cached_by_id(follower.id)
762 assert User.following?(follower, followed) == true
765 test "it works for incoming accepts which are referenced by IRI only" do
766 follower = insert(:user)
767 followed = insert(:user, %{info: %User.Info{locked: true}})
769 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
772 File.read!("test/fixtures/mastodon-accept-activity.json")
774 |> Map.put("actor", followed.ap_id)
775 |> Map.put("object", follow_activity.data["id"])
777 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
778 assert activity.data["object"] == follow_activity.data["id"]
780 follower = User.get_cached_by_id(follower.id)
782 assert User.following?(follower, followed) == true
785 test "it fails for incoming accepts which cannot be correlated" do
786 follower = insert(:user)
787 followed = insert(:user, %{info: %User.Info{locked: true}})
790 File.read!("test/fixtures/mastodon-accept-activity.json")
792 |> Map.put("actor", followed.ap_id)
795 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
797 :error = Transmogrifier.handle_incoming(accept_data)
799 follower = User.get_cached_by_id(follower.id)
801 refute User.following?(follower, followed) == true
804 test "it fails for incoming rejects which cannot be correlated" do
805 follower = insert(:user)
806 followed = insert(:user, %{info: %User.Info{locked: true}})
809 File.read!("test/fixtures/mastodon-reject-activity.json")
811 |> Map.put("actor", followed.ap_id)
814 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
816 :error = Transmogrifier.handle_incoming(accept_data)
818 follower = User.get_cached_by_id(follower.id)
820 refute User.following?(follower, followed) == true
823 test "it works for incoming rejects which are orphaned" do
824 follower = insert(:user)
825 followed = insert(:user, %{info: %User.Info{locked: true}})
827 {:ok, follower} = User.follow(follower, followed)
828 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
830 assert User.following?(follower, followed) == true
833 File.read!("test/fixtures/mastodon-reject-activity.json")
835 |> Map.put("actor", followed.ap_id)
838 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
840 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
841 refute activity.local
843 follower = User.get_cached_by_id(follower.id)
845 assert User.following?(follower, followed) == false
848 test "it works for incoming rejects which are referenced by IRI only" do
849 follower = insert(:user)
850 followed = insert(:user, %{info: %User.Info{locked: true}})
852 {:ok, follower} = User.follow(follower, followed)
853 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
855 assert User.following?(follower, followed) == true
858 File.read!("test/fixtures/mastodon-reject-activity.json")
860 |> Map.put("actor", followed.ap_id)
861 |> Map.put("object", follow_activity.data["id"])
863 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
865 follower = User.get_cached_by_id(follower.id)
867 assert User.following?(follower, followed) == false
870 test "it rejects activities without a valid ID" do
874 File.read!("test/fixtures/mastodon-follow-activity.json")
876 |> Map.put("object", user.ap_id)
879 :error = Transmogrifier.handle_incoming(data)
882 test "it remaps video URLs as attachments if necessary" do
884 Fetcher.fetch_object_from_id(
885 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
890 "mediaType" => "video/mp4",
892 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
893 "mimeType" => "video/mp4",
898 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
899 "mediaType" => "video/mp4",
906 assert object.data["url"] ==
907 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
909 assert object.data["attachment"] == [attachment]
912 test "it accepts Flag activities" do
914 other_user = insert(:user)
916 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
917 object = Object.normalize(activity)
920 "@context" => "https://www.w3.org/ns/activitystreams",
921 "cc" => [user.ap_id],
922 "object" => [user.ap_id, object.data["id"]],
924 "content" => "blocked AND reported!!!",
925 "actor" => other_user.ap_id
928 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
930 assert activity.data["object"] == [user.ap_id, object.data["id"]]
931 assert activity.data["content"] == "blocked AND reported!!!"
932 assert activity.data["actor"] == other_user.ap_id
933 assert activity.data["cc"] == [user.ap_id]
937 describe "prepare outgoing" do
938 test "it turns mentions into tags" do
940 other_user = insert(:user)
943 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
945 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
946 object = modified["object"]
948 expected_mention = %{
949 "href" => other_user.ap_id,
950 "name" => "@#{other_user.nickname}",
955 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
960 assert Enum.member?(object["tag"], expected_tag)
961 assert Enum.member?(object["tag"], expected_mention)
964 test "it adds the sensitive property" do
967 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
968 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
970 assert modified["object"]["sensitive"]
973 test "it adds the json-ld context and the conversation property" do
976 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
977 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
979 assert modified["@context"] ==
980 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
982 assert modified["object"]["conversation"] == modified["context"]
985 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
988 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
989 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
991 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
994 test "it translates ostatus IDs to external URLs" do
995 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
996 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1000 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1001 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1003 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1006 test "it translates ostatus reply_to IDs to external URLs" do
1007 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1008 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1010 user = insert(:user)
1013 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1015 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1017 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1020 test "it strips internal hashtag data" do
1021 user = insert(:user)
1023 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1026 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1027 "type" => "Hashtag",
1031 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1033 assert modified["object"]["tag"] == [expected_tag]
1036 test "it strips internal fields" do
1037 user = insert(:user)
1039 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1041 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1043 assert length(modified["object"]["tag"]) == 2
1045 assert is_nil(modified["object"]["emoji"])
1046 assert is_nil(modified["object"]["like_count"])
1047 assert is_nil(modified["object"]["announcements"])
1048 assert is_nil(modified["object"]["announcement_count"])
1049 assert is_nil(modified["object"]["context_id"])
1052 test "it strips internal fields of article" do
1053 activity = insert(:article_activity)
1055 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1057 assert length(modified["object"]["tag"]) == 2
1059 assert is_nil(modified["object"]["emoji"])
1060 assert is_nil(modified["object"]["like_count"])
1061 assert is_nil(modified["object"]["announcements"])
1062 assert is_nil(modified["object"]["announcement_count"])
1063 assert is_nil(modified["object"]["context_id"])
1066 test "it adds like collection to object" do
1067 activity = insert(:note_activity)
1068 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1070 assert modified["object"]["likes"]["type"] == "OrderedCollection"
1071 assert modified["object"]["likes"]["totalItems"] == 0
1074 test "the directMessage flag is present" do
1075 user = insert(:user)
1076 other_user = insert(:user)
1078 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1080 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1082 assert modified["directMessage"] == false
1085 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1087 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1089 assert modified["directMessage"] == false
1092 CommonAPI.post(user, %{
1093 "status" => "@#{other_user.nickname} :moominmamma:",
1094 "visibility" => "direct"
1097 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1099 assert modified["directMessage"] == true
1102 test "it strips BCC field" do
1103 user = insert(:user)
1104 {:ok, list} = Pleroma.List.create("foo", user)
1107 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1109 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1111 assert is_nil(modified["bcc"])
1115 describe "user upgrade" do
1116 test "it upgrades a user to activitypub" do
1119 nickname: "rye@niu.moe",
1121 ap_id: "https://niu.moe/users/rye",
1122 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1125 user_two = insert(:user, %{following: [user.follower_address]})
1127 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1128 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1129 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1131 user = User.get_cached_by_id(user.id)
1132 assert user.info.note_count == 1
1134 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1135 assert user.info.ap_enabled
1136 assert user.info.note_count == 1
1137 assert user.follower_address == "https://niu.moe/users/rye/followers"
1138 assert user.following_address == "https://niu.moe/users/rye/following"
1140 user = User.get_cached_by_id(user.id)
1141 assert user.info.note_count == 1
1143 activity = Activity.get_by_id(activity.id)
1144 assert user.follower_address in activity.recipients
1150 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1159 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1162 } = user.info.banner
1164 refute "..." in activity.recipients
1166 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1167 refute user.follower_address in unrelated_activity.recipients
1169 user_two = User.get_cached_by_id(user_two.id)
1170 assert user.follower_address in user_two.following
1171 refute "..." in user_two.following
1175 describe "maybe_retire_websub" do
1176 test "it deletes all websub client subscripitions with the user as topic" do
1177 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1178 {:ok, ws} = Repo.insert(subscription)
1180 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1181 {:ok, ws2} = Repo.insert(subscription)
1183 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1185 refute Repo.get(WebsubClientSubscription, ws.id)
1186 assert Repo.get(WebsubClientSubscription, ws2.id)
1190 describe "actor rewriting" do
1191 test "it fixes the actor URL property to be a proper URI" do
1193 "url" => %{"href" => "http://example.com"}
1196 rewritten = Transmogrifier.maybe_fix_user_object(data)
1197 assert rewritten["url"] == "http://example.com"
1201 describe "actor origin containment" do
1202 test "it rejects activities which reference objects with bogus origins" do
1204 "@context" => "https://www.w3.org/ns/activitystreams",
1205 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1206 "actor" => "http://mastodon.example.org/users/admin",
1207 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1208 "object" => "https://info.pleroma.site/activity.json",
1209 "type" => "Announce"
1212 :error = Transmogrifier.handle_incoming(data)
1215 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1217 "@context" => "https://www.w3.org/ns/activitystreams",
1218 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1219 "actor" => "http://mastodon.example.org/users/admin",
1220 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1221 "object" => "https://info.pleroma.site/activity2.json",
1222 "type" => "Announce"
1225 :error = Transmogrifier.handle_incoming(data)
1228 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1230 "@context" => "https://www.w3.org/ns/activitystreams",
1231 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1232 "actor" => "http://mastodon.example.org/users/admin",
1233 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1234 "object" => "https://info.pleroma.site/activity3.json",
1235 "type" => "Announce"
1238 :error = Transmogrifier.handle_incoming(data)
1242 describe "reserialization" do
1243 test "successfully reserializes a message with inReplyTo == nil" do
1244 user = insert(:user)
1247 "@context" => "https://www.w3.org/ns/activitystreams",
1248 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1252 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1257 "attributedTo" => user.ap_id
1259 "actor" => user.ap_id
1262 {:ok, activity} = Transmogrifier.handle_incoming(message)
1264 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1267 test "successfully reserializes a message with AS2 objects in IR" do
1268 user = insert(:user)
1271 "@context" => "https://www.w3.org/ns/activitystreams",
1272 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1276 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1281 "attributedTo" => user.ap_id,
1283 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1284 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1287 "actor" => user.ap_id
1290 {:ok, activity} = Transmogrifier.handle_incoming(message)
1292 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1296 test "Rewrites Answers to Notes" do
1297 user = insert(:user)
1299 {:ok, poll_activity} =
1300 CommonAPI.post(user, %{
1301 "status" => "suya...",
1302 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1305 poll_object = Object.normalize(poll_activity)
1306 # TODO: Replace with CommonAPI vote creation when implemented
1308 File.read!("test/fixtures/mastodon-vote.json")
1310 |> Kernel.put_in(["to"], user.ap_id)
1311 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1312 |> Kernel.put_in(["object", "to"], user.ap_id)
1314 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1315 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1317 assert data["object"]["type"] == "Note"
1320 describe "fix_explicit_addressing" do
1322 user = insert(:user)
1326 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1327 explicitly_mentioned_actors = [
1328 "https://pleroma.gold/users/user1",
1329 "https://pleroma.gold/user2"
1333 "actor" => user.ap_id,
1334 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1337 Enum.map(explicitly_mentioned_actors, fn href ->
1338 %{"type" => "Mention", "href" => href}
1342 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1343 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1344 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1345 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1348 test "does not move actor's follower collection to cc", %{user: user} do
1350 "actor" => user.ap_id,
1351 "to" => [user.follower_address],
1355 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1356 assert user.follower_address in fixed_object["to"]
1357 refute user.follower_address in fixed_object["cc"]
1360 test "removes recipient's follower collection from cc", %{user: user} do
1361 recipient = insert(:user)
1364 "actor" => user.ap_id,
1365 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1366 "cc" => [user.follower_address, recipient.follower_address]
1369 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1371 assert user.follower_address in fixed_object["cc"]
1372 refute recipient.follower_address in fixed_object["cc"]
1373 refute recipient.follower_address in fixed_object["to"]