1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Object.Fetcher
12 alias Pleroma.Tests.ObanHelpers
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
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 @tag capture_log: true
45 test "it fetches reply-to activities if we don't have them" do
47 File.read!("test/fixtures/mastodon-post-activity.json")
52 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
54 data = Map.put(data, "object", object)
55 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
56 returned_object = Object.normalize(returned_activity, false)
59 Activity.get_create_by_object_ap_id(
60 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
63 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
66 test "it does not fetch reply-to activities beyond max replies depth limit" do
68 File.read!("test/fixtures/mastodon-post-activity.json")
73 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
75 data = Map.put(data, "object", object)
77 with_mock Pleroma.Web.Federator,
78 allowed_thread_distance?: fn _ -> false end do
79 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
81 returned_object = Object.normalize(returned_activity, false)
83 refute Activity.get_create_by_object_ap_id(
84 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
87 assert returned_object.data["inReplyToAtomUri"] ==
88 "https://shitposter.club/notice/2827873"
92 test "it does not crash if the object in inReplyTo can't be fetched" do
94 File.read!("test/fixtures/mastodon-post-activity.json")
99 |> Map.put("inReplyTo", "https://404.site/whatever")
103 |> Map.put("object", object)
105 assert capture_log(fn ->
106 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
107 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
110 test "it works for incoming notices" do
111 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
113 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
116 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
118 assert data["context"] ==
119 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
121 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
123 assert data["cc"] == [
124 "http://mastodon.example.org/users/admin/followers",
125 "http://localtesting.pleroma.lol/users/lain"
128 assert data["actor"] == "http://mastodon.example.org/users/admin"
130 object_data = Object.normalize(data["object"]).data
132 assert object_data["id"] ==
133 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
135 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
137 assert object_data["cc"] == [
138 "http://mastodon.example.org/users/admin/followers",
139 "http://localtesting.pleroma.lol/users/lain"
142 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
143 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
145 assert object_data["context"] ==
146 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
148 assert object_data["sensitive"] == true
150 user = User.get_cached_by_ap_id(object_data["actor"])
152 assert user.note_count == 1
155 test "it works for incoming notices with hashtags" do
156 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
158 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
159 object = Object.normalize(data["object"])
161 assert Enum.at(object.data["tag"], 2) == "moo"
164 test "it works for incoming questions" do
165 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
167 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
169 object = Object.normalize(activity)
171 assert Enum.all?(object.data["oneOf"], fn choice ->
174 "Everyone knows that!",
175 "25 char limit is dumb",
176 "I can't even fit a funny"
181 test "it works for incoming listens" do
183 "@context" => "https://www.w3.org/ns/activitystreams",
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
187 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
188 "actor" => "http://mastodon.example.org/users/admin",
191 "id" => "http://mastodon.example.org/users/admin/listens/1234",
192 "attributedTo" => "http://mastodon.example.org/users/admin",
193 "title" => "lain radio episode 1",
195 "album" => "lain radio",
200 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
202 object = Object.normalize(activity)
204 assert object.data["title"] == "lain radio episode 1"
205 assert object.data["artist"] == "lain"
206 assert object.data["album"] == "lain radio"
207 assert object.data["length"] == 180_000
210 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
214 CommonAPI.post(user, %{
215 "status" => "suya...",
216 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
219 object = Object.normalize(activity)
222 File.read!("test/fixtures/mastodon-vote.json")
224 |> Kernel.put_in(["to"], user.ap_id)
225 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
226 |> Kernel.put_in(["object", "to"], user.ap_id)
228 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
229 answer_object = Object.normalize(activity)
230 assert answer_object.data["type"] == "Answer"
231 object = Object.get_by_ap_id(object.data["id"])
234 object.data["oneOf"],
236 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
242 test "it works for incoming notices with contentMap" do
244 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
246 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
247 object = Object.normalize(data["object"])
249 assert object.data["content"] ==
250 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
253 test "it works for incoming notices with to/cc not being an array (kroeg)" do
254 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
256 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
257 object = Object.normalize(data["object"])
259 assert object.data["content"] ==
260 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
263 test "it works for incoming announces with actor being inlined (kroeg)" do
264 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
266 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
268 assert data["actor"] == "https://puckipedia.com/"
271 test "it works for incoming notices with tag not being an array (kroeg)" do
272 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
274 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
275 object = Object.normalize(data["object"])
277 assert object.data["emoji"] == %{
278 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
281 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
283 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
284 object = Object.normalize(data["object"])
286 assert "test" in object.data["tag"]
289 test "it works for incoming notices with url not being a string (prismo)" do
290 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
292 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
293 object = Object.normalize(data["object"])
295 assert object.data["url"] == "https://prismo.news/posts/83"
298 test "it cleans up incoming notices which are not really DMs" do
300 other_user = insert(:user)
302 to = [user.ap_id, other_user.ap_id]
305 File.read!("test/fixtures/mastodon-post-activity.json")
315 data = Map.put(data, "object", object)
317 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
319 assert data["to"] == []
320 assert data["cc"] == to
322 object_data = Object.normalize(activity).data
324 assert object_data["to"] == []
325 assert object_data["cc"] == to
328 test "it works for incoming likes" do
330 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
333 File.read!("test/fixtures/mastodon-like.json")
335 |> Map.put("object", activity.data["object"])
337 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
339 assert data["actor"] == "http://mastodon.example.org/users/admin"
340 assert data["type"] == "Like"
341 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
342 assert data["object"] == activity.data["object"]
345 test "it works for incoming misskey likes, turning them into EmojiReacts" do
347 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
350 File.read!("test/fixtures/misskey-like.json")
352 |> Map.put("object", activity.data["object"])
354 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
356 assert data["actor"] == data["actor"]
357 assert data["type"] == "EmojiReact"
358 assert data["id"] == data["id"]
359 assert data["object"] == activity.data["object"]
360 assert data["content"] == "🍮"
363 test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReacts" do
365 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
368 File.read!("test/fixtures/misskey-like.json")
370 |> Map.put("object", activity.data["object"])
371 |> Map.put("_misskey_reaction", "⭐")
373 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
375 assert data["actor"] == data["actor"]
376 assert data["type"] == "EmojiReact"
377 assert data["id"] == data["id"]
378 assert data["object"] == activity.data["object"]
379 assert data["content"] == "⭐"
382 test "it works for incoming emoji reactions" do
384 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
387 File.read!("test/fixtures/emoji-reaction.json")
389 |> Map.put("object", activity.data["object"])
391 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
393 assert data["actor"] == "http://mastodon.example.org/users/admin"
394 assert data["type"] == "EmojiReact"
395 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
396 assert data["object"] == activity.data["object"]
397 assert data["content"] == "👌"
400 test "it reject invalid emoji reactions" do
402 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
405 File.read!("test/fixtures/emoji-reaction-too-long.json")
407 |> Map.put("object", activity.data["object"])
409 assert :error = Transmogrifier.handle_incoming(data)
412 File.read!("test/fixtures/emoji-reaction-no-emoji.json")
414 |> Map.put("object", activity.data["object"])
416 assert :error = Transmogrifier.handle_incoming(data)
419 test "it works for incoming emoji reaction undos" do
422 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
423 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
426 File.read!("test/fixtures/mastodon-undo-like.json")
428 |> Map.put("object", reaction_activity.data["id"])
429 |> Map.put("actor", user.ap_id)
431 {:ok, activity} = Transmogrifier.handle_incoming(data)
433 assert activity.actor == user.ap_id
434 assert activity.data["id"] == data["id"]
435 assert activity.data["type"] == "Undo"
438 test "it returns an error for incoming unlikes wihout a like activity" do
440 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
443 File.read!("test/fixtures/mastodon-undo-like.json")
445 |> Map.put("object", activity.data["object"])
447 assert Transmogrifier.handle_incoming(data) == :error
450 test "it works for incoming unlikes with an existing like activity" do
452 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
455 File.read!("test/fixtures/mastodon-like.json")
457 |> Map.put("object", activity.data["object"])
459 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
462 File.read!("test/fixtures/mastodon-undo-like.json")
464 |> Map.put("object", like_data)
465 |> Map.put("actor", like_data["actor"])
467 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
469 assert data["actor"] == "http://mastodon.example.org/users/admin"
470 assert data["type"] == "Undo"
471 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
472 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
475 test "it works for incoming unlikes with an existing like activity and a compact object" do
477 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
480 File.read!("test/fixtures/mastodon-like.json")
482 |> Map.put("object", activity.data["object"])
484 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
487 File.read!("test/fixtures/mastodon-undo-like.json")
489 |> Map.put("object", like_data["id"])
490 |> Map.put("actor", like_data["actor"])
492 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
494 assert data["actor"] == "http://mastodon.example.org/users/admin"
495 assert data["type"] == "Undo"
496 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
497 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
500 test "it works for incoming announces" do
501 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
503 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
505 assert data["actor"] == "http://mastodon.example.org/users/admin"
506 assert data["type"] == "Announce"
509 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
511 assert data["object"] ==
512 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
514 assert Activity.get_create_by_object_ap_id(data["object"])
517 test "it works for incoming announces with an existing activity" do
519 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
522 File.read!("test/fixtures/mastodon-announce.json")
524 |> Map.put("object", activity.data["object"])
526 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
528 assert data["actor"] == "http://mastodon.example.org/users/admin"
529 assert data["type"] == "Announce"
532 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
534 assert data["object"] == activity.data["object"]
536 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
539 test "it works for incoming announces with an inlined activity" do
541 File.read!("test/fixtures/mastodon-announce-private.json")
544 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
546 assert data["actor"] == "http://mastodon.example.org/users/admin"
547 assert data["type"] == "Announce"
550 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
552 object = Object.normalize(data["object"])
554 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
555 assert object.data["content"] == "this is a private toot"
558 @tag capture_log: true
559 test "it rejects incoming announces with an inlined activity from another origin" do
561 File.read!("test/fixtures/bogus-mastodon-announce.json")
564 assert :error = Transmogrifier.handle_incoming(data)
567 test "it does not clobber the addressing on announce activities" do
569 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
572 File.read!("test/fixtures/mastodon-announce.json")
574 |> Map.put("object", Object.normalize(activity).data["id"])
575 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
578 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
580 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
583 test "it ensures that as:Public activities make it to their followers collection" do
587 File.read!("test/fixtures/mastodon-post-activity.json")
589 |> Map.put("actor", user.ap_id)
590 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
595 |> Map.put("attributedTo", user.ap_id)
596 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
598 |> Map.put("id", user.ap_id <> "/activities/12345678")
600 data = Map.put(data, "object", object)
602 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
604 assert data["cc"] == [User.ap_followers(user)]
607 test "it ensures that address fields become lists" do
611 File.read!("test/fixtures/mastodon-post-activity.json")
613 |> Map.put("actor", user.ap_id)
614 |> Map.put("to", nil)
615 |> Map.put("cc", nil)
619 |> Map.put("attributedTo", user.ap_id)
620 |> Map.put("to", nil)
621 |> Map.put("cc", nil)
622 |> Map.put("id", user.ap_id <> "/activities/12345678")
624 data = Map.put(data, "object", object)
626 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
628 assert !is_nil(data["to"])
629 assert !is_nil(data["cc"])
632 test "it strips internal likes" do
634 File.read!("test/fixtures/mastodon-post-activity.json")
639 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
640 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
642 "type" => "OrderedCollection"
645 object = Map.put(data["object"], "likes", likes)
646 data = Map.put(data, "object", object)
648 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
650 refute Map.has_key?(object.data, "likes")
653 test "it strips internal reactions" do
655 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
656 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
658 %{object: object} = Activity.get_by_id_with_object(activity.id)
659 assert Map.has_key?(object.data, "reactions")
660 assert Map.has_key?(object.data, "reaction_count")
662 object_data = Transmogrifier.strip_internal_fields(object.data)
663 refute Map.has_key?(object_data, "reactions")
664 refute Map.has_key?(object_data, "reaction_count")
667 test "it works for incoming update activities" do
668 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
670 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
671 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
674 update_data["object"]
675 |> Map.put("actor", data["actor"])
676 |> Map.put("id", data["actor"])
680 |> Map.put("actor", data["actor"])
681 |> Map.put("object", object)
683 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
685 assert data["id"] == update_data["id"]
687 user = User.get_cached_by_ap_id(data["actor"])
688 assert user.name == "gargle"
690 assert user.avatar["url"] == [
693 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
697 assert user.banner["url"] == [
700 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
704 assert user.bio == "<p>Some bio</p>"
707 test "it works with alsoKnownAs" do
708 {:ok, %Activity{data: %{"actor" => actor}}} =
709 "test/fixtures/mastodon-post-activity.json"
712 |> Transmogrifier.handle_incoming()
714 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
717 "test/fixtures/mastodon-update.json"
720 |> Map.put("actor", actor)
721 |> Map.update!("object", fn object ->
723 |> Map.put("actor", actor)
724 |> Map.put("id", actor)
725 |> Map.put("alsoKnownAs", [
726 "http://mastodon.example.org/users/foo",
727 "http://example.org/users/bar"
730 |> Transmogrifier.handle_incoming()
732 assert User.get_cached_by_ap_id(actor).also_known_as == [
733 "http://mastodon.example.org/users/foo",
734 "http://example.org/users/bar"
738 test "it works with custom profile fields" do
740 "test/fixtures/mastodon-post-activity.json"
743 |> Transmogrifier.handle_incoming()
745 user = User.get_cached_by_ap_id(activity.actor)
747 assert User.fields(user) == [
748 %{"name" => "foo", "value" => "bar"},
749 %{"name" => "foo1", "value" => "bar1"}
752 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
755 update_data["object"]
756 |> Map.put("actor", user.ap_id)
757 |> Map.put("id", user.ap_id)
761 |> Map.put("actor", user.ap_id)
762 |> Map.put("object", object)
764 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
766 user = User.get_cached_by_ap_id(user.ap_id)
768 assert User.fields(user) == [
769 %{"name" => "foo", "value" => "updated"},
770 %{"name" => "foo1", "value" => "updated"}
773 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
776 put_in(update_data, ["object", "attachment"], [
777 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
778 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
779 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
782 {:ok, _} = Transmogrifier.handle_incoming(update_data)
784 user = User.get_cached_by_ap_id(user.ap_id)
786 assert User.fields(user) == [
787 %{"name" => "foo", "value" => "updated"},
788 %{"name" => "foo1", "value" => "updated"}
791 update_data = put_in(update_data, ["object", "attachment"], [])
793 {:ok, _} = Transmogrifier.handle_incoming(update_data)
795 user = User.get_cached_by_ap_id(user.ap_id)
797 assert User.fields(user) == []
800 test "it works for incoming update activities which lock the account" do
801 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
803 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
804 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
807 update_data["object"]
808 |> Map.put("actor", data["actor"])
809 |> Map.put("id", data["actor"])
810 |> Map.put("manuallyApprovesFollowers", true)
814 |> Map.put("actor", data["actor"])
815 |> Map.put("object", object)
817 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
819 user = User.get_cached_by_ap_id(data["actor"])
820 assert user.locked == true
823 test "it works for incoming deletes" do
824 activity = insert(:note_activity)
825 deleting_user = insert(:user)
828 File.read!("test/fixtures/mastodon-delete.json")
833 |> Map.put("id", activity.data["object"])
837 |> Map.put("object", object)
838 |> Map.put("actor", deleting_user.ap_id)
840 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
841 Transmogrifier.handle_incoming(data)
843 assert id == data["id"]
844 refute Activity.get_by_id(activity.id)
845 assert actor == deleting_user.ap_id
848 test "it fails for incoming deletes with spoofed origin" do
849 activity = insert(:note_activity)
852 File.read!("test/fixtures/mastodon-delete.json")
857 |> Map.put("id", activity.data["object"])
861 |> Map.put("object", object)
863 assert capture_log(fn ->
864 :error = Transmogrifier.handle_incoming(data)
866 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
868 assert Activity.get_by_id(activity.id)
871 @tag capture_log: true
872 test "it works for incoming user deletes" do
874 insert(:user, ap_id: "http://mastodon.example.org/users/admin", local: false)
877 File.read!("test/fixtures/mastodon-delete-user.json")
880 {:ok, _} = Transmogrifier.handle_incoming(data)
881 ObanHelpers.perform_all()
883 refute User.get_cached_by_ap_id(ap_id)
886 test "it fails for incoming user deletes with spoofed origin" do
887 %{ap_id: ap_id} = insert(:user)
890 File.read!("test/fixtures/mastodon-delete-user.json")
892 |> Map.put("actor", ap_id)
894 assert capture_log(fn ->
895 assert :error == Transmogrifier.handle_incoming(data)
896 end) =~ "Object containment failed"
898 assert User.get_cached_by_ap_id(ap_id)
901 test "it works for incoming unannounces with an existing notice" do
903 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
906 File.read!("test/fixtures/mastodon-announce.json")
908 |> Map.put("object", activity.data["object"])
910 {:ok, %Activity{data: announce_data, local: false}} =
911 Transmogrifier.handle_incoming(announce_data)
914 File.read!("test/fixtures/mastodon-undo-announce.json")
916 |> Map.put("object", announce_data)
917 |> Map.put("actor", announce_data["actor"])
919 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
921 assert data["type"] == "Undo"
922 assert object_data = data["object"]
923 assert object_data["type"] == "Announce"
924 assert object_data["object"] == activity.data["object"]
926 assert object_data["id"] ==
927 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
930 test "it works for incomming unfollows with an existing follow" do
934 File.read!("test/fixtures/mastodon-follow-activity.json")
936 |> Map.put("object", user.ap_id)
938 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
941 File.read!("test/fixtures/mastodon-unfollow-activity.json")
943 |> Map.put("object", follow_data)
945 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
947 assert data["type"] == "Undo"
948 assert data["object"]["type"] == "Follow"
949 assert data["object"]["object"] == user.ap_id
950 assert data["actor"] == "http://mastodon.example.org/users/admin"
952 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
955 test "it works for incoming follows to locked account" do
956 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
957 user = insert(:user, locked: true)
960 File.read!("test/fixtures/mastodon-follow-activity.json")
962 |> Map.put("object", user.ap_id)
964 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
966 assert data["type"] == "Follow"
967 assert data["object"] == user.ap_id
968 assert data["state"] == "pending"
969 assert data["actor"] == "http://mastodon.example.org/users/admin"
971 assert [^pending_follower] = User.get_follow_requests(user)
974 test "it works for incoming blocks" do
978 File.read!("test/fixtures/mastodon-block-activity.json")
980 |> Map.put("object", user.ap_id)
982 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
984 assert data["type"] == "Block"
985 assert data["object"] == user.ap_id
986 assert data["actor"] == "http://mastodon.example.org/users/admin"
988 blocker = User.get_cached_by_ap_id(data["actor"])
990 assert User.blocks?(blocker, user)
993 test "incoming blocks successfully tear down any follow relationship" do
994 blocker = insert(:user)
995 blocked = insert(:user)
998 File.read!("test/fixtures/mastodon-block-activity.json")
1000 |> Map.put("object", blocked.ap_id)
1001 |> Map.put("actor", blocker.ap_id)
1003 {:ok, blocker} = User.follow(blocker, blocked)
1004 {:ok, blocked} = User.follow(blocked, blocker)
1006 assert User.following?(blocker, blocked)
1007 assert User.following?(blocked, blocker)
1009 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
1011 assert data["type"] == "Block"
1012 assert data["object"] == blocked.ap_id
1013 assert data["actor"] == blocker.ap_id
1015 blocker = User.get_cached_by_ap_id(data["actor"])
1016 blocked = User.get_cached_by_ap_id(data["object"])
1018 assert User.blocks?(blocker, blocked)
1020 refute User.following?(blocker, blocked)
1021 refute User.following?(blocked, blocker)
1024 test "it works for incoming unblocks with an existing block" do
1025 user = insert(:user)
1028 File.read!("test/fixtures/mastodon-block-activity.json")
1030 |> Map.put("object", user.ap_id)
1032 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
1035 File.read!("test/fixtures/mastodon-unblock-activity.json")
1037 |> Map.put("object", block_data)
1039 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
1040 assert data["type"] == "Undo"
1041 assert data["object"]["type"] == "Block"
1042 assert data["object"]["object"] == user.ap_id
1043 assert data["actor"] == "http://mastodon.example.org/users/admin"
1045 blocker = User.get_cached_by_ap_id(data["actor"])
1047 refute User.blocks?(blocker, user)
1050 test "it works for incoming accepts which were pre-accepted" do
1051 follower = insert(:user)
1052 followed = insert(:user)
1054 {:ok, follower} = User.follow(follower, followed)
1055 assert User.following?(follower, followed) == true
1057 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1060 File.read!("test/fixtures/mastodon-accept-activity.json")
1062 |> Map.put("actor", followed.ap_id)
1065 accept_data["object"]
1066 |> Map.put("actor", follower.ap_id)
1067 |> Map.put("id", follow_activity.data["id"])
1069 accept_data = Map.put(accept_data, "object", object)
1071 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1072 refute activity.local
1074 assert activity.data["object"] == follow_activity.data["id"]
1076 assert activity.data["id"] == accept_data["id"]
1078 follower = User.get_cached_by_id(follower.id)
1080 assert User.following?(follower, followed) == true
1083 test "it works for incoming accepts which were orphaned" do
1084 follower = insert(:user)
1085 followed = insert(:user, locked: true)
1087 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1090 File.read!("test/fixtures/mastodon-accept-activity.json")
1092 |> Map.put("actor", followed.ap_id)
1095 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1097 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1098 assert activity.data["object"] == follow_activity.data["id"]
1100 follower = User.get_cached_by_id(follower.id)
1102 assert User.following?(follower, followed) == true
1105 test "it works for incoming accepts which are referenced by IRI only" do
1106 follower = insert(:user)
1107 followed = insert(:user, locked: true)
1109 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1112 File.read!("test/fixtures/mastodon-accept-activity.json")
1114 |> Map.put("actor", followed.ap_id)
1115 |> Map.put("object", follow_activity.data["id"])
1117 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1118 assert activity.data["object"] == follow_activity.data["id"]
1120 follower = User.get_cached_by_id(follower.id)
1122 assert User.following?(follower, followed) == true
1124 follower = User.get_by_id(follower.id)
1125 assert follower.following_count == 1
1127 followed = User.get_by_id(followed.id)
1128 assert followed.follower_count == 1
1131 test "it fails for incoming accepts which cannot be correlated" do
1132 follower = insert(:user)
1133 followed = insert(:user, locked: true)
1136 File.read!("test/fixtures/mastodon-accept-activity.json")
1138 |> Map.put("actor", followed.ap_id)
1141 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1143 :error = Transmogrifier.handle_incoming(accept_data)
1145 follower = User.get_cached_by_id(follower.id)
1147 refute User.following?(follower, followed) == true
1150 test "it fails for incoming rejects which cannot be correlated" do
1151 follower = insert(:user)
1152 followed = insert(:user, locked: true)
1155 File.read!("test/fixtures/mastodon-reject-activity.json")
1157 |> Map.put("actor", followed.ap_id)
1160 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1162 :error = Transmogrifier.handle_incoming(accept_data)
1164 follower = User.get_cached_by_id(follower.id)
1166 refute User.following?(follower, followed) == true
1169 test "it works for incoming rejects which are orphaned" do
1170 follower = insert(:user)
1171 followed = insert(:user, locked: true)
1173 {:ok, follower} = User.follow(follower, followed)
1174 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1176 assert User.following?(follower, followed) == true
1179 File.read!("test/fixtures/mastodon-reject-activity.json")
1181 |> Map.put("actor", followed.ap_id)
1184 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1186 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1187 refute activity.local
1188 assert activity.data["id"] == reject_data["id"]
1190 follower = User.get_cached_by_id(follower.id)
1192 assert User.following?(follower, followed) == false
1195 test "it works for incoming rejects which are referenced by IRI only" do
1196 follower = insert(:user)
1197 followed = insert(:user, locked: true)
1199 {:ok, follower} = User.follow(follower, followed)
1200 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1202 assert User.following?(follower, followed) == true
1205 File.read!("test/fixtures/mastodon-reject-activity.json")
1207 |> Map.put("actor", followed.ap_id)
1208 |> Map.put("object", follow_activity.data["id"])
1210 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1212 follower = User.get_cached_by_id(follower.id)
1214 assert User.following?(follower, followed) == false
1217 test "it rejects activities without a valid ID" do
1218 user = insert(:user)
1221 File.read!("test/fixtures/mastodon-follow-activity.json")
1223 |> Map.put("object", user.ap_id)
1224 |> Map.put("id", "")
1226 :error = Transmogrifier.handle_incoming(data)
1229 test "it remaps video URLs as attachments if necessary" do
1231 Fetcher.fetch_object_from_id(
1232 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1237 "mediaType" => "video/mp4",
1239 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1240 "mimeType" => "video/mp4",
1241 "size" => 5_015_880,
1245 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1246 "mediaType" => "video/mp4",
1253 assert object.data["url"] ==
1254 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1256 assert object.data["attachment"] == [attachment]
1259 test "it accepts Flag activities" do
1260 user = insert(:user)
1261 other_user = insert(:user)
1263 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1264 object = Object.normalize(activity)
1268 "id" => activity.data["id"],
1269 "content" => "test post",
1270 "published" => object.data["published"],
1271 "actor" => AccountView.render("show.json", %{user: user})
1275 "@context" => "https://www.w3.org/ns/activitystreams",
1276 "cc" => [user.ap_id],
1277 "object" => [user.ap_id, activity.data["id"]],
1279 "content" => "blocked AND reported!!!",
1280 "actor" => other_user.ap_id
1283 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1285 assert activity.data["object"] == [user.ap_id, note_obj]
1286 assert activity.data["content"] == "blocked AND reported!!!"
1287 assert activity.data["actor"] == other_user.ap_id
1288 assert activity.data["cc"] == [user.ap_id]
1291 test "it correctly processes messages with non-array to field" do
1292 user = insert(:user)
1295 "@context" => "https://www.w3.org/ns/activitystreams",
1296 "to" => "https://www.w3.org/ns/activitystreams#Public",
1299 "content" => "blah blah blah",
1301 "attributedTo" => user.ap_id,
1304 "actor" => user.ap_id
1307 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1309 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1312 test "it correctly processes messages with non-array cc field" do
1313 user = insert(:user)
1316 "@context" => "https://www.w3.org/ns/activitystreams",
1317 "to" => user.follower_address,
1318 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1321 "content" => "blah blah blah",
1323 "attributedTo" => user.ap_id,
1326 "actor" => user.ap_id
1329 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1331 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1332 assert [user.follower_address] == activity.data["to"]
1335 test "it accepts Move activities" do
1336 old_user = insert(:user)
1337 new_user = insert(:user)
1340 "@context" => "https://www.w3.org/ns/activitystreams",
1342 "actor" => old_user.ap_id,
1343 "object" => old_user.ap_id,
1344 "target" => new_user.ap_id
1347 assert :error = Transmogrifier.handle_incoming(message)
1349 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1351 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1352 assert activity.actor == old_user.ap_id
1353 assert activity.data["actor"] == old_user.ap_id
1354 assert activity.data["object"] == old_user.ap_id
1355 assert activity.data["target"] == new_user.ap_id
1356 assert activity.data["type"] == "Move"
1360 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1361 clear_config([:activitypub, :note_replies_output_limit]) do
1362 Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5)
1365 clear_config([:instance, :federation_incoming_replies_max_depth])
1369 "test/fixtures/mastodon-post-activity.json"
1373 items = get_in(data, ["object", "replies", "first", "items"])
1374 assert length(items) > 0
1376 %{data: data, items: items}
1379 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1383 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1385 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1388 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1389 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1393 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1395 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1397 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1399 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1403 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1404 clear_config([:activitypub, :note_replies_output_limit]) do
1405 Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5)
1408 clear_config([:instance, :federation_incoming_replies_max_depth])
1411 user = insert(:user)
1413 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1416 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1419 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1421 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1423 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1425 Repo.delete(activity.object)
1426 Repo.delete(activity)
1428 %{federation_output: federation_output, replies_uris: replies_uris}
1431 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1432 federation_output: federation_output,
1433 replies_uris: replies_uris
1435 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1437 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1439 for id <- replies_uris do
1440 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1441 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1445 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1446 %{federation_output: federation_output} do
1447 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1449 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1451 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1455 describe "prepare outgoing" do
1456 test "it inlines private announced objects" do
1457 user = insert(:user)
1459 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1461 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1463 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1465 assert modified["object"]["content"] == "hey"
1466 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1469 test "it turns mentions into tags" do
1470 user = insert(:user)
1471 other_user = insert(:user)
1474 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1476 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1477 object = modified["object"]
1479 expected_mention = %{
1480 "href" => other_user.ap_id,
1481 "name" => "@#{other_user.nickname}",
1486 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1487 "type" => "Hashtag",
1491 assert Enum.member?(object["tag"], expected_tag)
1492 assert Enum.member?(object["tag"], expected_mention)
1495 test "it adds the sensitive property" do
1496 user = insert(:user)
1498 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1499 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1501 assert modified["object"]["sensitive"]
1504 test "it adds the json-ld context and the conversation property" do
1505 user = insert(:user)
1507 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1508 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1510 assert modified["@context"] ==
1511 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1513 assert modified["object"]["conversation"] == modified["context"]
1516 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1517 user = insert(:user)
1519 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1520 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1522 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1525 test "it strips internal hashtag data" do
1526 user = insert(:user)
1528 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1531 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1532 "type" => "Hashtag",
1536 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1538 assert modified["object"]["tag"] == [expected_tag]
1541 test "it strips internal fields" do
1542 user = insert(:user)
1544 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1546 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1548 assert length(modified["object"]["tag"]) == 2
1550 assert is_nil(modified["object"]["emoji"])
1551 assert is_nil(modified["object"]["like_count"])
1552 assert is_nil(modified["object"]["announcements"])
1553 assert is_nil(modified["object"]["announcement_count"])
1554 assert is_nil(modified["object"]["context_id"])
1557 test "it strips internal fields of article" do
1558 activity = insert(:article_activity)
1560 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1562 assert length(modified["object"]["tag"]) == 2
1564 assert is_nil(modified["object"]["emoji"])
1565 assert is_nil(modified["object"]["like_count"])
1566 assert is_nil(modified["object"]["announcements"])
1567 assert is_nil(modified["object"]["announcement_count"])
1568 assert is_nil(modified["object"]["context_id"])
1569 assert is_nil(modified["object"]["likes"])
1572 test "the directMessage flag is present" do
1573 user = insert(:user)
1574 other_user = insert(:user)
1576 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1578 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1580 assert modified["directMessage"] == false
1583 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1585 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1587 assert modified["directMessage"] == false
1590 CommonAPI.post(user, %{
1591 "status" => "@#{other_user.nickname} :moominmamma:",
1592 "visibility" => "direct"
1595 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1597 assert modified["directMessage"] == true
1600 test "it strips BCC field" do
1601 user = insert(:user)
1602 {:ok, list} = Pleroma.List.create("foo", user)
1605 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1607 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1609 assert is_nil(modified["bcc"])
1612 test "it can handle Listen activities" do
1613 listen_activity = insert(:listen)
1615 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1617 assert modified["type"] == "Listen"
1619 user = insert(:user)
1621 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1623 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1627 describe "user upgrade" do
1628 test "it upgrades a user to activitypub" do
1631 nickname: "rye@niu.moe",
1633 ap_id: "https://niu.moe/users/rye",
1634 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1637 user_two = insert(:user)
1638 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1640 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1641 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1642 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1644 user = User.get_cached_by_id(user.id)
1645 assert user.note_count == 1
1647 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1648 ObanHelpers.perform_all()
1650 assert user.ap_enabled
1651 assert user.note_count == 1
1652 assert user.follower_address == "https://niu.moe/users/rye/followers"
1653 assert user.following_address == "https://niu.moe/users/rye/following"
1655 user = User.get_cached_by_id(user.id)
1656 assert user.note_count == 1
1658 activity = Activity.get_by_id(activity.id)
1659 assert user.follower_address in activity.recipients
1665 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1674 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1679 refute "..." in activity.recipients
1681 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1682 refute user.follower_address in unrelated_activity.recipients
1684 user_two = User.get_cached_by_id(user_two.id)
1685 assert User.following?(user_two, user)
1686 refute "..." in User.following(user_two)
1690 describe "actor rewriting" do
1691 test "it fixes the actor URL property to be a proper URI" do
1693 "url" => %{"href" => "http://example.com"}
1696 rewritten = Transmogrifier.maybe_fix_user_object(data)
1697 assert rewritten["url"] == "http://example.com"
1701 describe "actor origin containment" do
1702 test "it rejects activities which reference objects with bogus origins" do
1704 "@context" => "https://www.w3.org/ns/activitystreams",
1705 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1706 "actor" => "http://mastodon.example.org/users/admin",
1707 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1708 "object" => "https://info.pleroma.site/activity.json",
1709 "type" => "Announce"
1712 assert capture_log(fn ->
1713 :error = Transmogrifier.handle_incoming(data)
1714 end) =~ "Object containment failed"
1717 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1719 "@context" => "https://www.w3.org/ns/activitystreams",
1720 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1721 "actor" => "http://mastodon.example.org/users/admin",
1722 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1723 "object" => "https://info.pleroma.site/activity2.json",
1724 "type" => "Announce"
1727 assert capture_log(fn ->
1728 :error = Transmogrifier.handle_incoming(data)
1729 end) =~ "Object containment failed"
1732 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1734 "@context" => "https://www.w3.org/ns/activitystreams",
1735 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1736 "actor" => "http://mastodon.example.org/users/admin",
1737 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1738 "object" => "https://info.pleroma.site/activity3.json",
1739 "type" => "Announce"
1742 assert capture_log(fn ->
1743 :error = Transmogrifier.handle_incoming(data)
1744 end) =~ "Object containment failed"
1748 describe "reserialization" do
1749 test "successfully reserializes a message with inReplyTo == nil" do
1750 user = insert(:user)
1753 "@context" => "https://www.w3.org/ns/activitystreams",
1754 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1758 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1763 "attributedTo" => user.ap_id
1765 "actor" => user.ap_id
1768 {:ok, activity} = Transmogrifier.handle_incoming(message)
1770 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1773 test "successfully reserializes a message with AS2 objects in IR" do
1774 user = insert(:user)
1777 "@context" => "https://www.w3.org/ns/activitystreams",
1778 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1782 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1787 "attributedTo" => user.ap_id,
1789 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1790 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1793 "actor" => user.ap_id
1796 {:ok, activity} = Transmogrifier.handle_incoming(message)
1798 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1802 test "Rewrites Answers to Notes" do
1803 user = insert(:user)
1805 {:ok, poll_activity} =
1806 CommonAPI.post(user, %{
1807 "status" => "suya...",
1808 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1811 poll_object = Object.normalize(poll_activity)
1812 # TODO: Replace with CommonAPI vote creation when implemented
1814 File.read!("test/fixtures/mastodon-vote.json")
1816 |> Kernel.put_in(["to"], user.ap_id)
1817 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1818 |> Kernel.put_in(["object", "to"], user.ap_id)
1820 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1821 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1823 assert data["object"]["type"] == "Note"
1826 describe "fix_explicit_addressing" do
1828 user = insert(:user)
1832 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1833 explicitly_mentioned_actors = [
1834 "https://pleroma.gold/users/user1",
1835 "https://pleroma.gold/user2"
1839 "actor" => user.ap_id,
1840 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1843 Enum.map(explicitly_mentioned_actors, fn href ->
1844 %{"type" => "Mention", "href" => href}
1848 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1849 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1850 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1851 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1854 test "does not move actor's follower collection to cc", %{user: user} do
1856 "actor" => user.ap_id,
1857 "to" => [user.follower_address],
1861 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1862 assert user.follower_address in fixed_object["to"]
1863 refute user.follower_address in fixed_object["cc"]
1866 test "removes recipient's follower collection from cc", %{user: user} do
1867 recipient = insert(:user)
1870 "actor" => user.ap_id,
1871 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1872 "cc" => [user.follower_address, recipient.follower_address]
1875 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1877 assert user.follower_address in fixed_object["cc"]
1878 refute recipient.follower_address in fixed_object["cc"]
1879 refute recipient.follower_address in fixed_object["to"]
1883 describe "fix_summary/1" do
1884 test "returns fixed object" do
1885 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1886 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1887 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1891 describe "fix_in_reply_to/2" do
1892 clear_config([:instance, :federation_incoming_replies_max_depth])
1895 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1899 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1900 assert Transmogrifier.fix_in_reply_to(data) == data
1903 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1904 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1907 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1909 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1910 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1911 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1914 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1916 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1917 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1918 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1921 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1923 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1924 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1925 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1927 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1928 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1929 assert modified_object["inReplyTo"] == []
1930 assert modified_object["inReplyToAtomUri"] == ""
1933 @tag capture_log: true
1934 test "returns modified object when allowed incoming reply", %{data: data} do
1939 "https://shitposter.club/notice/2827873"
1942 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1943 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1945 assert modified_object["inReplyTo"] ==
1946 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1948 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1950 assert modified_object["conversation"] ==
1951 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1953 assert modified_object["context"] ==
1954 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1958 describe "fix_url/1" do
1959 test "fixes data for object when url is map" do
1963 "mimeType" => "video/mp4",
1964 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1968 assert Transmogrifier.fix_url(object) == %{
1969 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1973 test "fixes data for video object" do
1979 "mimeType" => "video/mp4",
1980 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1984 "mimeType" => "video/mp4",
1985 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1989 "mimeType" => "text/html",
1990 "href" => "https://peertube.-2d4c2d1630e3"
1994 "mimeType" => "text/html",
1995 "href" => "https://peertube.-2d4c2d16377-42"
2000 assert Transmogrifier.fix_url(object) == %{
2003 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
2004 "mimeType" => "video/mp4",
2009 "url" => "https://peertube.-2d4c2d1630e3"
2013 test "fixes url for not Video object" do
2019 "mimeType" => "text/html",
2020 "href" => "https://peertube.-2d4c2d1630e3"
2024 "mimeType" => "text/html",
2025 "href" => "https://peertube.-2d4c2d16377-42"
2030 assert Transmogrifier.fix_url(object) == %{
2032 "url" => "https://peertube.-2d4c2d1630e3"
2035 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
2041 test "retunrs not modified object" do
2042 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
2046 describe "get_obj_helper/2" do
2047 test "returns nil when cannot normalize object" do
2048 assert capture_log(fn ->
2049 refute Transmogrifier.get_obj_helper("test-obj-id")
2050 end) =~ "Unsupported URI scheme"
2053 @tag capture_log: true
2054 test "returns {:ok, %Object{}} for success case" do
2055 assert {:ok, %Object{}} =
2056 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
2060 describe "fix_attachments/1" do
2061 test "returns not modified object" do
2062 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2063 assert Transmogrifier.fix_attachments(data) == data
2066 test "returns modified object when attachment is map" do
2067 assert Transmogrifier.fix_attachments(%{
2069 "mediaType" => "video/mp4",
2070 "url" => "https://peertube.moe/stat-480.mp4"
2075 "mediaType" => "video/mp4",
2078 "href" => "https://peertube.moe/stat-480.mp4",
2079 "mediaType" => "video/mp4",
2088 test "returns modified object when attachment is list" do
2089 assert Transmogrifier.fix_attachments(%{
2091 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
2092 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
2097 "mediaType" => "video/mp4",
2100 "href" => "https://pe.er/stat-480.mp4",
2101 "mediaType" => "video/mp4",
2107 "href" => "https://pe.er/stat-480.mp4",
2108 "mediaType" => "video/mp4",
2109 "mimeType" => "video/mp4",
2112 "href" => "https://pe.er/stat-480.mp4",
2113 "mediaType" => "video/mp4",
2123 describe "fix_emoji/1" do
2124 test "returns not modified object when object not contains tags" do
2125 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2126 assert Transmogrifier.fix_emoji(data) == data
2129 test "returns object with emoji when object contains list tags" do
2130 assert Transmogrifier.fix_emoji(%{
2132 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
2133 %{"type" => "Hashtag"}
2136 "emoji" => %{"bib" => "/test"},
2138 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
2139 %{"type" => "Hashtag"}
2144 test "returns object with emoji when object contains map tag" do
2145 assert Transmogrifier.fix_emoji(%{
2146 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
2148 "emoji" => %{"bib" => "/test"},
2149 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
2154 describe "set_replies/1" do
2155 clear_config([:activitypub, :note_replies_output_limit]) do
2156 Pleroma.Config.put([:activitypub, :note_replies_output_limit], 2)
2159 test "returns unmodified object if activity doesn't have self-replies" do
2160 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2161 assert Transmogrifier.set_replies(data) == data
2164 test "sets `replies` collection with a limited number of self-replies" do
2165 [user, another_user] = insert_list(2, :user)
2167 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
2169 {:ok, %{id: id2} = self_reply1} =
2170 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
2172 {:ok, self_reply2} =
2173 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
2175 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
2177 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
2180 CommonAPI.post(user, %{
2181 "status" => "self-reply to self-reply",
2182 "in_reply_to_status_id" => id2
2186 CommonAPI.post(another_user, %{
2187 "status" => "another user's reply",
2188 "in_reply_to_status_id" => id1
2191 object = Object.normalize(activity)
2192 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
2194 assert %{"type" => "Collection", "items" => ^replies_uris} =
2195 Transmogrifier.set_replies(object.data)["replies"]