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"}
569 test "it works for incoming update activities which lock the account" do
570 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
572 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
573 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
576 update_data["object"]
577 |> Map.put("actor", data["actor"])
578 |> Map.put("id", data["actor"])
579 |> Map.put("manuallyApprovesFollowers", true)
583 |> Map.put("actor", data["actor"])
584 |> Map.put("object", object)
586 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
588 user = User.get_cached_by_ap_id(data["actor"])
589 assert user.info.locked == true
592 test "it works for incoming deletes" do
593 activity = insert(:note_activity)
596 File.read!("test/fixtures/mastodon-delete.json")
601 |> Map.put("id", activity.data["object"])
605 |> Map.put("object", object)
606 |> Map.put("actor", activity.data["actor"])
608 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
610 refute Activity.get_by_id(activity.id)
613 test "it fails for incoming deletes with spoofed origin" do
614 activity = insert(:note_activity)
617 File.read!("test/fixtures/mastodon-delete.json")
622 |> Map.put("id", activity.data["object"])
626 |> Map.put("object", object)
628 assert capture_log(fn ->
629 :error = Transmogrifier.handle_incoming(data)
631 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
633 assert Activity.get_by_id(activity.id)
636 test "it works for incoming user deletes" do
637 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
640 File.read!("test/fixtures/mastodon-delete-user.json")
643 {:ok, _} = Transmogrifier.handle_incoming(data)
644 ObanHelpers.perform_all()
646 refute User.get_cached_by_ap_id(ap_id)
649 test "it fails for incoming user deletes with spoofed origin" do
650 %{ap_id: ap_id} = insert(:user)
653 File.read!("test/fixtures/mastodon-delete-user.json")
655 |> Map.put("actor", ap_id)
657 assert :error == Transmogrifier.handle_incoming(data)
658 assert User.get_cached_by_ap_id(ap_id)
661 test "it works for incoming unannounces with an existing notice" do
663 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
666 File.read!("test/fixtures/mastodon-announce.json")
668 |> Map.put("object", activity.data["object"])
670 {:ok, %Activity{data: announce_data, local: false}} =
671 Transmogrifier.handle_incoming(announce_data)
674 File.read!("test/fixtures/mastodon-undo-announce.json")
676 |> Map.put("object", announce_data)
677 |> Map.put("actor", announce_data["actor"])
679 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
681 assert data["type"] == "Undo"
682 assert object_data = data["object"]
683 assert object_data["type"] == "Announce"
684 assert object_data["object"] == activity.data["object"]
686 assert object_data["id"] ==
687 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
690 test "it works for incomming unfollows with an existing follow" do
694 File.read!("test/fixtures/mastodon-follow-activity.json")
696 |> Map.put("object", user.ap_id)
698 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
701 File.read!("test/fixtures/mastodon-unfollow-activity.json")
703 |> Map.put("object", follow_data)
705 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
707 assert data["type"] == "Undo"
708 assert data["object"]["type"] == "Follow"
709 assert data["object"]["object"] == user.ap_id
710 assert data["actor"] == "http://mastodon.example.org/users/admin"
712 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
715 test "it works for incoming blocks" do
719 File.read!("test/fixtures/mastodon-block-activity.json")
721 |> Map.put("object", user.ap_id)
723 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
725 assert data["type"] == "Block"
726 assert data["object"] == user.ap_id
727 assert data["actor"] == "http://mastodon.example.org/users/admin"
729 blocker = User.get_cached_by_ap_id(data["actor"])
731 assert User.blocks?(blocker, user)
734 test "incoming blocks successfully tear down any follow relationship" do
735 blocker = insert(:user)
736 blocked = insert(:user)
739 File.read!("test/fixtures/mastodon-block-activity.json")
741 |> Map.put("object", blocked.ap_id)
742 |> Map.put("actor", blocker.ap_id)
744 {:ok, blocker} = User.follow(blocker, blocked)
745 {:ok, blocked} = User.follow(blocked, blocker)
747 assert User.following?(blocker, blocked)
748 assert User.following?(blocked, blocker)
750 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
752 assert data["type"] == "Block"
753 assert data["object"] == blocked.ap_id
754 assert data["actor"] == blocker.ap_id
756 blocker = User.get_cached_by_ap_id(data["actor"])
757 blocked = User.get_cached_by_ap_id(data["object"])
759 assert User.blocks?(blocker, blocked)
761 refute User.following?(blocker, blocked)
762 refute User.following?(blocked, blocker)
765 test "it works for incoming unblocks with an existing block" do
769 File.read!("test/fixtures/mastodon-block-activity.json")
771 |> Map.put("object", user.ap_id)
773 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
776 File.read!("test/fixtures/mastodon-unblock-activity.json")
778 |> Map.put("object", block_data)
780 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
781 assert data["type"] == "Undo"
782 assert data["object"]["type"] == "Block"
783 assert data["object"]["object"] == user.ap_id
784 assert data["actor"] == "http://mastodon.example.org/users/admin"
786 blocker = User.get_cached_by_ap_id(data["actor"])
788 refute User.blocks?(blocker, user)
791 test "it works for incoming accepts which were pre-accepted" do
792 follower = insert(:user)
793 followed = insert(:user)
795 {:ok, follower} = User.follow(follower, followed)
796 assert User.following?(follower, followed) == true
798 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
801 File.read!("test/fixtures/mastodon-accept-activity.json")
803 |> Map.put("actor", followed.ap_id)
806 accept_data["object"]
807 |> Map.put("actor", follower.ap_id)
808 |> Map.put("id", follow_activity.data["id"])
810 accept_data = Map.put(accept_data, "object", object)
812 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
813 refute activity.local
815 assert activity.data["object"] == follow_activity.data["id"]
817 follower = User.get_cached_by_id(follower.id)
819 assert User.following?(follower, followed) == true
822 test "it works for incoming accepts which were orphaned" do
823 follower = insert(:user)
824 followed = insert(:user, %{info: %User.Info{locked: true}})
826 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
829 File.read!("test/fixtures/mastodon-accept-activity.json")
831 |> Map.put("actor", followed.ap_id)
834 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
836 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
837 assert activity.data["object"] == follow_activity.data["id"]
839 follower = User.get_cached_by_id(follower.id)
841 assert User.following?(follower, followed) == true
844 test "it works for incoming accepts which are referenced by IRI only" do
845 follower = insert(:user)
846 followed = insert(:user, %{info: %User.Info{locked: true}})
848 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
851 File.read!("test/fixtures/mastodon-accept-activity.json")
853 |> Map.put("actor", followed.ap_id)
854 |> Map.put("object", follow_activity.data["id"])
856 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
857 assert activity.data["object"] == follow_activity.data["id"]
859 follower = User.get_cached_by_id(follower.id)
861 assert User.following?(follower, followed) == true
864 test "it fails for incoming accepts which cannot be correlated" do
865 follower = insert(:user)
866 followed = insert(:user, %{info: %User.Info{locked: true}})
869 File.read!("test/fixtures/mastodon-accept-activity.json")
871 |> Map.put("actor", followed.ap_id)
874 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
876 :error = Transmogrifier.handle_incoming(accept_data)
878 follower = User.get_cached_by_id(follower.id)
880 refute User.following?(follower, followed) == true
883 test "it fails for incoming rejects which cannot be correlated" do
884 follower = insert(:user)
885 followed = insert(:user, %{info: %User.Info{locked: true}})
888 File.read!("test/fixtures/mastodon-reject-activity.json")
890 |> Map.put("actor", followed.ap_id)
893 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
895 :error = Transmogrifier.handle_incoming(accept_data)
897 follower = User.get_cached_by_id(follower.id)
899 refute User.following?(follower, followed) == true
902 test "it works for incoming rejects which are orphaned" do
903 follower = insert(:user)
904 followed = insert(:user, %{info: %User.Info{locked: true}})
906 {:ok, follower} = User.follow(follower, followed)
907 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
909 assert User.following?(follower, followed) == true
912 File.read!("test/fixtures/mastodon-reject-activity.json")
914 |> Map.put("actor", followed.ap_id)
917 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
919 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
920 refute activity.local
922 follower = User.get_cached_by_id(follower.id)
924 assert User.following?(follower, followed) == false
927 test "it works for incoming rejects which are referenced by IRI only" do
928 follower = insert(:user)
929 followed = insert(:user, %{info: %User.Info{locked: true}})
931 {:ok, follower} = User.follow(follower, followed)
932 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
934 assert User.following?(follower, followed) == true
937 File.read!("test/fixtures/mastodon-reject-activity.json")
939 |> Map.put("actor", followed.ap_id)
940 |> Map.put("object", follow_activity.data["id"])
942 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
944 follower = User.get_cached_by_id(follower.id)
946 assert User.following?(follower, followed) == false
949 test "it rejects activities without a valid ID" do
953 File.read!("test/fixtures/mastodon-follow-activity.json")
955 |> Map.put("object", user.ap_id)
958 :error = Transmogrifier.handle_incoming(data)
961 test "it remaps video URLs as attachments if necessary" do
963 Fetcher.fetch_object_from_id(
964 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
969 "mediaType" => "video/mp4",
971 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
972 "mimeType" => "video/mp4",
977 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
978 "mediaType" => "video/mp4",
985 assert object.data["url"] ==
986 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
988 assert object.data["attachment"] == [attachment]
991 test "it accepts Flag activities" do
993 other_user = insert(:user)
995 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
996 object = Object.normalize(activity)
999 "@context" => "https://www.w3.org/ns/activitystreams",
1000 "cc" => [user.ap_id],
1001 "object" => [user.ap_id, object.data["id"]],
1003 "content" => "blocked AND reported!!!",
1004 "actor" => other_user.ap_id
1007 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1009 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1010 assert activity.data["content"] == "blocked AND reported!!!"
1011 assert activity.data["actor"] == other_user.ap_id
1012 assert activity.data["cc"] == [user.ap_id]
1016 describe "prepare outgoing" do
1017 test "it turns mentions into tags" do
1018 user = insert(:user)
1019 other_user = insert(:user)
1022 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1024 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1025 object = modified["object"]
1027 expected_mention = %{
1028 "href" => other_user.ap_id,
1029 "name" => "@#{other_user.nickname}",
1034 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1035 "type" => "Hashtag",
1039 assert Enum.member?(object["tag"], expected_tag)
1040 assert Enum.member?(object["tag"], expected_mention)
1043 test "it adds the sensitive property" do
1044 user = insert(:user)
1046 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1047 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1049 assert modified["object"]["sensitive"]
1052 test "it adds the json-ld context and the conversation property" do
1053 user = insert(:user)
1055 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1056 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1058 assert modified["@context"] ==
1059 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1061 assert modified["object"]["conversation"] == modified["context"]
1064 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1065 user = insert(:user)
1067 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1068 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1070 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1073 test "it translates ostatus IDs to external URLs" do
1074 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1075 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1077 user = insert(:user)
1079 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1080 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1082 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1085 test "it translates ostatus reply_to IDs to external URLs" do
1086 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1087 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1089 user = insert(:user)
1092 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1094 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1096 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1099 test "it strips internal hashtag data" do
1100 user = insert(:user)
1102 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1105 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1106 "type" => "Hashtag",
1110 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1112 assert modified["object"]["tag"] == [expected_tag]
1115 test "it strips internal fields" do
1116 user = insert(:user)
1118 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1120 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1122 assert length(modified["object"]["tag"]) == 2
1124 assert is_nil(modified["object"]["emoji"])
1125 assert is_nil(modified["object"]["like_count"])
1126 assert is_nil(modified["object"]["announcements"])
1127 assert is_nil(modified["object"]["announcement_count"])
1128 assert is_nil(modified["object"]["context_id"])
1131 test "it strips internal fields of article" do
1132 activity = insert(:article_activity)
1134 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1136 assert length(modified["object"]["tag"]) == 2
1138 assert is_nil(modified["object"]["emoji"])
1139 assert is_nil(modified["object"]["like_count"])
1140 assert is_nil(modified["object"]["announcements"])
1141 assert is_nil(modified["object"]["announcement_count"])
1142 assert is_nil(modified["object"]["context_id"])
1143 assert is_nil(modified["object"]["likes"])
1146 test "the directMessage flag is present" do
1147 user = insert(:user)
1148 other_user = insert(:user)
1150 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1152 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1154 assert modified["directMessage"] == false
1157 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1159 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1161 assert modified["directMessage"] == false
1164 CommonAPI.post(user, %{
1165 "status" => "@#{other_user.nickname} :moominmamma:",
1166 "visibility" => "direct"
1169 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1171 assert modified["directMessage"] == true
1174 test "it strips BCC field" do
1175 user = insert(:user)
1176 {:ok, list} = Pleroma.List.create("foo", user)
1179 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1181 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1183 assert is_nil(modified["bcc"])
1187 describe "user upgrade" do
1188 test "it upgrades a user to activitypub" do
1191 nickname: "rye@niu.moe",
1193 ap_id: "https://niu.moe/users/rye",
1194 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1197 user_two = insert(:user, %{following: [user.follower_address]})
1199 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1200 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1201 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1203 user = User.get_cached_by_id(user.id)
1204 assert user.info.note_count == 1
1206 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1207 ObanHelpers.perform_all()
1209 assert user.info.ap_enabled
1210 assert user.info.note_count == 1
1211 assert user.follower_address == "https://niu.moe/users/rye/followers"
1212 assert user.following_address == "https://niu.moe/users/rye/following"
1214 user = User.get_cached_by_id(user.id)
1215 assert user.info.note_count == 1
1217 activity = Activity.get_by_id(activity.id)
1218 assert user.follower_address in activity.recipients
1224 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1233 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1236 } = user.info.banner
1238 refute "..." in activity.recipients
1240 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1241 refute user.follower_address in unrelated_activity.recipients
1243 user_two = User.get_cached_by_id(user_two.id)
1244 assert user.follower_address in user_two.following
1245 refute "..." in user_two.following
1249 describe "maybe_retire_websub" do
1250 test "it deletes all websub client subscripitions with the user as topic" do
1251 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1252 {:ok, ws} = Repo.insert(subscription)
1254 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1255 {:ok, ws2} = Repo.insert(subscription)
1257 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1259 refute Repo.get(WebsubClientSubscription, ws.id)
1260 assert Repo.get(WebsubClientSubscription, ws2.id)
1264 describe "actor rewriting" do
1265 test "it fixes the actor URL property to be a proper URI" do
1267 "url" => %{"href" => "http://example.com"}
1270 rewritten = Transmogrifier.maybe_fix_user_object(data)
1271 assert rewritten["url"] == "http://example.com"
1275 describe "actor origin containment" do
1276 test "it rejects activities which reference objects with bogus origins" do
1278 "@context" => "https://www.w3.org/ns/activitystreams",
1279 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1280 "actor" => "http://mastodon.example.org/users/admin",
1281 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1282 "object" => "https://info.pleroma.site/activity.json",
1283 "type" => "Announce"
1286 :error = Transmogrifier.handle_incoming(data)
1289 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1291 "@context" => "https://www.w3.org/ns/activitystreams",
1292 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1293 "actor" => "http://mastodon.example.org/users/admin",
1294 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1295 "object" => "https://info.pleroma.site/activity2.json",
1296 "type" => "Announce"
1299 :error = Transmogrifier.handle_incoming(data)
1302 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1304 "@context" => "https://www.w3.org/ns/activitystreams",
1305 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1306 "actor" => "http://mastodon.example.org/users/admin",
1307 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1308 "object" => "https://info.pleroma.site/activity3.json",
1309 "type" => "Announce"
1312 :error = Transmogrifier.handle_incoming(data)
1316 describe "reserialization" do
1317 test "successfully reserializes a message with inReplyTo == nil" do
1318 user = insert(:user)
1321 "@context" => "https://www.w3.org/ns/activitystreams",
1322 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1326 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1331 "attributedTo" => user.ap_id
1333 "actor" => user.ap_id
1336 {:ok, activity} = Transmogrifier.handle_incoming(message)
1338 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1341 test "successfully reserializes a message with AS2 objects in IR" do
1342 user = insert(:user)
1345 "@context" => "https://www.w3.org/ns/activitystreams",
1346 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1350 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1355 "attributedTo" => user.ap_id,
1357 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1358 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1361 "actor" => user.ap_id
1364 {:ok, activity} = Transmogrifier.handle_incoming(message)
1366 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1370 test "Rewrites Answers to Notes" do
1371 user = insert(:user)
1373 {:ok, poll_activity} =
1374 CommonAPI.post(user, %{
1375 "status" => "suya...",
1376 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1379 poll_object = Object.normalize(poll_activity)
1380 # TODO: Replace with CommonAPI vote creation when implemented
1382 File.read!("test/fixtures/mastodon-vote.json")
1384 |> Kernel.put_in(["to"], user.ap_id)
1385 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1386 |> Kernel.put_in(["object", "to"], user.ap_id)
1388 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1389 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1391 assert data["object"]["type"] == "Note"
1394 describe "fix_explicit_addressing" do
1396 user = insert(:user)
1400 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1401 explicitly_mentioned_actors = [
1402 "https://pleroma.gold/users/user1",
1403 "https://pleroma.gold/user2"
1407 "actor" => user.ap_id,
1408 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1411 Enum.map(explicitly_mentioned_actors, fn href ->
1412 %{"type" => "Mention", "href" => href}
1416 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1417 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1418 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1419 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1422 test "does not move actor's follower collection to cc", %{user: user} do
1424 "actor" => user.ap_id,
1425 "to" => [user.follower_address],
1429 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1430 assert user.follower_address in fixed_object["to"]
1431 refute user.follower_address in fixed_object["cc"]
1434 test "removes recipient's follower collection from cc", %{user: user} do
1435 recipient = insert(:user)
1438 "actor" => user.ap_id,
1439 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1440 "cc" => [user.follower_address, recipient.follower_address]
1443 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1445 assert user.follower_address in fixed_object["cc"]
1446 refute recipient.follower_address in fixed_object["cc"]
1447 refute recipient.follower_address in fixed_object["to"]