1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
9 alias Pleroma.Object.Fetcher
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.OStatus
15 alias Pleroma.Web.Websub.WebsubClientSubscription
17 import Pleroma.Factory
18 alias Pleroma.Web.CommonAPI
21 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 describe "handle_incoming" do
26 test "it ignores an incoming notice if we already have it" do
27 activity = insert(:note_activity)
30 File.read!("test/fixtures/mastodon-post-activity.json")
32 |> Map.put("object", activity.data["object"])
34 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
36 assert activity == returned_activity
39 test "it fetches replied-to activities if we don't have them" do
41 File.read!("test/fixtures/mastodon-post-activity.json")
46 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
50 |> Map.put("object", object)
52 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
53 returned_object = Object.normalize(returned_activity.data["object"])
56 Activity.get_create_by_object_ap_id(
57 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
60 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
63 test "it works for incoming notices" do
64 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
66 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
69 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
71 assert data["context"] ==
72 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
74 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
76 assert data["cc"] == [
77 "http://mastodon.example.org/users/admin/followers",
78 "http://localtesting.pleroma.lol/users/lain"
81 assert data["actor"] == "http://mastodon.example.org/users/admin"
83 object = Object.normalize(data["object"]).data
84 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
86 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
88 assert object["cc"] == [
89 "http://mastodon.example.org/users/admin/followers",
90 "http://localtesting.pleroma.lol/users/lain"
93 assert object["actor"] == "http://mastodon.example.org/users/admin"
94 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
96 assert object["context"] ==
97 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
99 assert object["sensitive"] == true
101 user = User.get_cached_by_ap_id(object["actor"])
103 assert user.info.note_count == 1
106 test "it works for incoming notices with hashtags" do
107 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
109 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
110 object = Object.normalize(data["object"])
112 assert Enum.at(object.data["tag"], 2) == "moo"
115 test "it works for incoming questions" do
116 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
118 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
120 object = Object.normalize(activity)
122 assert Enum.all?(object.data["oneOf"], fn choice ->
125 "Everyone knows that!",
126 "25 char limit is dumb",
127 "I can't even fit a funny"
132 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
136 CommonAPI.post(user, %{
137 "status" => "suya...",
138 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
141 object = Object.normalize(activity)
144 File.read!("test/fixtures/mastodon-vote.json")
146 |> Kernel.put_in(["to"], user.ap_id)
147 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
148 |> Kernel.put_in(["object", "to"], user.ap_id)
150 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
151 answer_object = Object.normalize(activity)
152 assert answer_object.data["type"] == "Answer"
153 object = Object.get_by_ap_id(object.data["id"])
156 object.data["oneOf"],
158 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
164 test "it works for incoming notices with contentMap" do
166 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
168 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
169 object = Object.normalize(data["object"])
171 assert object.data["content"] ==
172 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
175 test "it works for incoming notices with to/cc not being an array (kroeg)" do
176 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
178 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
179 object = Object.normalize(data["object"])
181 assert object.data["content"] ==
182 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
185 test "it works for incoming announces with actor being inlined (kroeg)" do
186 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
188 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
190 assert data["actor"] == "https://puckipedia.com/"
193 test "it works for incoming notices with tag not being an array (kroeg)" do
194 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
196 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
197 object = Object.normalize(data["object"])
199 assert object.data["emoji"] == %{
200 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
203 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
205 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
206 object = Object.normalize(data["object"])
208 assert "test" in object.data["tag"]
211 test "it works for incoming notices with url not being a string (prismo)" do
212 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
214 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
215 object = Object.normalize(data["object"])
217 assert object.data["url"] == "https://prismo.news/posts/83"
220 test "it cleans up incoming notices which are not really DMs" do
222 other_user = insert(:user)
224 to = [user.ap_id, other_user.ap_id]
227 File.read!("test/fixtures/mastodon-post-activity.json")
237 data = Map.put(data, "object", object)
239 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
241 assert data["to"] == []
242 assert data["cc"] == to
244 object_data = Object.normalize(activity).data
246 assert object_data["to"] == []
247 assert object_data["cc"] == to
250 test "it works for incoming likes" do
252 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
255 File.read!("test/fixtures/mastodon-like.json")
257 |> Map.put("object", activity.data["object"])
259 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
261 assert data["actor"] == "http://mastodon.example.org/users/admin"
262 assert data["type"] == "Like"
263 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
264 assert data["object"] == activity.data["object"]
267 test "it returns an error for incoming unlikes wihout a like activity" do
269 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
272 File.read!("test/fixtures/mastodon-undo-like.json")
274 |> Map.put("object", activity.data["object"])
276 assert Transmogrifier.handle_incoming(data) == :error
279 test "it works for incoming unlikes with an existing like activity" do
281 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
284 File.read!("test/fixtures/mastodon-like.json")
286 |> Map.put("object", activity.data["object"])
288 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
291 File.read!("test/fixtures/mastodon-undo-like.json")
293 |> Map.put("object", like_data)
294 |> Map.put("actor", like_data["actor"])
296 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
298 assert data["actor"] == "http://mastodon.example.org/users/admin"
299 assert data["type"] == "Undo"
300 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
301 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
304 test "it works for incoming announces" do
305 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
307 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
309 assert data["actor"] == "http://mastodon.example.org/users/admin"
310 assert data["type"] == "Announce"
313 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
315 assert data["object"] ==
316 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
318 assert Activity.get_create_by_object_ap_id(data["object"])
321 test "it works for incoming announces with an existing activity" do
323 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
326 File.read!("test/fixtures/mastodon-announce.json")
328 |> Map.put("object", activity.data["object"])
330 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
332 assert data["actor"] == "http://mastodon.example.org/users/admin"
333 assert data["type"] == "Announce"
336 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
338 assert data["object"] == activity.data["object"]
340 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
343 test "it does not clobber the addressing on announce activities" do
345 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
348 File.read!("test/fixtures/mastodon-announce.json")
350 |> Map.put("object", Object.normalize(activity).data["id"])
351 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
354 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
356 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
359 test "it ensures that as:Public activities make it to their followers collection" do
363 File.read!("test/fixtures/mastodon-post-activity.json")
365 |> Map.put("actor", user.ap_id)
366 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
371 |> Map.put("attributedTo", user.ap_id)
372 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
375 data = Map.put(data, "object", object)
377 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
379 assert data["cc"] == [User.ap_followers(user)]
382 test "it ensures that address fields become lists" do
386 File.read!("test/fixtures/mastodon-post-activity.json")
388 |> Map.put("actor", user.ap_id)
389 |> Map.put("to", nil)
390 |> Map.put("cc", nil)
394 |> Map.put("attributedTo", user.ap_id)
395 |> Map.put("to", nil)
396 |> Map.put("cc", nil)
398 data = Map.put(data, "object", object)
400 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
402 assert !is_nil(data["to"])
403 assert !is_nil(data["cc"])
406 test "it works for incoming update activities" do
407 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
409 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
410 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
413 update_data["object"]
414 |> Map.put("actor", data["actor"])
415 |> Map.put("id", data["actor"])
419 |> Map.put("actor", data["actor"])
420 |> Map.put("object", object)
422 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
424 user = User.get_cached_by_ap_id(data["actor"])
425 assert user.name == "gargle"
427 assert user.avatar["url"] == [
430 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
434 assert user.info.banner["url"] == [
437 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
441 assert user.bio == "<p>Some bio</p>"
444 test "it works for incoming update activities which lock the account" do
445 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
447 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
448 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
451 update_data["object"]
452 |> Map.put("actor", data["actor"])
453 |> Map.put("id", data["actor"])
454 |> Map.put("manuallyApprovesFollowers", true)
458 |> Map.put("actor", data["actor"])
459 |> Map.put("object", object)
461 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
463 user = User.get_cached_by_ap_id(data["actor"])
464 assert user.info.locked == true
467 test "it works for incoming deletes" do
468 activity = insert(:note_activity)
471 File.read!("test/fixtures/mastodon-delete.json")
476 |> Map.put("id", activity.data["object"])
480 |> Map.put("object", object)
481 |> Map.put("actor", activity.data["actor"])
483 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
485 refute Activity.get_by_id(activity.id)
488 test "it fails for incoming deletes with spoofed origin" do
489 activity = insert(:note_activity)
492 File.read!("test/fixtures/mastodon-delete.json")
497 |> Map.put("id", activity.data["object"])
501 |> Map.put("object", object)
503 :error = Transmogrifier.handle_incoming(data)
505 assert Activity.get_by_id(activity.id)
508 test "it works for incoming unannounces with an existing notice" do
510 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
513 File.read!("test/fixtures/mastodon-announce.json")
515 |> Map.put("object", activity.data["object"])
517 {:ok, %Activity{data: announce_data, local: false}} =
518 Transmogrifier.handle_incoming(announce_data)
521 File.read!("test/fixtures/mastodon-undo-announce.json")
523 |> Map.put("object", announce_data)
524 |> Map.put("actor", announce_data["actor"])
526 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
528 assert data["type"] == "Undo"
529 assert data["object"]["type"] == "Announce"
530 assert data["object"]["object"] == activity.data["object"]
532 assert data["object"]["id"] ==
533 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
536 test "it works for incomming unfollows with an existing follow" do
540 File.read!("test/fixtures/mastodon-follow-activity.json")
542 |> Map.put("object", user.ap_id)
544 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
547 File.read!("test/fixtures/mastodon-unfollow-activity.json")
549 |> Map.put("object", follow_data)
551 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
553 assert data["type"] == "Undo"
554 assert data["object"]["type"] == "Follow"
555 assert data["object"]["object"] == user.ap_id
556 assert data["actor"] == "http://mastodon.example.org/users/admin"
558 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
561 test "it works for incoming blocks" do
565 File.read!("test/fixtures/mastodon-block-activity.json")
567 |> Map.put("object", user.ap_id)
569 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
571 assert data["type"] == "Block"
572 assert data["object"] == user.ap_id
573 assert data["actor"] == "http://mastodon.example.org/users/admin"
575 blocker = User.get_cached_by_ap_id(data["actor"])
577 assert User.blocks?(blocker, user)
580 test "incoming blocks successfully tear down any follow relationship" do
581 blocker = insert(:user)
582 blocked = insert(:user)
585 File.read!("test/fixtures/mastodon-block-activity.json")
587 |> Map.put("object", blocked.ap_id)
588 |> Map.put("actor", blocker.ap_id)
590 {:ok, blocker} = User.follow(blocker, blocked)
591 {:ok, blocked} = User.follow(blocked, blocker)
593 assert User.following?(blocker, blocked)
594 assert User.following?(blocked, blocker)
596 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
598 assert data["type"] == "Block"
599 assert data["object"] == blocked.ap_id
600 assert data["actor"] == blocker.ap_id
602 blocker = User.get_cached_by_ap_id(data["actor"])
603 blocked = User.get_cached_by_ap_id(data["object"])
605 assert User.blocks?(blocker, blocked)
607 refute User.following?(blocker, blocked)
608 refute User.following?(blocked, blocker)
611 test "it works for incoming unblocks with an existing block" do
615 File.read!("test/fixtures/mastodon-block-activity.json")
617 |> Map.put("object", user.ap_id)
619 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
622 File.read!("test/fixtures/mastodon-unblock-activity.json")
624 |> Map.put("object", block_data)
626 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
627 assert data["type"] == "Undo"
628 assert data["object"]["type"] == "Block"
629 assert data["object"]["object"] == user.ap_id
630 assert data["actor"] == "http://mastodon.example.org/users/admin"
632 blocker = User.get_cached_by_ap_id(data["actor"])
634 refute User.blocks?(blocker, user)
637 test "it works for incoming accepts which were pre-accepted" do
638 follower = insert(:user)
639 followed = insert(:user)
641 {:ok, follower} = User.follow(follower, followed)
642 assert User.following?(follower, followed) == true
644 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
647 File.read!("test/fixtures/mastodon-accept-activity.json")
649 |> Map.put("actor", followed.ap_id)
652 accept_data["object"]
653 |> Map.put("actor", follower.ap_id)
654 |> Map.put("id", follow_activity.data["id"])
656 accept_data = Map.put(accept_data, "object", object)
658 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
659 refute activity.local
661 assert activity.data["object"] == follow_activity.data["id"]
663 follower = User.get_cached_by_id(follower.id)
665 assert User.following?(follower, followed) == true
668 test "it works for incoming accepts which were orphaned" do
669 follower = insert(:user)
670 followed = insert(:user, %{info: %User.Info{locked: true}})
672 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
675 File.read!("test/fixtures/mastodon-accept-activity.json")
677 |> Map.put("actor", followed.ap_id)
680 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
682 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
683 assert activity.data["object"] == follow_activity.data["id"]
685 follower = User.get_cached_by_id(follower.id)
687 assert User.following?(follower, followed) == true
690 test "it works for incoming accepts which are referenced by IRI only" do
691 follower = insert(:user)
692 followed = insert(:user, %{info: %User.Info{locked: true}})
694 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
697 File.read!("test/fixtures/mastodon-accept-activity.json")
699 |> Map.put("actor", followed.ap_id)
700 |> Map.put("object", follow_activity.data["id"])
702 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
703 assert activity.data["object"] == follow_activity.data["id"]
705 follower = User.get_cached_by_id(follower.id)
707 assert User.following?(follower, followed) == true
710 test "it fails for incoming accepts which cannot be correlated" do
711 follower = insert(:user)
712 followed = insert(:user, %{info: %User.Info{locked: true}})
715 File.read!("test/fixtures/mastodon-accept-activity.json")
717 |> Map.put("actor", followed.ap_id)
720 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
722 :error = Transmogrifier.handle_incoming(accept_data)
724 follower = User.get_cached_by_id(follower.id)
726 refute User.following?(follower, followed) == true
729 test "it fails for incoming rejects which cannot be correlated" do
730 follower = insert(:user)
731 followed = insert(:user, %{info: %User.Info{locked: true}})
734 File.read!("test/fixtures/mastodon-reject-activity.json")
736 |> Map.put("actor", followed.ap_id)
739 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
741 :error = Transmogrifier.handle_incoming(accept_data)
743 follower = User.get_cached_by_id(follower.id)
745 refute User.following?(follower, followed) == true
748 test "it works for incoming rejects which are orphaned" do
749 follower = insert(:user)
750 followed = insert(:user, %{info: %User.Info{locked: true}})
752 {:ok, follower} = User.follow(follower, followed)
753 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
755 assert User.following?(follower, followed) == true
758 File.read!("test/fixtures/mastodon-reject-activity.json")
760 |> Map.put("actor", followed.ap_id)
763 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
765 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
766 refute activity.local
768 follower = User.get_cached_by_id(follower.id)
770 assert User.following?(follower, followed) == false
773 test "it works for incoming rejects which are referenced by IRI only" do
774 follower = insert(:user)
775 followed = insert(:user, %{info: %User.Info{locked: true}})
777 {:ok, follower} = User.follow(follower, followed)
778 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
780 assert User.following?(follower, followed) == true
783 File.read!("test/fixtures/mastodon-reject-activity.json")
785 |> Map.put("actor", followed.ap_id)
786 |> Map.put("object", follow_activity.data["id"])
788 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
790 follower = User.get_cached_by_id(follower.id)
792 assert User.following?(follower, followed) == false
795 test "it rejects activities without a valid ID" do
799 File.read!("test/fixtures/mastodon-follow-activity.json")
801 |> Map.put("object", user.ap_id)
804 :error = Transmogrifier.handle_incoming(data)
807 test "it remaps video URLs as attachments if necessary" do
809 Fetcher.fetch_object_from_id(
810 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
815 "mediaType" => "video/mp4",
817 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
818 "mimeType" => "video/mp4",
823 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
824 "mediaType" => "video/mp4",
831 assert object.data["url"] ==
832 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
834 assert object.data["attachment"] == [attachment]
837 test "it accepts Flag activities" do
839 other_user = insert(:user)
841 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
842 object = Object.normalize(activity.data["object"])
845 "@context" => "https://www.w3.org/ns/activitystreams",
846 "cc" => [user.ap_id],
847 "object" => [user.ap_id, object.data["id"]],
849 "content" => "blocked AND reported!!!",
850 "actor" => other_user.ap_id
853 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
855 assert activity.data["object"] == [user.ap_id, object.data["id"]]
856 assert activity.data["content"] == "blocked AND reported!!!"
857 assert activity.data["actor"] == other_user.ap_id
858 assert activity.data["cc"] == [user.ap_id]
862 describe "prepare outgoing" do
863 test "it turns mentions into tags" do
865 other_user = insert(:user)
868 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
870 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
871 object = modified["object"]
873 expected_mention = %{
874 "href" => other_user.ap_id,
875 "name" => "@#{other_user.nickname}",
880 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
885 assert Enum.member?(object["tag"], expected_tag)
886 assert Enum.member?(object["tag"], expected_mention)
889 test "it adds the sensitive property" do
892 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
893 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
895 assert modified["object"]["sensitive"]
898 test "it adds the json-ld context and the conversation property" do
901 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
902 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
904 assert modified["@context"] ==
905 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
907 assert modified["object"]["conversation"] == modified["context"]
910 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
913 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
914 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
916 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
919 test "it translates ostatus IDs to external URLs" do
920 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
921 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
925 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
926 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
928 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
931 test "it translates ostatus reply_to IDs to external URLs" do
932 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
933 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
938 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
940 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
942 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
945 test "it strips internal hashtag data" do
948 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
951 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
956 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
958 assert modified["object"]["tag"] == [expected_tag]
961 test "it strips internal fields" do
964 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
966 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
968 assert length(modified["object"]["tag"]) == 2
970 assert is_nil(modified["object"]["emoji"])
971 assert is_nil(modified["object"]["like_count"])
972 assert is_nil(modified["object"]["announcements"])
973 assert is_nil(modified["object"]["announcement_count"])
974 assert is_nil(modified["object"]["context_id"])
977 test "it strips internal fields of article" do
978 activity = insert(:article_activity)
980 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
982 assert length(modified["object"]["tag"]) == 2
984 assert is_nil(modified["object"]["emoji"])
985 assert is_nil(modified["object"]["like_count"])
986 assert is_nil(modified["object"]["announcements"])
987 assert is_nil(modified["object"]["announcement_count"])
988 assert is_nil(modified["object"]["context_id"])
991 test "it adds like collection to object" do
992 activity = insert(:note_activity)
993 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
995 assert modified["object"]["likes"]["type"] == "OrderedCollection"
996 assert modified["object"]["likes"]["totalItems"] == 0
999 test "the directMessage flag is present" do
1000 user = insert(:user)
1001 other_user = insert(:user)
1003 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1005 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1007 assert modified["directMessage"] == false
1010 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1012 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1014 assert modified["directMessage"] == false
1017 CommonAPI.post(user, %{
1018 "status" => "@#{other_user.nickname} :moominmamma:",
1019 "visibility" => "direct"
1022 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1024 assert modified["directMessage"] == true
1028 describe "user upgrade" do
1029 test "it upgrades a user to activitypub" do
1032 nickname: "rye@niu.moe",
1034 ap_id: "https://niu.moe/users/rye",
1035 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1038 user_two = insert(:user, %{following: [user.follower_address]})
1040 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1041 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1042 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1044 user = User.get_cached_by_id(user.id)
1045 assert user.info.note_count == 1
1047 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1048 assert user.info.ap_enabled
1049 assert user.info.note_count == 1
1050 assert user.follower_address == "https://niu.moe/users/rye/followers"
1052 user = User.get_cached_by_id(user.id)
1053 assert user.info.note_count == 1
1055 activity = Activity.get_by_id(activity.id)
1056 assert user.follower_address in activity.recipients
1062 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1071 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1074 } = user.info.banner
1076 refute "..." in activity.recipients
1078 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1079 refute user.follower_address in unrelated_activity.recipients
1081 user_two = User.get_cached_by_id(user_two.id)
1082 assert user.follower_address in user_two.following
1083 refute "..." in user_two.following
1087 describe "maybe_retire_websub" do
1088 test "it deletes all websub client subscripitions with the user as topic" do
1089 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1090 {:ok, ws} = Repo.insert(subscription)
1092 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1093 {:ok, ws2} = Repo.insert(subscription)
1095 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1097 refute Repo.get(WebsubClientSubscription, ws.id)
1098 assert Repo.get(WebsubClientSubscription, ws2.id)
1102 describe "actor rewriting" do
1103 test "it fixes the actor URL property to be a proper URI" do
1105 "url" => %{"href" => "http://example.com"}
1108 rewritten = Transmogrifier.maybe_fix_user_object(data)
1109 assert rewritten["url"] == "http://example.com"
1113 describe "actor origin containment" do
1114 test "it rejects activities which reference objects with bogus origins" do
1116 "@context" => "https://www.w3.org/ns/activitystreams",
1117 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1118 "actor" => "http://mastodon.example.org/users/admin",
1119 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1120 "object" => "https://info.pleroma.site/activity.json",
1121 "type" => "Announce"
1124 :error = Transmogrifier.handle_incoming(data)
1127 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1129 "@context" => "https://www.w3.org/ns/activitystreams",
1130 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1131 "actor" => "http://mastodon.example.org/users/admin",
1132 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1133 "object" => "https://info.pleroma.site/activity2.json",
1134 "type" => "Announce"
1137 :error = Transmogrifier.handle_incoming(data)
1140 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1142 "@context" => "https://www.w3.org/ns/activitystreams",
1143 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1144 "actor" => "http://mastodon.example.org/users/admin",
1145 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1146 "object" => "https://info.pleroma.site/activity3.json",
1147 "type" => "Announce"
1150 :error = Transmogrifier.handle_incoming(data)
1154 describe "reserialization" do
1155 test "successfully reserializes a message with inReplyTo == nil" do
1156 user = insert(:user)
1159 "@context" => "https://www.w3.org/ns/activitystreams",
1160 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1164 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1169 "attributedTo" => user.ap_id
1171 "actor" => user.ap_id
1174 {:ok, activity} = Transmogrifier.handle_incoming(message)
1176 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1179 test "successfully reserializes a message with AS2 objects in IR" do
1180 user = insert(:user)
1183 "@context" => "https://www.w3.org/ns/activitystreams",
1184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1188 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1193 "attributedTo" => user.ap_id,
1195 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1196 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1199 "actor" => user.ap_id
1202 {:ok, activity} = Transmogrifier.handle_incoming(message)
1204 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1208 test "Rewrites Answers to Notes" do
1209 user = insert(:user)
1211 {:ok, poll_activity} =
1212 CommonAPI.post(user, %{
1213 "status" => "suya...",
1214 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1217 poll_object = Object.normalize(poll_activity)
1218 # TODO: Replace with CommonAPI vote creation when implemented
1220 File.read!("test/fixtures/mastodon-vote.json")
1222 |> Kernel.put_in(["to"], user.ap_id)
1223 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1224 |> Kernel.put_in(["object", "to"], user.ap_id)
1226 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1227 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1229 assert data["object"]["type"] == "Note"
1232 describe "fix_explicit_addressing" do
1234 user = insert(:user)
1238 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1239 explicitly_mentioned_actors = [
1240 "https://pleroma.gold/users/user1",
1241 "https://pleroma.gold/user2"
1245 "actor" => user.ap_id,
1246 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1249 Enum.map(explicitly_mentioned_actors, fn href ->
1250 %{"type" => "Mention", "href" => href}
1254 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1255 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1256 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1257 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1260 test "does not move actor's follower collection to cc", %{user: user} do
1262 "actor" => user.ap_id,
1263 "to" => [user.follower_address],
1267 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1268 assert user.follower_address in fixed_object["to"]
1269 refute user.follower_address in fixed_object["cc"]
1272 test "removes recipient's follower collection from cc", %{user: user} do
1273 recipient = insert(:user)
1276 "actor" => user.ap_id,
1277 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1278 "cc" => [user.follower_address, recipient.follower_address]
1281 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1283 assert user.follower_address in fixed_object["cc"]
1284 refute recipient.follower_address in fixed_object["cc"]
1285 refute recipient.follower_address in fixed_object["to"]