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
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Transmogrifier
15 alias Pleroma.Web.CommonAPI
16 alias Pleroma.Web.OStatus
17 alias Pleroma.Web.Websub.WebsubClientSubscription
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 describe "handle_incoming" do
29 test "it ignores an incoming notice if we already have it" do
30 activity = insert(:note_activity)
33 File.read!("test/fixtures/mastodon-post-activity.json")
35 |> Map.put("object", Object.normalize(activity).data)
37 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
39 assert activity == returned_activity
42 test "it fetches replied-to activities if we don't have them" do
44 File.read!("test/fixtures/mastodon-post-activity.json")
49 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
51 data = Map.put(data, "object", object)
52 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
53 returned_object = Object.normalize(returned_activity, false)
56 Activity.get_create_by_object_ap_id(
57 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
60 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
63 test "it does not fetch replied-to activities beyond max_replies_depth" do
65 File.read!("test/fixtures/mastodon-post-activity.json")
70 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
72 data = Map.put(data, "object", object)
74 with_mock Pleroma.Web.Federator,
75 allowed_incoming_reply_depth?: fn _ -> false end do
76 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
78 returned_object = Object.normalize(returned_activity, false)
80 refute Activity.get_create_by_object_ap_id(
81 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
84 assert returned_object.data["inReplyToAtomUri"] ==
85 "https://shitposter.club/notice/2827873"
89 test "it does not crash if the object in inReplyTo can't be fetched" do
91 File.read!("test/fixtures/mastodon-post-activity.json")
96 |> Map.put("inReplyTo", "https://404.site/whatever")
100 |> Map.put("object", object)
102 assert capture_log(fn ->
103 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
104 end) =~ "[error] Couldn't fetch \"\"https://404.site/whatever\"\", error: nil"
107 test "it works for incoming notices" do
108 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
113 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
115 assert data["context"] ==
116 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
118 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
120 assert data["cc"] == [
121 "http://mastodon.example.org/users/admin/followers",
122 "http://localtesting.pleroma.lol/users/lain"
125 assert data["actor"] == "http://mastodon.example.org/users/admin"
127 object_data = Object.normalize(data["object"]).data
129 assert object_data["id"] ==
130 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
132 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
134 assert object_data["cc"] == [
135 "http://mastodon.example.org/users/admin/followers",
136 "http://localtesting.pleroma.lol/users/lain"
139 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
140 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
142 assert object_data["context"] ==
143 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
145 assert object_data["sensitive"] == true
147 user = User.get_cached_by_ap_id(object_data["actor"])
149 assert user.info.note_count == 1
152 test "it works for incoming notices with hashtags" do
153 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
155 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
156 object = Object.normalize(data["object"])
158 assert Enum.at(object.data["tag"], 2) == "moo"
161 test "it works for incoming questions" do
162 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
164 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
166 object = Object.normalize(activity)
168 assert Enum.all?(object.data["oneOf"], fn choice ->
171 "Everyone knows that!",
172 "25 char limit is dumb",
173 "I can't even fit a funny"
178 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
182 CommonAPI.post(user, %{
183 "status" => "suya...",
184 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
187 object = Object.normalize(activity)
190 File.read!("test/fixtures/mastodon-vote.json")
192 |> Kernel.put_in(["to"], user.ap_id)
193 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
194 |> Kernel.put_in(["object", "to"], user.ap_id)
196 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
197 answer_object = Object.normalize(activity)
198 assert answer_object.data["type"] == "Answer"
199 object = Object.get_by_ap_id(object.data["id"])
202 object.data["oneOf"],
204 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
210 test "it works for incoming notices with contentMap" do
212 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
214 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
215 object = Object.normalize(data["object"])
217 assert object.data["content"] ==
218 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
221 test "it works for incoming notices with to/cc not being an array (kroeg)" do
222 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
224 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
225 object = Object.normalize(data["object"])
227 assert object.data["content"] ==
228 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
231 test "it works for incoming announces with actor being inlined (kroeg)" do
232 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
234 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
236 assert data["actor"] == "https://puckipedia.com/"
239 test "it works for incoming notices with tag not being an array (kroeg)" do
240 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
242 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
243 object = Object.normalize(data["object"])
245 assert object.data["emoji"] == %{
246 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
249 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
251 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
252 object = Object.normalize(data["object"])
254 assert "test" in object.data["tag"]
257 test "it works for incoming notices with url not being a string (prismo)" do
258 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
260 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
261 object = Object.normalize(data["object"])
263 assert object.data["url"] == "https://prismo.news/posts/83"
266 test "it cleans up incoming notices which are not really DMs" do
268 other_user = insert(:user)
270 to = [user.ap_id, other_user.ap_id]
273 File.read!("test/fixtures/mastodon-post-activity.json")
283 data = Map.put(data, "object", object)
285 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
287 assert data["to"] == []
288 assert data["cc"] == to
290 object_data = Object.normalize(activity).data
292 assert object_data["to"] == []
293 assert object_data["cc"] == to
296 test "it works for incoming likes" do
298 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
301 File.read!("test/fixtures/mastodon-like.json")
303 |> Map.put("object", activity.data["object"])
305 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
307 assert data["actor"] == "http://mastodon.example.org/users/admin"
308 assert data["type"] == "Like"
309 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
310 assert data["object"] == activity.data["object"]
313 test "it returns an error for incoming unlikes wihout a like activity" do
315 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
318 File.read!("test/fixtures/mastodon-undo-like.json")
320 |> Map.put("object", activity.data["object"])
322 assert Transmogrifier.handle_incoming(data) == :error
325 test "it works for incoming unlikes with an existing like activity" do
327 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
330 File.read!("test/fixtures/mastodon-like.json")
332 |> Map.put("object", activity.data["object"])
334 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
337 File.read!("test/fixtures/mastodon-undo-like.json")
339 |> Map.put("object", like_data)
340 |> Map.put("actor", like_data["actor"])
342 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
344 assert data["actor"] == "http://mastodon.example.org/users/admin"
345 assert data["type"] == "Undo"
346 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
347 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
350 test "it works for incoming announces" do
351 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
353 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
355 assert data["actor"] == "http://mastodon.example.org/users/admin"
356 assert data["type"] == "Announce"
359 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
361 assert data["object"] ==
362 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
364 assert Activity.get_create_by_object_ap_id(data["object"])
367 test "it works for incoming announces with an existing activity" do
369 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
372 File.read!("test/fixtures/mastodon-announce.json")
374 |> Map.put("object", activity.data["object"])
376 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
378 assert data["actor"] == "http://mastodon.example.org/users/admin"
379 assert data["type"] == "Announce"
382 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
384 assert data["object"] == activity.data["object"]
386 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
389 test "it does not clobber the addressing on announce activities" do
391 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
394 File.read!("test/fixtures/mastodon-announce.json")
396 |> Map.put("object", Object.normalize(activity).data["id"])
397 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
400 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
402 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
405 test "it ensures that as:Public activities make it to their followers collection" do
409 File.read!("test/fixtures/mastodon-post-activity.json")
411 |> Map.put("actor", user.ap_id)
412 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
417 |> Map.put("attributedTo", user.ap_id)
418 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
420 |> Map.put("id", user.ap_id <> "/activities/12345678")
422 data = Map.put(data, "object", object)
424 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
426 assert data["cc"] == [User.ap_followers(user)]
429 test "it ensures that address fields become lists" do
433 File.read!("test/fixtures/mastodon-post-activity.json")
435 |> Map.put("actor", user.ap_id)
436 |> Map.put("to", nil)
437 |> Map.put("cc", nil)
441 |> Map.put("attributedTo", user.ap_id)
442 |> Map.put("to", nil)
443 |> Map.put("cc", nil)
444 |> Map.put("id", user.ap_id <> "/activities/12345678")
446 data = Map.put(data, "object", object)
448 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
450 assert !is_nil(data["to"])
451 assert !is_nil(data["cc"])
454 test "it works for incoming update activities" do
455 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
457 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
458 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
461 update_data["object"]
462 |> Map.put("actor", data["actor"])
463 |> Map.put("id", data["actor"])
467 |> Map.put("actor", data["actor"])
468 |> Map.put("object", object)
470 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
472 user = User.get_cached_by_ap_id(data["actor"])
473 assert user.name == "gargle"
475 assert user.avatar["url"] == [
478 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
482 assert user.info.banner["url"] == [
485 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
489 assert user.bio == "<p>Some bio</p>"
492 test "it works for incoming update activities which lock the account" do
493 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
495 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
496 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
499 update_data["object"]
500 |> Map.put("actor", data["actor"])
501 |> Map.put("id", data["actor"])
502 |> Map.put("manuallyApprovesFollowers", true)
506 |> Map.put("actor", data["actor"])
507 |> Map.put("object", object)
509 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
511 user = User.get_cached_by_ap_id(data["actor"])
512 assert user.info.locked == true
515 test "it works for incoming deletes" do
516 activity = insert(:note_activity)
519 File.read!("test/fixtures/mastodon-delete.json")
524 |> Map.put("id", activity.data["object"])
528 |> Map.put("object", object)
529 |> Map.put("actor", activity.data["actor"])
531 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
533 refute Activity.get_by_id(activity.id)
536 test "it fails for incoming deletes with spoofed origin" do
537 activity = insert(:note_activity)
540 File.read!("test/fixtures/mastodon-delete.json")
545 |> Map.put("id", activity.data["object"])
549 |> Map.put("object", object)
551 assert capture_log(fn ->
552 :error = Transmogrifier.handle_incoming(data)
554 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
556 assert Activity.get_by_id(activity.id)
559 test "it works for incoming user deletes" do
560 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
563 File.read!("test/fixtures/mastodon-delete-user.json")
566 {:ok, _} = Transmogrifier.handle_incoming(data)
567 ObanHelpers.perform_all()
569 refute User.get_cached_by_ap_id(ap_id)
572 test "it fails for incoming user deletes with spoofed origin" do
573 %{ap_id: ap_id} = insert(:user)
576 File.read!("test/fixtures/mastodon-delete-user.json")
578 |> Map.put("actor", ap_id)
580 assert :error == Transmogrifier.handle_incoming(data)
581 assert User.get_cached_by_ap_id(ap_id)
584 test "it works for incoming unannounces with an existing notice" do
586 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
589 File.read!("test/fixtures/mastodon-announce.json")
591 |> Map.put("object", activity.data["object"])
593 {:ok, %Activity{data: announce_data, local: false}} =
594 Transmogrifier.handle_incoming(announce_data)
597 File.read!("test/fixtures/mastodon-undo-announce.json")
599 |> Map.put("object", announce_data)
600 |> Map.put("actor", announce_data["actor"])
602 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
604 assert data["type"] == "Undo"
605 assert object_data = data["object"]
606 assert object_data["type"] == "Announce"
607 assert object_data["object"] == activity.data["object"]
609 assert object_data["id"] ==
610 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
613 test "it works for incomming unfollows with an existing follow" do
617 File.read!("test/fixtures/mastodon-follow-activity.json")
619 |> Map.put("object", user.ap_id)
621 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
624 File.read!("test/fixtures/mastodon-unfollow-activity.json")
626 |> Map.put("object", follow_data)
628 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
630 assert data["type"] == "Undo"
631 assert data["object"]["type"] == "Follow"
632 assert data["object"]["object"] == user.ap_id
633 assert data["actor"] == "http://mastodon.example.org/users/admin"
635 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
638 test "it works for incoming blocks" do
642 File.read!("test/fixtures/mastodon-block-activity.json")
644 |> Map.put("object", user.ap_id)
646 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
648 assert data["type"] == "Block"
649 assert data["object"] == user.ap_id
650 assert data["actor"] == "http://mastodon.example.org/users/admin"
652 blocker = User.get_cached_by_ap_id(data["actor"])
654 assert User.blocks?(blocker, user)
657 test "incoming blocks successfully tear down any follow relationship" do
658 blocker = insert(:user)
659 blocked = insert(:user)
662 File.read!("test/fixtures/mastodon-block-activity.json")
664 |> Map.put("object", blocked.ap_id)
665 |> Map.put("actor", blocker.ap_id)
667 {:ok, blocker} = User.follow(blocker, blocked)
668 {:ok, blocked} = User.follow(blocked, blocker)
670 assert User.following?(blocker, blocked)
671 assert User.following?(blocked, blocker)
673 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
675 assert data["type"] == "Block"
676 assert data["object"] == blocked.ap_id
677 assert data["actor"] == blocker.ap_id
679 blocker = User.get_cached_by_ap_id(data["actor"])
680 blocked = User.get_cached_by_ap_id(data["object"])
682 assert User.blocks?(blocker, blocked)
684 refute User.following?(blocker, blocked)
685 refute User.following?(blocked, blocker)
688 test "it works for incoming unblocks with an existing block" do
692 File.read!("test/fixtures/mastodon-block-activity.json")
694 |> Map.put("object", user.ap_id)
696 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
699 File.read!("test/fixtures/mastodon-unblock-activity.json")
701 |> Map.put("object", block_data)
703 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
704 assert data["type"] == "Undo"
705 assert data["object"]["type"] == "Block"
706 assert data["object"]["object"] == user.ap_id
707 assert data["actor"] == "http://mastodon.example.org/users/admin"
709 blocker = User.get_cached_by_ap_id(data["actor"])
711 refute User.blocks?(blocker, user)
714 test "it works for incoming accepts which were pre-accepted" do
715 follower = insert(:user)
716 followed = insert(:user)
718 {:ok, follower} = User.follow(follower, followed)
719 assert User.following?(follower, followed) == true
721 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
724 File.read!("test/fixtures/mastodon-accept-activity.json")
726 |> Map.put("actor", followed.ap_id)
729 accept_data["object"]
730 |> Map.put("actor", follower.ap_id)
731 |> Map.put("id", follow_activity.data["id"])
733 accept_data = Map.put(accept_data, "object", object)
735 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
736 refute activity.local
738 assert activity.data["object"] == follow_activity.data["id"]
740 follower = User.get_cached_by_id(follower.id)
742 assert User.following?(follower, followed) == true
745 test "it works for incoming accepts which were orphaned" do
746 follower = insert(:user)
747 followed = insert(:user, %{info: %User.Info{locked: true}})
749 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
752 File.read!("test/fixtures/mastodon-accept-activity.json")
754 |> Map.put("actor", followed.ap_id)
757 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
759 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
760 assert activity.data["object"] == follow_activity.data["id"]
762 follower = User.get_cached_by_id(follower.id)
764 assert User.following?(follower, followed) == true
767 test "it works for incoming accepts which are referenced by IRI only" do
768 follower = insert(:user)
769 followed = insert(:user, %{info: %User.Info{locked: true}})
771 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
774 File.read!("test/fixtures/mastodon-accept-activity.json")
776 |> Map.put("actor", followed.ap_id)
777 |> Map.put("object", follow_activity.data["id"])
779 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
780 assert activity.data["object"] == follow_activity.data["id"]
782 follower = User.get_cached_by_id(follower.id)
784 assert User.following?(follower, followed) == true
787 test "it fails for incoming accepts which cannot be correlated" do
788 follower = insert(:user)
789 followed = insert(:user, %{info: %User.Info{locked: true}})
792 File.read!("test/fixtures/mastodon-accept-activity.json")
794 |> Map.put("actor", followed.ap_id)
797 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
799 :error = Transmogrifier.handle_incoming(accept_data)
801 follower = User.get_cached_by_id(follower.id)
803 refute User.following?(follower, followed) == true
806 test "it fails for incoming rejects which cannot be correlated" do
807 follower = insert(:user)
808 followed = insert(:user, %{info: %User.Info{locked: true}})
811 File.read!("test/fixtures/mastodon-reject-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 works for incoming rejects which are orphaned" do
826 follower = insert(:user)
827 followed = insert(:user, %{info: %User.Info{locked: true}})
829 {:ok, follower} = User.follow(follower, followed)
830 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
832 assert User.following?(follower, followed) == true
835 File.read!("test/fixtures/mastodon-reject-activity.json")
837 |> Map.put("actor", followed.ap_id)
840 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
842 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
843 refute activity.local
845 follower = User.get_cached_by_id(follower.id)
847 assert User.following?(follower, followed) == false
850 test "it works for incoming rejects which are referenced by IRI only" do
851 follower = insert(:user)
852 followed = insert(:user, %{info: %User.Info{locked: true}})
854 {:ok, follower} = User.follow(follower, followed)
855 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
857 assert User.following?(follower, followed) == true
860 File.read!("test/fixtures/mastodon-reject-activity.json")
862 |> Map.put("actor", followed.ap_id)
863 |> Map.put("object", follow_activity.data["id"])
865 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
867 follower = User.get_cached_by_id(follower.id)
869 assert User.following?(follower, followed) == false
872 test "it rejects activities without a valid ID" do
876 File.read!("test/fixtures/mastodon-follow-activity.json")
878 |> Map.put("object", user.ap_id)
881 :error = Transmogrifier.handle_incoming(data)
884 test "it remaps video URLs as attachments if necessary" do
886 Fetcher.fetch_object_from_id(
887 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
892 "mediaType" => "video/mp4",
894 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
895 "mimeType" => "video/mp4",
900 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
901 "mediaType" => "video/mp4",
908 assert object.data["url"] ==
909 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
911 assert object.data["attachment"] == [attachment]
914 test "it accepts Flag activities" do
916 other_user = insert(:user)
918 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
919 object = Object.normalize(activity)
922 "@context" => "https://www.w3.org/ns/activitystreams",
923 "cc" => [user.ap_id],
924 "object" => [user.ap_id, object.data["id"]],
926 "content" => "blocked AND reported!!!",
927 "actor" => other_user.ap_id
930 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
932 assert activity.data["object"] == [user.ap_id, object.data["id"]]
933 assert activity.data["content"] == "blocked AND reported!!!"
934 assert activity.data["actor"] == other_user.ap_id
935 assert activity.data["cc"] == [user.ap_id]
939 describe "prepare outgoing" do
940 test "it turns mentions into tags" do
942 other_user = insert(:user)
945 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
947 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
948 object = modified["object"]
950 expected_mention = %{
951 "href" => other_user.ap_id,
952 "name" => "@#{other_user.nickname}",
957 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
962 assert Enum.member?(object["tag"], expected_tag)
963 assert Enum.member?(object["tag"], expected_mention)
966 test "it adds the sensitive property" do
969 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
970 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
972 assert modified["object"]["sensitive"]
975 test "it adds the json-ld context and the conversation property" do
978 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
979 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
981 assert modified["@context"] ==
982 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
984 assert modified["object"]["conversation"] == modified["context"]
987 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
990 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
991 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
993 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
996 test "it translates ostatus IDs to external URLs" do
997 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
998 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1000 user = insert(:user)
1002 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1003 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1005 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1008 test "it translates ostatus reply_to IDs to external URLs" do
1009 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1010 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1012 user = insert(:user)
1015 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1017 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1019 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1022 test "it strips internal hashtag data" do
1023 user = insert(:user)
1025 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1028 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1029 "type" => "Hashtag",
1033 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1035 assert modified["object"]["tag"] == [expected_tag]
1038 test "it strips internal fields" do
1039 user = insert(:user)
1041 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1043 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1045 assert length(modified["object"]["tag"]) == 2
1047 assert is_nil(modified["object"]["emoji"])
1048 assert is_nil(modified["object"]["like_count"])
1049 assert is_nil(modified["object"]["announcements"])
1050 assert is_nil(modified["object"]["announcement_count"])
1051 assert is_nil(modified["object"]["context_id"])
1054 test "it strips internal fields of article" do
1055 activity = insert(:article_activity)
1057 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1059 assert length(modified["object"]["tag"]) == 2
1061 assert is_nil(modified["object"]["emoji"])
1062 assert is_nil(modified["object"]["like_count"])
1063 assert is_nil(modified["object"]["announcements"])
1064 assert is_nil(modified["object"]["announcement_count"])
1065 assert is_nil(modified["object"]["context_id"])
1068 test "it adds like collection to object" do
1069 activity = insert(:note_activity)
1070 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1072 assert modified["object"]["likes"]["type"] == "OrderedCollection"
1073 assert modified["object"]["likes"]["totalItems"] == 0
1076 test "the directMessage flag is present" do
1077 user = insert(:user)
1078 other_user = insert(:user)
1080 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1082 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1084 assert modified["directMessage"] == false
1087 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1089 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1091 assert modified["directMessage"] == false
1094 CommonAPI.post(user, %{
1095 "status" => "@#{other_user.nickname} :moominmamma:",
1096 "visibility" => "direct"
1099 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1101 assert modified["directMessage"] == true
1104 test "it strips BCC field" do
1105 user = insert(:user)
1106 {:ok, list} = Pleroma.List.create("foo", user)
1109 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1111 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1113 assert is_nil(modified["bcc"])
1117 describe "user upgrade" do
1118 test "it upgrades a user to activitypub" do
1121 nickname: "rye@niu.moe",
1123 ap_id: "https://niu.moe/users/rye",
1124 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1127 user_two = insert(:user, %{following: [user.follower_address]})
1129 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1130 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1131 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1133 user = User.get_cached_by_id(user.id)
1134 assert user.info.note_count == 1
1136 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1137 ObanHelpers.perform_all()
1139 assert user.info.ap_enabled
1140 assert user.info.note_count == 1
1141 assert user.follower_address == "https://niu.moe/users/rye/followers"
1142 assert user.following_address == "https://niu.moe/users/rye/following"
1144 user = User.get_cached_by_id(user.id)
1145 assert user.info.note_count == 1
1147 activity = Activity.get_by_id(activity.id)
1148 assert user.follower_address in activity.recipients
1154 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1163 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1166 } = user.info.banner
1168 refute "..." in activity.recipients
1170 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1171 refute user.follower_address in unrelated_activity.recipients
1173 user_two = User.get_cached_by_id(user_two.id)
1174 assert user.follower_address in user_two.following
1175 refute "..." in user_two.following
1179 describe "maybe_retire_websub" do
1180 test "it deletes all websub client subscripitions with the user as topic" do
1181 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1182 {:ok, ws} = Repo.insert(subscription)
1184 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1185 {:ok, ws2} = Repo.insert(subscription)
1187 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1189 refute Repo.get(WebsubClientSubscription, ws.id)
1190 assert Repo.get(WebsubClientSubscription, ws2.id)
1194 describe "actor rewriting" do
1195 test "it fixes the actor URL property to be a proper URI" do
1197 "url" => %{"href" => "http://example.com"}
1200 rewritten = Transmogrifier.maybe_fix_user_object(data)
1201 assert rewritten["url"] == "http://example.com"
1205 describe "actor origin containment" do
1206 test "it rejects activities which reference objects with bogus origins" do
1208 "@context" => "https://www.w3.org/ns/activitystreams",
1209 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1210 "actor" => "http://mastodon.example.org/users/admin",
1211 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1212 "object" => "https://info.pleroma.site/activity.json",
1213 "type" => "Announce"
1216 :error = Transmogrifier.handle_incoming(data)
1219 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1221 "@context" => "https://www.w3.org/ns/activitystreams",
1222 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1223 "actor" => "http://mastodon.example.org/users/admin",
1224 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1225 "object" => "https://info.pleroma.site/activity2.json",
1226 "type" => "Announce"
1229 :error = Transmogrifier.handle_incoming(data)
1232 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1234 "@context" => "https://www.w3.org/ns/activitystreams",
1235 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1236 "actor" => "http://mastodon.example.org/users/admin",
1237 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1238 "object" => "https://info.pleroma.site/activity3.json",
1239 "type" => "Announce"
1242 :error = Transmogrifier.handle_incoming(data)
1246 describe "reserialization" do
1247 test "successfully reserializes a message with inReplyTo == nil" do
1248 user = insert(:user)
1251 "@context" => "https://www.w3.org/ns/activitystreams",
1252 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1256 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1261 "attributedTo" => user.ap_id
1263 "actor" => user.ap_id
1266 {:ok, activity} = Transmogrifier.handle_incoming(message)
1268 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1271 test "successfully reserializes a message with AS2 objects in IR" do
1272 user = insert(:user)
1275 "@context" => "https://www.w3.org/ns/activitystreams",
1276 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1280 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1285 "attributedTo" => user.ap_id,
1287 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1288 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1291 "actor" => user.ap_id
1294 {:ok, activity} = Transmogrifier.handle_incoming(message)
1296 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1300 test "Rewrites Answers to Notes" do
1301 user = insert(:user)
1303 {:ok, poll_activity} =
1304 CommonAPI.post(user, %{
1305 "status" => "suya...",
1306 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1309 poll_object = Object.normalize(poll_activity)
1310 # TODO: Replace with CommonAPI vote creation when implemented
1312 File.read!("test/fixtures/mastodon-vote.json")
1314 |> Kernel.put_in(["to"], user.ap_id)
1315 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1316 |> Kernel.put_in(["object", "to"], user.ap_id)
1318 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1319 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1321 assert data["object"]["type"] == "Note"
1324 describe "fix_explicit_addressing" do
1326 user = insert(:user)
1330 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1331 explicitly_mentioned_actors = [
1332 "https://pleroma.gold/users/user1",
1333 "https://pleroma.gold/user2"
1337 "actor" => user.ap_id,
1338 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1341 Enum.map(explicitly_mentioned_actors, fn href ->
1342 %{"type" => "Mention", "href" => href}
1346 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1347 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1348 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1349 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1352 test "does not move actor's follower collection to cc", %{user: user} do
1354 "actor" => user.ap_id,
1355 "to" => [user.follower_address],
1359 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1360 assert user.follower_address in fixed_object["to"]
1361 refute user.follower_address in fixed_object["cc"]
1364 test "removes recipient's follower collection from cc", %{user: user} do
1365 recipient = insert(:user)
1368 "actor" => user.ap_id,
1369 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1370 "cc" => [user.follower_address, recipient.follower_address]
1373 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1375 assert user.follower_address in fixed_object["cc"]
1376 refute recipient.follower_address in fixed_object["cc"]
1377 refute recipient.follower_address in fixed_object["to"]