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 setup do: 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} = activity} = Transmogrifier.handle_incoming(data)
339 refute Enum.empty?(activity.recipients)
341 assert data["actor"] == "http://mastodon.example.org/users/admin"
342 assert data["type"] == "Like"
343 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
344 assert data["object"] == activity.data["object"]
347 test "it works for incoming misskey likes, turning them into EmojiReacts" do
349 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
352 File.read!("test/fixtures/misskey-like.json")
354 |> Map.put("object", activity.data["object"])
356 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
358 assert data["actor"] == data["actor"]
359 assert data["type"] == "EmojiReact"
360 assert data["id"] == data["id"]
361 assert data["object"] == activity.data["object"]
362 assert data["content"] == "🍮"
365 test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReacts" do
367 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
370 File.read!("test/fixtures/misskey-like.json")
372 |> Map.put("object", activity.data["object"])
373 |> Map.put("_misskey_reaction", "⭐")
375 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
377 assert data["actor"] == data["actor"]
378 assert data["type"] == "EmojiReact"
379 assert data["id"] == data["id"]
380 assert data["object"] == activity.data["object"]
381 assert data["content"] == "⭐"
384 test "it works for incoming emoji reactions" do
386 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
389 File.read!("test/fixtures/emoji-reaction.json")
391 |> Map.put("object", activity.data["object"])
393 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
395 assert data["actor"] == "http://mastodon.example.org/users/admin"
396 assert data["type"] == "EmojiReact"
397 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
398 assert data["object"] == activity.data["object"]
399 assert data["content"] == "👌"
402 test "it reject invalid emoji reactions" do
404 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
407 File.read!("test/fixtures/emoji-reaction-too-long.json")
409 |> Map.put("object", activity.data["object"])
411 assert :error = Transmogrifier.handle_incoming(data)
414 File.read!("test/fixtures/emoji-reaction-no-emoji.json")
416 |> Map.put("object", activity.data["object"])
418 assert :error = Transmogrifier.handle_incoming(data)
421 test "it works for incoming emoji reaction undos" do
424 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
425 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
428 File.read!("test/fixtures/mastodon-undo-like.json")
430 |> Map.put("object", reaction_activity.data["id"])
431 |> Map.put("actor", user.ap_id)
433 {:ok, activity} = Transmogrifier.handle_incoming(data)
435 assert activity.actor == user.ap_id
436 assert activity.data["id"] == data["id"]
437 assert activity.data["type"] == "Undo"
440 test "it returns an error for incoming unlikes wihout a like activity" do
442 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
445 File.read!("test/fixtures/mastodon-undo-like.json")
447 |> Map.put("object", activity.data["object"])
449 assert Transmogrifier.handle_incoming(data) == :error
452 test "it works for incoming unlikes with an existing like activity" do
454 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
457 File.read!("test/fixtures/mastodon-like.json")
459 |> Map.put("object", activity.data["object"])
461 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
464 File.read!("test/fixtures/mastodon-undo-like.json")
466 |> Map.put("object", like_data)
467 |> Map.put("actor", like_data["actor"])
469 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
471 assert data["actor"] == "http://mastodon.example.org/users/admin"
472 assert data["type"] == "Undo"
473 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
474 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
477 test "it works for incoming unlikes with an existing like activity and a compact object" do
479 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
482 File.read!("test/fixtures/mastodon-like.json")
484 |> Map.put("object", activity.data["object"])
486 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
489 File.read!("test/fixtures/mastodon-undo-like.json")
491 |> Map.put("object", like_data["id"])
492 |> Map.put("actor", like_data["actor"])
494 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
496 assert data["actor"] == "http://mastodon.example.org/users/admin"
497 assert data["type"] == "Undo"
498 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
499 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
502 test "it works for incoming announces" do
503 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
505 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
507 assert data["actor"] == "http://mastodon.example.org/users/admin"
508 assert data["type"] == "Announce"
511 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
513 assert data["object"] ==
514 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
516 assert Activity.get_create_by_object_ap_id(data["object"])
519 test "it works for incoming announces with an existing activity" do
521 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
524 File.read!("test/fixtures/mastodon-announce.json")
526 |> Map.put("object", activity.data["object"])
528 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
530 assert data["actor"] == "http://mastodon.example.org/users/admin"
531 assert data["type"] == "Announce"
534 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
536 assert data["object"] == activity.data["object"]
538 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
541 test "it works for incoming announces with an inlined activity" do
543 File.read!("test/fixtures/mastodon-announce-private.json")
546 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
548 assert data["actor"] == "http://mastodon.example.org/users/admin"
549 assert data["type"] == "Announce"
552 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
554 object = Object.normalize(data["object"])
556 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
557 assert object.data["content"] == "this is a private toot"
560 @tag capture_log: true
561 test "it rejects incoming announces with an inlined activity from another origin" do
563 File.read!("test/fixtures/bogus-mastodon-announce.json")
566 assert :error = Transmogrifier.handle_incoming(data)
569 test "it does not clobber the addressing on announce activities" do
571 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
574 File.read!("test/fixtures/mastodon-announce.json")
576 |> Map.put("object", Object.normalize(activity).data["id"])
577 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
580 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
582 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
585 test "it ensures that as:Public activities make it to their followers collection" do
589 File.read!("test/fixtures/mastodon-post-activity.json")
591 |> Map.put("actor", user.ap_id)
592 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
597 |> Map.put("attributedTo", user.ap_id)
598 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
600 |> Map.put("id", user.ap_id <> "/activities/12345678")
602 data = Map.put(data, "object", object)
604 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
606 assert data["cc"] == [User.ap_followers(user)]
609 test "it ensures that address fields become lists" do
613 File.read!("test/fixtures/mastodon-post-activity.json")
615 |> Map.put("actor", user.ap_id)
616 |> Map.put("to", nil)
617 |> Map.put("cc", nil)
621 |> Map.put("attributedTo", user.ap_id)
622 |> Map.put("to", nil)
623 |> Map.put("cc", nil)
624 |> Map.put("id", user.ap_id <> "/activities/12345678")
626 data = Map.put(data, "object", object)
628 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
630 assert !is_nil(data["to"])
631 assert !is_nil(data["cc"])
634 test "it strips internal likes" do
636 File.read!("test/fixtures/mastodon-post-activity.json")
641 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
642 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
644 "type" => "OrderedCollection"
647 object = Map.put(data["object"], "likes", likes)
648 data = Map.put(data, "object", object)
650 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
652 refute Map.has_key?(object.data, "likes")
655 test "it strips internal reactions" do
657 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
658 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
660 %{object: object} = Activity.get_by_id_with_object(activity.id)
661 assert Map.has_key?(object.data, "reactions")
662 assert Map.has_key?(object.data, "reaction_count")
664 object_data = Transmogrifier.strip_internal_fields(object.data)
665 refute Map.has_key?(object_data, "reactions")
666 refute Map.has_key?(object_data, "reaction_count")
669 test "it works for incoming update activities" do
670 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
672 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
673 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
676 update_data["object"]
677 |> Map.put("actor", data["actor"])
678 |> Map.put("id", data["actor"])
682 |> Map.put("actor", data["actor"])
683 |> Map.put("object", object)
685 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
687 assert data["id"] == update_data["id"]
689 user = User.get_cached_by_ap_id(data["actor"])
690 assert user.name == "gargle"
692 assert user.avatar["url"] == [
695 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
699 assert user.banner["url"] == [
702 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
706 assert user.bio == "<p>Some bio</p>"
709 test "it works with alsoKnownAs" do
710 {:ok, %Activity{data: %{"actor" => actor}}} =
711 "test/fixtures/mastodon-post-activity.json"
714 |> Transmogrifier.handle_incoming()
716 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
719 "test/fixtures/mastodon-update.json"
722 |> Map.put("actor", actor)
723 |> Map.update!("object", fn object ->
725 |> Map.put("actor", actor)
726 |> Map.put("id", actor)
727 |> Map.put("alsoKnownAs", [
728 "http://mastodon.example.org/users/foo",
729 "http://example.org/users/bar"
732 |> Transmogrifier.handle_incoming()
734 assert User.get_cached_by_ap_id(actor).also_known_as == [
735 "http://mastodon.example.org/users/foo",
736 "http://example.org/users/bar"
740 test "it works with custom profile fields" do
742 "test/fixtures/mastodon-post-activity.json"
745 |> Transmogrifier.handle_incoming()
747 user = User.get_cached_by_ap_id(activity.actor)
749 assert user.fields == [
750 %{"name" => "foo", "value" => "bar"},
751 %{"name" => "foo1", "value" => "bar1"}
754 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
757 update_data["object"]
758 |> Map.put("actor", user.ap_id)
759 |> Map.put("id", user.ap_id)
763 |> Map.put("actor", user.ap_id)
764 |> Map.put("object", object)
766 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
768 user = User.get_cached_by_ap_id(user.ap_id)
770 assert user.fields == [
771 %{"name" => "foo", "value" => "updated"},
772 %{"name" => "foo1", "value" => "updated"}
775 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
778 put_in(update_data, ["object", "attachment"], [
779 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
780 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
781 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
784 {:ok, _} = Transmogrifier.handle_incoming(update_data)
786 user = User.get_cached_by_ap_id(user.ap_id)
788 assert user.fields == [
789 %{"name" => "foo", "value" => "updated"},
790 %{"name" => "foo1", "value" => "updated"}
793 update_data = put_in(update_data, ["object", "attachment"], [])
795 {:ok, _} = Transmogrifier.handle_incoming(update_data)
797 user = User.get_cached_by_ap_id(user.ap_id)
799 assert user.fields == []
802 test "it works for incoming update activities which lock the account" do
803 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
805 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
806 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
809 update_data["object"]
810 |> Map.put("actor", data["actor"])
811 |> Map.put("id", data["actor"])
812 |> Map.put("manuallyApprovesFollowers", true)
816 |> Map.put("actor", data["actor"])
817 |> Map.put("object", object)
819 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
821 user = User.get_cached_by_ap_id(data["actor"])
822 assert user.locked == true
825 test "it works for incoming unannounces with an existing notice" do
827 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
830 File.read!("test/fixtures/mastodon-announce.json")
832 |> Map.put("object", activity.data["object"])
834 {:ok, %Activity{data: announce_data, local: false}} =
835 Transmogrifier.handle_incoming(announce_data)
838 File.read!("test/fixtures/mastodon-undo-announce.json")
840 |> Map.put("object", announce_data)
841 |> Map.put("actor", announce_data["actor"])
843 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
845 assert data["type"] == "Undo"
846 assert object_data = data["object"]
847 assert object_data["type"] == "Announce"
848 assert object_data["object"] == activity.data["object"]
850 assert object_data["id"] ==
851 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
854 test "it works for incomming unfollows with an existing follow" do
858 File.read!("test/fixtures/mastodon-follow-activity.json")
860 |> Map.put("object", user.ap_id)
862 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
865 File.read!("test/fixtures/mastodon-unfollow-activity.json")
867 |> Map.put("object", follow_data)
869 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
871 assert data["type"] == "Undo"
872 assert data["object"]["type"] == "Follow"
873 assert data["object"]["object"] == user.ap_id
874 assert data["actor"] == "http://mastodon.example.org/users/admin"
876 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
879 test "it works for incoming follows to locked account" do
880 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
881 user = insert(:user, locked: true)
884 File.read!("test/fixtures/mastodon-follow-activity.json")
886 |> Map.put("object", user.ap_id)
888 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
890 assert data["type"] == "Follow"
891 assert data["object"] == user.ap_id
892 assert data["state"] == "pending"
893 assert data["actor"] == "http://mastodon.example.org/users/admin"
895 assert [^pending_follower] = User.get_follow_requests(user)
898 test "it works for incoming blocks" do
902 File.read!("test/fixtures/mastodon-block-activity.json")
904 |> Map.put("object", user.ap_id)
906 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
908 assert data["type"] == "Block"
909 assert data["object"] == user.ap_id
910 assert data["actor"] == "http://mastodon.example.org/users/admin"
912 blocker = User.get_cached_by_ap_id(data["actor"])
914 assert User.blocks?(blocker, user)
917 test "incoming blocks successfully tear down any follow relationship" do
918 blocker = insert(:user)
919 blocked = insert(:user)
922 File.read!("test/fixtures/mastodon-block-activity.json")
924 |> Map.put("object", blocked.ap_id)
925 |> Map.put("actor", blocker.ap_id)
927 {:ok, blocker} = User.follow(blocker, blocked)
928 {:ok, blocked} = User.follow(blocked, blocker)
930 assert User.following?(blocker, blocked)
931 assert User.following?(blocked, blocker)
933 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
935 assert data["type"] == "Block"
936 assert data["object"] == blocked.ap_id
937 assert data["actor"] == blocker.ap_id
939 blocker = User.get_cached_by_ap_id(data["actor"])
940 blocked = User.get_cached_by_ap_id(data["object"])
942 assert User.blocks?(blocker, blocked)
944 refute User.following?(blocker, blocked)
945 refute User.following?(blocked, blocker)
948 test "it works for incoming unblocks with an existing block" do
952 File.read!("test/fixtures/mastodon-block-activity.json")
954 |> Map.put("object", user.ap_id)
956 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
959 File.read!("test/fixtures/mastodon-unblock-activity.json")
961 |> Map.put("object", block_data)
963 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
964 assert data["type"] == "Undo"
965 assert data["object"]["type"] == "Block"
966 assert data["object"]["object"] == user.ap_id
967 assert data["actor"] == "http://mastodon.example.org/users/admin"
969 blocker = User.get_cached_by_ap_id(data["actor"])
971 refute User.blocks?(blocker, user)
974 test "it works for incoming accepts which were pre-accepted" do
975 follower = insert(:user)
976 followed = insert(:user)
978 {:ok, follower} = User.follow(follower, followed)
979 assert User.following?(follower, followed) == true
981 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
984 File.read!("test/fixtures/mastodon-accept-activity.json")
986 |> Map.put("actor", followed.ap_id)
989 accept_data["object"]
990 |> Map.put("actor", follower.ap_id)
991 |> Map.put("id", follow_activity.data["id"])
993 accept_data = Map.put(accept_data, "object", object)
995 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
996 refute activity.local
998 assert activity.data["object"] == follow_activity.data["id"]
1000 assert activity.data["id"] == accept_data["id"]
1002 follower = User.get_cached_by_id(follower.id)
1004 assert User.following?(follower, followed) == true
1007 test "it works for incoming accepts which were orphaned" do
1008 follower = insert(:user)
1009 followed = insert(:user, locked: true)
1011 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1014 File.read!("test/fixtures/mastodon-accept-activity.json")
1016 |> Map.put("actor", followed.ap_id)
1019 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1021 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1022 assert activity.data["object"] == follow_activity.data["id"]
1024 follower = User.get_cached_by_id(follower.id)
1026 assert User.following?(follower, followed) == true
1029 test "it works for incoming accepts which are referenced by IRI only" do
1030 follower = insert(:user)
1031 followed = insert(:user, locked: true)
1033 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1036 File.read!("test/fixtures/mastodon-accept-activity.json")
1038 |> Map.put("actor", followed.ap_id)
1039 |> Map.put("object", follow_activity.data["id"])
1041 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1042 assert activity.data["object"] == follow_activity.data["id"]
1044 follower = User.get_cached_by_id(follower.id)
1046 assert User.following?(follower, followed) == true
1049 test "it fails for incoming accepts which cannot be correlated" do
1050 follower = insert(:user)
1051 followed = insert(:user, locked: true)
1054 File.read!("test/fixtures/mastodon-accept-activity.json")
1056 |> Map.put("actor", followed.ap_id)
1059 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1061 :error = Transmogrifier.handle_incoming(accept_data)
1063 follower = User.get_cached_by_id(follower.id)
1065 refute User.following?(follower, followed) == true
1068 test "it fails for incoming rejects which cannot be correlated" do
1069 follower = insert(:user)
1070 followed = insert(:user, locked: true)
1073 File.read!("test/fixtures/mastodon-reject-activity.json")
1075 |> Map.put("actor", followed.ap_id)
1078 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1080 :error = Transmogrifier.handle_incoming(accept_data)
1082 follower = User.get_cached_by_id(follower.id)
1084 refute User.following?(follower, followed) == true
1087 test "it works for incoming rejects which are orphaned" do
1088 follower = insert(:user)
1089 followed = insert(:user, locked: true)
1091 {:ok, follower} = User.follow(follower, followed)
1092 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1094 assert User.following?(follower, followed) == true
1097 File.read!("test/fixtures/mastodon-reject-activity.json")
1099 |> Map.put("actor", followed.ap_id)
1102 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1104 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1105 refute activity.local
1106 assert activity.data["id"] == reject_data["id"]
1108 follower = User.get_cached_by_id(follower.id)
1110 assert User.following?(follower, followed) == false
1113 test "it works for incoming rejects which are referenced by IRI only" do
1114 follower = insert(:user)
1115 followed = insert(:user, locked: true)
1117 {:ok, follower} = User.follow(follower, followed)
1118 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1120 assert User.following?(follower, followed) == true
1123 File.read!("test/fixtures/mastodon-reject-activity.json")
1125 |> Map.put("actor", followed.ap_id)
1126 |> Map.put("object", follow_activity.data["id"])
1128 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1130 follower = User.get_cached_by_id(follower.id)
1132 assert User.following?(follower, followed) == false
1135 test "it rejects activities without a valid ID" do
1136 user = insert(:user)
1139 File.read!("test/fixtures/mastodon-follow-activity.json")
1141 |> Map.put("object", user.ap_id)
1142 |> Map.put("id", "")
1144 :error = Transmogrifier.handle_incoming(data)
1147 test "it remaps video URLs as attachments if necessary" do
1149 Fetcher.fetch_object_from_id(
1150 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1155 "mediaType" => "video/mp4",
1159 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1160 "mediaType" => "video/mp4"
1165 assert object.data["url"] ==
1166 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1168 assert object.data["attachment"] == [attachment]
1171 test "it accepts Flag activities" do
1172 user = insert(:user)
1173 other_user = insert(:user)
1175 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1176 object = Object.normalize(activity)
1180 "id" => activity.data["id"],
1181 "content" => "test post",
1182 "published" => object.data["published"],
1183 "actor" => AccountView.render("show.json", %{user: user})
1187 "@context" => "https://www.w3.org/ns/activitystreams",
1188 "cc" => [user.ap_id],
1189 "object" => [user.ap_id, activity.data["id"]],
1191 "content" => "blocked AND reported!!!",
1192 "actor" => other_user.ap_id
1195 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1197 assert activity.data["object"] == [user.ap_id, note_obj]
1198 assert activity.data["content"] == "blocked AND reported!!!"
1199 assert activity.data["actor"] == other_user.ap_id
1200 assert activity.data["cc"] == [user.ap_id]
1203 test "it correctly processes messages with non-array to field" do
1204 user = insert(:user)
1207 "@context" => "https://www.w3.org/ns/activitystreams",
1208 "to" => "https://www.w3.org/ns/activitystreams#Public",
1211 "content" => "blah blah blah",
1213 "attributedTo" => user.ap_id,
1216 "actor" => user.ap_id
1219 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1221 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1224 test "it correctly processes messages with non-array cc field" do
1225 user = insert(:user)
1228 "@context" => "https://www.w3.org/ns/activitystreams",
1229 "to" => user.follower_address,
1230 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1233 "content" => "blah blah blah",
1235 "attributedTo" => user.ap_id,
1238 "actor" => user.ap_id
1241 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1243 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1244 assert [user.follower_address] == activity.data["to"]
1247 test "it accepts Move activities" do
1248 old_user = insert(:user)
1249 new_user = insert(:user)
1252 "@context" => "https://www.w3.org/ns/activitystreams",
1254 "actor" => old_user.ap_id,
1255 "object" => old_user.ap_id,
1256 "target" => new_user.ap_id
1259 assert :error = Transmogrifier.handle_incoming(message)
1261 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1263 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1264 assert activity.actor == old_user.ap_id
1265 assert activity.data["actor"] == old_user.ap_id
1266 assert activity.data["object"] == old_user.ap_id
1267 assert activity.data["target"] == new_user.ap_id
1268 assert activity.data["type"] == "Move"
1272 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1273 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1274 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1278 "test/fixtures/mastodon-post-activity.json"
1282 items = get_in(data, ["object", "replies", "first", "items"])
1283 assert length(items) > 0
1285 %{data: data, items: items}
1288 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1292 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1294 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1297 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1298 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1302 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1304 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1306 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1308 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1312 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1313 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1314 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1317 user = insert(:user)
1319 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1322 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1325 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1327 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1329 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1331 Repo.delete(activity.object)
1332 Repo.delete(activity)
1334 %{federation_output: federation_output, replies_uris: replies_uris}
1337 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1338 federation_output: federation_output,
1339 replies_uris: replies_uris
1341 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1343 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1345 for id <- replies_uris do
1346 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1347 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1351 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1352 %{federation_output: federation_output} do
1353 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1355 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1357 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1361 describe "prepare outgoing" do
1362 test "it inlines private announced objects" do
1363 user = insert(:user)
1365 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1367 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1369 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1371 assert modified["object"]["content"] == "hey"
1372 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1375 test "it turns mentions into tags" do
1376 user = insert(:user)
1377 other_user = insert(:user)
1380 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1382 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1383 object = modified["object"]
1385 expected_mention = %{
1386 "href" => other_user.ap_id,
1387 "name" => "@#{other_user.nickname}",
1392 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1393 "type" => "Hashtag",
1397 assert Enum.member?(object["tag"], expected_tag)
1398 assert Enum.member?(object["tag"], expected_mention)
1401 test "it adds the sensitive property" do
1402 user = insert(:user)
1404 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1405 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1407 assert modified["object"]["sensitive"]
1410 test "it adds the json-ld context and the conversation property" do
1411 user = insert(:user)
1413 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1414 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1416 assert modified["@context"] ==
1417 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1419 assert modified["object"]["conversation"] == modified["context"]
1422 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1423 user = insert(:user)
1425 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1426 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1428 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1431 test "it strips internal hashtag data" do
1432 user = insert(:user)
1434 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1437 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1438 "type" => "Hashtag",
1442 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1444 assert modified["object"]["tag"] == [expected_tag]
1447 test "it strips internal fields" do
1448 user = insert(:user)
1450 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1452 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1454 assert length(modified["object"]["tag"]) == 2
1456 assert is_nil(modified["object"]["emoji"])
1457 assert is_nil(modified["object"]["like_count"])
1458 assert is_nil(modified["object"]["announcements"])
1459 assert is_nil(modified["object"]["announcement_count"])
1460 assert is_nil(modified["object"]["context_id"])
1463 test "it strips internal fields of article" do
1464 activity = insert(:article_activity)
1466 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1468 assert length(modified["object"]["tag"]) == 2
1470 assert is_nil(modified["object"]["emoji"])
1471 assert is_nil(modified["object"]["like_count"])
1472 assert is_nil(modified["object"]["announcements"])
1473 assert is_nil(modified["object"]["announcement_count"])
1474 assert is_nil(modified["object"]["context_id"])
1475 assert is_nil(modified["object"]["likes"])
1478 test "the directMessage flag is present" do
1479 user = insert(:user)
1480 other_user = insert(:user)
1482 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1484 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1486 assert modified["directMessage"] == false
1489 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1491 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1493 assert modified["directMessage"] == false
1496 CommonAPI.post(user, %{
1497 "status" => "@#{other_user.nickname} :moominmamma:",
1498 "visibility" => "direct"
1501 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1503 assert modified["directMessage"] == true
1506 test "it strips BCC field" do
1507 user = insert(:user)
1508 {:ok, list} = Pleroma.List.create("foo", user)
1511 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1513 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1515 assert is_nil(modified["bcc"])
1518 test "it can handle Listen activities" do
1519 listen_activity = insert(:listen)
1521 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1523 assert modified["type"] == "Listen"
1525 user = insert(:user)
1527 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1529 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1533 describe "user upgrade" do
1534 test "it upgrades a user to activitypub" do
1537 nickname: "rye@niu.moe",
1539 ap_id: "https://niu.moe/users/rye",
1540 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1543 user_two = insert(:user)
1544 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1546 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1547 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1548 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1550 user = User.get_cached_by_id(user.id)
1551 assert user.note_count == 1
1553 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1554 ObanHelpers.perform_all()
1556 assert user.ap_enabled
1557 assert user.note_count == 1
1558 assert user.follower_address == "https://niu.moe/users/rye/followers"
1559 assert user.following_address == "https://niu.moe/users/rye/following"
1561 user = User.get_cached_by_id(user.id)
1562 assert user.note_count == 1
1564 activity = Activity.get_by_id(activity.id)
1565 assert user.follower_address in activity.recipients
1571 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1580 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1585 refute "..." in activity.recipients
1587 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1588 refute user.follower_address in unrelated_activity.recipients
1590 user_two = User.get_cached_by_id(user_two.id)
1591 assert User.following?(user_two, user)
1592 refute "..." in User.following(user_two)
1596 describe "actor rewriting" do
1597 test "it fixes the actor URL property to be a proper URI" do
1599 "url" => %{"href" => "http://example.com"}
1602 rewritten = Transmogrifier.maybe_fix_user_object(data)
1603 assert rewritten["url"] == "http://example.com"
1607 describe "actor origin containment" do
1608 test "it rejects activities which reference objects with bogus origins" do
1610 "@context" => "https://www.w3.org/ns/activitystreams",
1611 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1612 "actor" => "http://mastodon.example.org/users/admin",
1613 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1614 "object" => "https://info.pleroma.site/activity.json",
1615 "type" => "Announce"
1618 assert capture_log(fn ->
1619 :error = Transmogrifier.handle_incoming(data)
1620 end) =~ "Object containment failed"
1623 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1625 "@context" => "https://www.w3.org/ns/activitystreams",
1626 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1627 "actor" => "http://mastodon.example.org/users/admin",
1628 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1629 "object" => "https://info.pleroma.site/activity2.json",
1630 "type" => "Announce"
1633 assert capture_log(fn ->
1634 :error = Transmogrifier.handle_incoming(data)
1635 end) =~ "Object containment failed"
1638 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1640 "@context" => "https://www.w3.org/ns/activitystreams",
1641 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1642 "actor" => "http://mastodon.example.org/users/admin",
1643 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1644 "object" => "https://info.pleroma.site/activity3.json",
1645 "type" => "Announce"
1648 assert capture_log(fn ->
1649 :error = Transmogrifier.handle_incoming(data)
1650 end) =~ "Object containment failed"
1654 describe "reserialization" do
1655 test "successfully reserializes a message with inReplyTo == nil" do
1656 user = insert(:user)
1659 "@context" => "https://www.w3.org/ns/activitystreams",
1660 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1664 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1669 "attributedTo" => user.ap_id
1671 "actor" => user.ap_id
1674 {:ok, activity} = Transmogrifier.handle_incoming(message)
1676 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1679 test "successfully reserializes a message with AS2 objects in IR" do
1680 user = insert(:user)
1683 "@context" => "https://www.w3.org/ns/activitystreams",
1684 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1688 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1693 "attributedTo" => user.ap_id,
1695 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1696 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1699 "actor" => user.ap_id
1702 {:ok, activity} = Transmogrifier.handle_incoming(message)
1704 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1708 test "Rewrites Answers to Notes" do
1709 user = insert(:user)
1711 {:ok, poll_activity} =
1712 CommonAPI.post(user, %{
1713 "status" => "suya...",
1714 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1717 poll_object = Object.normalize(poll_activity)
1718 # TODO: Replace with CommonAPI vote creation when implemented
1720 File.read!("test/fixtures/mastodon-vote.json")
1722 |> Kernel.put_in(["to"], user.ap_id)
1723 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1724 |> Kernel.put_in(["object", "to"], user.ap_id)
1726 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1727 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1729 assert data["object"]["type"] == "Note"
1732 describe "fix_explicit_addressing" do
1734 user = insert(:user)
1738 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1739 explicitly_mentioned_actors = [
1740 "https://pleroma.gold/users/user1",
1741 "https://pleroma.gold/user2"
1745 "actor" => user.ap_id,
1746 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1749 Enum.map(explicitly_mentioned_actors, fn href ->
1750 %{"type" => "Mention", "href" => href}
1754 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1755 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1756 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1757 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1760 test "does not move actor's follower collection to cc", %{user: user} do
1762 "actor" => user.ap_id,
1763 "to" => [user.follower_address],
1767 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1768 assert user.follower_address in fixed_object["to"]
1769 refute user.follower_address in fixed_object["cc"]
1772 test "removes recipient's follower collection from cc", %{user: user} do
1773 recipient = insert(:user)
1776 "actor" => user.ap_id,
1777 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1778 "cc" => [user.follower_address, recipient.follower_address]
1781 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1783 assert user.follower_address in fixed_object["cc"]
1784 refute recipient.follower_address in fixed_object["cc"]
1785 refute recipient.follower_address in fixed_object["to"]
1789 describe "fix_summary/1" do
1790 test "returns fixed object" do
1791 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1792 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1793 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1797 describe "fix_in_reply_to/2" do
1798 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1801 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1805 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1806 assert Transmogrifier.fix_in_reply_to(data) == data
1809 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1810 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1813 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1815 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1816 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1817 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1820 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1822 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1823 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1824 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1827 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1829 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1830 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1831 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1833 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1834 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1835 assert modified_object["inReplyTo"] == []
1836 assert modified_object["inReplyToAtomUri"] == ""
1839 @tag capture_log: true
1840 test "returns modified object when allowed incoming reply", %{data: data} do
1845 "https://shitposter.club/notice/2827873"
1848 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1849 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1851 assert modified_object["inReplyTo"] ==
1852 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1854 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1856 assert modified_object["conversation"] ==
1857 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1859 assert modified_object["context"] ==
1860 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1864 describe "fix_url/1" do
1865 test "fixes data for object when url is map" do
1869 "mimeType" => "video/mp4",
1870 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1874 assert Transmogrifier.fix_url(object) == %{
1875 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1879 test "fixes data for video object" do
1885 "mimeType" => "video/mp4",
1886 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1890 "mimeType" => "video/mp4",
1891 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1895 "mimeType" => "text/html",
1896 "href" => "https://peertube.-2d4c2d1630e3"
1900 "mimeType" => "text/html",
1901 "href" => "https://peertube.-2d4c2d16377-42"
1906 assert Transmogrifier.fix_url(object) == %{
1909 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1910 "mimeType" => "video/mp4",
1915 "url" => "https://peertube.-2d4c2d1630e3"
1919 test "fixes url for not Video object" do
1925 "mimeType" => "text/html",
1926 "href" => "https://peertube.-2d4c2d1630e3"
1930 "mimeType" => "text/html",
1931 "href" => "https://peertube.-2d4c2d16377-42"
1936 assert Transmogrifier.fix_url(object) == %{
1938 "url" => "https://peertube.-2d4c2d1630e3"
1941 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1947 test "retunrs not modified object" do
1948 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1952 describe "get_obj_helper/2" do
1953 test "returns nil when cannot normalize object" do
1954 assert capture_log(fn ->
1955 refute Transmogrifier.get_obj_helper("test-obj-id")
1956 end) =~ "Unsupported URI scheme"
1959 @tag capture_log: true
1960 test "returns {:ok, %Object{}} for success case" do
1961 assert {:ok, %Object{}} =
1962 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1966 describe "fix_attachments/1" do
1967 test "returns not modified object" do
1968 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1969 assert Transmogrifier.fix_attachments(data) == data
1972 test "returns modified object when attachment is map" do
1973 assert Transmogrifier.fix_attachments(%{
1975 "mediaType" => "video/mp4",
1976 "url" => "https://peertube.moe/stat-480.mp4"
1981 "mediaType" => "video/mp4",
1983 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1990 test "returns modified object when attachment is list" do
1991 assert Transmogrifier.fix_attachments(%{
1993 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1994 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1999 "mediaType" => "video/mp4",
2001 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2005 "mediaType" => "video/mp4",
2007 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2015 describe "fix_emoji/1" do
2016 test "returns not modified object when object not contains tags" do
2017 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2018 assert Transmogrifier.fix_emoji(data) == data
2021 test "returns object with emoji when object contains list tags" do
2022 assert Transmogrifier.fix_emoji(%{
2024 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
2025 %{"type" => "Hashtag"}
2028 "emoji" => %{"bib" => "/test"},
2030 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
2031 %{"type" => "Hashtag"}
2036 test "returns object with emoji when object contains map tag" do
2037 assert Transmogrifier.fix_emoji(%{
2038 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
2040 "emoji" => %{"bib" => "/test"},
2041 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
2046 describe "set_replies/1" do
2047 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
2049 test "returns unmodified object if activity doesn't have self-replies" do
2050 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2051 assert Transmogrifier.set_replies(data) == data
2054 test "sets `replies` collection with a limited number of self-replies" do
2055 [user, another_user] = insert_list(2, :user)
2057 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
2059 {:ok, %{id: id2} = self_reply1} =
2060 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
2062 {:ok, self_reply2} =
2063 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
2065 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
2067 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
2070 CommonAPI.post(user, %{
2071 "status" => "self-reply to self-reply",
2072 "in_reply_to_status_id" => id2
2076 CommonAPI.post(another_user, %{
2077 "status" => "another user's reply",
2078 "in_reply_to_status_id" => id1
2081 object = Object.normalize(activity)
2082 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
2084 assert %{"type" => "Collection", "items" => ^replies_uris} =
2085 Transmogrifier.set_replies(object.data)["replies"]
2089 test "take_emoji_tags/1" do
2090 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
2092 assert Transmogrifier.take_emoji_tags(user) == [
2094 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
2095 "id" => "https://example.org/firefox.png",
2096 "name" => ":firefox:",
2098 "updated" => "1970-01-01T00:00:00Z"