1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
9 alias Pleroma.Object.Fetcher
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 describe "handle_incoming" do
28 test "it ignores an incoming notice if we already have it" do
29 activity = insert(:note_activity)
32 File.read!("test/fixtures/mastodon-post-activity.json")
34 |> Map.put("object", activity.data["object"])
36 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
38 assert activity == returned_activity
41 test "it fetches replied-to activities if we don't have them" do
43 File.read!("test/fixtures/mastodon-post-activity.json")
48 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
50 data = Map.put(data, "object", object)
51 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
53 returned_object = Object.normalize(returned_activity.data["object"], false)
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 fetch replied-to activities beyond max_replies_depth" do
65 File.read!("test/fixtures/mastodon-post-activity.json")
70 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
72 data = Map.put(data, "object", object)
74 with_mock Pleroma.Web.Federator,
75 max_replies_depth: fn -> 0 end do
76 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
78 returned_object = Object.normalize(returned_activity.data["object"], false)
80 refute Activity.get_create_by_object_ap_id(
81 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
84 assert returned_object.data["inReplyToAtomUri"] ==
85 "https://shitposter.club/notice/2827873"
89 test "it does not crash if the object in inReplyTo can't be fetched" do
91 File.read!("test/fixtures/mastodon-post-activity.json")
96 |> Map.put("inReplyTo", "https://404.site/whatever")
100 |> Map.put("object", object)
102 assert capture_log(fn ->
103 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
104 end) =~ "[error] Couldn't fetch \"\"https://404.site/whatever\"\", error: nil"
107 test "it works for incoming notices" do
108 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
113 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
115 assert data["context"] ==
116 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
118 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
120 assert data["cc"] == [
121 "http://mastodon.example.org/users/admin/followers",
122 "http://localtesting.pleroma.lol/users/lain"
125 assert data["actor"] == "http://mastodon.example.org/users/admin"
127 object = Object.normalize(data["object"]).data
128 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
130 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
132 assert object["cc"] == [
133 "http://mastodon.example.org/users/admin/followers",
134 "http://localtesting.pleroma.lol/users/lain"
137 assert object["actor"] == "http://mastodon.example.org/users/admin"
138 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
140 assert object["context"] ==
141 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
143 assert object["sensitive"] == true
145 user = User.get_cached_by_ap_id(object["actor"])
147 assert user.info.note_count == 1
150 test "it works for incoming notices with hashtags" do
151 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
153 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
154 object = Object.normalize(data["object"])
156 assert Enum.at(object.data["tag"], 2) == "moo"
159 test "it works for incoming questions" do
160 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
162 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
164 object = Object.normalize(activity)
166 assert Enum.all?(object.data["oneOf"], fn choice ->
169 "Everyone knows that!",
170 "25 char limit is dumb",
171 "I can't even fit a funny"
176 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
180 CommonAPI.post(user, %{
181 "status" => "suya...",
182 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
185 object = Object.normalize(activity)
188 File.read!("test/fixtures/mastodon-vote.json")
190 |> Kernel.put_in(["to"], user.ap_id)
191 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
192 |> Kernel.put_in(["object", "to"], user.ap_id)
194 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
195 answer_object = Object.normalize(activity)
196 assert answer_object.data["type"] == "Answer"
197 object = Object.get_by_ap_id(object.data["id"])
200 object.data["oneOf"],
202 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
208 test "it works for incoming notices with contentMap" do
210 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
212 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
213 object = Object.normalize(data["object"])
215 assert object.data["content"] ==
216 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
219 test "it works for incoming notices with to/cc not being an array (kroeg)" do
220 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
222 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
223 object = Object.normalize(data["object"])
225 assert object.data["content"] ==
226 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
229 test "it works for incoming announces with actor being inlined (kroeg)" do
230 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
232 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
234 assert data["actor"] == "https://puckipedia.com/"
237 test "it works for incoming notices with tag not being an array (kroeg)" do
238 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
240 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
241 object = Object.normalize(data["object"])
243 assert object.data["emoji"] == %{
244 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
247 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
249 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
250 object = Object.normalize(data["object"])
252 assert "test" in object.data["tag"]
255 test "it works for incoming notices with url not being a string (prismo)" do
256 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
258 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
259 object = Object.normalize(data["object"])
261 assert object.data["url"] == "https://prismo.news/posts/83"
264 test "it cleans up incoming notices which are not really DMs" do
266 other_user = insert(:user)
268 to = [user.ap_id, other_user.ap_id]
271 File.read!("test/fixtures/mastodon-post-activity.json")
281 data = Map.put(data, "object", object)
283 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
285 assert data["to"] == []
286 assert data["cc"] == to
288 object_data = Object.normalize(activity).data
290 assert object_data["to"] == []
291 assert object_data["cc"] == to
294 test "it works for incoming likes" do
296 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
299 File.read!("test/fixtures/mastodon-like.json")
301 |> Map.put("object", activity.data["object"])
303 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
305 assert data["actor"] == "http://mastodon.example.org/users/admin"
306 assert data["type"] == "Like"
307 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
308 assert data["object"] == activity.data["object"]
311 test "it returns an error for incoming unlikes wihout a like activity" do
313 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
316 File.read!("test/fixtures/mastodon-undo-like.json")
318 |> Map.put("object", activity.data["object"])
320 assert Transmogrifier.handle_incoming(data) == :error
323 test "it works for incoming unlikes with an existing like activity" do
325 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
328 File.read!("test/fixtures/mastodon-like.json")
330 |> Map.put("object", activity.data["object"])
332 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
335 File.read!("test/fixtures/mastodon-undo-like.json")
337 |> Map.put("object", like_data)
338 |> Map.put("actor", like_data["actor"])
340 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
342 assert data["actor"] == "http://mastodon.example.org/users/admin"
343 assert data["type"] == "Undo"
344 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
345 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
348 test "it works for incoming announces" do
349 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
351 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
353 assert data["actor"] == "http://mastodon.example.org/users/admin"
354 assert data["type"] == "Announce"
357 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
359 assert data["object"] ==
360 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
362 assert Activity.get_create_by_object_ap_id(data["object"])
365 test "it works for incoming announces with an existing activity" do
367 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
370 File.read!("test/fixtures/mastodon-announce.json")
372 |> Map.put("object", activity.data["object"])
374 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
376 assert data["actor"] == "http://mastodon.example.org/users/admin"
377 assert data["type"] == "Announce"
380 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
382 assert data["object"] == activity.data["object"]
384 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
387 test "it does not clobber the addressing on announce activities" do
389 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
392 File.read!("test/fixtures/mastodon-announce.json")
394 |> Map.put("object", Object.normalize(activity).data["id"])
395 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
398 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
400 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
403 test "it ensures that as:Public activities make it to their followers collection" do
407 File.read!("test/fixtures/mastodon-post-activity.json")
409 |> Map.put("actor", user.ap_id)
410 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
415 |> Map.put("attributedTo", user.ap_id)
416 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
419 data = Map.put(data, "object", object)
421 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
423 assert data["cc"] == [User.ap_followers(user)]
426 test "it ensures that address fields become lists" do
430 File.read!("test/fixtures/mastodon-post-activity.json")
432 |> Map.put("actor", user.ap_id)
433 |> Map.put("to", nil)
434 |> Map.put("cc", nil)
438 |> Map.put("attributedTo", user.ap_id)
439 |> Map.put("to", nil)
440 |> Map.put("cc", nil)
442 data = Map.put(data, "object", object)
444 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
446 assert !is_nil(data["to"])
447 assert !is_nil(data["cc"])
450 test "it works for incoming update activities" do
451 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
453 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
454 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
457 update_data["object"]
458 |> Map.put("actor", data["actor"])
459 |> Map.put("id", data["actor"])
463 |> Map.put("actor", data["actor"])
464 |> Map.put("object", object)
466 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
468 user = User.get_cached_by_ap_id(data["actor"])
469 assert user.name == "gargle"
471 assert user.avatar["url"] == [
474 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
478 assert user.info.banner["url"] == [
481 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
485 assert user.bio == "<p>Some bio</p>"
488 test "it works for incoming update activities which lock the account" do
489 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
491 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
492 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
495 update_data["object"]
496 |> Map.put("actor", data["actor"])
497 |> Map.put("id", data["actor"])
498 |> Map.put("manuallyApprovesFollowers", true)
502 |> Map.put("actor", data["actor"])
503 |> Map.put("object", object)
505 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
507 user = User.get_cached_by_ap_id(data["actor"])
508 assert user.info.locked == true
511 test "it works for incoming deletes" do
512 activity = insert(:note_activity)
515 File.read!("test/fixtures/mastodon-delete.json")
520 |> Map.put("id", activity.data["object"])
524 |> Map.put("object", object)
525 |> Map.put("actor", activity.data["actor"])
527 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
529 refute Activity.get_by_id(activity.id)
532 test "it fails for incoming deletes with spoofed origin" do
533 activity = insert(:note_activity)
536 File.read!("test/fixtures/mastodon-delete.json")
541 |> Map.put("id", activity.data["object"])
545 |> Map.put("object", object)
547 assert capture_log(fn ->
548 :error = Transmogrifier.handle_incoming(data)
550 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
552 assert Activity.get_by_id(activity.id)
555 test "it works for incoming unannounces with an existing notice" do
557 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
560 File.read!("test/fixtures/mastodon-announce.json")
562 |> Map.put("object", activity.data["object"])
564 {:ok, %Activity{data: announce_data, local: false}} =
565 Transmogrifier.handle_incoming(announce_data)
568 File.read!("test/fixtures/mastodon-undo-announce.json")
570 |> Map.put("object", announce_data)
571 |> Map.put("actor", announce_data["actor"])
573 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
575 assert data["type"] == "Undo"
576 assert data["object"]["type"] == "Announce"
577 assert data["object"]["object"] == activity.data["object"]
579 assert data["object"]["id"] ==
580 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
583 test "it works for incomming unfollows with an existing follow" do
587 File.read!("test/fixtures/mastodon-follow-activity.json")
589 |> Map.put("object", user.ap_id)
591 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
594 File.read!("test/fixtures/mastodon-unfollow-activity.json")
596 |> Map.put("object", follow_data)
598 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
600 assert data["type"] == "Undo"
601 assert data["object"]["type"] == "Follow"
602 assert data["object"]["object"] == user.ap_id
603 assert data["actor"] == "http://mastodon.example.org/users/admin"
605 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
608 test "it works for incoming blocks" do
612 File.read!("test/fixtures/mastodon-block-activity.json")
614 |> Map.put("object", user.ap_id)
616 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
618 assert data["type"] == "Block"
619 assert data["object"] == user.ap_id
620 assert data["actor"] == "http://mastodon.example.org/users/admin"
622 blocker = User.get_cached_by_ap_id(data["actor"])
624 assert User.blocks?(blocker, user)
627 test "incoming blocks successfully tear down any follow relationship" do
628 blocker = insert(:user)
629 blocked = insert(:user)
632 File.read!("test/fixtures/mastodon-block-activity.json")
634 |> Map.put("object", blocked.ap_id)
635 |> Map.put("actor", blocker.ap_id)
637 {:ok, blocker} = User.follow(blocker, blocked)
638 {:ok, blocked} = User.follow(blocked, blocker)
640 assert User.following?(blocker, blocked)
641 assert User.following?(blocked, blocker)
643 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
645 assert data["type"] == "Block"
646 assert data["object"] == blocked.ap_id
647 assert data["actor"] == blocker.ap_id
649 blocker = User.get_cached_by_ap_id(data["actor"])
650 blocked = User.get_cached_by_ap_id(data["object"])
652 assert User.blocks?(blocker, blocked)
654 refute User.following?(blocker, blocked)
655 refute User.following?(blocked, blocker)
658 test "it works for incoming unblocks with an existing block" do
662 File.read!("test/fixtures/mastodon-block-activity.json")
664 |> Map.put("object", user.ap_id)
666 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
669 File.read!("test/fixtures/mastodon-unblock-activity.json")
671 |> Map.put("object", block_data)
673 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
674 assert data["type"] == "Undo"
675 assert data["object"]["type"] == "Block"
676 assert data["object"]["object"] == user.ap_id
677 assert data["actor"] == "http://mastodon.example.org/users/admin"
679 blocker = User.get_cached_by_ap_id(data["actor"])
681 refute User.blocks?(blocker, user)
684 test "it works for incoming accepts which were pre-accepted" do
685 follower = insert(:user)
686 followed = insert(:user)
688 {:ok, follower} = User.follow(follower, followed)
689 assert User.following?(follower, followed) == true
691 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
694 File.read!("test/fixtures/mastodon-accept-activity.json")
696 |> Map.put("actor", followed.ap_id)
699 accept_data["object"]
700 |> Map.put("actor", follower.ap_id)
701 |> Map.put("id", follow_activity.data["id"])
703 accept_data = Map.put(accept_data, "object", object)
705 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
706 refute activity.local
708 assert activity.data["object"] == follow_activity.data["id"]
710 follower = User.get_cached_by_id(follower.id)
712 assert User.following?(follower, followed) == true
715 test "it works for incoming accepts which were orphaned" do
716 follower = insert(:user)
717 followed = insert(:user, %{info: %User.Info{locked: true}})
719 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
722 File.read!("test/fixtures/mastodon-accept-activity.json")
724 |> Map.put("actor", followed.ap_id)
727 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
729 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
730 assert activity.data["object"] == follow_activity.data["id"]
732 follower = User.get_cached_by_id(follower.id)
734 assert User.following?(follower, followed) == true
737 test "it works for incoming accepts which are referenced by IRI only" do
738 follower = insert(:user)
739 followed = insert(:user, %{info: %User.Info{locked: true}})
741 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
744 File.read!("test/fixtures/mastodon-accept-activity.json")
746 |> Map.put("actor", followed.ap_id)
747 |> Map.put("object", follow_activity.data["id"])
749 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
750 assert activity.data["object"] == follow_activity.data["id"]
752 follower = User.get_cached_by_id(follower.id)
754 assert User.following?(follower, followed) == true
757 test "it fails for incoming accepts which cannot be correlated" do
758 follower = insert(:user)
759 followed = insert(:user, %{info: %User.Info{locked: true}})
762 File.read!("test/fixtures/mastodon-accept-activity.json")
764 |> Map.put("actor", followed.ap_id)
767 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
769 :error = Transmogrifier.handle_incoming(accept_data)
771 follower = User.get_cached_by_id(follower.id)
773 refute User.following?(follower, followed) == true
776 test "it fails for incoming rejects which cannot be correlated" do
777 follower = insert(:user)
778 followed = insert(:user, %{info: %User.Info{locked: true}})
781 File.read!("test/fixtures/mastodon-reject-activity.json")
783 |> Map.put("actor", followed.ap_id)
786 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
788 :error = Transmogrifier.handle_incoming(accept_data)
790 follower = User.get_cached_by_id(follower.id)
792 refute User.following?(follower, followed) == true
795 test "it works for incoming rejects which are orphaned" do
796 follower = insert(:user)
797 followed = insert(:user, %{info: %User.Info{locked: true}})
799 {:ok, follower} = User.follow(follower, followed)
800 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
802 assert User.following?(follower, followed) == true
805 File.read!("test/fixtures/mastodon-reject-activity.json")
807 |> Map.put("actor", followed.ap_id)
810 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
812 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
813 refute activity.local
815 follower = User.get_cached_by_id(follower.id)
817 assert User.following?(follower, followed) == false
820 test "it works for incoming rejects which are referenced by IRI only" do
821 follower = insert(:user)
822 followed = insert(:user, %{info: %User.Info{locked: true}})
824 {:ok, follower} = User.follow(follower, followed)
825 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
827 assert User.following?(follower, followed) == true
830 File.read!("test/fixtures/mastodon-reject-activity.json")
832 |> Map.put("actor", followed.ap_id)
833 |> Map.put("object", follow_activity.data["id"])
835 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
837 follower = User.get_cached_by_id(follower.id)
839 assert User.following?(follower, followed) == false
842 test "it rejects activities without a valid ID" do
846 File.read!("test/fixtures/mastodon-follow-activity.json")
848 |> Map.put("object", user.ap_id)
851 :error = Transmogrifier.handle_incoming(data)
854 test "it remaps video URLs as attachments if necessary" do
856 Fetcher.fetch_object_from_id(
857 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
862 "mediaType" => "video/mp4",
864 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
865 "mimeType" => "video/mp4",
870 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
871 "mediaType" => "video/mp4",
878 assert object.data["url"] ==
879 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
881 assert object.data["attachment"] == [attachment]
884 test "it accepts Flag activities" do
886 other_user = insert(:user)
888 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
889 object = Object.normalize(activity.data["object"])
892 "@context" => "https://www.w3.org/ns/activitystreams",
893 "cc" => [user.ap_id],
894 "object" => [user.ap_id, object.data["id"]],
896 "content" => "blocked AND reported!!!",
897 "actor" => other_user.ap_id
900 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
902 assert activity.data["object"] == [user.ap_id, object.data["id"]]
903 assert activity.data["content"] == "blocked AND reported!!!"
904 assert activity.data["actor"] == other_user.ap_id
905 assert activity.data["cc"] == [user.ap_id]
909 describe "prepare outgoing" do
910 test "it turns mentions into tags" do
912 other_user = insert(:user)
915 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
917 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
918 object = modified["object"]
920 expected_mention = %{
921 "href" => other_user.ap_id,
922 "name" => "@#{other_user.nickname}",
927 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
932 assert Enum.member?(object["tag"], expected_tag)
933 assert Enum.member?(object["tag"], expected_mention)
936 test "it adds the sensitive property" do
939 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
940 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
942 assert modified["object"]["sensitive"]
945 test "it adds the json-ld context and the conversation property" do
948 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
949 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
951 assert modified["@context"] ==
952 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
954 assert modified["object"]["conversation"] == modified["context"]
957 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
960 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
961 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
963 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
966 test "it translates ostatus IDs to external URLs" do
967 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
968 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
972 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
973 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
975 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
978 test "it translates ostatus reply_to IDs to external URLs" do
979 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
980 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
985 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
987 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
989 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
992 test "it strips internal hashtag data" do
995 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
998 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1003 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1005 assert modified["object"]["tag"] == [expected_tag]
1008 test "it strips internal fields" do
1009 user = insert(:user)
1011 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1013 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1015 assert length(modified["object"]["tag"]) == 2
1017 assert is_nil(modified["object"]["emoji"])
1018 assert is_nil(modified["object"]["like_count"])
1019 assert is_nil(modified["object"]["announcements"])
1020 assert is_nil(modified["object"]["announcement_count"])
1021 assert is_nil(modified["object"]["context_id"])
1024 test "it strips internal fields of article" do
1025 activity = insert(:article_activity)
1027 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1029 assert length(modified["object"]["tag"]) == 2
1031 assert is_nil(modified["object"]["emoji"])
1032 assert is_nil(modified["object"]["like_count"])
1033 assert is_nil(modified["object"]["announcements"])
1034 assert is_nil(modified["object"]["announcement_count"])
1035 assert is_nil(modified["object"]["context_id"])
1038 test "it adds like collection to object" do
1039 activity = insert(:note_activity)
1040 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1042 assert modified["object"]["likes"]["type"] == "OrderedCollection"
1043 assert modified["object"]["likes"]["totalItems"] == 0
1046 test "the directMessage flag is present" do
1047 user = insert(:user)
1048 other_user = insert(:user)
1050 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1052 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1054 assert modified["directMessage"] == false
1057 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1059 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1061 assert modified["directMessage"] == false
1064 CommonAPI.post(user, %{
1065 "status" => "@#{other_user.nickname} :moominmamma:",
1066 "visibility" => "direct"
1069 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1071 assert modified["directMessage"] == true
1075 describe "user upgrade" do
1076 test "it upgrades a user to activitypub" do
1079 nickname: "rye@niu.moe",
1081 ap_id: "https://niu.moe/users/rye",
1082 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1085 user_two = insert(:user, %{following: [user.follower_address]})
1087 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1088 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1089 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1091 user = User.get_cached_by_id(user.id)
1092 assert user.info.note_count == 1
1094 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1095 assert user.info.ap_enabled
1096 assert user.info.note_count == 1
1097 assert user.follower_address == "https://niu.moe/users/rye/followers"
1099 user = User.get_cached_by_id(user.id)
1100 assert user.info.note_count == 1
1102 activity = Activity.get_by_id(activity.id)
1103 assert user.follower_address in activity.recipients
1109 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1118 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1121 } = user.info.banner
1123 refute "..." in activity.recipients
1125 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1126 refute user.follower_address in unrelated_activity.recipients
1128 user_two = User.get_cached_by_id(user_two.id)
1129 assert user.follower_address in user_two.following
1130 refute "..." in user_two.following
1134 describe "maybe_retire_websub" do
1135 test "it deletes all websub client subscripitions with the user as topic" do
1136 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1137 {:ok, ws} = Repo.insert(subscription)
1139 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1140 {:ok, ws2} = Repo.insert(subscription)
1142 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1144 refute Repo.get(WebsubClientSubscription, ws.id)
1145 assert Repo.get(WebsubClientSubscription, ws2.id)
1149 describe "actor rewriting" do
1150 test "it fixes the actor URL property to be a proper URI" do
1152 "url" => %{"href" => "http://example.com"}
1155 rewritten = Transmogrifier.maybe_fix_user_object(data)
1156 assert rewritten["url"] == "http://example.com"
1160 describe "actor origin containment" do
1161 test "it rejects activities which reference objects with bogus origins" do
1163 "@context" => "https://www.w3.org/ns/activitystreams",
1164 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1165 "actor" => "http://mastodon.example.org/users/admin",
1166 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1167 "object" => "https://info.pleroma.site/activity.json",
1168 "type" => "Announce"
1171 :error = Transmogrifier.handle_incoming(data)
1174 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1176 "@context" => "https://www.w3.org/ns/activitystreams",
1177 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1178 "actor" => "http://mastodon.example.org/users/admin",
1179 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1180 "object" => "https://info.pleroma.site/activity2.json",
1181 "type" => "Announce"
1184 :error = Transmogrifier.handle_incoming(data)
1187 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1189 "@context" => "https://www.w3.org/ns/activitystreams",
1190 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1191 "actor" => "http://mastodon.example.org/users/admin",
1192 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1193 "object" => "https://info.pleroma.site/activity3.json",
1194 "type" => "Announce"
1197 :error = Transmogrifier.handle_incoming(data)
1201 describe "reserialization" do
1202 test "successfully reserializes a message with inReplyTo == nil" do
1203 user = insert(:user)
1206 "@context" => "https://www.w3.org/ns/activitystreams",
1207 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1211 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1216 "attributedTo" => user.ap_id
1218 "actor" => user.ap_id
1221 {:ok, activity} = Transmogrifier.handle_incoming(message)
1223 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1226 test "successfully reserializes a message with AS2 objects in IR" do
1227 user = insert(:user)
1230 "@context" => "https://www.w3.org/ns/activitystreams",
1231 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1235 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1240 "attributedTo" => user.ap_id,
1242 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1243 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1246 "actor" => user.ap_id
1249 {:ok, activity} = Transmogrifier.handle_incoming(message)
1251 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1255 test "Rewrites Answers to Notes" do
1256 user = insert(:user)
1258 {:ok, poll_activity} =
1259 CommonAPI.post(user, %{
1260 "status" => "suya...",
1261 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1264 poll_object = Object.normalize(poll_activity)
1265 # TODO: Replace with CommonAPI vote creation when implemented
1267 File.read!("test/fixtures/mastodon-vote.json")
1269 |> Kernel.put_in(["to"], user.ap_id)
1270 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1271 |> Kernel.put_in(["object", "to"], user.ap_id)
1273 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1274 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1276 assert data["object"]["type"] == "Note"
1279 describe "fix_explicit_addressing" do
1281 user = insert(:user)
1285 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1286 explicitly_mentioned_actors = [
1287 "https://pleroma.gold/users/user1",
1288 "https://pleroma.gold/user2"
1292 "actor" => user.ap_id,
1293 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1296 Enum.map(explicitly_mentioned_actors, fn href ->
1297 %{"type" => "Mention", "href" => href}
1301 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1302 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1303 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1304 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1307 test "does not move actor's follower collection to cc", %{user: user} do
1309 "actor" => user.ap_id,
1310 "to" => [user.follower_address],
1314 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1315 assert user.follower_address in fixed_object["to"]
1316 refute user.follower_address in fixed_object["cc"]
1319 test "removes recipient's follower collection from cc", %{user: user} do
1320 recipient = insert(:user)
1323 "actor" => user.ap_id,
1324 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1325 "cc" => [user.follower_address, recipient.follower_address]
1328 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1330 assert user.follower_address in fixed_object["cc"]
1331 refute recipient.follower_address in fixed_object["cc"]
1332 refute recipient.follower_address in fixed_object["to"]