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 does not crash if the object in inReplyTo can't be fetched" do
65 File.read!("test/fixtures/mastodon-post-activity.json")
70 |> Map.put("inReplyTo", "https://nonexistent.space/whatever")
74 |> Map.put("object", object)
76 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
79 test "it works for incoming notices" do
80 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
82 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
85 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
87 assert data["context"] ==
88 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
90 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
92 assert data["cc"] == [
93 "http://mastodon.example.org/users/admin/followers",
94 "http://localtesting.pleroma.lol/users/lain"
97 assert data["actor"] == "http://mastodon.example.org/users/admin"
99 object = Object.normalize(data["object"]).data
100 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
102 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
104 assert object["cc"] == [
105 "http://mastodon.example.org/users/admin/followers",
106 "http://localtesting.pleroma.lol/users/lain"
109 assert object["actor"] == "http://mastodon.example.org/users/admin"
110 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
112 assert object["context"] ==
113 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
115 assert object["sensitive"] == true
117 user = User.get_cached_by_ap_id(object["actor"])
119 assert user.info.note_count == 1
122 test "it works for incoming notices with hashtags" do
123 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
125 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
126 object = Object.normalize(data["object"])
128 assert Enum.at(object.data["tag"], 2) == "moo"
131 test "it works for incoming questions" do
132 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
134 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
136 object = Object.normalize(activity)
138 assert Enum.all?(object.data["oneOf"], fn choice ->
141 "Everyone knows that!",
142 "25 char limit is dumb",
143 "I can't even fit a funny"
148 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
152 CommonAPI.post(user, %{
153 "status" => "suya...",
154 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
157 object = Object.normalize(activity)
160 File.read!("test/fixtures/mastodon-vote.json")
162 |> Kernel.put_in(["to"], user.ap_id)
163 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
164 |> Kernel.put_in(["object", "to"], user.ap_id)
166 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
167 answer_object = Object.normalize(activity)
168 assert answer_object.data["type"] == "Answer"
169 object = Object.get_by_ap_id(object.data["id"])
172 object.data["oneOf"],
174 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
180 test "it works for incoming notices with contentMap" do
182 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
184 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
185 object = Object.normalize(data["object"])
187 assert object.data["content"] ==
188 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
191 test "it works for incoming notices with to/cc not being an array (kroeg)" do
192 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
194 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
195 object = Object.normalize(data["object"])
197 assert object.data["content"] ==
198 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
201 test "it works for incoming announces with actor being inlined (kroeg)" do
202 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
204 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
206 assert data["actor"] == "https://puckipedia.com/"
209 test "it works for incoming notices with tag not being an array (kroeg)" do
210 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
212 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
213 object = Object.normalize(data["object"])
215 assert object.data["emoji"] == %{
216 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
219 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
221 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
222 object = Object.normalize(data["object"])
224 assert "test" in object.data["tag"]
227 test "it works for incoming notices with url not being a string (prismo)" do
228 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
230 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
231 object = Object.normalize(data["object"])
233 assert object.data["url"] == "https://prismo.news/posts/83"
236 test "it cleans up incoming notices which are not really DMs" do
238 other_user = insert(:user)
240 to = [user.ap_id, other_user.ap_id]
243 File.read!("test/fixtures/mastodon-post-activity.json")
253 data = Map.put(data, "object", object)
255 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
257 assert data["to"] == []
258 assert data["cc"] == to
260 object_data = Object.normalize(activity).data
262 assert object_data["to"] == []
263 assert object_data["cc"] == to
266 test "it works for incoming likes" do
268 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
271 File.read!("test/fixtures/mastodon-like.json")
273 |> Map.put("object", activity.data["object"])
275 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
277 assert data["actor"] == "http://mastodon.example.org/users/admin"
278 assert data["type"] == "Like"
279 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
280 assert data["object"] == activity.data["object"]
283 test "it returns an error for incoming unlikes wihout a like activity" do
285 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
288 File.read!("test/fixtures/mastodon-undo-like.json")
290 |> Map.put("object", activity.data["object"])
292 assert Transmogrifier.handle_incoming(data) == :error
295 test "it works for incoming unlikes with an existing like activity" do
297 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
300 File.read!("test/fixtures/mastodon-like.json")
302 |> Map.put("object", activity.data["object"])
304 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
307 File.read!("test/fixtures/mastodon-undo-like.json")
309 |> Map.put("object", like_data)
310 |> Map.put("actor", like_data["actor"])
312 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
314 assert data["actor"] == "http://mastodon.example.org/users/admin"
315 assert data["type"] == "Undo"
316 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
317 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
320 test "it works for incoming announces" do
321 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
323 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
325 assert data["actor"] == "http://mastodon.example.org/users/admin"
326 assert data["type"] == "Announce"
329 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
331 assert data["object"] ==
332 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
334 assert Activity.get_create_by_object_ap_id(data["object"])
337 test "it works for incoming announces with an existing activity" do
339 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
342 File.read!("test/fixtures/mastodon-announce.json")
344 |> Map.put("object", activity.data["object"])
346 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
348 assert data["actor"] == "http://mastodon.example.org/users/admin"
349 assert data["type"] == "Announce"
352 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
354 assert data["object"] == activity.data["object"]
356 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
359 test "it does not clobber the addressing on announce activities" do
361 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
364 File.read!("test/fixtures/mastodon-announce.json")
366 |> Map.put("object", Object.normalize(activity).data["id"])
367 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
370 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
372 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
375 test "it ensures that as:Public activities make it to their followers collection" do
379 File.read!("test/fixtures/mastodon-post-activity.json")
381 |> Map.put("actor", user.ap_id)
382 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
387 |> Map.put("attributedTo", user.ap_id)
388 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
391 data = Map.put(data, "object", object)
393 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
395 assert data["cc"] == [User.ap_followers(user)]
398 test "it ensures that address fields become lists" do
402 File.read!("test/fixtures/mastodon-post-activity.json")
404 |> Map.put("actor", user.ap_id)
405 |> Map.put("to", nil)
406 |> Map.put("cc", nil)
410 |> Map.put("attributedTo", user.ap_id)
411 |> Map.put("to", nil)
412 |> Map.put("cc", nil)
414 data = Map.put(data, "object", object)
416 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
418 assert !is_nil(data["to"])
419 assert !is_nil(data["cc"])
422 test "it works for incoming update activities" do
423 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
425 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
426 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
429 update_data["object"]
430 |> Map.put("actor", data["actor"])
431 |> Map.put("id", data["actor"])
435 |> Map.put("actor", data["actor"])
436 |> Map.put("object", object)
438 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
440 user = User.get_cached_by_ap_id(data["actor"])
441 assert user.name == "gargle"
443 assert user.avatar["url"] == [
446 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
450 assert user.info.banner["url"] == [
453 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
457 assert user.bio == "<p>Some bio</p>"
460 test "it works for incoming update activities which lock the account" do
461 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
463 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
464 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
467 update_data["object"]
468 |> Map.put("actor", data["actor"])
469 |> Map.put("id", data["actor"])
470 |> Map.put("manuallyApprovesFollowers", true)
474 |> Map.put("actor", data["actor"])
475 |> Map.put("object", object)
477 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
479 user = User.get_cached_by_ap_id(data["actor"])
480 assert user.info.locked == true
483 test "it works for incoming deletes" do
484 activity = insert(:note_activity)
487 File.read!("test/fixtures/mastodon-delete.json")
492 |> Map.put("id", activity.data["object"])
496 |> Map.put("object", object)
497 |> Map.put("actor", activity.data["actor"])
499 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
501 refute Activity.get_by_id(activity.id)
504 test "it fails for incoming deletes with spoofed origin" do
505 activity = insert(:note_activity)
508 File.read!("test/fixtures/mastodon-delete.json")
513 |> Map.put("id", activity.data["object"])
517 |> Map.put("object", object)
519 :error = Transmogrifier.handle_incoming(data)
521 assert Activity.get_by_id(activity.id)
524 test "it works for incoming unannounces with an existing notice" do
526 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
529 File.read!("test/fixtures/mastodon-announce.json")
531 |> Map.put("object", activity.data["object"])
533 {:ok, %Activity{data: announce_data, local: false}} =
534 Transmogrifier.handle_incoming(announce_data)
537 File.read!("test/fixtures/mastodon-undo-announce.json")
539 |> Map.put("object", announce_data)
540 |> Map.put("actor", announce_data["actor"])
542 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
544 assert data["type"] == "Undo"
545 assert data["object"]["type"] == "Announce"
546 assert data["object"]["object"] == activity.data["object"]
548 assert data["object"]["id"] ==
549 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
552 test "it works for incomming unfollows with an existing follow" do
556 File.read!("test/fixtures/mastodon-follow-activity.json")
558 |> Map.put("object", user.ap_id)
560 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
563 File.read!("test/fixtures/mastodon-unfollow-activity.json")
565 |> Map.put("object", follow_data)
567 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
569 assert data["type"] == "Undo"
570 assert data["object"]["type"] == "Follow"
571 assert data["object"]["object"] == user.ap_id
572 assert data["actor"] == "http://mastodon.example.org/users/admin"
574 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
577 test "it works for incoming blocks" do
581 File.read!("test/fixtures/mastodon-block-activity.json")
583 |> Map.put("object", user.ap_id)
585 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
587 assert data["type"] == "Block"
588 assert data["object"] == user.ap_id
589 assert data["actor"] == "http://mastodon.example.org/users/admin"
591 blocker = User.get_cached_by_ap_id(data["actor"])
593 assert User.blocks?(blocker, user)
596 test "incoming blocks successfully tear down any follow relationship" do
597 blocker = insert(:user)
598 blocked = insert(:user)
601 File.read!("test/fixtures/mastodon-block-activity.json")
603 |> Map.put("object", blocked.ap_id)
604 |> Map.put("actor", blocker.ap_id)
606 {:ok, blocker} = User.follow(blocker, blocked)
607 {:ok, blocked} = User.follow(blocked, blocker)
609 assert User.following?(blocker, blocked)
610 assert User.following?(blocked, blocker)
612 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
614 assert data["type"] == "Block"
615 assert data["object"] == blocked.ap_id
616 assert data["actor"] == blocker.ap_id
618 blocker = User.get_cached_by_ap_id(data["actor"])
619 blocked = User.get_cached_by_ap_id(data["object"])
621 assert User.blocks?(blocker, blocked)
623 refute User.following?(blocker, blocked)
624 refute User.following?(blocked, blocker)
627 test "it works for incoming unblocks with an existing block" do
631 File.read!("test/fixtures/mastodon-block-activity.json")
633 |> Map.put("object", user.ap_id)
635 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
638 File.read!("test/fixtures/mastodon-unblock-activity.json")
640 |> Map.put("object", block_data)
642 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
643 assert data["type"] == "Undo"
644 assert data["object"]["type"] == "Block"
645 assert data["object"]["object"] == user.ap_id
646 assert data["actor"] == "http://mastodon.example.org/users/admin"
648 blocker = User.get_cached_by_ap_id(data["actor"])
650 refute User.blocks?(blocker, user)
653 test "it works for incoming accepts which were pre-accepted" do
654 follower = insert(:user)
655 followed = insert(:user)
657 {:ok, follower} = User.follow(follower, followed)
658 assert User.following?(follower, followed) == true
660 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
663 File.read!("test/fixtures/mastodon-accept-activity.json")
665 |> Map.put("actor", followed.ap_id)
668 accept_data["object"]
669 |> Map.put("actor", follower.ap_id)
670 |> Map.put("id", follow_activity.data["id"])
672 accept_data = Map.put(accept_data, "object", object)
674 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
675 refute activity.local
677 assert activity.data["object"] == follow_activity.data["id"]
679 follower = User.get_cached_by_id(follower.id)
681 assert User.following?(follower, followed) == true
684 test "it works for incoming accepts which were orphaned" do
685 follower = insert(:user)
686 followed = insert(:user, %{info: %User.Info{locked: true}})
688 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
691 File.read!("test/fixtures/mastodon-accept-activity.json")
693 |> Map.put("actor", followed.ap_id)
696 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
698 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
699 assert activity.data["object"] == follow_activity.data["id"]
701 follower = User.get_cached_by_id(follower.id)
703 assert User.following?(follower, followed) == true
706 test "it works for incoming accepts which are referenced by IRI only" do
707 follower = insert(:user)
708 followed = insert(:user, %{info: %User.Info{locked: true}})
710 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
713 File.read!("test/fixtures/mastodon-accept-activity.json")
715 |> Map.put("actor", followed.ap_id)
716 |> Map.put("object", follow_activity.data["id"])
718 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
719 assert activity.data["object"] == follow_activity.data["id"]
721 follower = User.get_cached_by_id(follower.id)
723 assert User.following?(follower, followed) == true
726 test "it fails for incoming accepts which cannot be correlated" do
727 follower = insert(:user)
728 followed = insert(:user, %{info: %User.Info{locked: true}})
731 File.read!("test/fixtures/mastodon-accept-activity.json")
733 |> Map.put("actor", followed.ap_id)
736 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
738 :error = Transmogrifier.handle_incoming(accept_data)
740 follower = User.get_cached_by_id(follower.id)
742 refute User.following?(follower, followed) == true
745 test "it fails for incoming rejects which cannot be correlated" do
746 follower = insert(:user)
747 followed = insert(:user, %{info: %User.Info{locked: true}})
750 File.read!("test/fixtures/mastodon-reject-activity.json")
752 |> Map.put("actor", followed.ap_id)
755 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
757 :error = Transmogrifier.handle_incoming(accept_data)
759 follower = User.get_cached_by_id(follower.id)
761 refute User.following?(follower, followed) == true
764 test "it works for incoming rejects which are orphaned" do
765 follower = insert(:user)
766 followed = insert(:user, %{info: %User.Info{locked: true}})
768 {:ok, follower} = User.follow(follower, followed)
769 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
771 assert User.following?(follower, followed) == true
774 File.read!("test/fixtures/mastodon-reject-activity.json")
776 |> Map.put("actor", followed.ap_id)
779 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
781 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
782 refute activity.local
784 follower = User.get_cached_by_id(follower.id)
786 assert User.following?(follower, followed) == false
789 test "it works for incoming rejects which are referenced by IRI only" do
790 follower = insert(:user)
791 followed = insert(:user, %{info: %User.Info{locked: true}})
793 {:ok, follower} = User.follow(follower, followed)
794 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
796 assert User.following?(follower, followed) == true
799 File.read!("test/fixtures/mastodon-reject-activity.json")
801 |> Map.put("actor", followed.ap_id)
802 |> Map.put("object", follow_activity.data["id"])
804 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
806 follower = User.get_cached_by_id(follower.id)
808 assert User.following?(follower, followed) == false
811 test "it rejects activities without a valid ID" do
815 File.read!("test/fixtures/mastodon-follow-activity.json")
817 |> Map.put("object", user.ap_id)
820 :error = Transmogrifier.handle_incoming(data)
823 test "it remaps video URLs as attachments if necessary" do
825 Fetcher.fetch_object_from_id(
826 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
831 "mediaType" => "video/mp4",
833 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
834 "mimeType" => "video/mp4",
839 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
840 "mediaType" => "video/mp4",
847 assert object.data["url"] ==
848 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
850 assert object.data["attachment"] == [attachment]
853 test "it accepts Flag activities" do
855 other_user = insert(:user)
857 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
858 object = Object.normalize(activity.data["object"])
861 "@context" => "https://www.w3.org/ns/activitystreams",
862 "cc" => [user.ap_id],
863 "object" => [user.ap_id, object.data["id"]],
865 "content" => "blocked AND reported!!!",
866 "actor" => other_user.ap_id
869 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
871 assert activity.data["object"] == [user.ap_id, object.data["id"]]
872 assert activity.data["content"] == "blocked AND reported!!!"
873 assert activity.data["actor"] == other_user.ap_id
874 assert activity.data["cc"] == [user.ap_id]
878 describe "prepare outgoing" do
879 test "it turns mentions into tags" do
881 other_user = insert(:user)
884 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
886 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
887 object = modified["object"]
889 expected_mention = %{
890 "href" => other_user.ap_id,
891 "name" => "@#{other_user.nickname}",
896 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
901 assert Enum.member?(object["tag"], expected_tag)
902 assert Enum.member?(object["tag"], expected_mention)
905 test "it adds the sensitive property" do
908 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
909 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
911 assert modified["object"]["sensitive"]
914 test "it adds the json-ld context and the conversation property" do
917 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
918 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
920 assert modified["@context"] ==
921 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
923 assert modified["object"]["conversation"] == modified["context"]
926 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
929 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
930 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
932 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
935 test "it translates ostatus IDs to external URLs" do
936 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
937 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
941 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
942 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
944 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
947 test "it translates ostatus reply_to IDs to external URLs" do
948 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
949 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
954 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
956 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
958 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
961 test "it strips internal hashtag data" do
964 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
967 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
972 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
974 assert modified["object"]["tag"] == [expected_tag]
977 test "it strips internal fields" do
980 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
982 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
984 assert length(modified["object"]["tag"]) == 2
986 assert is_nil(modified["object"]["emoji"])
987 assert is_nil(modified["object"]["like_count"])
988 assert is_nil(modified["object"]["announcements"])
989 assert is_nil(modified["object"]["announcement_count"])
990 assert is_nil(modified["object"]["context_id"])
993 test "it strips internal fields of article" do
994 activity = insert(:article_activity)
996 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
998 assert length(modified["object"]["tag"]) == 2
1000 assert is_nil(modified["object"]["emoji"])
1001 assert is_nil(modified["object"]["like_count"])
1002 assert is_nil(modified["object"]["announcements"])
1003 assert is_nil(modified["object"]["announcement_count"])
1004 assert is_nil(modified["object"]["context_id"])
1007 test "it adds like collection to object" do
1008 activity = insert(:note_activity)
1009 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1011 assert modified["object"]["likes"]["type"] == "OrderedCollection"
1012 assert modified["object"]["likes"]["totalItems"] == 0
1015 test "the directMessage flag is present" do
1016 user = insert(:user)
1017 other_user = insert(:user)
1019 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1021 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1023 assert modified["directMessage"] == false
1026 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1028 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1030 assert modified["directMessage"] == false
1033 CommonAPI.post(user, %{
1034 "status" => "@#{other_user.nickname} :moominmamma:",
1035 "visibility" => "direct"
1038 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1040 assert modified["directMessage"] == true
1044 describe "user upgrade" do
1045 test "it upgrades a user to activitypub" do
1048 nickname: "rye@niu.moe",
1050 ap_id: "https://niu.moe/users/rye",
1051 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1054 user_two = insert(:user, %{following: [user.follower_address]})
1056 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1057 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1058 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1060 user = User.get_cached_by_id(user.id)
1061 assert user.info.note_count == 1
1063 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1064 assert user.info.ap_enabled
1065 assert user.info.note_count == 1
1066 assert user.follower_address == "https://niu.moe/users/rye/followers"
1068 user = User.get_cached_by_id(user.id)
1069 assert user.info.note_count == 1
1071 activity = Activity.get_by_id(activity.id)
1072 assert user.follower_address in activity.recipients
1078 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1087 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1090 } = user.info.banner
1092 refute "..." in activity.recipients
1094 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1095 refute user.follower_address in unrelated_activity.recipients
1097 user_two = User.get_cached_by_id(user_two.id)
1098 assert user.follower_address in user_two.following
1099 refute "..." in user_two.following
1103 describe "maybe_retire_websub" do
1104 test "it deletes all websub client subscripitions with the user as topic" do
1105 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1106 {:ok, ws} = Repo.insert(subscription)
1108 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1109 {:ok, ws2} = Repo.insert(subscription)
1111 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1113 refute Repo.get(WebsubClientSubscription, ws.id)
1114 assert Repo.get(WebsubClientSubscription, ws2.id)
1118 describe "actor rewriting" do
1119 test "it fixes the actor URL property to be a proper URI" do
1121 "url" => %{"href" => "http://example.com"}
1124 rewritten = Transmogrifier.maybe_fix_user_object(data)
1125 assert rewritten["url"] == "http://example.com"
1129 describe "actor origin containment" do
1130 test "it rejects activities which reference objects with bogus origins" do
1132 "@context" => "https://www.w3.org/ns/activitystreams",
1133 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1134 "actor" => "http://mastodon.example.org/users/admin",
1135 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1136 "object" => "https://info.pleroma.site/activity.json",
1137 "type" => "Announce"
1140 :error = Transmogrifier.handle_incoming(data)
1143 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1145 "@context" => "https://www.w3.org/ns/activitystreams",
1146 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1147 "actor" => "http://mastodon.example.org/users/admin",
1148 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1149 "object" => "https://info.pleroma.site/activity2.json",
1150 "type" => "Announce"
1153 :error = Transmogrifier.handle_incoming(data)
1156 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1158 "@context" => "https://www.w3.org/ns/activitystreams",
1159 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1160 "actor" => "http://mastodon.example.org/users/admin",
1161 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1162 "object" => "https://info.pleroma.site/activity3.json",
1163 "type" => "Announce"
1166 :error = Transmogrifier.handle_incoming(data)
1170 describe "reserialization" do
1171 test "successfully reserializes a message with inReplyTo == nil" do
1172 user = insert(:user)
1175 "@context" => "https://www.w3.org/ns/activitystreams",
1176 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1180 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1185 "attributedTo" => user.ap_id
1187 "actor" => user.ap_id
1190 {:ok, activity} = Transmogrifier.handle_incoming(message)
1192 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1195 test "successfully reserializes a message with AS2 objects in IR" do
1196 user = insert(:user)
1199 "@context" => "https://www.w3.org/ns/activitystreams",
1200 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1204 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1209 "attributedTo" => user.ap_id,
1211 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1212 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1215 "actor" => user.ap_id
1218 {:ok, activity} = Transmogrifier.handle_incoming(message)
1220 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1224 test "Rewrites Answers to Notes" do
1225 user = insert(:user)
1227 {:ok, poll_activity} =
1228 CommonAPI.post(user, %{
1229 "status" => "suya...",
1230 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1233 poll_object = Object.normalize(poll_activity)
1234 # TODO: Replace with CommonAPI vote creation when implemented
1236 File.read!("test/fixtures/mastodon-vote.json")
1238 |> Kernel.put_in(["to"], user.ap_id)
1239 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1240 |> Kernel.put_in(["object", "to"], user.ap_id)
1242 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1243 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1245 assert data["object"]["type"] == "Note"
1248 describe "fix_explicit_addressing" do
1250 user = insert(:user)
1254 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1255 explicitly_mentioned_actors = [
1256 "https://pleroma.gold/users/user1",
1257 "https://pleroma.gold/user2"
1261 "actor" => user.ap_id,
1262 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1265 Enum.map(explicitly_mentioned_actors, fn href ->
1266 %{"type" => "Mention", "href" => href}
1270 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1271 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1272 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1273 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1276 test "does not move actor's follower collection to cc", %{user: user} do
1278 "actor" => user.ap_id,
1279 "to" => [user.follower_address],
1283 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1284 assert user.follower_address in fixed_object["to"]
1285 refute user.follower_address in fixed_object["cc"]
1288 test "removes recipient's follower collection from cc", %{user: user} do
1289 recipient = insert(:user)
1292 "actor" => user.ap_id,
1293 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1294 "cc" => [user.follower_address, recipient.follower_address]
1297 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1299 assert user.follower_address in fixed_object["cc"]
1300 refute recipient.follower_address in fixed_object["cc"]
1301 refute recipient.follower_address in fixed_object["to"]