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.OStatus
15 alias Pleroma.Web.Websub.WebsubClientSubscription
17 import Pleroma.Factory
18 import ExUnit.CaptureLog
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 does not crash if the object in inReplyTo can't be fetched" do
66 File.read!("test/fixtures/mastodon-post-activity.json")
71 |> Map.put("inReplyTo", "https://404.site/whatever")
75 |> Map.put("object", object)
77 assert capture_log(fn ->
78 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
79 end) =~ "[error] Couldn't fetch \"\"https://404.site/whatever\"\", error: nil"
82 test "it works for incoming notices" do
83 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
85 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
88 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
90 assert data["context"] ==
91 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
93 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
95 assert data["cc"] == [
96 "http://mastodon.example.org/users/admin/followers",
97 "http://localtesting.pleroma.lol/users/lain"
100 assert data["actor"] == "http://mastodon.example.org/users/admin"
102 object = Object.normalize(data["object"]).data
103 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
105 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
107 assert object["cc"] == [
108 "http://mastodon.example.org/users/admin/followers",
109 "http://localtesting.pleroma.lol/users/lain"
112 assert object["actor"] == "http://mastodon.example.org/users/admin"
113 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
115 assert object["context"] ==
116 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
118 assert object["sensitive"] == true
120 user = User.get_cached_by_ap_id(object["actor"])
122 assert user.info.note_count == 1
125 test "it works for incoming notices with hashtags" do
126 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
128 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
129 object = Object.normalize(data["object"])
131 assert Enum.at(object.data["tag"], 2) == "moo"
134 test "it works for incoming questions" do
135 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
137 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
139 object = Object.normalize(activity)
141 assert Enum.all?(object.data["oneOf"], fn choice ->
144 "Everyone knows that!",
145 "25 char limit is dumb",
146 "I can't even fit a funny"
151 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
155 CommonAPI.post(user, %{
156 "status" => "suya...",
157 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
160 object = Object.normalize(activity)
163 File.read!("test/fixtures/mastodon-vote.json")
165 |> Kernel.put_in(["to"], user.ap_id)
166 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
167 |> Kernel.put_in(["object", "to"], user.ap_id)
169 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
170 answer_object = Object.normalize(activity)
171 assert answer_object.data["type"] == "Answer"
172 object = Object.get_by_ap_id(object.data["id"])
175 object.data["oneOf"],
177 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
183 test "it works for incoming notices with contentMap" do
185 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
187 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
188 object = Object.normalize(data["object"])
190 assert object.data["content"] ==
191 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
194 test "it works for incoming notices with to/cc not being an array (kroeg)" do
195 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
197 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
198 object = Object.normalize(data["object"])
200 assert object.data["content"] ==
201 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
204 test "it works for incoming announces with actor being inlined (kroeg)" do
205 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
207 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
209 assert data["actor"] == "https://puckipedia.com/"
212 test "it works for incoming notices with tag not being an array (kroeg)" do
213 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
215 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
216 object = Object.normalize(data["object"])
218 assert object.data["emoji"] == %{
219 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
222 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
224 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
225 object = Object.normalize(data["object"])
227 assert "test" in object.data["tag"]
230 test "it works for incoming notices with url not being a string (prismo)" do
231 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
233 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
234 object = Object.normalize(data["object"])
236 assert object.data["url"] == "https://prismo.news/posts/83"
239 test "it cleans up incoming notices which are not really DMs" do
241 other_user = insert(:user)
243 to = [user.ap_id, other_user.ap_id]
246 File.read!("test/fixtures/mastodon-post-activity.json")
256 data = Map.put(data, "object", object)
258 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
260 assert data["to"] == []
261 assert data["cc"] == to
263 object_data = Object.normalize(activity).data
265 assert object_data["to"] == []
266 assert object_data["cc"] == to
269 test "it works for incoming likes" do
271 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
274 File.read!("test/fixtures/mastodon-like.json")
276 |> Map.put("object", activity.data["object"])
278 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
280 assert data["actor"] == "http://mastodon.example.org/users/admin"
281 assert data["type"] == "Like"
282 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
283 assert data["object"] == activity.data["object"]
286 test "it returns an error for incoming unlikes wihout a like activity" do
288 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
291 File.read!("test/fixtures/mastodon-undo-like.json")
293 |> Map.put("object", activity.data["object"])
295 assert Transmogrifier.handle_incoming(data) == :error
298 test "it works for incoming unlikes with an existing like activity" do
300 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
303 File.read!("test/fixtures/mastodon-like.json")
305 |> Map.put("object", activity.data["object"])
307 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
310 File.read!("test/fixtures/mastodon-undo-like.json")
312 |> Map.put("object", like_data)
313 |> Map.put("actor", like_data["actor"])
315 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
317 assert data["actor"] == "http://mastodon.example.org/users/admin"
318 assert data["type"] == "Undo"
319 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
320 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
323 test "it works for incoming announces" do
324 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
326 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
328 assert data["actor"] == "http://mastodon.example.org/users/admin"
329 assert data["type"] == "Announce"
332 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
334 assert data["object"] ==
335 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
337 assert Activity.get_create_by_object_ap_id(data["object"])
340 test "it works for incoming announces with an existing activity" do
342 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
345 File.read!("test/fixtures/mastodon-announce.json")
347 |> Map.put("object", activity.data["object"])
349 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
351 assert data["actor"] == "http://mastodon.example.org/users/admin"
352 assert data["type"] == "Announce"
355 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
357 assert data["object"] == activity.data["object"]
359 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
362 test "it does not clobber the addressing on announce activities" do
364 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
367 File.read!("test/fixtures/mastodon-announce.json")
369 |> Map.put("object", Object.normalize(activity).data["id"])
370 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
373 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
375 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
378 test "it ensures that as:Public activities make it to their followers collection" do
382 File.read!("test/fixtures/mastodon-post-activity.json")
384 |> Map.put("actor", user.ap_id)
385 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
390 |> Map.put("attributedTo", user.ap_id)
391 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
394 data = Map.put(data, "object", object)
396 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
398 assert data["cc"] == [User.ap_followers(user)]
401 test "it ensures that address fields become lists" do
405 File.read!("test/fixtures/mastodon-post-activity.json")
407 |> Map.put("actor", user.ap_id)
408 |> Map.put("to", nil)
409 |> Map.put("cc", nil)
413 |> Map.put("attributedTo", user.ap_id)
414 |> Map.put("to", nil)
415 |> Map.put("cc", nil)
417 data = Map.put(data, "object", object)
419 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
421 assert !is_nil(data["to"])
422 assert !is_nil(data["cc"])
425 test "it works for incoming update activities" do
426 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
428 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
429 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
432 update_data["object"]
433 |> Map.put("actor", data["actor"])
434 |> Map.put("id", data["actor"])
438 |> Map.put("actor", data["actor"])
439 |> Map.put("object", object)
441 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
443 user = User.get_cached_by_ap_id(data["actor"])
444 assert user.name == "gargle"
446 assert user.avatar["url"] == [
449 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
453 assert user.info.banner["url"] == [
456 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
460 assert user.bio == "<p>Some bio</p>"
463 test "it works for incoming update activities which lock the account" do
464 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
466 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
467 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
470 update_data["object"]
471 |> Map.put("actor", data["actor"])
472 |> Map.put("id", data["actor"])
473 |> Map.put("manuallyApprovesFollowers", true)
477 |> Map.put("actor", data["actor"])
478 |> Map.put("object", object)
480 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
482 user = User.get_cached_by_ap_id(data["actor"])
483 assert user.info.locked == true
486 test "it works for incoming deletes" do
487 activity = insert(:note_activity)
490 File.read!("test/fixtures/mastodon-delete.json")
495 |> Map.put("id", activity.data["object"])
499 |> Map.put("object", object)
500 |> Map.put("actor", activity.data["actor"])
502 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
504 refute Activity.get_by_id(activity.id)
507 test "it fails for incoming deletes with spoofed origin" do
508 activity = insert(:note_activity)
511 File.read!("test/fixtures/mastodon-delete.json")
516 |> Map.put("id", activity.data["object"])
520 |> Map.put("object", object)
522 assert capture_log(fn ->
523 :error = Transmogrifier.handle_incoming(data)
525 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
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)
1230 test "Rewrites Answers to Notes" do
1231 user = insert(:user)
1233 {:ok, poll_activity} =
1234 CommonAPI.post(user, %{
1235 "status" => "suya...",
1236 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1239 poll_object = Object.normalize(poll_activity)
1240 # TODO: Replace with CommonAPI vote creation when implemented
1242 File.read!("test/fixtures/mastodon-vote.json")
1244 |> Kernel.put_in(["to"], user.ap_id)
1245 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1246 |> Kernel.put_in(["object", "to"], user.ap_id)
1248 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1249 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1251 assert data["object"]["type"] == "Note"
1254 describe "fix_explicit_addressing" do
1256 user = insert(:user)
1260 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1261 explicitly_mentioned_actors = [
1262 "https://pleroma.gold/users/user1",
1263 "https://pleroma.gold/user2"
1267 "actor" => user.ap_id,
1268 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1271 Enum.map(explicitly_mentioned_actors, fn href ->
1272 %{"type" => "Mention", "href" => href}
1276 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1277 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1278 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1279 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1282 test "does not move actor's follower collection to cc", %{user: user} do
1284 "actor" => user.ap_id,
1285 "to" => [user.follower_address],
1289 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1290 assert user.follower_address in fixed_object["to"]
1291 refute user.follower_address in fixed_object["cc"]
1294 test "removes recipient's follower collection from cc", %{user: user} do
1295 recipient = insert(:user)
1298 "actor" => user.ap_id,
1299 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1300 "cc" => [user.follower_address, recipient.follower_address]
1303 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1305 assert user.follower_address in fixed_object["cc"]
1306 refute recipient.follower_address in fixed_object["cc"]
1307 refute recipient.follower_address in fixed_object["to"]