1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
9 alias Pleroma.Object.Fetcher
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
18 import Pleroma.Factory
19 alias Pleroma.Web.CommonAPI
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 describe "handle_incoming" do
27 test "it ignores an incoming notice if we already have it" do
28 activity = insert(:note_activity)
31 File.read!("test/fixtures/mastodon-post-activity.json")
33 |> Map.put("object", activity.data["object"])
35 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
37 assert activity == returned_activity
40 test "it fetches replied-to activities if we don't have them" do
42 File.read!("test/fixtures/mastodon-post-activity.json")
47 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
51 |> Map.put("object", object)
53 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 returned_object = Object.normalize(returned_activity.data["object"])
57 Activity.get_create_by_object_ap_id(
58 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
61 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
64 test "it works for incoming notices" do
65 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
67 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
70 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
72 assert data["context"] ==
73 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
75 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
77 assert data["cc"] == [
78 "http://mastodon.example.org/users/admin/followers",
79 "http://localtesting.pleroma.lol/users/lain"
82 assert data["actor"] == "http://mastodon.example.org/users/admin"
84 object = Object.normalize(data["object"]).data
85 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
87 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
89 assert object["cc"] == [
90 "http://mastodon.example.org/users/admin/followers",
91 "http://localtesting.pleroma.lol/users/lain"
94 assert object["actor"] == "http://mastodon.example.org/users/admin"
95 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
97 assert object["context"] ==
98 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
100 assert object["sensitive"] == true
102 user = User.get_by_ap_id(object["actor"])
104 assert user.info.note_count == 1
107 test "it works for incoming notices with hashtags" do
108 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
111 object = Object.normalize(data["object"])
113 assert Enum.at(object.data["tag"], 2) == "moo"
116 test "it works for incoming notices with contentMap" do
118 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
120 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
121 object = Object.normalize(data["object"])
123 assert object.data["content"] ==
124 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
127 test "it works for incoming notices with to/cc not being an array (kroeg)" do
128 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
130 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
131 object = Object.normalize(data["object"])
133 assert object.data["content"] ==
134 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
137 test "it works for incoming announces with actor being inlined (kroeg)" do
138 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
140 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
142 assert data["actor"] == "https://puckipedia.com/"
145 test "it works for incoming notices with tag not being an array (kroeg)" do
146 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
148 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
149 object = Object.normalize(data["object"])
151 assert object.data["emoji"] == %{
152 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
155 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
157 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
158 object = Object.normalize(data["object"])
160 assert "test" in object.data["tag"]
163 test "it works for incoming notices with url not being a string (prismo)" do
164 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
166 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
167 object = Object.normalize(data["object"])
169 assert object.data["url"] == "https://prismo.news/posts/83"
172 test "it cleans up incoming notices which are not really DMs" do
174 other_user = insert(:user)
176 to = [user.ap_id, other_user.ap_id]
179 File.read!("test/fixtures/mastodon-post-activity.json")
189 data = Map.put(data, "object", object)
191 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
193 assert data["to"] == []
194 assert data["cc"] == to
196 object_data = Object.normalize(activity).data
198 assert object_data["to"] == []
199 assert object_data["cc"] == to
202 test "it works for incoming follow requests" do
206 File.read!("test/fixtures/mastodon-follow-activity.json")
208 |> Map.put("object", user.ap_id)
210 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
212 assert data["actor"] == "http://mastodon.example.org/users/admin"
213 assert data["type"] == "Follow"
214 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
215 assert User.following?(User.get_by_ap_id(data["actor"]), user)
218 test "it works for incoming follow requests from hubzilla" do
222 File.read!("test/fixtures/hubzilla-follow-activity.json")
224 |> Map.put("object", user.ap_id)
225 |> Utils.normalize_params()
227 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
229 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
230 assert data["type"] == "Follow"
231 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
232 assert User.following?(User.get_by_ap_id(data["actor"]), user)
235 test "it works for incoming likes" do
237 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
240 File.read!("test/fixtures/mastodon-like.json")
242 |> Map.put("object", activity.data["object"])
244 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
246 assert data["actor"] == "http://mastodon.example.org/users/admin"
247 assert data["type"] == "Like"
248 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
249 assert data["object"] == activity.data["object"]
252 test "it returns an error for incoming unlikes wihout a like activity" do
254 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
257 File.read!("test/fixtures/mastodon-undo-like.json")
259 |> Map.put("object", activity.data["object"])
261 assert Transmogrifier.handle_incoming(data) == :error
264 test "it works for incoming unlikes with an existing like activity" do
266 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
269 File.read!("test/fixtures/mastodon-like.json")
271 |> Map.put("object", activity.data["object"])
273 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
276 File.read!("test/fixtures/mastodon-undo-like.json")
278 |> Map.put("object", like_data)
279 |> Map.put("actor", like_data["actor"])
281 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
283 assert data["actor"] == "http://mastodon.example.org/users/admin"
284 assert data["type"] == "Undo"
285 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
286 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
289 test "it works for incoming announces" do
290 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
292 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
294 assert data["actor"] == "http://mastodon.example.org/users/admin"
295 assert data["type"] == "Announce"
298 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
300 assert data["object"] ==
301 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
303 assert Activity.get_create_by_object_ap_id(data["object"])
306 test "it works for incoming announces with an existing activity" do
308 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
311 File.read!("test/fixtures/mastodon-announce.json")
313 |> Map.put("object", activity.data["object"])
315 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
317 assert data["actor"] == "http://mastodon.example.org/users/admin"
318 assert data["type"] == "Announce"
321 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
323 assert data["object"] == activity.data["object"]
325 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
328 test "it does not clobber the addressing on announce activities" do
330 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
333 File.read!("test/fixtures/mastodon-announce.json")
335 |> Map.put("object", Object.normalize(activity).data["id"])
336 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
339 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
341 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
344 test "it ensures that as:Public activities make it to their followers collection" do
348 File.read!("test/fixtures/mastodon-post-activity.json")
350 |> Map.put("actor", user.ap_id)
351 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
356 |> Map.put("attributedTo", user.ap_id)
357 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
360 data = Map.put(data, "object", object)
362 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
364 assert data["cc"] == [User.ap_followers(user)]
367 test "it ensures that address fields become lists" do
371 File.read!("test/fixtures/mastodon-post-activity.json")
373 |> Map.put("actor", user.ap_id)
374 |> Map.put("to", nil)
375 |> Map.put("cc", nil)
379 |> Map.put("attributedTo", user.ap_id)
380 |> Map.put("to", nil)
381 |> Map.put("cc", nil)
383 data = Map.put(data, "object", object)
385 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
387 assert !is_nil(data["to"])
388 assert !is_nil(data["cc"])
391 test "it works for incoming update activities" do
392 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
394 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
395 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
398 update_data["object"]
399 |> Map.put("actor", data["actor"])
400 |> Map.put("id", data["actor"])
404 |> Map.put("actor", data["actor"])
405 |> Map.put("object", object)
407 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
409 user = User.get_cached_by_ap_id(data["actor"])
410 assert user.name == "gargle"
412 assert user.avatar["url"] == [
415 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
419 assert user.info.banner["url"] == [
422 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
426 assert user.bio == "<p>Some bio</p>"
429 test "it works for incoming update activities which lock the account" do
430 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
432 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
433 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
436 update_data["object"]
437 |> Map.put("actor", data["actor"])
438 |> Map.put("id", data["actor"])
439 |> Map.put("manuallyApprovesFollowers", true)
443 |> Map.put("actor", data["actor"])
444 |> Map.put("object", object)
446 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
448 user = User.get_cached_by_ap_id(data["actor"])
449 assert user.info.locked == true
452 test "it works for incoming deletes" do
453 activity = insert(:note_activity)
456 File.read!("test/fixtures/mastodon-delete.json")
461 |> Map.put("id", activity.data["object"])
465 |> Map.put("object", object)
466 |> Map.put("actor", activity.data["actor"])
468 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
470 refute Activity.get_by_id(activity.id)
473 test "it fails for incoming deletes with spoofed origin" do
474 activity = insert(:note_activity)
477 File.read!("test/fixtures/mastodon-delete.json")
482 |> Map.put("id", activity.data["object"])
486 |> Map.put("object", object)
488 :error = Transmogrifier.handle_incoming(data)
490 assert Activity.get_by_id(activity.id)
493 test "it works for incoming unannounces with an existing notice" do
495 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
498 File.read!("test/fixtures/mastodon-announce.json")
500 |> Map.put("object", activity.data["object"])
502 {:ok, %Activity{data: announce_data, local: false}} =
503 Transmogrifier.handle_incoming(announce_data)
506 File.read!("test/fixtures/mastodon-undo-announce.json")
508 |> Map.put("object", announce_data)
509 |> Map.put("actor", announce_data["actor"])
511 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
513 assert data["type"] == "Undo"
514 assert data["object"]["type"] == "Announce"
515 assert data["object"]["object"] == activity.data["object"]
517 assert data["object"]["id"] ==
518 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
521 test "it works for incomming unfollows with an existing follow" do
525 File.read!("test/fixtures/mastodon-follow-activity.json")
527 |> Map.put("object", user.ap_id)
529 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
532 File.read!("test/fixtures/mastodon-unfollow-activity.json")
534 |> Map.put("object", follow_data)
536 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
538 assert data["type"] == "Undo"
539 assert data["object"]["type"] == "Follow"
540 assert data["object"]["object"] == user.ap_id
541 assert data["actor"] == "http://mastodon.example.org/users/admin"
543 refute User.following?(User.get_by_ap_id(data["actor"]), user)
546 test "it works for incoming blocks" do
550 File.read!("test/fixtures/mastodon-block-activity.json")
552 |> Map.put("object", user.ap_id)
554 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
556 assert data["type"] == "Block"
557 assert data["object"] == user.ap_id
558 assert data["actor"] == "http://mastodon.example.org/users/admin"
560 blocker = User.get_by_ap_id(data["actor"])
562 assert User.blocks?(blocker, user)
565 test "incoming blocks successfully tear down any follow relationship" do
566 blocker = insert(:user)
567 blocked = insert(:user)
570 File.read!("test/fixtures/mastodon-block-activity.json")
572 |> Map.put("object", blocked.ap_id)
573 |> Map.put("actor", blocker.ap_id)
575 {:ok, blocker} = User.follow(blocker, blocked)
576 {:ok, blocked} = User.follow(blocked, blocker)
578 assert User.following?(blocker, blocked)
579 assert User.following?(blocked, blocker)
581 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
583 assert data["type"] == "Block"
584 assert data["object"] == blocked.ap_id
585 assert data["actor"] == blocker.ap_id
587 blocker = User.get_by_ap_id(data["actor"])
588 blocked = User.get_by_ap_id(data["object"])
590 assert User.blocks?(blocker, blocked)
592 refute User.following?(blocker, blocked)
593 refute User.following?(blocked, blocker)
596 test "it works for incoming unblocks with an existing block" do
600 File.read!("test/fixtures/mastodon-block-activity.json")
602 |> Map.put("object", user.ap_id)
604 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
607 File.read!("test/fixtures/mastodon-unblock-activity.json")
609 |> Map.put("object", block_data)
611 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
612 assert data["type"] == "Undo"
613 assert data["object"]["type"] == "Block"
614 assert data["object"]["object"] == user.ap_id
615 assert data["actor"] == "http://mastodon.example.org/users/admin"
617 blocker = User.get_by_ap_id(data["actor"])
619 refute User.blocks?(blocker, user)
622 test "it works for incoming accepts which were pre-accepted" do
623 follower = insert(:user)
624 followed = insert(:user)
626 {:ok, follower} = User.follow(follower, followed)
627 assert User.following?(follower, followed) == true
629 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
632 File.read!("test/fixtures/mastodon-accept-activity.json")
634 |> Map.put("actor", followed.ap_id)
637 accept_data["object"]
638 |> Map.put("actor", follower.ap_id)
639 |> Map.put("id", follow_activity.data["id"])
641 accept_data = Map.put(accept_data, "object", object)
643 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
644 refute activity.local
646 assert activity.data["object"] == follow_activity.data["id"]
648 follower = User.get_by_id(follower.id)
650 assert User.following?(follower, followed) == true
653 test "it works for incoming accepts which were orphaned" do
654 follower = insert(:user)
655 followed = insert(:user, %{info: %User.Info{locked: true}})
657 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
660 File.read!("test/fixtures/mastodon-accept-activity.json")
662 |> Map.put("actor", followed.ap_id)
665 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
667 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
668 assert activity.data["object"] == follow_activity.data["id"]
670 follower = User.get_by_id(follower.id)
672 assert User.following?(follower, followed) == true
675 test "it works for incoming accepts which are referenced by IRI only" do
676 follower = insert(:user)
677 followed = insert(:user, %{info: %User.Info{locked: true}})
679 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
682 File.read!("test/fixtures/mastodon-accept-activity.json")
684 |> Map.put("actor", followed.ap_id)
685 |> Map.put("object", follow_activity.data["id"])
687 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
688 assert activity.data["object"] == follow_activity.data["id"]
690 follower = User.get_by_id(follower.id)
692 assert User.following?(follower, followed) == true
695 test "it fails for incoming accepts which cannot be correlated" do
696 follower = insert(:user)
697 followed = insert(:user, %{info: %User.Info{locked: true}})
700 File.read!("test/fixtures/mastodon-accept-activity.json")
702 |> Map.put("actor", followed.ap_id)
705 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
707 :error = Transmogrifier.handle_incoming(accept_data)
709 follower = User.get_by_id(follower.id)
711 refute User.following?(follower, followed) == true
714 test "it fails for incoming rejects which cannot be correlated" do
715 follower = insert(:user)
716 followed = insert(:user, %{info: %User.Info{locked: true}})
719 File.read!("test/fixtures/mastodon-reject-activity.json")
721 |> Map.put("actor", followed.ap_id)
724 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
726 :error = Transmogrifier.handle_incoming(accept_data)
728 follower = User.get_by_id(follower.id)
730 refute User.following?(follower, followed) == true
733 test "it works for incoming rejects which are orphaned" do
734 follower = insert(:user)
735 followed = insert(:user, %{info: %User.Info{locked: true}})
737 {:ok, follower} = User.follow(follower, followed)
738 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
740 assert User.following?(follower, followed) == true
743 File.read!("test/fixtures/mastodon-reject-activity.json")
745 |> Map.put("actor", followed.ap_id)
748 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
750 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
751 refute activity.local
753 follower = User.get_by_id(follower.id)
755 assert User.following?(follower, followed) == false
758 test "it works for incoming rejects which are referenced by IRI only" do
759 follower = insert(:user)
760 followed = insert(:user, %{info: %User.Info{locked: true}})
762 {:ok, follower} = User.follow(follower, followed)
763 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
765 assert User.following?(follower, followed) == true
768 File.read!("test/fixtures/mastodon-reject-activity.json")
770 |> Map.put("actor", followed.ap_id)
771 |> Map.put("object", follow_activity.data["id"])
773 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
775 follower = User.get_by_id(follower.id)
777 assert User.following?(follower, followed) == false
780 test "it rejects activities without a valid ID" do
784 File.read!("test/fixtures/mastodon-follow-activity.json")
786 |> Map.put("object", user.ap_id)
789 :error = Transmogrifier.handle_incoming(data)
792 test "it remaps video URLs as attachments if necessary" do
794 Fetcher.fetch_object_from_id(
795 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
800 "mediaType" => "video/mp4",
802 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
803 "mimeType" => "video/mp4",
808 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
809 "mediaType" => "video/mp4",
816 assert object.data["url"] ==
817 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
819 assert object.data["attachment"] == [attachment]
822 test "it accepts Flag activities" do
824 other_user = insert(:user)
826 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
827 object = Object.normalize(activity.data["object"])
830 "@context" => "https://www.w3.org/ns/activitystreams",
831 "cc" => [user.ap_id],
832 "object" => [user.ap_id, object.data["id"]],
834 "content" => "blocked AND reported!!!",
835 "actor" => other_user.ap_id
838 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
840 assert activity.data["object"] == [user.ap_id, object.data["id"]]
841 assert activity.data["content"] == "blocked AND reported!!!"
842 assert activity.data["actor"] == other_user.ap_id
843 assert activity.data["cc"] == [user.ap_id]
847 describe "prepare outgoing" do
848 test "it turns mentions into tags" do
850 other_user = insert(:user)
853 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
855 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
856 object = modified["object"]
858 expected_mention = %{
859 "href" => other_user.ap_id,
860 "name" => "@#{other_user.nickname}",
865 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
870 assert Enum.member?(object["tag"], expected_tag)
871 assert Enum.member?(object["tag"], expected_mention)
874 test "it adds the sensitive property" do
877 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
878 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
880 assert modified["object"]["sensitive"]
883 test "it adds the json-ld context and the conversation property" do
886 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
887 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
889 assert modified["@context"] ==
890 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
892 assert modified["object"]["conversation"] == modified["context"]
895 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
898 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
899 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
901 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
904 test "it translates ostatus IDs to external URLs" do
905 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
906 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
910 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
911 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
913 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
916 test "it translates ostatus reply_to IDs to external URLs" do
917 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
918 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
923 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
925 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
927 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
930 test "it strips internal hashtag data" do
933 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
936 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
941 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
943 assert modified["object"]["tag"] == [expected_tag]
946 test "it strips internal fields" do
949 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :moominmamma:"})
951 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
953 assert length(modified["object"]["tag"]) == 2
955 assert is_nil(modified["object"]["emoji"])
956 assert is_nil(modified["object"]["like_count"])
957 assert is_nil(modified["object"]["announcements"])
958 assert is_nil(modified["object"]["announcement_count"])
959 assert is_nil(modified["object"]["context_id"])
962 test "it strips internal fields of article" do
963 activity = insert(:article_activity)
965 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
967 assert length(modified["object"]["tag"]) == 2
969 assert is_nil(modified["object"]["emoji"])
970 assert is_nil(modified["object"]["like_count"])
971 assert is_nil(modified["object"]["announcements"])
972 assert is_nil(modified["object"]["announcement_count"])
973 assert is_nil(modified["object"]["context_id"])
976 test "it adds like collection to object" do
977 activity = insert(:note_activity)
978 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
980 assert modified["object"]["likes"]["type"] == "OrderedCollection"
981 assert modified["object"]["likes"]["totalItems"] == 0
984 test "the directMessage flag is present" do
986 other_user = insert(:user)
988 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
990 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
992 assert modified["directMessage"] == false
995 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
997 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
999 assert modified["directMessage"] == false
1002 CommonAPI.post(user, %{
1003 "status" => "@#{other_user.nickname} :moominmamma:",
1004 "visibility" => "direct"
1007 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1009 assert modified["directMessage"] == true
1013 describe "user upgrade" do
1014 test "it upgrades a user to activitypub" do
1017 nickname: "rye@niu.moe",
1019 ap_id: "https://niu.moe/users/rye",
1020 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1023 user_two = insert(:user, %{following: [user.follower_address]})
1025 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1026 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1027 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1029 user = User.get_by_id(user.id)
1030 assert user.info.note_count == 1
1032 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1033 assert user.info.ap_enabled
1034 assert user.info.note_count == 1
1035 assert user.follower_address == "https://niu.moe/users/rye/followers"
1037 user = User.get_by_id(user.id)
1038 assert user.info.note_count == 1
1040 activity = Activity.get_by_id(activity.id)
1041 assert user.follower_address in activity.recipients
1047 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1056 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1059 } = user.info.banner
1061 refute "..." in activity.recipients
1063 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1064 refute user.follower_address in unrelated_activity.recipients
1066 user_two = User.get_by_id(user_two.id)
1067 assert user.follower_address in user_two.following
1068 refute "..." in user_two.following
1072 describe "maybe_retire_websub" do
1073 test "it deletes all websub client subscripitions with the user as topic" do
1074 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1075 {:ok, ws} = Repo.insert(subscription)
1077 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1078 {:ok, ws2} = Repo.insert(subscription)
1080 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1082 refute Repo.get(WebsubClientSubscription, ws.id)
1083 assert Repo.get(WebsubClientSubscription, ws2.id)
1087 describe "actor rewriting" do
1088 test "it fixes the actor URL property to be a proper URI" do
1090 "url" => %{"href" => "http://example.com"}
1093 rewritten = Transmogrifier.maybe_fix_user_object(data)
1094 assert rewritten["url"] == "http://example.com"
1098 describe "actor origin containment" do
1099 test "it rejects activities which reference objects with bogus origins" do
1101 "@context" => "https://www.w3.org/ns/activitystreams",
1102 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1103 "actor" => "http://mastodon.example.org/users/admin",
1104 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1105 "object" => "https://info.pleroma.site/activity.json",
1106 "type" => "Announce"
1109 :error = Transmogrifier.handle_incoming(data)
1112 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1114 "@context" => "https://www.w3.org/ns/activitystreams",
1115 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1116 "actor" => "http://mastodon.example.org/users/admin",
1117 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1118 "object" => "https://info.pleroma.site/activity2.json",
1119 "type" => "Announce"
1122 :error = Transmogrifier.handle_incoming(data)
1125 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1127 "@context" => "https://www.w3.org/ns/activitystreams",
1128 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1129 "actor" => "http://mastodon.example.org/users/admin",
1130 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1131 "object" => "https://info.pleroma.site/activity3.json",
1132 "type" => "Announce"
1135 :error = Transmogrifier.handle_incoming(data)
1139 describe "reserialization" do
1140 test "successfully reserializes a message with inReplyTo == nil" do
1141 user = insert(:user)
1144 "@context" => "https://www.w3.org/ns/activitystreams",
1145 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1149 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1154 "attributedTo" => user.ap_id
1156 "actor" => user.ap_id
1159 {:ok, activity} = Transmogrifier.handle_incoming(message)
1161 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1164 test "successfully reserializes a message with AS2 objects in IR" do
1165 user = insert(:user)
1168 "@context" => "https://www.w3.org/ns/activitystreams",
1169 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1173 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1178 "attributedTo" => user.ap_id,
1180 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1181 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1184 "actor" => user.ap_id
1187 {:ok, activity} = Transmogrifier.handle_incoming(message)
1189 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)