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.source_data["attachment"] == [
522 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
523 %{"name" => "foo1", "type" => "PropertyValue", "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.source_data["attachment"] == [
543 %{"name" => "foo", "type" => "PropertyValue", "value" => "updated"},
544 %{"name" => "foo1", "type" => "PropertyValue", "value" => "updated"}
548 test "it works for incoming update activities which lock the account" do
549 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
551 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
552 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
555 update_data["object"]
556 |> Map.put("actor", data["actor"])
557 |> Map.put("id", data["actor"])
558 |> Map.put("manuallyApprovesFollowers", true)
562 |> Map.put("actor", data["actor"])
563 |> Map.put("object", object)
565 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
567 user = User.get_cached_by_ap_id(data["actor"])
568 assert user.info.locked == true
571 test "it works for incoming deletes" do
572 activity = insert(:note_activity)
575 File.read!("test/fixtures/mastodon-delete.json")
580 |> Map.put("id", activity.data["object"])
584 |> Map.put("object", object)
585 |> Map.put("actor", activity.data["actor"])
587 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
589 refute Activity.get_by_id(activity.id)
592 test "it fails for incoming deletes with spoofed origin" do
593 activity = insert(:note_activity)
596 File.read!("test/fixtures/mastodon-delete.json")
601 |> Map.put("id", activity.data["object"])
605 |> Map.put("object", object)
607 assert capture_log(fn ->
608 :error = Transmogrifier.handle_incoming(data)
610 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
612 assert Activity.get_by_id(activity.id)
615 test "it works for incoming user deletes" do
616 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
619 File.read!("test/fixtures/mastodon-delete-user.json")
622 {:ok, _} = Transmogrifier.handle_incoming(data)
624 refute User.get_cached_by_ap_id(ap_id)
627 test "it fails for incoming user deletes with spoofed origin" do
628 %{ap_id: ap_id} = insert(:user)
631 File.read!("test/fixtures/mastodon-delete-user.json")
633 |> Map.put("actor", ap_id)
635 assert :error == Transmogrifier.handle_incoming(data)
636 assert User.get_cached_by_ap_id(ap_id)
639 test "it works for incoming unannounces with an existing notice" do
641 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
644 File.read!("test/fixtures/mastodon-announce.json")
646 |> Map.put("object", activity.data["object"])
648 {:ok, %Activity{data: announce_data, local: false}} =
649 Transmogrifier.handle_incoming(announce_data)
652 File.read!("test/fixtures/mastodon-undo-announce.json")
654 |> Map.put("object", announce_data)
655 |> Map.put("actor", announce_data["actor"])
657 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
659 assert data["type"] == "Undo"
660 assert object_data = data["object"]
661 assert object_data["type"] == "Announce"
662 assert object_data["object"] == activity.data["object"]
664 assert object_data["id"] ==
665 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
668 test "it works for incomming unfollows with an existing follow" do
672 File.read!("test/fixtures/mastodon-follow-activity.json")
674 |> Map.put("object", user.ap_id)
676 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
679 File.read!("test/fixtures/mastodon-unfollow-activity.json")
681 |> Map.put("object", follow_data)
683 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
685 assert data["type"] == "Undo"
686 assert data["object"]["type"] == "Follow"
687 assert data["object"]["object"] == user.ap_id
688 assert data["actor"] == "http://mastodon.example.org/users/admin"
690 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
693 test "it works for incoming blocks" do
697 File.read!("test/fixtures/mastodon-block-activity.json")
699 |> Map.put("object", user.ap_id)
701 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
703 assert data["type"] == "Block"
704 assert data["object"] == user.ap_id
705 assert data["actor"] == "http://mastodon.example.org/users/admin"
707 blocker = User.get_cached_by_ap_id(data["actor"])
709 assert User.blocks?(blocker, user)
712 test "incoming blocks successfully tear down any follow relationship" do
713 blocker = insert(:user)
714 blocked = insert(:user)
717 File.read!("test/fixtures/mastodon-block-activity.json")
719 |> Map.put("object", blocked.ap_id)
720 |> Map.put("actor", blocker.ap_id)
722 {:ok, blocker} = User.follow(blocker, blocked)
723 {:ok, blocked} = User.follow(blocked, blocker)
725 assert User.following?(blocker, blocked)
726 assert User.following?(blocked, blocker)
728 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
730 assert data["type"] == "Block"
731 assert data["object"] == blocked.ap_id
732 assert data["actor"] == blocker.ap_id
734 blocker = User.get_cached_by_ap_id(data["actor"])
735 blocked = User.get_cached_by_ap_id(data["object"])
737 assert User.blocks?(blocker, blocked)
739 refute User.following?(blocker, blocked)
740 refute User.following?(blocked, blocker)
743 test "it works for incoming unblocks with an existing block" do
747 File.read!("test/fixtures/mastodon-block-activity.json")
749 |> Map.put("object", user.ap_id)
751 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
754 File.read!("test/fixtures/mastodon-unblock-activity.json")
756 |> Map.put("object", block_data)
758 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
759 assert data["type"] == "Undo"
760 assert data["object"]["type"] == "Block"
761 assert data["object"]["object"] == user.ap_id
762 assert data["actor"] == "http://mastodon.example.org/users/admin"
764 blocker = User.get_cached_by_ap_id(data["actor"])
766 refute User.blocks?(blocker, user)
769 test "it works for incoming accepts which were pre-accepted" do
770 follower = insert(:user)
771 followed = insert(:user)
773 {:ok, follower} = User.follow(follower, followed)
774 assert User.following?(follower, followed) == true
776 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
779 File.read!("test/fixtures/mastodon-accept-activity.json")
781 |> Map.put("actor", followed.ap_id)
784 accept_data["object"]
785 |> Map.put("actor", follower.ap_id)
786 |> Map.put("id", follow_activity.data["id"])
788 accept_data = Map.put(accept_data, "object", object)
790 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
791 refute activity.local
793 assert activity.data["object"] == follow_activity.data["id"]
795 follower = User.get_cached_by_id(follower.id)
797 assert User.following?(follower, followed) == true
800 test "it works for incoming accepts which were orphaned" do
801 follower = insert(:user)
802 followed = insert(:user, %{info: %User.Info{locked: true}})
804 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
807 File.read!("test/fixtures/mastodon-accept-activity.json")
809 |> Map.put("actor", followed.ap_id)
812 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
814 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
815 assert activity.data["object"] == follow_activity.data["id"]
817 follower = User.get_cached_by_id(follower.id)
819 assert User.following?(follower, followed) == true
822 test "it works for incoming accepts which are referenced by IRI only" do
823 follower = insert(:user)
824 followed = insert(:user, %{info: %User.Info{locked: true}})
826 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
829 File.read!("test/fixtures/mastodon-accept-activity.json")
831 |> Map.put("actor", followed.ap_id)
832 |> Map.put("object", follow_activity.data["id"])
834 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
835 assert activity.data["object"] == follow_activity.data["id"]
837 follower = User.get_cached_by_id(follower.id)
839 assert User.following?(follower, followed) == true
842 test "it fails for incoming accepts which cannot be correlated" do
843 follower = insert(:user)
844 followed = insert(:user, %{info: %User.Info{locked: true}})
847 File.read!("test/fixtures/mastodon-accept-activity.json")
849 |> Map.put("actor", followed.ap_id)
852 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
854 :error = Transmogrifier.handle_incoming(accept_data)
856 follower = User.get_cached_by_id(follower.id)
858 refute User.following?(follower, followed) == true
861 test "it fails for incoming rejects which cannot be correlated" do
862 follower = insert(:user)
863 followed = insert(:user, %{info: %User.Info{locked: true}})
866 File.read!("test/fixtures/mastodon-reject-activity.json")
868 |> Map.put("actor", followed.ap_id)
871 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
873 :error = Transmogrifier.handle_incoming(accept_data)
875 follower = User.get_cached_by_id(follower.id)
877 refute User.following?(follower, followed) == true
880 test "it works for incoming rejects which are orphaned" do
881 follower = insert(:user)
882 followed = insert(:user, %{info: %User.Info{locked: true}})
884 {:ok, follower} = User.follow(follower, followed)
885 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
887 assert User.following?(follower, followed) == true
890 File.read!("test/fixtures/mastodon-reject-activity.json")
892 |> Map.put("actor", followed.ap_id)
895 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
897 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
898 refute activity.local
900 follower = User.get_cached_by_id(follower.id)
902 assert User.following?(follower, followed) == false
905 test "it works for incoming rejects which are referenced by IRI only" do
906 follower = insert(:user)
907 followed = insert(:user, %{info: %User.Info{locked: true}})
909 {:ok, follower} = User.follow(follower, followed)
910 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
912 assert User.following?(follower, followed) == true
915 File.read!("test/fixtures/mastodon-reject-activity.json")
917 |> Map.put("actor", followed.ap_id)
918 |> Map.put("object", follow_activity.data["id"])
920 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
922 follower = User.get_cached_by_id(follower.id)
924 assert User.following?(follower, followed) == false
927 test "it rejects activities without a valid ID" do
931 File.read!("test/fixtures/mastodon-follow-activity.json")
933 |> Map.put("object", user.ap_id)
936 :error = Transmogrifier.handle_incoming(data)
939 test "it remaps video URLs as attachments if necessary" do
941 Fetcher.fetch_object_from_id(
942 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
947 "mediaType" => "video/mp4",
949 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
950 "mimeType" => "video/mp4",
955 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
956 "mediaType" => "video/mp4",
963 assert object.data["url"] ==
964 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
966 assert object.data["attachment"] == [attachment]
969 test "it accepts Flag activities" do
971 other_user = insert(:user)
973 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
974 object = Object.normalize(activity)
977 "@context" => "https://www.w3.org/ns/activitystreams",
978 "cc" => [user.ap_id],
979 "object" => [user.ap_id, object.data["id"]],
981 "content" => "blocked AND reported!!!",
982 "actor" => other_user.ap_id
985 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
987 assert activity.data["object"] == [user.ap_id, object.data["id"]]
988 assert activity.data["content"] == "blocked AND reported!!!"
989 assert activity.data["actor"] == other_user.ap_id
990 assert activity.data["cc"] == [user.ap_id]
994 describe "prepare outgoing" do
995 test "it turns mentions into tags" do
997 other_user = insert(:user)
1000 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1002 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1003 object = modified["object"]
1005 expected_mention = %{
1006 "href" => other_user.ap_id,
1007 "name" => "@#{other_user.nickname}",
1012 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1013 "type" => "Hashtag",
1017 assert Enum.member?(object["tag"], expected_tag)
1018 assert Enum.member?(object["tag"], expected_mention)
1021 test "it adds the sensitive property" do
1022 user = insert(:user)
1024 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1025 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1027 assert modified["object"]["sensitive"]
1030 test "it adds the json-ld context and the conversation property" do
1031 user = insert(:user)
1033 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1034 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1036 assert modified["@context"] ==
1037 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1039 assert modified["object"]["conversation"] == modified["context"]
1042 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1043 user = insert(:user)
1045 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1046 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1048 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1051 test "it translates ostatus IDs to external URLs" do
1052 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1053 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1055 user = insert(:user)
1057 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1058 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1060 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1063 test "it translates ostatus reply_to IDs to external URLs" do
1064 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1065 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1067 user = insert(:user)
1070 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1072 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1074 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1077 test "it strips internal hashtag data" do
1078 user = insert(:user)
1080 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1083 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1084 "type" => "Hashtag",
1088 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1090 assert modified["object"]["tag"] == [expected_tag]
1093 test "it strips internal fields" do
1094 user = insert(:user)
1096 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1098 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1100 assert length(modified["object"]["tag"]) == 2
1102 assert is_nil(modified["object"]["emoji"])
1103 assert is_nil(modified["object"]["like_count"])
1104 assert is_nil(modified["object"]["announcements"])
1105 assert is_nil(modified["object"]["announcement_count"])
1106 assert is_nil(modified["object"]["context_id"])
1109 test "it strips internal fields of article" do
1110 activity = insert(:article_activity)
1112 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1114 assert length(modified["object"]["tag"]) == 2
1116 assert is_nil(modified["object"]["emoji"])
1117 assert is_nil(modified["object"]["like_count"])
1118 assert is_nil(modified["object"]["announcements"])
1119 assert is_nil(modified["object"]["announcement_count"])
1120 assert is_nil(modified["object"]["context_id"])
1121 assert is_nil(modified["object"]["likes"])
1124 test "the directMessage flag is present" do
1125 user = insert(:user)
1126 other_user = insert(:user)
1128 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1130 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1132 assert modified["directMessage"] == false
1135 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1137 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1139 assert modified["directMessage"] == false
1142 CommonAPI.post(user, %{
1143 "status" => "@#{other_user.nickname} :moominmamma:",
1144 "visibility" => "direct"
1147 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1149 assert modified["directMessage"] == true
1152 test "it strips BCC field" do
1153 user = insert(:user)
1154 {:ok, list} = Pleroma.List.create("foo", user)
1157 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1159 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1161 assert is_nil(modified["bcc"])
1165 describe "user upgrade" do
1166 test "it upgrades a user to activitypub" do
1169 nickname: "rye@niu.moe",
1171 ap_id: "https://niu.moe/users/rye",
1172 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1175 user_two = insert(:user, %{following: [user.follower_address]})
1177 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1178 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1179 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1181 user = User.get_cached_by_id(user.id)
1182 assert user.info.note_count == 1
1184 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1185 assert user.info.ap_enabled
1186 assert user.info.note_count == 1
1187 assert user.follower_address == "https://niu.moe/users/rye/followers"
1188 assert user.following_address == "https://niu.moe/users/rye/following"
1190 user = User.get_cached_by_id(user.id)
1191 assert user.info.note_count == 1
1193 activity = Activity.get_by_id(activity.id)
1194 assert user.follower_address in activity.recipients
1200 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1209 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1212 } = user.info.banner
1214 refute "..." in activity.recipients
1216 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1217 refute user.follower_address in unrelated_activity.recipients
1219 user_two = User.get_cached_by_id(user_two.id)
1220 assert user.follower_address in user_two.following
1221 refute "..." in user_two.following
1225 describe "maybe_retire_websub" do
1226 test "it deletes all websub client subscripitions with the user as topic" do
1227 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1228 {:ok, ws} = Repo.insert(subscription)
1230 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1231 {:ok, ws2} = Repo.insert(subscription)
1233 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1235 refute Repo.get(WebsubClientSubscription, ws.id)
1236 assert Repo.get(WebsubClientSubscription, ws2.id)
1240 describe "actor rewriting" do
1241 test "it fixes the actor URL property to be a proper URI" do
1243 "url" => %{"href" => "http://example.com"}
1246 rewritten = Transmogrifier.maybe_fix_user_object(data)
1247 assert rewritten["url"] == "http://example.com"
1251 describe "actor origin containment" do
1252 test "it rejects activities which reference objects with bogus origins" do
1254 "@context" => "https://www.w3.org/ns/activitystreams",
1255 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1256 "actor" => "http://mastodon.example.org/users/admin",
1257 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1258 "object" => "https://info.pleroma.site/activity.json",
1259 "type" => "Announce"
1262 :error = Transmogrifier.handle_incoming(data)
1265 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1267 "@context" => "https://www.w3.org/ns/activitystreams",
1268 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1269 "actor" => "http://mastodon.example.org/users/admin",
1270 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1271 "object" => "https://info.pleroma.site/activity2.json",
1272 "type" => "Announce"
1275 :error = Transmogrifier.handle_incoming(data)
1278 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1280 "@context" => "https://www.w3.org/ns/activitystreams",
1281 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1282 "actor" => "http://mastodon.example.org/users/admin",
1283 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1284 "object" => "https://info.pleroma.site/activity3.json",
1285 "type" => "Announce"
1288 :error = Transmogrifier.handle_incoming(data)
1292 describe "reserialization" do
1293 test "successfully reserializes a message with inReplyTo == nil" do
1294 user = insert(:user)
1297 "@context" => "https://www.w3.org/ns/activitystreams",
1298 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1302 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1307 "attributedTo" => user.ap_id
1309 "actor" => user.ap_id
1312 {:ok, activity} = Transmogrifier.handle_incoming(message)
1314 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1317 test "successfully reserializes a message with AS2 objects in IR" do
1318 user = insert(:user)
1321 "@context" => "https://www.w3.org/ns/activitystreams",
1322 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1326 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1331 "attributedTo" => user.ap_id,
1333 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1334 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1337 "actor" => user.ap_id
1340 {:ok, activity} = Transmogrifier.handle_incoming(message)
1342 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1346 test "Rewrites Answers to Notes" do
1347 user = insert(:user)
1349 {:ok, poll_activity} =
1350 CommonAPI.post(user, %{
1351 "status" => "suya...",
1352 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1355 poll_object = Object.normalize(poll_activity)
1356 # TODO: Replace with CommonAPI vote creation when implemented
1358 File.read!("test/fixtures/mastodon-vote.json")
1360 |> Kernel.put_in(["to"], user.ap_id)
1361 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1362 |> Kernel.put_in(["object", "to"], user.ap_id)
1364 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1365 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1367 assert data["object"]["type"] == "Note"
1370 describe "fix_explicit_addressing" do
1372 user = insert(:user)
1376 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1377 explicitly_mentioned_actors = [
1378 "https://pleroma.gold/users/user1",
1379 "https://pleroma.gold/user2"
1383 "actor" => user.ap_id,
1384 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1387 Enum.map(explicitly_mentioned_actors, fn href ->
1388 %{"type" => "Mention", "href" => href}
1392 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1393 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1394 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1395 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1398 test "does not move actor's follower collection to cc", %{user: user} do
1400 "actor" => user.ap_id,
1401 "to" => [user.follower_address],
1405 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1406 assert user.follower_address in fixed_object["to"]
1407 refute user.follower_address in fixed_object["cc"]
1410 test "removes recipient's follower collection from cc", %{user: user} do
1411 recipient = insert(:user)
1414 "actor" => user.ap_id,
1415 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1416 "cc" => [user.follower_address, recipient.follower_address]
1419 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1421 assert user.follower_address in fixed_object["cc"]
1422 refute recipient.follower_address in fixed_object["cc"]
1423 refute recipient.follower_address in fixed_object["to"]