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
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Transmogrifier
15 alias Pleroma.Web.CommonAPI
16 alias Pleroma.Web.OStatus
17 alias Pleroma.Web.Websub.WebsubClientSubscription
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 clear_config([:instance, :max_remote_account_fields])
30 describe "handle_incoming" do
31 test "it ignores an incoming notice if we already have it" do
32 activity = insert(:note_activity)
35 File.read!("test/fixtures/mastodon-post-activity.json")
37 |> Map.put("object", Object.normalize(activity).data)
39 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
41 assert activity == returned_activity
44 test "it fetches replied-to activities if we don't have them" do
46 File.read!("test/fixtures/mastodon-post-activity.json")
51 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
53 data = Map.put(data, "object", object)
54 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
55 returned_object = Object.normalize(returned_activity, false)
58 Activity.get_create_by_object_ap_id(
59 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
62 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
65 test "it does not fetch replied-to activities beyond max_replies_depth" do
67 File.read!("test/fixtures/mastodon-post-activity.json")
72 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
74 data = Map.put(data, "object", object)
76 with_mock Pleroma.Web.Federator,
77 allowed_incoming_reply_depth?: fn _ -> false end do
78 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
80 returned_object = Object.normalize(returned_activity, false)
82 refute Activity.get_create_by_object_ap_id(
83 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
86 assert returned_object.data["inReplyToAtomUri"] ==
87 "https://shitposter.club/notice/2827873"
91 test "it does not crash if the object in inReplyTo can't be fetched" do
93 File.read!("test/fixtures/mastodon-post-activity.json")
98 |> Map.put("inReplyTo", "https://404.site/whatever")
102 |> Map.put("object", object)
104 assert capture_log(fn ->
105 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
106 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
109 test "it works for incoming notices" do
110 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
112 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
115 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
117 assert data["context"] ==
118 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
120 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
122 assert data["cc"] == [
123 "http://mastodon.example.org/users/admin/followers",
124 "http://localtesting.pleroma.lol/users/lain"
127 assert data["actor"] == "http://mastodon.example.org/users/admin"
129 object_data = Object.normalize(data["object"]).data
131 assert object_data["id"] ==
132 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
134 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
136 assert object_data["cc"] == [
137 "http://mastodon.example.org/users/admin/followers",
138 "http://localtesting.pleroma.lol/users/lain"
141 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
142 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
144 assert object_data["context"] ==
145 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
147 assert object_data["sensitive"] == true
149 user = User.get_cached_by_ap_id(object_data["actor"])
151 assert user.info.note_count == 1
154 test "it works for incoming notices with hashtags" do
155 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
157 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
158 object = Object.normalize(data["object"])
160 assert Enum.at(object.data["tag"], 2) == "moo"
163 test "it works for incoming questions" do
164 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
166 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
168 object = Object.normalize(activity)
170 assert Enum.all?(object.data["oneOf"], fn choice ->
173 "Everyone knows that!",
174 "25 char limit is dumb",
175 "I can't even fit a funny"
180 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
184 CommonAPI.post(user, %{
185 "status" => "suya...",
186 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
189 object = Object.normalize(activity)
192 File.read!("test/fixtures/mastodon-vote.json")
194 |> Kernel.put_in(["to"], user.ap_id)
195 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
196 |> Kernel.put_in(["object", "to"], user.ap_id)
198 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
199 answer_object = Object.normalize(activity)
200 assert answer_object.data["type"] == "Answer"
201 object = Object.get_by_ap_id(object.data["id"])
204 object.data["oneOf"],
206 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
212 test "it works for incoming notices with contentMap" do
214 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
216 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
217 object = Object.normalize(data["object"])
219 assert object.data["content"] ==
220 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
223 test "it works for incoming notices with to/cc not being an array (kroeg)" do
224 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
226 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
227 object = Object.normalize(data["object"])
229 assert object.data["content"] ==
230 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
233 test "it works for incoming announces with actor being inlined (kroeg)" do
234 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
236 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
238 assert data["actor"] == "https://puckipedia.com/"
241 test "it works for incoming notices with tag not being an array (kroeg)" do
242 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
244 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
245 object = Object.normalize(data["object"])
247 assert object.data["emoji"] == %{
248 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
251 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
253 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
254 object = Object.normalize(data["object"])
256 assert "test" in object.data["tag"]
259 test "it works for incoming notices with url not being a string (prismo)" do
260 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
262 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
263 object = Object.normalize(data["object"])
265 assert object.data["url"] == "https://prismo.news/posts/83"
268 test "it cleans up incoming notices which are not really DMs" do
270 other_user = insert(:user)
272 to = [user.ap_id, other_user.ap_id]
275 File.read!("test/fixtures/mastodon-post-activity.json")
285 data = Map.put(data, "object", object)
287 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
289 assert data["to"] == []
290 assert data["cc"] == to
292 object_data = Object.normalize(activity).data
294 assert object_data["to"] == []
295 assert object_data["cc"] == to
298 test "it works for incoming likes" do
300 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
303 File.read!("test/fixtures/mastodon-like.json")
305 |> Map.put("object", activity.data["object"])
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"] == "Like"
311 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
312 assert data["object"] == activity.data["object"]
315 test "it returns an error for incoming unlikes wihout a like activity" do
317 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
320 File.read!("test/fixtures/mastodon-undo-like.json")
322 |> Map.put("object", activity.data["object"])
324 assert Transmogrifier.handle_incoming(data) == :error
327 test "it works for incoming unlikes with an existing like activity" do
329 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
332 File.read!("test/fixtures/mastodon-like.json")
334 |> Map.put("object", activity.data["object"])
336 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
339 File.read!("test/fixtures/mastodon-undo-like.json")
341 |> Map.put("object", like_data)
342 |> Map.put("actor", like_data["actor"])
344 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
346 assert data["actor"] == "http://mastodon.example.org/users/admin"
347 assert data["type"] == "Undo"
348 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
349 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
352 test "it works for incoming announces" do
353 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
355 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
357 assert data["actor"] == "http://mastodon.example.org/users/admin"
358 assert data["type"] == "Announce"
361 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
363 assert data["object"] ==
364 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
366 assert Activity.get_create_by_object_ap_id(data["object"])
369 test "it works for incoming announces with an existing activity" do
371 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
374 File.read!("test/fixtures/mastodon-announce.json")
376 |> Map.put("object", activity.data["object"])
378 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
380 assert data["actor"] == "http://mastodon.example.org/users/admin"
381 assert data["type"] == "Announce"
384 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
386 assert data["object"] == activity.data["object"]
388 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
391 test "it does not clobber the addressing on announce activities" do
393 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
396 File.read!("test/fixtures/mastodon-announce.json")
398 |> Map.put("object", Object.normalize(activity).data["id"])
399 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
402 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
404 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
407 test "it ensures that as:Public activities make it to their followers collection" do
411 File.read!("test/fixtures/mastodon-post-activity.json")
413 |> Map.put("actor", user.ap_id)
414 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
419 |> Map.put("attributedTo", user.ap_id)
420 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
422 |> Map.put("id", user.ap_id <> "/activities/12345678")
424 data = Map.put(data, "object", object)
426 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
428 assert data["cc"] == [User.ap_followers(user)]
431 test "it ensures that address fields become lists" do
435 File.read!("test/fixtures/mastodon-post-activity.json")
437 |> Map.put("actor", user.ap_id)
438 |> Map.put("to", nil)
439 |> Map.put("cc", nil)
443 |> Map.put("attributedTo", user.ap_id)
444 |> Map.put("to", nil)
445 |> Map.put("cc", nil)
446 |> Map.put("id", user.ap_id <> "/activities/12345678")
448 data = Map.put(data, "object", object)
450 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
452 assert !is_nil(data["to"])
453 assert !is_nil(data["cc"])
456 test "it strips internal likes" do
458 File.read!("test/fixtures/mastodon-post-activity.json")
463 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
464 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
466 "type" => "OrderedCollection"
469 object = Map.put(data["object"], "likes", likes)
470 data = Map.put(data, "object", object)
472 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
474 refute Map.has_key?(object.data, "likes")
477 test "it works for incoming update activities" do
478 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
480 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
481 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
484 update_data["object"]
485 |> Map.put("actor", data["actor"])
486 |> Map.put("id", data["actor"])
490 |> Map.put("actor", data["actor"])
491 |> Map.put("object", object)
493 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
495 user = User.get_cached_by_ap_id(data["actor"])
496 assert user.name == "gargle"
498 assert user.avatar["url"] == [
501 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
505 assert user.info.banner["url"] == [
508 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
512 assert user.bio == "<p>Some bio</p>"
515 test "it works with custom profile fields" do
517 "test/fixtures/mastodon-post-activity.json"
520 |> Transmogrifier.handle_incoming()
522 user = User.get_cached_by_ap_id(activity.actor)
524 assert User.Info.fields(user.info) == [
525 %{"name" => "foo", "value" => "bar"},
526 %{"name" => "foo1", "value" => "bar1"}
529 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
532 update_data["object"]
533 |> Map.put("actor", user.ap_id)
534 |> Map.put("id", user.ap_id)
538 |> Map.put("actor", user.ap_id)
539 |> Map.put("object", object)
541 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
543 user = User.get_cached_by_ap_id(user.ap_id)
545 assert User.Info.fields(user.info) == [
546 %{"name" => "foo", "value" => "updated"},
547 %{"name" => "foo1", "value" => "updated"}
550 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
553 put_in(update_data, ["object", "attachment"], [
554 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
555 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
556 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
559 {:ok, _} = Transmogrifier.handle_incoming(update_data)
561 user = User.get_cached_by_ap_id(user.ap_id)
563 assert User.Info.fields(user.info) == [
564 %{"name" => "foo", "value" => "updated"},
565 %{"name" => "foo1", "value" => "updated"}
568 update_data = put_in(update_data, ["object", "attachment"], [])
570 {:ok, _} = Transmogrifier.handle_incoming(update_data)
572 user = User.get_cached_by_ap_id(user.ap_id)
574 assert User.Info.fields(user.info) == []
577 test "it works for incoming update activities which lock the account" do
578 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
580 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
581 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
584 update_data["object"]
585 |> Map.put("actor", data["actor"])
586 |> Map.put("id", data["actor"])
587 |> Map.put("manuallyApprovesFollowers", true)
591 |> Map.put("actor", data["actor"])
592 |> Map.put("object", object)
594 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
596 user = User.get_cached_by_ap_id(data["actor"])
597 assert user.info.locked == true
600 test "it works for incoming deletes" do
601 activity = insert(:note_activity)
604 File.read!("test/fixtures/mastodon-delete.json")
609 |> Map.put("id", activity.data["object"])
613 |> Map.put("object", object)
614 |> Map.put("actor", activity.data["actor"])
616 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
618 refute Activity.get_by_id(activity.id)
621 test "it fails for incoming deletes with spoofed origin" do
622 activity = insert(:note_activity)
625 File.read!("test/fixtures/mastodon-delete.json")
630 |> Map.put("id", activity.data["object"])
634 |> Map.put("object", object)
636 assert capture_log(fn ->
637 :error = Transmogrifier.handle_incoming(data)
639 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
641 assert Activity.get_by_id(activity.id)
644 test "it works for incoming user deletes" do
645 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
648 File.read!("test/fixtures/mastodon-delete-user.json")
651 {:ok, _} = Transmogrifier.handle_incoming(data)
652 ObanHelpers.perform_all()
654 refute User.get_cached_by_ap_id(ap_id)
657 test "it fails for incoming user deletes with spoofed origin" do
658 %{ap_id: ap_id} = insert(:user)
661 File.read!("test/fixtures/mastodon-delete-user.json")
663 |> Map.put("actor", ap_id)
665 assert :error == Transmogrifier.handle_incoming(data)
666 assert User.get_cached_by_ap_id(ap_id)
669 test "it works for incoming unannounces with an existing notice" do
671 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
674 File.read!("test/fixtures/mastodon-announce.json")
676 |> Map.put("object", activity.data["object"])
678 {:ok, %Activity{data: announce_data, local: false}} =
679 Transmogrifier.handle_incoming(announce_data)
682 File.read!("test/fixtures/mastodon-undo-announce.json")
684 |> Map.put("object", announce_data)
685 |> Map.put("actor", announce_data["actor"])
687 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
689 assert data["type"] == "Undo"
690 assert object_data = data["object"]
691 assert object_data["type"] == "Announce"
692 assert object_data["object"] == activity.data["object"]
694 assert object_data["id"] ==
695 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
698 test "it works for incomming unfollows with an existing follow" do
702 File.read!("test/fixtures/mastodon-follow-activity.json")
704 |> Map.put("object", user.ap_id)
706 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
709 File.read!("test/fixtures/mastodon-unfollow-activity.json")
711 |> Map.put("object", follow_data)
713 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
715 assert data["type"] == "Undo"
716 assert data["object"]["type"] == "Follow"
717 assert data["object"]["object"] == user.ap_id
718 assert data["actor"] == "http://mastodon.example.org/users/admin"
720 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
723 test "it works for incoming blocks" do
727 File.read!("test/fixtures/mastodon-block-activity.json")
729 |> Map.put("object", user.ap_id)
731 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
733 assert data["type"] == "Block"
734 assert data["object"] == user.ap_id
735 assert data["actor"] == "http://mastodon.example.org/users/admin"
737 blocker = User.get_cached_by_ap_id(data["actor"])
739 assert User.blocks?(blocker, user)
742 test "incoming blocks successfully tear down any follow relationship" do
743 blocker = insert(:user)
744 blocked = insert(:user)
747 File.read!("test/fixtures/mastodon-block-activity.json")
749 |> Map.put("object", blocked.ap_id)
750 |> Map.put("actor", blocker.ap_id)
752 {:ok, blocker} = User.follow(blocker, blocked)
753 {:ok, blocked} = User.follow(blocked, blocker)
755 assert User.following?(blocker, blocked)
756 assert User.following?(blocked, blocker)
758 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
760 assert data["type"] == "Block"
761 assert data["object"] == blocked.ap_id
762 assert data["actor"] == blocker.ap_id
764 blocker = User.get_cached_by_ap_id(data["actor"])
765 blocked = User.get_cached_by_ap_id(data["object"])
767 assert User.blocks?(blocker, blocked)
769 refute User.following?(blocker, blocked)
770 refute User.following?(blocked, blocker)
773 test "it works for incoming unblocks with an existing block" do
777 File.read!("test/fixtures/mastodon-block-activity.json")
779 |> Map.put("object", user.ap_id)
781 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
784 File.read!("test/fixtures/mastodon-unblock-activity.json")
786 |> Map.put("object", block_data)
788 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
789 assert data["type"] == "Undo"
790 assert data["object"]["type"] == "Block"
791 assert data["object"]["object"] == user.ap_id
792 assert data["actor"] == "http://mastodon.example.org/users/admin"
794 blocker = User.get_cached_by_ap_id(data["actor"])
796 refute User.blocks?(blocker, user)
799 test "it works for incoming accepts which were pre-accepted" do
800 follower = insert(:user)
801 followed = insert(:user)
803 {:ok, follower} = User.follow(follower, followed)
804 assert User.following?(follower, followed) == true
806 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
809 File.read!("test/fixtures/mastodon-accept-activity.json")
811 |> Map.put("actor", followed.ap_id)
814 accept_data["object"]
815 |> Map.put("actor", follower.ap_id)
816 |> Map.put("id", follow_activity.data["id"])
818 accept_data = Map.put(accept_data, "object", object)
820 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
821 refute activity.local
823 assert activity.data["object"] == follow_activity.data["id"]
825 follower = User.get_cached_by_id(follower.id)
827 assert User.following?(follower, followed) == true
830 test "it works for incoming accepts which were orphaned" do
831 follower = insert(:user)
832 followed = insert(:user, %{info: %User.Info{locked: true}})
834 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
837 File.read!("test/fixtures/mastodon-accept-activity.json")
839 |> Map.put("actor", followed.ap_id)
842 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
844 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
845 assert activity.data["object"] == follow_activity.data["id"]
847 follower = User.get_cached_by_id(follower.id)
849 assert User.following?(follower, followed) == true
852 test "it works for incoming accepts which are referenced by IRI only" do
853 follower = insert(:user)
854 followed = insert(:user, %{info: %User.Info{locked: true}})
856 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
859 File.read!("test/fixtures/mastodon-accept-activity.json")
861 |> Map.put("actor", followed.ap_id)
862 |> Map.put("object", follow_activity.data["id"])
864 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
865 assert activity.data["object"] == follow_activity.data["id"]
867 follower = User.get_cached_by_id(follower.id)
869 assert User.following?(follower, followed) == true
872 test "it fails for incoming accepts which cannot be correlated" do
873 follower = insert(:user)
874 followed = insert(:user, %{info: %User.Info{locked: true}})
877 File.read!("test/fixtures/mastodon-accept-activity.json")
879 |> Map.put("actor", followed.ap_id)
882 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
884 :error = Transmogrifier.handle_incoming(accept_data)
886 follower = User.get_cached_by_id(follower.id)
888 refute User.following?(follower, followed) == true
891 test "it fails for incoming rejects which cannot be correlated" do
892 follower = insert(:user)
893 followed = insert(:user, %{info: %User.Info{locked: true}})
896 File.read!("test/fixtures/mastodon-reject-activity.json")
898 |> Map.put("actor", followed.ap_id)
901 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
903 :error = Transmogrifier.handle_incoming(accept_data)
905 follower = User.get_cached_by_id(follower.id)
907 refute User.following?(follower, followed) == true
910 test "it works for incoming rejects which are orphaned" do
911 follower = insert(:user)
912 followed = insert(:user, %{info: %User.Info{locked: true}})
914 {:ok, follower} = User.follow(follower, followed)
915 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
917 assert User.following?(follower, followed) == true
920 File.read!("test/fixtures/mastodon-reject-activity.json")
922 |> Map.put("actor", followed.ap_id)
925 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
927 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
928 refute activity.local
930 follower = User.get_cached_by_id(follower.id)
932 assert User.following?(follower, followed) == false
935 test "it works for incoming rejects which are referenced by IRI only" do
936 follower = insert(:user)
937 followed = insert(:user, %{info: %User.Info{locked: true}})
939 {:ok, follower} = User.follow(follower, followed)
940 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
942 assert User.following?(follower, followed) == true
945 File.read!("test/fixtures/mastodon-reject-activity.json")
947 |> Map.put("actor", followed.ap_id)
948 |> Map.put("object", follow_activity.data["id"])
950 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
952 follower = User.get_cached_by_id(follower.id)
954 assert User.following?(follower, followed) == false
957 test "it rejects activities without a valid ID" do
961 File.read!("test/fixtures/mastodon-follow-activity.json")
963 |> Map.put("object", user.ap_id)
966 :error = Transmogrifier.handle_incoming(data)
969 test "it remaps video URLs as attachments if necessary" do
971 Fetcher.fetch_object_from_id(
972 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
977 "mediaType" => "video/mp4",
979 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
980 "mimeType" => "video/mp4",
985 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
986 "mediaType" => "video/mp4",
993 assert object.data["url"] ==
994 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
996 assert object.data["attachment"] == [attachment]
999 test "it accepts Flag activities" do
1000 user = insert(:user)
1001 other_user = insert(:user)
1003 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1004 object = Object.normalize(activity)
1007 "@context" => "https://www.w3.org/ns/activitystreams",
1008 "cc" => [user.ap_id],
1009 "object" => [user.ap_id, object.data["id"]],
1011 "content" => "blocked AND reported!!!",
1012 "actor" => other_user.ap_id
1015 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1017 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1018 assert activity.data["content"] == "blocked AND reported!!!"
1019 assert activity.data["actor"] == other_user.ap_id
1020 assert activity.data["cc"] == [user.ap_id]
1024 describe "prepare outgoing" do
1025 test "it turns mentions into tags" do
1026 user = insert(:user)
1027 other_user = insert(:user)
1030 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1032 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1033 object = modified["object"]
1035 expected_mention = %{
1036 "href" => other_user.ap_id,
1037 "name" => "@#{other_user.nickname}",
1042 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1043 "type" => "Hashtag",
1047 assert Enum.member?(object["tag"], expected_tag)
1048 assert Enum.member?(object["tag"], expected_mention)
1051 test "it adds the sensitive property" do
1052 user = insert(:user)
1054 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1055 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1057 assert modified["object"]["sensitive"]
1060 test "it adds the json-ld context and the conversation property" do
1061 user = insert(:user)
1063 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1064 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1066 assert modified["@context"] ==
1067 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1069 assert modified["object"]["conversation"] == modified["context"]
1072 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1073 user = insert(:user)
1075 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1076 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1078 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1081 test "it translates ostatus IDs to external URLs" do
1082 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1083 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1085 user = insert(:user)
1087 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1088 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1090 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1093 test "it translates ostatus reply_to IDs to external URLs" do
1094 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1095 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1097 user = insert(:user)
1100 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1102 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1104 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1107 test "it strips internal hashtag data" do
1108 user = insert(:user)
1110 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1113 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1114 "type" => "Hashtag",
1118 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1120 assert modified["object"]["tag"] == [expected_tag]
1123 test "it strips internal fields" do
1124 user = insert(:user)
1126 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1128 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1130 assert length(modified["object"]["tag"]) == 2
1132 assert is_nil(modified["object"]["emoji"])
1133 assert is_nil(modified["object"]["like_count"])
1134 assert is_nil(modified["object"]["announcements"])
1135 assert is_nil(modified["object"]["announcement_count"])
1136 assert is_nil(modified["object"]["context_id"])
1139 test "it strips internal fields of article" do
1140 activity = insert(:article_activity)
1142 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1144 assert length(modified["object"]["tag"]) == 2
1146 assert is_nil(modified["object"]["emoji"])
1147 assert is_nil(modified["object"]["like_count"])
1148 assert is_nil(modified["object"]["announcements"])
1149 assert is_nil(modified["object"]["announcement_count"])
1150 assert is_nil(modified["object"]["context_id"])
1151 assert is_nil(modified["object"]["likes"])
1154 test "the directMessage flag is present" do
1155 user = insert(:user)
1156 other_user = insert(:user)
1158 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1160 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1162 assert modified["directMessage"] == false
1165 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1167 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1169 assert modified["directMessage"] == false
1172 CommonAPI.post(user, %{
1173 "status" => "@#{other_user.nickname} :moominmamma:",
1174 "visibility" => "direct"
1177 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1179 assert modified["directMessage"] == true
1182 test "it strips BCC field" do
1183 user = insert(:user)
1184 {:ok, list} = Pleroma.List.create("foo", user)
1187 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1189 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1191 assert is_nil(modified["bcc"])
1195 describe "user upgrade" do
1196 test "it upgrades a user to activitypub" do
1199 nickname: "rye@niu.moe",
1201 ap_id: "https://niu.moe/users/rye",
1202 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1205 user_two = insert(:user, %{following: [user.follower_address]})
1207 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1208 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1209 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1211 user = User.get_cached_by_id(user.id)
1212 assert user.info.note_count == 1
1214 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1215 ObanHelpers.perform_all()
1217 assert user.info.ap_enabled
1218 assert user.info.note_count == 1
1219 assert user.follower_address == "https://niu.moe/users/rye/followers"
1220 assert user.following_address == "https://niu.moe/users/rye/following"
1222 user = User.get_cached_by_id(user.id)
1223 assert user.info.note_count == 1
1225 activity = Activity.get_by_id(activity.id)
1226 assert user.follower_address in activity.recipients
1232 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1241 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1244 } = user.info.banner
1246 refute "..." in activity.recipients
1248 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1249 refute user.follower_address in unrelated_activity.recipients
1251 user_two = User.get_cached_by_id(user_two.id)
1252 assert user.follower_address in user_two.following
1253 refute "..." in user_two.following
1257 describe "maybe_retire_websub" do
1258 test "it deletes all websub client subscripitions with the user as topic" do
1259 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1260 {:ok, ws} = Repo.insert(subscription)
1262 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1263 {:ok, ws2} = Repo.insert(subscription)
1265 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1267 refute Repo.get(WebsubClientSubscription, ws.id)
1268 assert Repo.get(WebsubClientSubscription, ws2.id)
1272 describe "actor rewriting" do
1273 test "it fixes the actor URL property to be a proper URI" do
1275 "url" => %{"href" => "http://example.com"}
1278 rewritten = Transmogrifier.maybe_fix_user_object(data)
1279 assert rewritten["url"] == "http://example.com"
1283 describe "actor origin containment" do
1284 test "it rejects activities which reference objects with bogus origins" do
1286 "@context" => "https://www.w3.org/ns/activitystreams",
1287 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1288 "actor" => "http://mastodon.example.org/users/admin",
1289 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1290 "object" => "https://info.pleroma.site/activity.json",
1291 "type" => "Announce"
1294 :error = Transmogrifier.handle_incoming(data)
1297 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1299 "@context" => "https://www.w3.org/ns/activitystreams",
1300 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1301 "actor" => "http://mastodon.example.org/users/admin",
1302 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1303 "object" => "https://info.pleroma.site/activity2.json",
1304 "type" => "Announce"
1307 :error = Transmogrifier.handle_incoming(data)
1310 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1312 "@context" => "https://www.w3.org/ns/activitystreams",
1313 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1314 "actor" => "http://mastodon.example.org/users/admin",
1315 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1316 "object" => "https://info.pleroma.site/activity3.json",
1317 "type" => "Announce"
1320 :error = Transmogrifier.handle_incoming(data)
1324 describe "reserialization" do
1325 test "successfully reserializes a message with inReplyTo == nil" do
1326 user = insert(:user)
1329 "@context" => "https://www.w3.org/ns/activitystreams",
1330 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1334 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1339 "attributedTo" => user.ap_id
1341 "actor" => user.ap_id
1344 {:ok, activity} = Transmogrifier.handle_incoming(message)
1346 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1349 test "successfully reserializes a message with AS2 objects in IR" do
1350 user = insert(:user)
1353 "@context" => "https://www.w3.org/ns/activitystreams",
1354 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1358 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1363 "attributedTo" => user.ap_id,
1365 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1366 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1369 "actor" => user.ap_id
1372 {:ok, activity} = Transmogrifier.handle_incoming(message)
1374 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1378 test "Rewrites Answers to Notes" do
1379 user = insert(:user)
1381 {:ok, poll_activity} =
1382 CommonAPI.post(user, %{
1383 "status" => "suya...",
1384 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1387 poll_object = Object.normalize(poll_activity)
1388 # TODO: Replace with CommonAPI vote creation when implemented
1390 File.read!("test/fixtures/mastodon-vote.json")
1392 |> Kernel.put_in(["to"], user.ap_id)
1393 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1394 |> Kernel.put_in(["object", "to"], user.ap_id)
1396 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1397 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1399 assert data["object"]["type"] == "Note"
1402 describe "fix_explicit_addressing" do
1404 user = insert(:user)
1408 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1409 explicitly_mentioned_actors = [
1410 "https://pleroma.gold/users/user1",
1411 "https://pleroma.gold/user2"
1415 "actor" => user.ap_id,
1416 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1419 Enum.map(explicitly_mentioned_actors, fn href ->
1420 %{"type" => "Mention", "href" => href}
1424 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1425 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1426 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1427 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1430 test "does not move actor's follower collection to cc", %{user: user} do
1432 "actor" => user.ap_id,
1433 "to" => [user.follower_address],
1437 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1438 assert user.follower_address in fixed_object["to"]
1439 refute user.follower_address in fixed_object["cc"]
1442 test "removes recipient's follower collection from cc", %{user: user} do
1443 recipient = insert(:user)
1446 "actor" => user.ap_id,
1447 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1448 "cc" => [user.follower_address, recipient.follower_address]
1451 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1453 assert user.follower_address in fixed_object["cc"]
1454 refute recipient.follower_address in fixed_object["cc"]
1455 refute recipient.follower_address in fixed_object["to"]