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.ActivityPub.Utils
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
18 import Pleroma.Factory
19 alias Pleroma.Web.CommonAPI
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 describe "handle_incoming" do
27 test "it ignores an incoming notice if we already have it" do
28 activity = insert(:note_activity)
31 File.read!("test/fixtures/mastodon-post-activity.json")
33 |> Map.put("object", activity.data["object"])
35 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
37 assert activity == returned_activity
40 test "it fetches replied-to activities if we don't have them" do
42 File.read!("test/fixtures/mastodon-post-activity.json")
47 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
51 |> Map.put("object", object)
53 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 returned_object = Object.normalize(returned_activity.data["object"])
57 Activity.get_create_by_object_ap_id(
58 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
61 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
64 test "it works for incoming notices" do
65 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
67 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
70 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
72 assert data["context"] ==
73 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
75 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
77 assert data["cc"] == [
78 "http://mastodon.example.org/users/admin/followers",
79 "http://localtesting.pleroma.lol/users/lain"
82 assert data["actor"] == "http://mastodon.example.org/users/admin"
84 object = Object.normalize(data["object"]).data
85 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
87 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
89 assert object["cc"] == [
90 "http://mastodon.example.org/users/admin/followers",
91 "http://localtesting.pleroma.lol/users/lain"
94 assert object["actor"] == "http://mastodon.example.org/users/admin"
95 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
97 assert object["context"] ==
98 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
100 assert object["sensitive"] == true
102 user = User.get_cached_by_ap_id(object["actor"])
104 assert user.info.note_count == 1
107 test "it works for incoming notices with hashtags" do
108 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
111 object = Object.normalize(data["object"])
113 assert Enum.at(object.data["tag"], 2) == "moo"
116 test "it works for incoming questions" do
117 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
119 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
121 object = Object.normalize(activity)
123 assert Enum.all?(object.data["oneOf"], fn choice ->
126 "Everyone knows that!",
127 "25 char limit is dumb",
128 "I can't even fit a funny"
133 test "it works for incoming notices with contentMap" do
135 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
137 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
138 object = Object.normalize(data["object"])
140 assert object.data["content"] ==
141 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
144 test "it works for incoming notices with to/cc not being an array (kroeg)" do
145 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
147 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
148 object = Object.normalize(data["object"])
150 assert object.data["content"] ==
151 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
154 test "it works for incoming announces with actor being inlined (kroeg)" do
155 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
157 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
159 assert data["actor"] == "https://puckipedia.com/"
162 test "it works for incoming notices with tag not being an array (kroeg)" do
163 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
165 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
166 object = Object.normalize(data["object"])
168 assert object.data["emoji"] == %{
169 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
172 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
174 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
175 object = Object.normalize(data["object"])
177 assert "test" in object.data["tag"]
180 test "it works for incoming notices with url not being a string (prismo)" do
181 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
183 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
184 object = Object.normalize(data["object"])
186 assert object.data["url"] == "https://prismo.news/posts/83"
189 test "it cleans up incoming notices which are not really DMs" do
191 other_user = insert(:user)
193 to = [user.ap_id, other_user.ap_id]
196 File.read!("test/fixtures/mastodon-post-activity.json")
206 data = Map.put(data, "object", object)
208 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
210 assert data["to"] == []
211 assert data["cc"] == to
213 object_data = Object.normalize(activity).data
215 assert object_data["to"] == []
216 assert object_data["cc"] == to
219 test "it works for incoming follow requests" do
223 File.read!("test/fixtures/mastodon-follow-activity.json")
225 |> Map.put("object", user.ap_id)
227 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
229 assert data["actor"] == "http://mastodon.example.org/users/admin"
230 assert data["type"] == "Follow"
231 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
232 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
235 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
236 Pleroma.Config.put([:user, :deny_follow_blocked], true)
239 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
241 {:ok, user} = User.block(user, target)
244 File.read!("test/fixtures/mastodon-follow-activity.json")
246 |> Map.put("object", user.ap_id)
248 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
250 %Activity{} = activity = Activity.get_by_ap_id(id)
252 assert activity.data["state"] == "reject"
255 test "it works for incoming follow requests from hubzilla" do
259 File.read!("test/fixtures/hubzilla-follow-activity.json")
261 |> Map.put("object", user.ap_id)
262 |> Utils.normalize_params()
264 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
266 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
267 assert data["type"] == "Follow"
268 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
269 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
272 test "it works for incoming likes" do
274 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
277 File.read!("test/fixtures/mastodon-like.json")
279 |> Map.put("object", activity.data["object"])
281 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
283 assert data["actor"] == "http://mastodon.example.org/users/admin"
284 assert data["type"] == "Like"
285 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
286 assert data["object"] == activity.data["object"]
289 test "it returns an error for incoming unlikes wihout a like activity" do
291 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
294 File.read!("test/fixtures/mastodon-undo-like.json")
296 |> Map.put("object", activity.data["object"])
298 assert Transmogrifier.handle_incoming(data) == :error
301 test "it works for incoming unlikes with an existing like activity" do
303 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
306 File.read!("test/fixtures/mastodon-like.json")
308 |> Map.put("object", activity.data["object"])
310 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
313 File.read!("test/fixtures/mastodon-undo-like.json")
315 |> Map.put("object", like_data)
316 |> Map.put("actor", like_data["actor"])
318 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
320 assert data["actor"] == "http://mastodon.example.org/users/admin"
321 assert data["type"] == "Undo"
322 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
323 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
326 test "it works for incoming announces" do
327 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
329 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
331 assert data["actor"] == "http://mastodon.example.org/users/admin"
332 assert data["type"] == "Announce"
335 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
337 assert data["object"] ==
338 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
340 assert Activity.get_create_by_object_ap_id(data["object"])
343 test "it works for incoming announces with an existing activity" do
345 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
348 File.read!("test/fixtures/mastodon-announce.json")
350 |> Map.put("object", activity.data["object"])
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"] == activity.data["object"]
362 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
365 test "it does not clobber the addressing on announce activities" do
367 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
370 File.read!("test/fixtures/mastodon-announce.json")
372 |> Map.put("object", Object.normalize(activity).data["id"])
373 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
376 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
378 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
381 test "it ensures that as:Public activities make it to their followers collection" do
385 File.read!("test/fixtures/mastodon-post-activity.json")
387 |> Map.put("actor", user.ap_id)
388 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
393 |> Map.put("attributedTo", user.ap_id)
394 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
397 data = Map.put(data, "object", object)
399 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
401 assert data["cc"] == [User.ap_followers(user)]
404 test "it ensures that address fields become lists" do
408 File.read!("test/fixtures/mastodon-post-activity.json")
410 |> Map.put("actor", user.ap_id)
411 |> Map.put("to", nil)
412 |> Map.put("cc", nil)
416 |> Map.put("attributedTo", user.ap_id)
417 |> Map.put("to", nil)
418 |> Map.put("cc", nil)
420 data = Map.put(data, "object", object)
422 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
424 assert !is_nil(data["to"])
425 assert !is_nil(data["cc"])
428 test "it works for incoming update activities" do
429 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
431 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
432 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
435 update_data["object"]
436 |> Map.put("actor", data["actor"])
437 |> Map.put("id", data["actor"])
441 |> Map.put("actor", data["actor"])
442 |> Map.put("object", object)
444 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
446 user = User.get_cached_by_ap_id(data["actor"])
447 assert user.name == "gargle"
449 assert user.avatar["url"] == [
452 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
456 assert user.info.banner["url"] == [
459 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
463 assert user.bio == "<p>Some bio</p>"
466 test "it works for incoming update activities which lock the account" do
467 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
469 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
470 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
473 update_data["object"]
474 |> Map.put("actor", data["actor"])
475 |> Map.put("id", data["actor"])
476 |> Map.put("manuallyApprovesFollowers", true)
480 |> Map.put("actor", data["actor"])
481 |> Map.put("object", object)
483 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
485 user = User.get_cached_by_ap_id(data["actor"])
486 assert user.info.locked == true
489 test "it works for incoming deletes" do
490 activity = insert(:note_activity)
493 File.read!("test/fixtures/mastodon-delete.json")
498 |> Map.put("id", activity.data["object"])
502 |> Map.put("object", object)
503 |> Map.put("actor", activity.data["actor"])
505 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
507 refute Activity.get_by_id(activity.id)
510 test "it fails for incoming deletes with spoofed origin" do
511 activity = insert(:note_activity)
514 File.read!("test/fixtures/mastodon-delete.json")
519 |> Map.put("id", activity.data["object"])
523 |> Map.put("object", object)
525 :error = Transmogrifier.handle_incoming(data)
527 assert Activity.get_by_id(activity.id)
530 test "it works for incoming unannounces with an existing notice" do
532 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
535 File.read!("test/fixtures/mastodon-announce.json")
537 |> Map.put("object", activity.data["object"])
539 {:ok, %Activity{data: announce_data, local: false}} =
540 Transmogrifier.handle_incoming(announce_data)
543 File.read!("test/fixtures/mastodon-undo-announce.json")
545 |> Map.put("object", announce_data)
546 |> Map.put("actor", announce_data["actor"])
548 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
550 assert data["type"] == "Undo"
551 assert data["object"]["type"] == "Announce"
552 assert data["object"]["object"] == activity.data["object"]
554 assert data["object"]["id"] ==
555 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
558 test "it works for incomming unfollows with an existing follow" do
562 File.read!("test/fixtures/mastodon-follow-activity.json")
564 |> Map.put("object", user.ap_id)
566 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
569 File.read!("test/fixtures/mastodon-unfollow-activity.json")
571 |> Map.put("object", follow_data)
573 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
575 assert data["type"] == "Undo"
576 assert data["object"]["type"] == "Follow"
577 assert data["object"]["object"] == user.ap_id
578 assert data["actor"] == "http://mastodon.example.org/users/admin"
580 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
583 test "it works for incoming blocks" do
587 File.read!("test/fixtures/mastodon-block-activity.json")
589 |> Map.put("object", user.ap_id)
591 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
593 assert data["type"] == "Block"
594 assert data["object"] == user.ap_id
595 assert data["actor"] == "http://mastodon.example.org/users/admin"
597 blocker = User.get_cached_by_ap_id(data["actor"])
599 assert User.blocks?(blocker, user)
602 test "incoming blocks successfully tear down any follow relationship" do
603 blocker = insert(:user)
604 blocked = insert(:user)
607 File.read!("test/fixtures/mastodon-block-activity.json")
609 |> Map.put("object", blocked.ap_id)
610 |> Map.put("actor", blocker.ap_id)
612 {:ok, blocker} = User.follow(blocker, blocked)
613 {:ok, blocked} = User.follow(blocked, blocker)
615 assert User.following?(blocker, blocked)
616 assert User.following?(blocked, blocker)
618 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
620 assert data["type"] == "Block"
621 assert data["object"] == blocked.ap_id
622 assert data["actor"] == blocker.ap_id
624 blocker = User.get_cached_by_ap_id(data["actor"])
625 blocked = User.get_cached_by_ap_id(data["object"])
627 assert User.blocks?(blocker, blocked)
629 refute User.following?(blocker, blocked)
630 refute User.following?(blocked, blocker)
633 test "it works for incoming unblocks with an existing block" do
637 File.read!("test/fixtures/mastodon-block-activity.json")
639 |> Map.put("object", user.ap_id)
641 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
644 File.read!("test/fixtures/mastodon-unblock-activity.json")
646 |> Map.put("object", block_data)
648 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
649 assert data["type"] == "Undo"
650 assert data["object"]["type"] == "Block"
651 assert data["object"]["object"] == user.ap_id
652 assert data["actor"] == "http://mastodon.example.org/users/admin"
654 blocker = User.get_cached_by_ap_id(data["actor"])
656 refute User.blocks?(blocker, user)
659 test "it works for incoming accepts which were pre-accepted" do
660 follower = insert(:user)
661 followed = insert(:user)
663 {:ok, follower} = User.follow(follower, followed)
664 assert User.following?(follower, followed) == true
666 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
669 File.read!("test/fixtures/mastodon-accept-activity.json")
671 |> Map.put("actor", followed.ap_id)
674 accept_data["object"]
675 |> Map.put("actor", follower.ap_id)
676 |> Map.put("id", follow_activity.data["id"])
678 accept_data = Map.put(accept_data, "object", object)
680 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
681 refute activity.local
683 assert activity.data["object"] == follow_activity.data["id"]
685 follower = User.get_cached_by_id(follower.id)
687 assert User.following?(follower, followed) == true
690 test "it works for incoming accepts which were orphaned" do
691 follower = insert(:user)
692 followed = insert(:user, %{info: %User.Info{locked: true}})
694 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
697 File.read!("test/fixtures/mastodon-accept-activity.json")
699 |> Map.put("actor", followed.ap_id)
702 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
704 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
705 assert activity.data["object"] == follow_activity.data["id"]
707 follower = User.get_cached_by_id(follower.id)
709 assert User.following?(follower, followed) == true
712 test "it works for incoming accepts which are referenced by IRI only" do
713 follower = insert(:user)
714 followed = insert(:user, %{info: %User.Info{locked: true}})
716 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
719 File.read!("test/fixtures/mastodon-accept-activity.json")
721 |> Map.put("actor", followed.ap_id)
722 |> Map.put("object", follow_activity.data["id"])
724 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
725 assert activity.data["object"] == follow_activity.data["id"]
727 follower = User.get_cached_by_id(follower.id)
729 assert User.following?(follower, followed) == true
732 test "it fails for incoming accepts which cannot be correlated" do
733 follower = insert(:user)
734 followed = insert(:user, %{info: %User.Info{locked: true}})
737 File.read!("test/fixtures/mastodon-accept-activity.json")
739 |> Map.put("actor", followed.ap_id)
742 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
744 :error = Transmogrifier.handle_incoming(accept_data)
746 follower = User.get_cached_by_id(follower.id)
748 refute User.following?(follower, followed) == true
751 test "it fails for incoming rejects which cannot be correlated" do
752 follower = insert(:user)
753 followed = insert(:user, %{info: %User.Info{locked: true}})
756 File.read!("test/fixtures/mastodon-reject-activity.json")
758 |> Map.put("actor", followed.ap_id)
761 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
763 :error = Transmogrifier.handle_incoming(accept_data)
765 follower = User.get_cached_by_id(follower.id)
767 refute User.following?(follower, followed) == true
770 test "it works for incoming rejects which are orphaned" do
771 follower = insert(:user)
772 followed = insert(:user, %{info: %User.Info{locked: true}})
774 {:ok, follower} = User.follow(follower, followed)
775 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
777 assert User.following?(follower, followed) == true
780 File.read!("test/fixtures/mastodon-reject-activity.json")
782 |> Map.put("actor", followed.ap_id)
785 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
787 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
788 refute activity.local
790 follower = User.get_cached_by_id(follower.id)
792 assert User.following?(follower, followed) == false
795 test "it works for incoming rejects which are referenced by IRI only" do
796 follower = insert(:user)
797 followed = insert(:user, %{info: %User.Info{locked: true}})
799 {:ok, follower} = User.follow(follower, followed)
800 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
802 assert User.following?(follower, followed) == true
805 File.read!("test/fixtures/mastodon-reject-activity.json")
807 |> Map.put("actor", followed.ap_id)
808 |> Map.put("object", follow_activity.data["id"])
810 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
812 follower = User.get_cached_by_id(follower.id)
814 assert User.following?(follower, followed) == false
817 test "it rejects activities without a valid ID" do
821 File.read!("test/fixtures/mastodon-follow-activity.json")
823 |> Map.put("object", user.ap_id)
826 :error = Transmogrifier.handle_incoming(data)
829 test "it remaps video URLs as attachments if necessary" do
831 Fetcher.fetch_object_from_id(
832 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
837 "mediaType" => "video/mp4",
839 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
840 "mimeType" => "video/mp4",
845 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
846 "mediaType" => "video/mp4",
853 assert object.data["url"] ==
854 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
856 assert object.data["attachment"] == [attachment]
859 test "it accepts Flag activities" do
861 other_user = insert(:user)
863 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
864 object = Object.normalize(activity.data["object"])
867 "@context" => "https://www.w3.org/ns/activitystreams",
868 "cc" => [user.ap_id],
869 "object" => [user.ap_id, object.data["id"]],
871 "content" => "blocked AND reported!!!",
872 "actor" => other_user.ap_id
875 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
877 assert activity.data["object"] == [user.ap_id, object.data["id"]]
878 assert activity.data["content"] == "blocked AND reported!!!"
879 assert activity.data["actor"] == other_user.ap_id
880 assert activity.data["cc"] == [user.ap_id]
884 describe "prepare outgoing" do
885 test "it turns mentions into tags" do
887 other_user = insert(:user)
890 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
892 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
893 object = modified["object"]
895 expected_mention = %{
896 "href" => other_user.ap_id,
897 "name" => "@#{other_user.nickname}",
902 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
907 assert Enum.member?(object["tag"], expected_tag)
908 assert Enum.member?(object["tag"], expected_mention)
911 test "it adds the sensitive property" do
914 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
915 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
917 assert modified["object"]["sensitive"]
920 test "it adds the json-ld context and the conversation property" do
923 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
924 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
926 assert modified["@context"] ==
927 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
929 assert modified["object"]["conversation"] == modified["context"]
932 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
935 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
936 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
938 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
941 test "it translates ostatus IDs to external URLs" do
942 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
943 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
947 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
948 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
950 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
953 test "it translates ostatus reply_to IDs to external URLs" do
954 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
955 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
960 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
962 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
964 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
967 test "it strips internal hashtag data" do
970 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
973 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
978 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
980 assert modified["object"]["tag"] == [expected_tag]
983 test "it strips internal fields" do
986 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
988 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
990 assert length(modified["object"]["tag"]) == 2
992 assert is_nil(modified["object"]["emoji"])
993 assert is_nil(modified["object"]["like_count"])
994 assert is_nil(modified["object"]["announcements"])
995 assert is_nil(modified["object"]["announcement_count"])
996 assert is_nil(modified["object"]["context_id"])
999 test "it strips internal fields of article" do
1000 activity = insert(:article_activity)
1002 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1004 assert length(modified["object"]["tag"]) == 2
1006 assert is_nil(modified["object"]["emoji"])
1007 assert is_nil(modified["object"]["like_count"])
1008 assert is_nil(modified["object"]["announcements"])
1009 assert is_nil(modified["object"]["announcement_count"])
1010 assert is_nil(modified["object"]["context_id"])
1013 test "it adds like collection to object" do
1014 activity = insert(:note_activity)
1015 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1017 assert modified["object"]["likes"]["type"] == "OrderedCollection"
1018 assert modified["object"]["likes"]["totalItems"] == 0
1021 test "the directMessage flag is present" do
1022 user = insert(:user)
1023 other_user = insert(:user)
1025 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1027 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1029 assert modified["directMessage"] == false
1032 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1034 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1036 assert modified["directMessage"] == false
1039 CommonAPI.post(user, %{
1040 "status" => "@#{other_user.nickname} :moominmamma:",
1041 "visibility" => "direct"
1044 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1046 assert modified["directMessage"] == true
1050 describe "user upgrade" do
1051 test "it upgrades a user to activitypub" do
1054 nickname: "rye@niu.moe",
1056 ap_id: "https://niu.moe/users/rye",
1057 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1060 user_two = insert(:user, %{following: [user.follower_address]})
1062 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1063 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1064 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1066 user = User.get_cached_by_id(user.id)
1067 assert user.info.note_count == 1
1069 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1070 assert user.info.ap_enabled
1071 assert user.info.note_count == 1
1072 assert user.follower_address == "https://niu.moe/users/rye/followers"
1074 user = User.get_cached_by_id(user.id)
1075 assert user.info.note_count == 1
1077 activity = Activity.get_by_id(activity.id)
1078 assert user.follower_address in activity.recipients
1084 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1093 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1096 } = user.info.banner
1098 refute "..." in activity.recipients
1100 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1101 refute user.follower_address in unrelated_activity.recipients
1103 user_two = User.get_cached_by_id(user_two.id)
1104 assert user.follower_address in user_two.following
1105 refute "..." in user_two.following
1109 describe "maybe_retire_websub" do
1110 test "it deletes all websub client subscripitions with the user as topic" do
1111 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1112 {:ok, ws} = Repo.insert(subscription)
1114 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1115 {:ok, ws2} = Repo.insert(subscription)
1117 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1119 refute Repo.get(WebsubClientSubscription, ws.id)
1120 assert Repo.get(WebsubClientSubscription, ws2.id)
1124 describe "actor rewriting" do
1125 test "it fixes the actor URL property to be a proper URI" do
1127 "url" => %{"href" => "http://example.com"}
1130 rewritten = Transmogrifier.maybe_fix_user_object(data)
1131 assert rewritten["url"] == "http://example.com"
1135 describe "actor origin containment" do
1136 test "it rejects activities which reference objects with bogus origins" do
1138 "@context" => "https://www.w3.org/ns/activitystreams",
1139 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1140 "actor" => "http://mastodon.example.org/users/admin",
1141 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1142 "object" => "https://info.pleroma.site/activity.json",
1143 "type" => "Announce"
1146 :error = Transmogrifier.handle_incoming(data)
1149 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1151 "@context" => "https://www.w3.org/ns/activitystreams",
1152 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1153 "actor" => "http://mastodon.example.org/users/admin",
1154 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1155 "object" => "https://info.pleroma.site/activity2.json",
1156 "type" => "Announce"
1159 :error = Transmogrifier.handle_incoming(data)
1162 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1164 "@context" => "https://www.w3.org/ns/activitystreams",
1165 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1166 "actor" => "http://mastodon.example.org/users/admin",
1167 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1168 "object" => "https://info.pleroma.site/activity3.json",
1169 "type" => "Announce"
1172 :error = Transmogrifier.handle_incoming(data)
1176 describe "reserialization" do
1177 test "successfully reserializes a message with inReplyTo == nil" do
1178 user = insert(:user)
1181 "@context" => "https://www.w3.org/ns/activitystreams",
1182 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1186 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1191 "attributedTo" => user.ap_id
1193 "actor" => user.ap_id
1196 {:ok, activity} = Transmogrifier.handle_incoming(message)
1198 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1201 test "successfully reserializes a message with AS2 objects in IR" do
1202 user = insert(:user)
1205 "@context" => "https://www.w3.org/ns/activitystreams",
1206 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1210 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1215 "attributedTo" => user.ap_id,
1217 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1218 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1221 "actor" => user.ap_id
1224 {:ok, activity} = Transmogrifier.handle_incoming(message)
1226 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)