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 strips internal likes" do
455 File.read!("test/fixtures/mastodon-post-activity.json")
460 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
461 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
463 "type" => "OrderedCollection"
466 object = Map.put(data["object"], "likes", likes)
467 data = Map.put(data, "object", object)
469 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
471 refute Map.has_key?(object.data, "likes")
474 test "it works for incoming update activities" do
475 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
477 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
478 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
481 update_data["object"]
482 |> Map.put("actor", data["actor"])
483 |> Map.put("id", data["actor"])
487 |> Map.put("actor", data["actor"])
488 |> Map.put("object", object)
490 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
492 user = User.get_cached_by_ap_id(data["actor"])
493 assert user.name == "gargle"
495 assert user.avatar["url"] == [
498 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
502 assert user.info.banner["url"] == [
505 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
509 assert user.bio == "<p>Some bio</p>"
512 test "it works for incoming update activities which lock the account" do
513 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
515 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
516 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
519 update_data["object"]
520 |> Map.put("actor", data["actor"])
521 |> Map.put("id", data["actor"])
522 |> Map.put("manuallyApprovesFollowers", true)
526 |> Map.put("actor", data["actor"])
527 |> Map.put("object", object)
529 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
531 user = User.get_cached_by_ap_id(data["actor"])
532 assert user.info.locked == true
535 test "it works for incoming deletes" 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)
549 |> Map.put("actor", activity.data["actor"])
551 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
553 refute Activity.get_by_id(activity.id)
556 test "it fails for incoming deletes with spoofed origin" do
557 activity = insert(:note_activity)
560 File.read!("test/fixtures/mastodon-delete.json")
565 |> Map.put("id", activity.data["object"])
569 |> Map.put("object", object)
571 assert capture_log(fn ->
572 :error = Transmogrifier.handle_incoming(data)
574 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
576 assert Activity.get_by_id(activity.id)
579 test "it works for incoming user deletes" do
580 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
583 File.read!("test/fixtures/mastodon-delete-user.json")
586 {:ok, _} = Transmogrifier.handle_incoming(data)
588 refute User.get_cached_by_ap_id(ap_id)
591 test "it fails for incoming user deletes with spoofed origin" do
592 %{ap_id: ap_id} = insert(:user)
595 File.read!("test/fixtures/mastodon-delete-user.json")
597 |> Map.put("actor", ap_id)
599 assert :error == Transmogrifier.handle_incoming(data)
600 assert User.get_cached_by_ap_id(ap_id)
603 test "it works for incoming unannounces with an existing notice" do
605 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
608 File.read!("test/fixtures/mastodon-announce.json")
610 |> Map.put("object", activity.data["object"])
612 {:ok, %Activity{data: announce_data, local: false}} =
613 Transmogrifier.handle_incoming(announce_data)
616 File.read!("test/fixtures/mastodon-undo-announce.json")
618 |> Map.put("object", announce_data)
619 |> Map.put("actor", announce_data["actor"])
621 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
623 assert data["type"] == "Undo"
624 assert object_data = data["object"]
625 assert object_data["type"] == "Announce"
626 assert object_data["object"] == activity.data["object"]
628 assert object_data["id"] ==
629 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
632 test "it works for incomming unfollows with an existing follow" do
636 File.read!("test/fixtures/mastodon-follow-activity.json")
638 |> Map.put("object", user.ap_id)
640 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
643 File.read!("test/fixtures/mastodon-unfollow-activity.json")
645 |> Map.put("object", follow_data)
647 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
649 assert data["type"] == "Undo"
650 assert data["object"]["type"] == "Follow"
651 assert data["object"]["object"] == user.ap_id
652 assert data["actor"] == "http://mastodon.example.org/users/admin"
654 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
657 test "it works for incoming blocks" do
661 File.read!("test/fixtures/mastodon-block-activity.json")
663 |> Map.put("object", user.ap_id)
665 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
667 assert data["type"] == "Block"
668 assert data["object"] == user.ap_id
669 assert data["actor"] == "http://mastodon.example.org/users/admin"
671 blocker = User.get_cached_by_ap_id(data["actor"])
673 assert User.blocks?(blocker, user)
676 test "incoming blocks successfully tear down any follow relationship" do
677 blocker = insert(:user)
678 blocked = insert(:user)
681 File.read!("test/fixtures/mastodon-block-activity.json")
683 |> Map.put("object", blocked.ap_id)
684 |> Map.put("actor", blocker.ap_id)
686 {:ok, blocker} = User.follow(blocker, blocked)
687 {:ok, blocked} = User.follow(blocked, blocker)
689 assert User.following?(blocker, blocked)
690 assert User.following?(blocked, blocker)
692 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
694 assert data["type"] == "Block"
695 assert data["object"] == blocked.ap_id
696 assert data["actor"] == blocker.ap_id
698 blocker = User.get_cached_by_ap_id(data["actor"])
699 blocked = User.get_cached_by_ap_id(data["object"])
701 assert User.blocks?(blocker, blocked)
703 refute User.following?(blocker, blocked)
704 refute User.following?(blocked, blocker)
707 test "it works for incoming unblocks with an existing block" do
711 File.read!("test/fixtures/mastodon-block-activity.json")
713 |> Map.put("object", user.ap_id)
715 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
718 File.read!("test/fixtures/mastodon-unblock-activity.json")
720 |> Map.put("object", block_data)
722 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
723 assert data["type"] == "Undo"
724 assert data["object"]["type"] == "Block"
725 assert data["object"]["object"] == user.ap_id
726 assert data["actor"] == "http://mastodon.example.org/users/admin"
728 blocker = User.get_cached_by_ap_id(data["actor"])
730 refute User.blocks?(blocker, user)
733 test "it works for incoming accepts which were pre-accepted" do
734 follower = insert(:user)
735 followed = insert(:user)
737 {:ok, follower} = User.follow(follower, followed)
738 assert User.following?(follower, followed) == true
740 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
743 File.read!("test/fixtures/mastodon-accept-activity.json")
745 |> Map.put("actor", followed.ap_id)
748 accept_data["object"]
749 |> Map.put("actor", follower.ap_id)
750 |> Map.put("id", follow_activity.data["id"])
752 accept_data = Map.put(accept_data, "object", object)
754 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
755 refute activity.local
757 assert activity.data["object"] == follow_activity.data["id"]
759 follower = User.get_cached_by_id(follower.id)
761 assert User.following?(follower, followed) == true
764 test "it works for incoming accepts which were orphaned" do
765 follower = insert(:user)
766 followed = insert(:user, %{info: %User.Info{locked: true}})
768 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
771 File.read!("test/fixtures/mastodon-accept-activity.json")
773 |> Map.put("actor", followed.ap_id)
776 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
778 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
779 assert activity.data["object"] == follow_activity.data["id"]
781 follower = User.get_cached_by_id(follower.id)
783 assert User.following?(follower, followed) == true
786 test "it works for incoming accepts which are referenced by IRI only" do
787 follower = insert(:user)
788 followed = insert(:user, %{info: %User.Info{locked: true}})
790 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
793 File.read!("test/fixtures/mastodon-accept-activity.json")
795 |> Map.put("actor", followed.ap_id)
796 |> Map.put("object", follow_activity.data["id"])
798 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
799 assert activity.data["object"] == follow_activity.data["id"]
801 follower = User.get_cached_by_id(follower.id)
803 assert User.following?(follower, followed) == true
806 test "it fails for incoming accepts which cannot be correlated" do
807 follower = insert(:user)
808 followed = insert(:user, %{info: %User.Info{locked: true}})
811 File.read!("test/fixtures/mastodon-accept-activity.json")
813 |> Map.put("actor", followed.ap_id)
816 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
818 :error = Transmogrifier.handle_incoming(accept_data)
820 follower = User.get_cached_by_id(follower.id)
822 refute User.following?(follower, followed) == true
825 test "it fails for incoming rejects which cannot be correlated" do
826 follower = insert(:user)
827 followed = insert(:user, %{info: %User.Info{locked: true}})
830 File.read!("test/fixtures/mastodon-reject-activity.json")
832 |> Map.put("actor", followed.ap_id)
835 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
837 :error = Transmogrifier.handle_incoming(accept_data)
839 follower = User.get_cached_by_id(follower.id)
841 refute User.following?(follower, followed) == true
844 test "it works for incoming rejects which are orphaned" do
845 follower = insert(:user)
846 followed = insert(:user, %{info: %User.Info{locked: true}})
848 {:ok, follower} = User.follow(follower, followed)
849 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
851 assert User.following?(follower, followed) == true
854 File.read!("test/fixtures/mastodon-reject-activity.json")
856 |> Map.put("actor", followed.ap_id)
859 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
861 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
862 refute activity.local
864 follower = User.get_cached_by_id(follower.id)
866 assert User.following?(follower, followed) == false
869 test "it works for incoming rejects which are referenced by IRI only" do
870 follower = insert(:user)
871 followed = insert(:user, %{info: %User.Info{locked: true}})
873 {:ok, follower} = User.follow(follower, followed)
874 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
876 assert User.following?(follower, followed) == true
879 File.read!("test/fixtures/mastodon-reject-activity.json")
881 |> Map.put("actor", followed.ap_id)
882 |> Map.put("object", follow_activity.data["id"])
884 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
886 follower = User.get_cached_by_id(follower.id)
888 assert User.following?(follower, followed) == false
891 test "it rejects activities without a valid ID" do
895 File.read!("test/fixtures/mastodon-follow-activity.json")
897 |> Map.put("object", user.ap_id)
900 :error = Transmogrifier.handle_incoming(data)
903 test "it remaps video URLs as attachments if necessary" do
905 Fetcher.fetch_object_from_id(
906 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
911 "mediaType" => "video/mp4",
913 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
914 "mimeType" => "video/mp4",
919 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
920 "mediaType" => "video/mp4",
927 assert object.data["url"] ==
928 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
930 assert object.data["attachment"] == [attachment]
933 test "it accepts Flag activities" do
935 other_user = insert(:user)
937 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
938 object = Object.normalize(activity)
941 "@context" => "https://www.w3.org/ns/activitystreams",
942 "cc" => [user.ap_id],
943 "object" => [user.ap_id, object.data["id"]],
945 "content" => "blocked AND reported!!!",
946 "actor" => other_user.ap_id
949 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
951 assert activity.data["object"] == [user.ap_id, object.data["id"]]
952 assert activity.data["content"] == "blocked AND reported!!!"
953 assert activity.data["actor"] == other_user.ap_id
954 assert activity.data["cc"] == [user.ap_id]
958 describe "prepare outgoing" do
959 test "it turns mentions into tags" do
961 other_user = insert(:user)
964 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
966 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
967 object = modified["object"]
969 expected_mention = %{
970 "href" => other_user.ap_id,
971 "name" => "@#{other_user.nickname}",
976 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
981 assert Enum.member?(object["tag"], expected_tag)
982 assert Enum.member?(object["tag"], expected_mention)
985 test "it adds the sensitive property" do
988 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
989 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
991 assert modified["object"]["sensitive"]
994 test "it adds the json-ld context and the conversation property" do
997 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
998 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1000 assert modified["@context"] ==
1001 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1003 assert modified["object"]["conversation"] == modified["context"]
1006 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1007 user = insert(:user)
1009 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1010 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1012 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1015 test "it translates ostatus IDs to external URLs" do
1016 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1017 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1019 user = insert(:user)
1021 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1022 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1024 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1027 test "it translates ostatus reply_to IDs to external URLs" do
1028 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1029 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1031 user = insert(:user)
1034 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1036 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1038 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1041 test "it strips internal hashtag data" do
1042 user = insert(:user)
1044 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1047 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1048 "type" => "Hashtag",
1052 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1054 assert modified["object"]["tag"] == [expected_tag]
1057 test "it strips internal fields" do
1058 user = insert(:user)
1060 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1062 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1064 assert length(modified["object"]["tag"]) == 2
1066 assert is_nil(modified["object"]["emoji"])
1067 assert is_nil(modified["object"]["like_count"])
1068 assert is_nil(modified["object"]["announcements"])
1069 assert is_nil(modified["object"]["announcement_count"])
1070 assert is_nil(modified["object"]["context_id"])
1073 test "it strips internal fields of article" do
1074 activity = insert(:article_activity)
1076 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1078 assert length(modified["object"]["tag"]) == 2
1080 assert is_nil(modified["object"]["emoji"])
1081 assert is_nil(modified["object"]["like_count"])
1082 assert is_nil(modified["object"]["announcements"])
1083 assert is_nil(modified["object"]["announcement_count"])
1084 assert is_nil(modified["object"]["context_id"])
1085 assert is_nil(modified["object"]["likes"])
1088 test "the directMessage flag is present" do
1089 user = insert(:user)
1090 other_user = insert(:user)
1092 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1094 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1096 assert modified["directMessage"] == false
1099 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1101 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1103 assert modified["directMessage"] == false
1106 CommonAPI.post(user, %{
1107 "status" => "@#{other_user.nickname} :moominmamma:",
1108 "visibility" => "direct"
1111 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1113 assert modified["directMessage"] == true
1116 test "it strips BCC field" do
1117 user = insert(:user)
1118 {:ok, list} = Pleroma.List.create("foo", user)
1121 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1123 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1125 assert is_nil(modified["bcc"])
1129 describe "user upgrade" do
1130 test "it upgrades a user to activitypub" do
1133 nickname: "rye@niu.moe",
1135 ap_id: "https://niu.moe/users/rye",
1136 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1139 user_two = insert(:user, %{following: [user.follower_address]})
1141 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1142 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1143 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1145 user = User.get_cached_by_id(user.id)
1146 assert user.info.note_count == 1
1148 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1149 assert user.info.ap_enabled
1150 assert user.info.note_count == 1
1151 assert user.follower_address == "https://niu.moe/users/rye/followers"
1152 assert user.following_address == "https://niu.moe/users/rye/following"
1154 user = User.get_cached_by_id(user.id)
1155 assert user.info.note_count == 1
1157 activity = Activity.get_by_id(activity.id)
1158 assert user.follower_address in activity.recipients
1164 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1173 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1176 } = user.info.banner
1178 refute "..." in activity.recipients
1180 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1181 refute user.follower_address in unrelated_activity.recipients
1183 user_two = User.get_cached_by_id(user_two.id)
1184 assert user.follower_address in user_two.following
1185 refute "..." in user_two.following
1189 describe "maybe_retire_websub" do
1190 test "it deletes all websub client subscripitions with the user as topic" do
1191 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1192 {:ok, ws} = Repo.insert(subscription)
1194 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1195 {:ok, ws2} = Repo.insert(subscription)
1197 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1199 refute Repo.get(WebsubClientSubscription, ws.id)
1200 assert Repo.get(WebsubClientSubscription, ws2.id)
1204 describe "actor rewriting" do
1205 test "it fixes the actor URL property to be a proper URI" do
1207 "url" => %{"href" => "http://example.com"}
1210 rewritten = Transmogrifier.maybe_fix_user_object(data)
1211 assert rewritten["url"] == "http://example.com"
1215 describe "actor origin containment" do
1216 test "it rejects activities which reference objects with bogus origins" do
1218 "@context" => "https://www.w3.org/ns/activitystreams",
1219 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1220 "actor" => "http://mastodon.example.org/users/admin",
1221 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1222 "object" => "https://info.pleroma.site/activity.json",
1223 "type" => "Announce"
1226 :error = Transmogrifier.handle_incoming(data)
1229 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1231 "@context" => "https://www.w3.org/ns/activitystreams",
1232 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1233 "actor" => "http://mastodon.example.org/users/admin",
1234 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1235 "object" => "https://info.pleroma.site/activity2.json",
1236 "type" => "Announce"
1239 :error = Transmogrifier.handle_incoming(data)
1242 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1244 "@context" => "https://www.w3.org/ns/activitystreams",
1245 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1246 "actor" => "http://mastodon.example.org/users/admin",
1247 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1248 "object" => "https://info.pleroma.site/activity3.json",
1249 "type" => "Announce"
1252 :error = Transmogrifier.handle_incoming(data)
1256 describe "reserialization" do
1257 test "successfully reserializes a message with inReplyTo == nil" do
1258 user = insert(:user)
1261 "@context" => "https://www.w3.org/ns/activitystreams",
1262 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1266 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1271 "attributedTo" => user.ap_id
1273 "actor" => user.ap_id
1276 {:ok, activity} = Transmogrifier.handle_incoming(message)
1278 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1281 test "successfully reserializes a message with AS2 objects in IR" do
1282 user = insert(:user)
1285 "@context" => "https://www.w3.org/ns/activitystreams",
1286 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1290 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1295 "attributedTo" => user.ap_id,
1297 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1298 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1301 "actor" => user.ap_id
1304 {:ok, activity} = Transmogrifier.handle_incoming(message)
1306 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1310 test "Rewrites Answers to Notes" do
1311 user = insert(:user)
1313 {:ok, poll_activity} =
1314 CommonAPI.post(user, %{
1315 "status" => "suya...",
1316 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1319 poll_object = Object.normalize(poll_activity)
1320 # TODO: Replace with CommonAPI vote creation when implemented
1322 File.read!("test/fixtures/mastodon-vote.json")
1324 |> Kernel.put_in(["to"], user.ap_id)
1325 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1326 |> Kernel.put_in(["object", "to"], user.ap_id)
1328 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1329 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1331 assert data["object"]["type"] == "Note"
1334 describe "fix_explicit_addressing" do
1336 user = insert(:user)
1340 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1341 explicitly_mentioned_actors = [
1342 "https://pleroma.gold/users/user1",
1343 "https://pleroma.gold/user2"
1347 "actor" => user.ap_id,
1348 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1351 Enum.map(explicitly_mentioned_actors, fn href ->
1352 %{"type" => "Mention", "href" => href}
1356 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1357 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1358 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1359 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1362 test "does not move actor's follower collection to cc", %{user: user} do
1364 "actor" => user.ap_id,
1365 "to" => [user.follower_address],
1369 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1370 assert user.follower_address in fixed_object["to"]
1371 refute user.follower_address in fixed_object["cc"]
1374 test "removes recipient's follower collection from cc", %{user: user} do
1375 recipient = insert(:user)
1378 "actor" => user.ap_id,
1379 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1380 "cc" => [user.follower_address, recipient.follower_address]
1383 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1385 assert user.follower_address in fixed_object["cc"]
1386 refute recipient.follower_address in fixed_object["cc"]
1387 refute recipient.follower_address in fixed_object["to"]