1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
9 alias Pleroma.Object.Fetcher
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 describe "handle_incoming" do
28 test "it ignores an incoming notice if we already have it" do
29 activity = insert(:note_activity)
32 File.read!("test/fixtures/mastodon-post-activity.json")
34 |> Map.put("object", Object.normalize(activity).data)
36 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
38 assert activity == returned_activity
41 test "it fetches replied-to activities if we don't have them" do
43 File.read!("test/fixtures/mastodon-post-activity.json")
48 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
50 data = Map.put(data, "object", object)
51 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
52 returned_object = Object.normalize(returned_activity, false)
55 Activity.get_create_by_object_ap_id(
56 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
59 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
62 test "it does not fetch replied-to activities beyond max_replies_depth" do
64 File.read!("test/fixtures/mastodon-post-activity.json")
69 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
71 data = Map.put(data, "object", object)
73 with_mock Pleroma.Web.Federator,
74 allowed_incoming_reply_depth?: fn _ -> false end do
75 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
77 returned_object = Object.normalize(returned_activity, false)
79 refute Activity.get_create_by_object_ap_id(
80 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
83 assert returned_object.data["inReplyToAtomUri"] ==
84 "https://shitposter.club/notice/2827873"
88 test "it does not crash if the object in inReplyTo can't be fetched" do
90 File.read!("test/fixtures/mastodon-post-activity.json")
95 |> Map.put("inReplyTo", "https://404.site/whatever")
99 |> Map.put("object", object)
101 assert capture_log(fn ->
102 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
103 end) =~ "[error] Couldn't fetch \"\"https://404.site/whatever\"\", error: nil"
106 test "it works for incoming notices" do
107 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
109 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
112 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
114 assert data["context"] ==
115 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
117 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
119 assert data["cc"] == [
120 "http://mastodon.example.org/users/admin/followers",
121 "http://localtesting.pleroma.lol/users/lain"
124 assert data["actor"] == "http://mastodon.example.org/users/admin"
126 object_data = Object.normalize(data["object"]).data
128 assert object_data["id"] ==
129 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
131 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
133 assert object_data["cc"] == [
134 "http://mastodon.example.org/users/admin/followers",
135 "http://localtesting.pleroma.lol/users/lain"
138 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
139 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
141 assert object_data["context"] ==
142 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
144 assert object_data["sensitive"] == true
146 user = User.get_cached_by_ap_id(object_data["actor"])
148 assert user.info.note_count == 1
151 test "it works for incoming notices with hashtags" do
152 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
154 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
155 object = Object.normalize(data["object"])
157 assert Enum.at(object.data["tag"], 2) == "moo"
160 test "it works for incoming questions" do
161 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
163 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
165 object = Object.normalize(activity)
167 assert Enum.all?(object.data["oneOf"], fn choice ->
170 "Everyone knows that!",
171 "25 char limit is dumb",
172 "I can't even fit a funny"
177 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
181 CommonAPI.post(user, %{
182 "status" => "suya...",
183 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
186 object = Object.normalize(activity)
189 File.read!("test/fixtures/mastodon-vote.json")
191 |> Kernel.put_in(["to"], user.ap_id)
192 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
193 |> Kernel.put_in(["object", "to"], user.ap_id)
195 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
196 answer_object = Object.normalize(activity)
197 assert answer_object.data["type"] == "Answer"
198 object = Object.get_by_ap_id(object.data["id"])
201 object.data["oneOf"],
203 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
209 test "it works for incoming notices with contentMap" do
211 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
213 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
214 object = Object.normalize(data["object"])
216 assert object.data["content"] ==
217 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
220 test "it works for incoming notices with to/cc not being an array (kroeg)" do
221 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
223 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
224 object = Object.normalize(data["object"])
226 assert object.data["content"] ==
227 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
230 test "it works for incoming announces with actor being inlined (kroeg)" do
231 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
233 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
235 assert data["actor"] == "https://puckipedia.com/"
238 test "it works for incoming notices with tag not being an array (kroeg)" do
239 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
241 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
242 object = Object.normalize(data["object"])
244 assert object.data["emoji"] == %{
245 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
248 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
250 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
251 object = Object.normalize(data["object"])
253 assert "test" in object.data["tag"]
256 test "it works for incoming notices with url not being a string (prismo)" do
257 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
259 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
260 object = Object.normalize(data["object"])
262 assert object.data["url"] == "https://prismo.news/posts/83"
265 test "it cleans up incoming notices which are not really DMs" do
267 other_user = insert(:user)
269 to = [user.ap_id, other_user.ap_id]
272 File.read!("test/fixtures/mastodon-post-activity.json")
282 data = Map.put(data, "object", object)
284 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
286 assert data["to"] == []
287 assert data["cc"] == to
289 object_data = Object.normalize(activity).data
291 assert object_data["to"] == []
292 assert object_data["cc"] == to
295 test "it works for incoming likes" do
297 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
300 File.read!("test/fixtures/mastodon-like.json")
302 |> Map.put("object", activity.data["object"])
304 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
306 assert data["actor"] == "http://mastodon.example.org/users/admin"
307 assert data["type"] == "Like"
308 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
309 assert data["object"] == activity.data["object"]
312 test "it returns an error for incoming unlikes wihout a like activity" do
314 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
317 File.read!("test/fixtures/mastodon-undo-like.json")
319 |> Map.put("object", activity.data["object"])
321 assert Transmogrifier.handle_incoming(data) == :error
324 test "it works for incoming unlikes with an existing like activity" do
326 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
329 File.read!("test/fixtures/mastodon-like.json")
331 |> Map.put("object", activity.data["object"])
333 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
336 File.read!("test/fixtures/mastodon-undo-like.json")
338 |> Map.put("object", like_data)
339 |> Map.put("actor", like_data["actor"])
341 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
343 assert data["actor"] == "http://mastodon.example.org/users/admin"
344 assert data["type"] == "Undo"
345 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
346 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
349 test "it works for incoming announces" do
350 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
352 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
354 assert data["actor"] == "http://mastodon.example.org/users/admin"
355 assert data["type"] == "Announce"
358 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
360 assert data["object"] ==
361 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
363 assert Activity.get_create_by_object_ap_id(data["object"])
366 test "it works for incoming announces with an existing activity" do
368 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
371 File.read!("test/fixtures/mastodon-announce.json")
373 |> Map.put("object", activity.data["object"])
375 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
377 assert data["actor"] == "http://mastodon.example.org/users/admin"
378 assert data["type"] == "Announce"
381 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
383 assert data["object"] == activity.data["object"]
385 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
388 test "it does not clobber the addressing on announce activities" do
390 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
393 File.read!("test/fixtures/mastodon-announce.json")
395 |> Map.put("object", Object.normalize(activity).data["id"])
396 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
399 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
401 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
404 test "it ensures that as:Public activities make it to their followers collection" do
408 File.read!("test/fixtures/mastodon-post-activity.json")
410 |> Map.put("actor", user.ap_id)
411 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
416 |> Map.put("attributedTo", user.ap_id)
417 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
419 |> Map.put("id", user.ap_id <> "/activities/12345678")
421 data = Map.put(data, "object", object)
423 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
425 assert data["cc"] == [User.ap_followers(user)]
428 test "it ensures that address fields become lists" do
432 File.read!("test/fixtures/mastodon-post-activity.json")
434 |> Map.put("actor", user.ap_id)
435 |> Map.put("to", nil)
436 |> Map.put("cc", nil)
440 |> Map.put("attributedTo", user.ap_id)
441 |> Map.put("to", nil)
442 |> Map.put("cc", nil)
443 |> Map.put("id", user.ap_id <> "/activities/12345678")
445 data = Map.put(data, "object", object)
447 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
449 assert !is_nil(data["to"])
450 assert !is_nil(data["cc"])
453 test "it strips internal likes" do
455 File.read!("test/fixtures/mastodon-post-activity.json")
460 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
461 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
463 "type" => "OrderedCollection"
466 object = Map.put(data["object"], "likes", likes)
467 data = Map.put(data, "object", object)
469 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
471 refute Map.has_key?(object.data, "likes")
474 test "it works for incoming update activities" do
475 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
477 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
478 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
481 update_data["object"]
482 |> Map.put("actor", data["actor"])
483 |> Map.put("id", data["actor"])
487 |> Map.put("actor", data["actor"])
488 |> Map.put("object", object)
490 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
492 user = User.get_cached_by_ap_id(data["actor"])
493 assert user.name == "gargle"
495 assert user.avatar["url"] == [
498 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
502 assert user.info.banner["url"] == [
505 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
509 assert user.bio == "<p>Some bio</p>"
512 test "it works with custom profile fields" do
514 "test/fixtures/mastodon-post-activity.json"
517 |> Transmogrifier.handle_incoming()
519 user = User.get_cached_by_ap_id(activity.actor)
521 assert User.Info.fields(user.info) == [
522 %{"name" => "foo", "value" => "bar"},
523 %{"name" => "foo1", "value" => "bar1"}
526 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
529 update_data["object"]
530 |> Map.put("actor", user.ap_id)
531 |> Map.put("id", user.ap_id)
535 |> Map.put("actor", user.ap_id)
536 |> Map.put("object", object)
538 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
540 user = User.get_cached_by_ap_id(user.ap_id)
542 assert User.Info.fields(user.info) == [
543 %{"name" => "foo", "value" => "updated"},
544 %{"name" => "foo1", "value" => "updated"}
547 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
550 put_in(update_data, ["object", "attachment"], [
551 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
552 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
553 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
556 {:ok, _} = Transmogrifier.handle_incoming(update_data)
558 user = User.get_cached_by_ap_id(user.ap_id)
560 assert User.Info.fields(user.info) == [
561 %{"name" => "foo", "value" => "updated"},
562 %{"name" => "foo1", "value" => "updated"}
566 test "it works for incoming update activities which lock the account" do
567 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
569 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
570 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
573 update_data["object"]
574 |> Map.put("actor", data["actor"])
575 |> Map.put("id", data["actor"])
576 |> Map.put("manuallyApprovesFollowers", true)
580 |> Map.put("actor", data["actor"])
581 |> Map.put("object", object)
583 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
585 user = User.get_cached_by_ap_id(data["actor"])
586 assert user.info.locked == true
589 test "it works for incoming deletes" do
590 activity = insert(:note_activity)
593 File.read!("test/fixtures/mastodon-delete.json")
598 |> Map.put("id", activity.data["object"])
602 |> Map.put("object", object)
603 |> Map.put("actor", activity.data["actor"])
605 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
607 refute Activity.get_by_id(activity.id)
610 test "it fails for incoming deletes with spoofed origin" do
611 activity = insert(:note_activity)
614 File.read!("test/fixtures/mastodon-delete.json")
619 |> Map.put("id", activity.data["object"])
623 |> Map.put("object", object)
625 assert capture_log(fn ->
626 :error = Transmogrifier.handle_incoming(data)
628 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
630 assert Activity.get_by_id(activity.id)
633 test "it works for incoming user deletes" do
634 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
637 File.read!("test/fixtures/mastodon-delete-user.json")
640 {:ok, _} = Transmogrifier.handle_incoming(data)
642 refute User.get_cached_by_ap_id(ap_id)
645 test "it fails for incoming user deletes with spoofed origin" do
646 %{ap_id: ap_id} = insert(:user)
649 File.read!("test/fixtures/mastodon-delete-user.json")
651 |> Map.put("actor", ap_id)
653 assert :error == Transmogrifier.handle_incoming(data)
654 assert User.get_cached_by_ap_id(ap_id)
657 test "it works for incoming unannounces with an existing notice" do
659 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
662 File.read!("test/fixtures/mastodon-announce.json")
664 |> Map.put("object", activity.data["object"])
666 {:ok, %Activity{data: announce_data, local: false}} =
667 Transmogrifier.handle_incoming(announce_data)
670 File.read!("test/fixtures/mastodon-undo-announce.json")
672 |> Map.put("object", announce_data)
673 |> Map.put("actor", announce_data["actor"])
675 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
677 assert data["type"] == "Undo"
678 assert object_data = data["object"]
679 assert object_data["type"] == "Announce"
680 assert object_data["object"] == activity.data["object"]
682 assert object_data["id"] ==
683 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
686 test "it works for incomming unfollows with an existing follow" do
690 File.read!("test/fixtures/mastodon-follow-activity.json")
692 |> Map.put("object", user.ap_id)
694 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
697 File.read!("test/fixtures/mastodon-unfollow-activity.json")
699 |> Map.put("object", follow_data)
701 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
703 assert data["type"] == "Undo"
704 assert data["object"]["type"] == "Follow"
705 assert data["object"]["object"] == user.ap_id
706 assert data["actor"] == "http://mastodon.example.org/users/admin"
708 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
711 test "it works for incoming blocks" do
715 File.read!("test/fixtures/mastodon-block-activity.json")
717 |> Map.put("object", user.ap_id)
719 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
721 assert data["type"] == "Block"
722 assert data["object"] == user.ap_id
723 assert data["actor"] == "http://mastodon.example.org/users/admin"
725 blocker = User.get_cached_by_ap_id(data["actor"])
727 assert User.blocks?(blocker, user)
730 test "incoming blocks successfully tear down any follow relationship" do
731 blocker = insert(:user)
732 blocked = insert(:user)
735 File.read!("test/fixtures/mastodon-block-activity.json")
737 |> Map.put("object", blocked.ap_id)
738 |> Map.put("actor", blocker.ap_id)
740 {:ok, blocker} = User.follow(blocker, blocked)
741 {:ok, blocked} = User.follow(blocked, blocker)
743 assert User.following?(blocker, blocked)
744 assert User.following?(blocked, blocker)
746 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
748 assert data["type"] == "Block"
749 assert data["object"] == blocked.ap_id
750 assert data["actor"] == blocker.ap_id
752 blocker = User.get_cached_by_ap_id(data["actor"])
753 blocked = User.get_cached_by_ap_id(data["object"])
755 assert User.blocks?(blocker, blocked)
757 refute User.following?(blocker, blocked)
758 refute User.following?(blocked, blocker)
761 test "it works for incoming unblocks with an existing block" do
765 File.read!("test/fixtures/mastodon-block-activity.json")
767 |> Map.put("object", user.ap_id)
769 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
772 File.read!("test/fixtures/mastodon-unblock-activity.json")
774 |> Map.put("object", block_data)
776 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
777 assert data["type"] == "Undo"
778 assert data["object"]["type"] == "Block"
779 assert data["object"]["object"] == user.ap_id
780 assert data["actor"] == "http://mastodon.example.org/users/admin"
782 blocker = User.get_cached_by_ap_id(data["actor"])
784 refute User.blocks?(blocker, user)
787 test "it works for incoming accepts which were pre-accepted" do
788 follower = insert(:user)
789 followed = insert(:user)
791 {:ok, follower} = User.follow(follower, followed)
792 assert User.following?(follower, followed) == true
794 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
797 File.read!("test/fixtures/mastodon-accept-activity.json")
799 |> Map.put("actor", followed.ap_id)
802 accept_data["object"]
803 |> Map.put("actor", follower.ap_id)
804 |> Map.put("id", follow_activity.data["id"])
806 accept_data = Map.put(accept_data, "object", object)
808 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
809 refute activity.local
811 assert activity.data["object"] == follow_activity.data["id"]
813 follower = User.get_cached_by_id(follower.id)
815 assert User.following?(follower, followed) == true
818 test "it works for incoming accepts which were orphaned" do
819 follower = insert(:user)
820 followed = insert(:user, %{info: %User.Info{locked: true}})
822 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
825 File.read!("test/fixtures/mastodon-accept-activity.json")
827 |> Map.put("actor", followed.ap_id)
830 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
832 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
833 assert activity.data["object"] == follow_activity.data["id"]
835 follower = User.get_cached_by_id(follower.id)
837 assert User.following?(follower, followed) == true
840 test "it works for incoming accepts which are referenced by IRI only" do
841 follower = insert(:user)
842 followed = insert(:user, %{info: %User.Info{locked: true}})
844 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
847 File.read!("test/fixtures/mastodon-accept-activity.json")
849 |> Map.put("actor", followed.ap_id)
850 |> Map.put("object", follow_activity.data["id"])
852 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
853 assert activity.data["object"] == follow_activity.data["id"]
855 follower = User.get_cached_by_id(follower.id)
857 assert User.following?(follower, followed) == true
860 test "it fails for incoming accepts which cannot be correlated" do
861 follower = insert(:user)
862 followed = insert(:user, %{info: %User.Info{locked: true}})
865 File.read!("test/fixtures/mastodon-accept-activity.json")
867 |> Map.put("actor", followed.ap_id)
870 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
872 :error = Transmogrifier.handle_incoming(accept_data)
874 follower = User.get_cached_by_id(follower.id)
876 refute User.following?(follower, followed) == true
879 test "it fails for incoming rejects which cannot be correlated" do
880 follower = insert(:user)
881 followed = insert(:user, %{info: %User.Info{locked: true}})
884 File.read!("test/fixtures/mastodon-reject-activity.json")
886 |> Map.put("actor", followed.ap_id)
889 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
891 :error = Transmogrifier.handle_incoming(accept_data)
893 follower = User.get_cached_by_id(follower.id)
895 refute User.following?(follower, followed) == true
898 test "it works for incoming rejects which are orphaned" do
899 follower = insert(:user)
900 followed = insert(:user, %{info: %User.Info{locked: true}})
902 {:ok, follower} = User.follow(follower, followed)
903 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
905 assert User.following?(follower, followed) == true
908 File.read!("test/fixtures/mastodon-reject-activity.json")
910 |> Map.put("actor", followed.ap_id)
913 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
915 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
916 refute activity.local
918 follower = User.get_cached_by_id(follower.id)
920 assert User.following?(follower, followed) == false
923 test "it works for incoming rejects which are referenced by IRI only" do
924 follower = insert(:user)
925 followed = insert(:user, %{info: %User.Info{locked: true}})
927 {:ok, follower} = User.follow(follower, followed)
928 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
930 assert User.following?(follower, followed) == true
933 File.read!("test/fixtures/mastodon-reject-activity.json")
935 |> Map.put("actor", followed.ap_id)
936 |> Map.put("object", follow_activity.data["id"])
938 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
940 follower = User.get_cached_by_id(follower.id)
942 assert User.following?(follower, followed) == false
945 test "it rejects activities without a valid ID" do
949 File.read!("test/fixtures/mastodon-follow-activity.json")
951 |> Map.put("object", user.ap_id)
954 :error = Transmogrifier.handle_incoming(data)
957 test "it remaps video URLs as attachments if necessary" do
959 Fetcher.fetch_object_from_id(
960 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
965 "mediaType" => "video/mp4",
967 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
968 "mimeType" => "video/mp4",
973 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
974 "mediaType" => "video/mp4",
981 assert object.data["url"] ==
982 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
984 assert object.data["attachment"] == [attachment]
987 test "it accepts Flag activities" do
989 other_user = insert(:user)
991 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
992 object = Object.normalize(activity)
995 "@context" => "https://www.w3.org/ns/activitystreams",
996 "cc" => [user.ap_id],
997 "object" => [user.ap_id, object.data["id"]],
999 "content" => "blocked AND reported!!!",
1000 "actor" => other_user.ap_id
1003 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1005 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1006 assert activity.data["content"] == "blocked AND reported!!!"
1007 assert activity.data["actor"] == other_user.ap_id
1008 assert activity.data["cc"] == [user.ap_id]
1012 describe "prepare outgoing" do
1013 test "it turns mentions into tags" do
1014 user = insert(:user)
1015 other_user = insert(:user)
1018 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1020 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1021 object = modified["object"]
1023 expected_mention = %{
1024 "href" => other_user.ap_id,
1025 "name" => "@#{other_user.nickname}",
1030 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1031 "type" => "Hashtag",
1035 assert Enum.member?(object["tag"], expected_tag)
1036 assert Enum.member?(object["tag"], expected_mention)
1039 test "it adds the sensitive property" do
1040 user = insert(:user)
1042 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1043 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1045 assert modified["object"]["sensitive"]
1048 test "it adds the json-ld context and the conversation property" do
1049 user = insert(:user)
1051 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1052 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1054 assert modified["@context"] ==
1055 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1057 assert modified["object"]["conversation"] == modified["context"]
1060 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1061 user = insert(:user)
1063 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1064 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1066 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1069 test "it translates ostatus IDs to external URLs" do
1070 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1071 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1073 user = insert(:user)
1075 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1076 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1078 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1081 test "it translates ostatus reply_to IDs to external URLs" do
1082 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1083 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1085 user = insert(:user)
1088 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1090 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1092 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1095 test "it strips internal hashtag data" do
1096 user = insert(:user)
1098 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1101 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1102 "type" => "Hashtag",
1106 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1108 assert modified["object"]["tag"] == [expected_tag]
1111 test "it strips internal fields" do
1112 user = insert(:user)
1114 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1116 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1118 assert length(modified["object"]["tag"]) == 2
1120 assert is_nil(modified["object"]["emoji"])
1121 assert is_nil(modified["object"]["like_count"])
1122 assert is_nil(modified["object"]["announcements"])
1123 assert is_nil(modified["object"]["announcement_count"])
1124 assert is_nil(modified["object"]["context_id"])
1127 test "it strips internal fields of article" do
1128 activity = insert(:article_activity)
1130 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1132 assert length(modified["object"]["tag"]) == 2
1134 assert is_nil(modified["object"]["emoji"])
1135 assert is_nil(modified["object"]["like_count"])
1136 assert is_nil(modified["object"]["announcements"])
1137 assert is_nil(modified["object"]["announcement_count"])
1138 assert is_nil(modified["object"]["context_id"])
1139 assert is_nil(modified["object"]["likes"])
1142 test "the directMessage flag is present" do
1143 user = insert(:user)
1144 other_user = insert(:user)
1146 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1148 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1150 assert modified["directMessage"] == false
1153 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1155 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1157 assert modified["directMessage"] == false
1160 CommonAPI.post(user, %{
1161 "status" => "@#{other_user.nickname} :moominmamma:",
1162 "visibility" => "direct"
1165 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1167 assert modified["directMessage"] == true
1170 test "it strips BCC field" do
1171 user = insert(:user)
1172 {:ok, list} = Pleroma.List.create("foo", user)
1175 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1177 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1179 assert is_nil(modified["bcc"])
1183 describe "user upgrade" do
1184 test "it upgrades a user to activitypub" do
1187 nickname: "rye@niu.moe",
1189 ap_id: "https://niu.moe/users/rye",
1190 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1193 user_two = insert(:user, %{following: [user.follower_address]})
1195 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1196 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1197 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1199 user = User.get_cached_by_id(user.id)
1200 assert user.info.note_count == 1
1202 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1203 assert user.info.ap_enabled
1204 assert user.info.note_count == 1
1205 assert user.follower_address == "https://niu.moe/users/rye/followers"
1206 assert user.following_address == "https://niu.moe/users/rye/following"
1208 user = User.get_cached_by_id(user.id)
1209 assert user.info.note_count == 1
1211 activity = Activity.get_by_id(activity.id)
1212 assert user.follower_address in activity.recipients
1218 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1227 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1230 } = user.info.banner
1232 refute "..." in activity.recipients
1234 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1235 refute user.follower_address in unrelated_activity.recipients
1237 user_two = User.get_cached_by_id(user_two.id)
1238 assert user.follower_address in user_two.following
1239 refute "..." in user_two.following
1243 describe "maybe_retire_websub" do
1244 test "it deletes all websub client subscripitions with the user as topic" do
1245 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1246 {:ok, ws} = Repo.insert(subscription)
1248 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1249 {:ok, ws2} = Repo.insert(subscription)
1251 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1253 refute Repo.get(WebsubClientSubscription, ws.id)
1254 assert Repo.get(WebsubClientSubscription, ws2.id)
1258 describe "actor rewriting" do
1259 test "it fixes the actor URL property to be a proper URI" do
1261 "url" => %{"href" => "http://example.com"}
1264 rewritten = Transmogrifier.maybe_fix_user_object(data)
1265 assert rewritten["url"] == "http://example.com"
1269 describe "actor origin containment" do
1270 test "it rejects activities which reference objects with bogus origins" do
1272 "@context" => "https://www.w3.org/ns/activitystreams",
1273 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1274 "actor" => "http://mastodon.example.org/users/admin",
1275 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1276 "object" => "https://info.pleroma.site/activity.json",
1277 "type" => "Announce"
1280 :error = Transmogrifier.handle_incoming(data)
1283 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1285 "@context" => "https://www.w3.org/ns/activitystreams",
1286 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1287 "actor" => "http://mastodon.example.org/users/admin",
1288 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1289 "object" => "https://info.pleroma.site/activity2.json",
1290 "type" => "Announce"
1293 :error = Transmogrifier.handle_incoming(data)
1296 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1298 "@context" => "https://www.w3.org/ns/activitystreams",
1299 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1300 "actor" => "http://mastodon.example.org/users/admin",
1301 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1302 "object" => "https://info.pleroma.site/activity3.json",
1303 "type" => "Announce"
1306 :error = Transmogrifier.handle_incoming(data)
1310 describe "reserialization" do
1311 test "successfully reserializes a message with inReplyTo == nil" do
1312 user = insert(:user)
1315 "@context" => "https://www.w3.org/ns/activitystreams",
1316 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1320 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1325 "attributedTo" => user.ap_id
1327 "actor" => user.ap_id
1330 {:ok, activity} = Transmogrifier.handle_incoming(message)
1332 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1335 test "successfully reserializes a message with AS2 objects in IR" do
1336 user = insert(:user)
1339 "@context" => "https://www.w3.org/ns/activitystreams",
1340 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1344 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1349 "attributedTo" => user.ap_id,
1351 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1352 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1355 "actor" => user.ap_id
1358 {:ok, activity} = Transmogrifier.handle_incoming(message)
1360 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1364 test "Rewrites Answers to Notes" do
1365 user = insert(:user)
1367 {:ok, poll_activity} =
1368 CommonAPI.post(user, %{
1369 "status" => "suya...",
1370 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1373 poll_object = Object.normalize(poll_activity)
1374 # TODO: Replace with CommonAPI vote creation when implemented
1376 File.read!("test/fixtures/mastodon-vote.json")
1378 |> Kernel.put_in(["to"], user.ap_id)
1379 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1380 |> Kernel.put_in(["object", "to"], user.ap_id)
1382 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1383 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1385 assert data["object"]["type"] == "Note"
1388 describe "fix_explicit_addressing" do
1390 user = insert(:user)
1394 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1395 explicitly_mentioned_actors = [
1396 "https://pleroma.gold/users/user1",
1397 "https://pleroma.gold/user2"
1401 "actor" => user.ap_id,
1402 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1405 Enum.map(explicitly_mentioned_actors, fn href ->
1406 %{"type" => "Mention", "href" => href}
1410 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1411 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1412 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1413 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1416 test "does not move actor's follower collection to cc", %{user: user} do
1418 "actor" => user.ap_id,
1419 "to" => [user.follower_address],
1423 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1424 assert user.follower_address in fixed_object["to"]
1425 refute user.follower_address in fixed_object["cc"]
1428 test "removes recipient's follower collection from cc", %{user: user} do
1429 recipient = insert(:user)
1432 "actor" => user.ap_id,
1433 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1434 "cc" => [user.follower_address, recipient.follower_address]
1437 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1439 assert user.follower_address in fixed_object["cc"]
1440 refute recipient.follower_address in fixed_object["cc"]
1441 refute recipient.follower_address in fixed_object["to"]