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 deletes" do
826 activity = insert(:note_activity)
827 deleting_user = insert(:user)
830 File.read!("test/fixtures/mastodon-delete.json")
835 |> Map.put("id", activity.data["object"])
839 |> Map.put("object", object)
840 |> Map.put("actor", deleting_user.ap_id)
842 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
843 Transmogrifier.handle_incoming(data)
845 assert id == data["id"]
846 refute Activity.get_by_id(activity.id)
847 assert actor == deleting_user.ap_id
850 test "it fails for incoming deletes with spoofed origin" do
851 activity = insert(:note_activity)
854 File.read!("test/fixtures/mastodon-delete.json")
859 |> Map.put("id", activity.data["object"])
863 |> Map.put("object", object)
865 assert capture_log(fn ->
866 :error = Transmogrifier.handle_incoming(data)
868 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
870 assert Activity.get_by_id(activity.id)
873 @tag capture_log: true
874 test "it works for incoming user deletes" do
875 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
878 File.read!("test/fixtures/mastodon-delete-user.json")
881 {:ok, _} = Transmogrifier.handle_incoming(data)
882 ObanHelpers.perform_all()
884 refute User.get_cached_by_ap_id(ap_id)
887 test "it fails for incoming user deletes with spoofed origin" do
888 %{ap_id: ap_id} = insert(:user)
891 File.read!("test/fixtures/mastodon-delete-user.json")
893 |> Map.put("actor", ap_id)
895 assert capture_log(fn ->
896 assert :error == Transmogrifier.handle_incoming(data)
897 end) =~ "Object containment failed"
899 assert User.get_cached_by_ap_id(ap_id)
902 test "it works for incoming unannounces with an existing notice" do
904 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
907 File.read!("test/fixtures/mastodon-announce.json")
909 |> Map.put("object", activity.data["object"])
911 {:ok, %Activity{data: announce_data, local: false}} =
912 Transmogrifier.handle_incoming(announce_data)
915 File.read!("test/fixtures/mastodon-undo-announce.json")
917 |> Map.put("object", announce_data)
918 |> Map.put("actor", announce_data["actor"])
920 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
922 assert data["type"] == "Undo"
923 assert object_data = data["object"]
924 assert object_data["type"] == "Announce"
925 assert object_data["object"] == activity.data["object"]
927 assert object_data["id"] ==
928 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
931 test "it works for incomming unfollows with an existing follow" do
935 File.read!("test/fixtures/mastodon-follow-activity.json")
937 |> Map.put("object", user.ap_id)
939 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
942 File.read!("test/fixtures/mastodon-unfollow-activity.json")
944 |> Map.put("object", follow_data)
946 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
948 assert data["type"] == "Undo"
949 assert data["object"]["type"] == "Follow"
950 assert data["object"]["object"] == user.ap_id
951 assert data["actor"] == "http://mastodon.example.org/users/admin"
953 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
956 test "it works for incoming follows to locked account" do
957 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
958 user = insert(:user, locked: true)
961 File.read!("test/fixtures/mastodon-follow-activity.json")
963 |> Map.put("object", user.ap_id)
965 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
967 assert data["type"] == "Follow"
968 assert data["object"] == user.ap_id
969 assert data["state"] == "pending"
970 assert data["actor"] == "http://mastodon.example.org/users/admin"
972 assert [^pending_follower] = User.get_follow_requests(user)
975 test "it works for incoming blocks" do
979 File.read!("test/fixtures/mastodon-block-activity.json")
981 |> Map.put("object", user.ap_id)
983 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
985 assert data["type"] == "Block"
986 assert data["object"] == user.ap_id
987 assert data["actor"] == "http://mastodon.example.org/users/admin"
989 blocker = User.get_cached_by_ap_id(data["actor"])
991 assert User.blocks?(blocker, user)
994 test "incoming blocks successfully tear down any follow relationship" do
995 blocker = insert(:user)
996 blocked = insert(:user)
999 File.read!("test/fixtures/mastodon-block-activity.json")
1001 |> Map.put("object", blocked.ap_id)
1002 |> Map.put("actor", blocker.ap_id)
1004 {:ok, blocker} = User.follow(blocker, blocked)
1005 {:ok, blocked} = User.follow(blocked, blocker)
1007 assert User.following?(blocker, blocked)
1008 assert User.following?(blocked, blocker)
1010 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
1012 assert data["type"] == "Block"
1013 assert data["object"] == blocked.ap_id
1014 assert data["actor"] == blocker.ap_id
1016 blocker = User.get_cached_by_ap_id(data["actor"])
1017 blocked = User.get_cached_by_ap_id(data["object"])
1019 assert User.blocks?(blocker, blocked)
1021 refute User.following?(blocker, blocked)
1022 refute User.following?(blocked, blocker)
1025 test "it works for incoming unblocks with an existing block" do
1026 user = insert(:user)
1029 File.read!("test/fixtures/mastodon-block-activity.json")
1031 |> Map.put("object", user.ap_id)
1033 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
1036 File.read!("test/fixtures/mastodon-unblock-activity.json")
1038 |> Map.put("object", block_data)
1040 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
1041 assert data["type"] == "Undo"
1042 assert data["object"]["type"] == "Block"
1043 assert data["object"]["object"] == user.ap_id
1044 assert data["actor"] == "http://mastodon.example.org/users/admin"
1046 blocker = User.get_cached_by_ap_id(data["actor"])
1048 refute User.blocks?(blocker, user)
1051 test "it works for incoming accepts which were pre-accepted" do
1052 follower = insert(:user)
1053 followed = insert(:user)
1055 {:ok, follower} = User.follow(follower, followed)
1056 assert User.following?(follower, followed) == true
1058 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1061 File.read!("test/fixtures/mastodon-accept-activity.json")
1063 |> Map.put("actor", followed.ap_id)
1066 accept_data["object"]
1067 |> Map.put("actor", follower.ap_id)
1068 |> Map.put("id", follow_activity.data["id"])
1070 accept_data = Map.put(accept_data, "object", object)
1072 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1073 refute activity.local
1075 assert activity.data["object"] == follow_activity.data["id"]
1077 assert activity.data["id"] == accept_data["id"]
1079 follower = User.get_cached_by_id(follower.id)
1081 assert User.following?(follower, followed) == true
1084 test "it works for incoming accepts which were orphaned" do
1085 follower = insert(:user)
1086 followed = insert(:user, locked: true)
1088 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1091 File.read!("test/fixtures/mastodon-accept-activity.json")
1093 |> Map.put("actor", followed.ap_id)
1096 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1098 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1099 assert activity.data["object"] == follow_activity.data["id"]
1101 follower = User.get_cached_by_id(follower.id)
1103 assert User.following?(follower, followed) == true
1106 test "it works for incoming accepts which are referenced by IRI only" do
1107 follower = insert(:user)
1108 followed = insert(:user, locked: true)
1110 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1113 File.read!("test/fixtures/mastodon-accept-activity.json")
1115 |> Map.put("actor", followed.ap_id)
1116 |> Map.put("object", follow_activity.data["id"])
1118 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1119 assert activity.data["object"] == follow_activity.data["id"]
1121 follower = User.get_cached_by_id(follower.id)
1123 assert User.following?(follower, followed) == true
1126 test "it fails for incoming accepts which cannot be correlated" do
1127 follower = insert(:user)
1128 followed = insert(:user, locked: true)
1131 File.read!("test/fixtures/mastodon-accept-activity.json")
1133 |> Map.put("actor", followed.ap_id)
1136 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1138 :error = Transmogrifier.handle_incoming(accept_data)
1140 follower = User.get_cached_by_id(follower.id)
1142 refute User.following?(follower, followed) == true
1145 test "it fails for incoming rejects which cannot be correlated" do
1146 follower = insert(:user)
1147 followed = insert(:user, locked: true)
1150 File.read!("test/fixtures/mastodon-reject-activity.json")
1152 |> Map.put("actor", followed.ap_id)
1155 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1157 :error = Transmogrifier.handle_incoming(accept_data)
1159 follower = User.get_cached_by_id(follower.id)
1161 refute User.following?(follower, followed) == true
1164 test "it works for incoming rejects which are orphaned" do
1165 follower = insert(:user)
1166 followed = insert(:user, locked: true)
1168 {:ok, follower} = User.follow(follower, followed)
1169 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1171 assert User.following?(follower, followed) == true
1174 File.read!("test/fixtures/mastodon-reject-activity.json")
1176 |> Map.put("actor", followed.ap_id)
1179 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1181 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1182 refute activity.local
1183 assert activity.data["id"] == reject_data["id"]
1185 follower = User.get_cached_by_id(follower.id)
1187 assert User.following?(follower, followed) == false
1190 test "it works for incoming rejects which are referenced by IRI only" do
1191 follower = insert(:user)
1192 followed = insert(:user, locked: true)
1194 {:ok, follower} = User.follow(follower, followed)
1195 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1197 assert User.following?(follower, followed) == true
1200 File.read!("test/fixtures/mastodon-reject-activity.json")
1202 |> Map.put("actor", followed.ap_id)
1203 |> Map.put("object", follow_activity.data["id"])
1205 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1207 follower = User.get_cached_by_id(follower.id)
1209 assert User.following?(follower, followed) == false
1212 test "it rejects activities without a valid ID" do
1213 user = insert(:user)
1216 File.read!("test/fixtures/mastodon-follow-activity.json")
1218 |> Map.put("object", user.ap_id)
1219 |> Map.put("id", "")
1221 :error = Transmogrifier.handle_incoming(data)
1224 test "skip converting the content when it is nil" do
1225 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1227 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1230 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
1232 assert result["content"] == nil
1235 test "it converts content of object to html" do
1236 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
1238 {:ok, %{"content" => content_markdown}} =
1239 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
1241 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
1242 Fetcher.fetch_object_from_id(object_id)
1244 assert content_markdown ==
1245 "Support this and our other Michigan!/usr/group videos and meetings. Learn more at http://mug.org/membership\n\nTwenty Years in Jail: FreeBSD's Jails, Then and Now\n\nJails started as a limited virtualization system, but over the last two years they've..."
1248 "<p>Support this and our other Michigan!/usr/group videos and meetings. Learn more at <a href=\"http://mug.org/membership\">http://mug.org/membership</a></p><p>Twenty Years in Jail: FreeBSD’s Jails, Then and Now</p><p>Jails started as a limited virtualization system, but over the last two years they’ve…</p>"
1250 assert object.data["mediaType"] == "text/html"
1253 test "it remaps video URLs as attachments if necessary" do
1255 Fetcher.fetch_object_from_id(
1256 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1261 "mediaType" => "video/mp4",
1265 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1266 "mediaType" => "video/mp4"
1271 assert object.data["url"] ==
1272 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1274 assert object.data["attachment"] == [attachment]
1277 test "it accepts Flag activities" do
1278 user = insert(:user)
1279 other_user = insert(:user)
1281 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1282 object = Object.normalize(activity)
1286 "id" => activity.data["id"],
1287 "content" => "test post",
1288 "published" => object.data["published"],
1289 "actor" => AccountView.render("show.json", %{user: user})
1293 "@context" => "https://www.w3.org/ns/activitystreams",
1294 "cc" => [user.ap_id],
1295 "object" => [user.ap_id, activity.data["id"]],
1297 "content" => "blocked AND reported!!!",
1298 "actor" => other_user.ap_id
1301 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1303 assert activity.data["object"] == [user.ap_id, note_obj]
1304 assert activity.data["content"] == "blocked AND reported!!!"
1305 assert activity.data["actor"] == other_user.ap_id
1306 assert activity.data["cc"] == [user.ap_id]
1309 test "it correctly processes messages with non-array to field" do
1310 user = insert(:user)
1313 "@context" => "https://www.w3.org/ns/activitystreams",
1314 "to" => "https://www.w3.org/ns/activitystreams#Public",
1317 "content" => "blah blah blah",
1319 "attributedTo" => user.ap_id,
1322 "actor" => user.ap_id
1325 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1327 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1330 test "it correctly processes messages with non-array cc field" do
1331 user = insert(:user)
1334 "@context" => "https://www.w3.org/ns/activitystreams",
1335 "to" => user.follower_address,
1336 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1339 "content" => "blah blah blah",
1341 "attributedTo" => user.ap_id,
1344 "actor" => user.ap_id
1347 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1349 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1350 assert [user.follower_address] == activity.data["to"]
1353 test "it accepts Move activities" do
1354 old_user = insert(:user)
1355 new_user = insert(:user)
1358 "@context" => "https://www.w3.org/ns/activitystreams",
1360 "actor" => old_user.ap_id,
1361 "object" => old_user.ap_id,
1362 "target" => new_user.ap_id
1365 assert :error = Transmogrifier.handle_incoming(message)
1367 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1369 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1370 assert activity.actor == old_user.ap_id
1371 assert activity.data["actor"] == old_user.ap_id
1372 assert activity.data["object"] == old_user.ap_id
1373 assert activity.data["target"] == new_user.ap_id
1374 assert activity.data["type"] == "Move"
1378 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
1379 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1380 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1384 "test/fixtures/mastodon-post-activity.json"
1388 items = get_in(data, ["object", "replies", "first", "items"])
1389 assert length(items) > 0
1391 %{data: data, items: items}
1394 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1398 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1400 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1403 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1404 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1408 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1410 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1412 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1414 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1418 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1419 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1420 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1423 user = insert(:user)
1425 {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"})
1428 CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id})
1431 CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id})
1433 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1435 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1437 Repo.delete(activity.object)
1438 Repo.delete(activity)
1440 %{federation_output: federation_output, replies_uris: replies_uris}
1443 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1444 federation_output: federation_output,
1445 replies_uris: replies_uris
1447 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1449 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1451 for id <- replies_uris do
1452 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1453 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1457 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1458 %{federation_output: federation_output} do
1459 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1461 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1463 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1467 describe "prepare outgoing" do
1468 test "it inlines private announced objects" do
1469 user = insert(:user)
1471 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1473 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1475 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1477 assert modified["object"]["content"] == "hey"
1478 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1481 test "it turns mentions into tags" do
1482 user = insert(:user)
1483 other_user = insert(:user)
1486 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1488 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1489 object = modified["object"]
1491 expected_mention = %{
1492 "href" => other_user.ap_id,
1493 "name" => "@#{other_user.nickname}",
1498 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1499 "type" => "Hashtag",
1503 assert Enum.member?(object["tag"], expected_tag)
1504 assert Enum.member?(object["tag"], expected_mention)
1507 test "it adds the sensitive property" do
1508 user = insert(:user)
1510 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1511 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1513 assert modified["object"]["sensitive"]
1516 test "it adds the json-ld context and the conversation property" do
1517 user = insert(:user)
1519 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1520 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1522 assert modified["@context"] ==
1523 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1525 assert modified["object"]["conversation"] == modified["context"]
1528 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1529 user = insert(:user)
1531 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1532 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1534 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1537 test "it strips internal hashtag data" do
1538 user = insert(:user)
1540 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1543 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1544 "type" => "Hashtag",
1548 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1550 assert modified["object"]["tag"] == [expected_tag]
1553 test "it strips internal fields" do
1554 user = insert(:user)
1556 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1558 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1560 assert length(modified["object"]["tag"]) == 2
1562 assert is_nil(modified["object"]["emoji"])
1563 assert is_nil(modified["object"]["like_count"])
1564 assert is_nil(modified["object"]["announcements"])
1565 assert is_nil(modified["object"]["announcement_count"])
1566 assert is_nil(modified["object"]["context_id"])
1569 test "it strips internal fields of article" do
1570 activity = insert(:article_activity)
1572 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1574 assert length(modified["object"]["tag"]) == 2
1576 assert is_nil(modified["object"]["emoji"])
1577 assert is_nil(modified["object"]["like_count"])
1578 assert is_nil(modified["object"]["announcements"])
1579 assert is_nil(modified["object"]["announcement_count"])
1580 assert is_nil(modified["object"]["context_id"])
1581 assert is_nil(modified["object"]["likes"])
1584 test "the directMessage flag is present" do
1585 user = insert(:user)
1586 other_user = insert(:user)
1588 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1590 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1592 assert modified["directMessage"] == false
1595 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1597 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1599 assert modified["directMessage"] == false
1602 CommonAPI.post(user, %{
1603 "status" => "@#{other_user.nickname} :moominmamma:",
1604 "visibility" => "direct"
1607 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1609 assert modified["directMessage"] == true
1612 test "it strips BCC field" do
1613 user = insert(:user)
1614 {:ok, list} = Pleroma.List.create("foo", user)
1617 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1619 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1621 assert is_nil(modified["bcc"])
1624 test "it can handle Listen activities" do
1625 listen_activity = insert(:listen)
1627 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1629 assert modified["type"] == "Listen"
1631 user = insert(:user)
1633 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1635 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1639 describe "user upgrade" do
1640 test "it upgrades a user to activitypub" do
1643 nickname: "rye@niu.moe",
1645 ap_id: "https://niu.moe/users/rye",
1646 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1649 user_two = insert(:user)
1650 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1652 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1653 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1654 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1656 user = User.get_cached_by_id(user.id)
1657 assert user.note_count == 1
1659 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1660 ObanHelpers.perform_all()
1662 assert user.ap_enabled
1663 assert user.note_count == 1
1664 assert user.follower_address == "https://niu.moe/users/rye/followers"
1665 assert user.following_address == "https://niu.moe/users/rye/following"
1667 user = User.get_cached_by_id(user.id)
1668 assert user.note_count == 1
1670 activity = Activity.get_by_id(activity.id)
1671 assert user.follower_address in activity.recipients
1677 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1686 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1691 refute "..." in activity.recipients
1693 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1694 refute user.follower_address in unrelated_activity.recipients
1696 user_two = User.get_cached_by_id(user_two.id)
1697 assert User.following?(user_two, user)
1698 refute "..." in User.following(user_two)
1702 describe "actor rewriting" do
1703 test "it fixes the actor URL property to be a proper URI" do
1705 "url" => %{"href" => "http://example.com"}
1708 rewritten = Transmogrifier.maybe_fix_user_object(data)
1709 assert rewritten["url"] == "http://example.com"
1713 describe "actor origin containment" do
1714 test "it rejects activities which reference objects with bogus origins" do
1716 "@context" => "https://www.w3.org/ns/activitystreams",
1717 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1718 "actor" => "http://mastodon.example.org/users/admin",
1719 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1720 "object" => "https://info.pleroma.site/activity.json",
1721 "type" => "Announce"
1724 assert capture_log(fn ->
1725 :error = Transmogrifier.handle_incoming(data)
1726 end) =~ "Object containment failed"
1729 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1731 "@context" => "https://www.w3.org/ns/activitystreams",
1732 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1733 "actor" => "http://mastodon.example.org/users/admin",
1734 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1735 "object" => "https://info.pleroma.site/activity2.json",
1736 "type" => "Announce"
1739 assert capture_log(fn ->
1740 :error = Transmogrifier.handle_incoming(data)
1741 end) =~ "Object containment failed"
1744 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1746 "@context" => "https://www.w3.org/ns/activitystreams",
1747 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1748 "actor" => "http://mastodon.example.org/users/admin",
1749 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1750 "object" => "https://info.pleroma.site/activity3.json",
1751 "type" => "Announce"
1754 assert capture_log(fn ->
1755 :error = Transmogrifier.handle_incoming(data)
1756 end) =~ "Object containment failed"
1760 describe "reserialization" do
1761 test "successfully reserializes a message with inReplyTo == nil" do
1762 user = insert(:user)
1765 "@context" => "https://www.w3.org/ns/activitystreams",
1766 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1770 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1775 "attributedTo" => user.ap_id
1777 "actor" => user.ap_id
1780 {:ok, activity} = Transmogrifier.handle_incoming(message)
1782 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1785 test "successfully reserializes a message with AS2 objects in IR" do
1786 user = insert(:user)
1789 "@context" => "https://www.w3.org/ns/activitystreams",
1790 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1794 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1799 "attributedTo" => user.ap_id,
1801 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1802 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1805 "actor" => user.ap_id
1808 {:ok, activity} = Transmogrifier.handle_incoming(message)
1810 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1814 test "Rewrites Answers to Notes" do
1815 user = insert(:user)
1817 {:ok, poll_activity} =
1818 CommonAPI.post(user, %{
1819 "status" => "suya...",
1820 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1823 poll_object = Object.normalize(poll_activity)
1824 # TODO: Replace with CommonAPI vote creation when implemented
1826 File.read!("test/fixtures/mastodon-vote.json")
1828 |> Kernel.put_in(["to"], user.ap_id)
1829 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1830 |> Kernel.put_in(["object", "to"], user.ap_id)
1832 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1833 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1835 assert data["object"]["type"] == "Note"
1838 describe "fix_explicit_addressing" do
1840 user = insert(:user)
1844 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1845 explicitly_mentioned_actors = [
1846 "https://pleroma.gold/users/user1",
1847 "https://pleroma.gold/user2"
1851 "actor" => user.ap_id,
1852 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1855 Enum.map(explicitly_mentioned_actors, fn href ->
1856 %{"type" => "Mention", "href" => href}
1860 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1861 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1862 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1863 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1866 test "does not move actor's follower collection to cc", %{user: user} do
1868 "actor" => user.ap_id,
1869 "to" => [user.follower_address],
1873 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1874 assert user.follower_address in fixed_object["to"]
1875 refute user.follower_address in fixed_object["cc"]
1878 test "removes recipient's follower collection from cc", %{user: user} do
1879 recipient = insert(:user)
1882 "actor" => user.ap_id,
1883 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1884 "cc" => [user.follower_address, recipient.follower_address]
1887 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1889 assert user.follower_address in fixed_object["cc"]
1890 refute recipient.follower_address in fixed_object["cc"]
1891 refute recipient.follower_address in fixed_object["to"]
1895 describe "fix_summary/1" do
1896 test "returns fixed object" do
1897 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1898 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1899 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1903 describe "fix_in_reply_to/2" do
1904 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1907 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1911 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1912 assert Transmogrifier.fix_in_reply_to(data) == data
1915 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1916 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1919 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1921 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1922 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1923 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1926 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1928 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1929 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1930 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1933 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1935 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1936 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1937 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1939 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1940 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1941 assert modified_object["inReplyTo"] == []
1942 assert modified_object["inReplyToAtomUri"] == ""
1945 @tag capture_log: true
1946 test "returns modified object when allowed incoming reply", %{data: data} do
1951 "https://shitposter.club/notice/2827873"
1954 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1955 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1957 assert modified_object["inReplyTo"] ==
1958 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1960 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1962 assert modified_object["conversation"] ==
1963 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1965 assert modified_object["context"] ==
1966 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1970 describe "fix_url/1" do
1971 test "fixes data for object when url is map" do
1975 "mimeType" => "video/mp4",
1976 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1980 assert Transmogrifier.fix_url(object) == %{
1981 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1985 test "fixes data for video object" do
1991 "mimeType" => "video/mp4",
1992 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1996 "mimeType" => "video/mp4",
1997 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
2001 "mimeType" => "text/html",
2002 "href" => "https://peertube.-2d4c2d1630e3"
2006 "mimeType" => "text/html",
2007 "href" => "https://peertube.-2d4c2d16377-42"
2012 assert Transmogrifier.fix_url(object) == %{
2015 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
2016 "mimeType" => "video/mp4",
2021 "url" => "https://peertube.-2d4c2d1630e3"
2025 test "fixes url for not Video object" do
2031 "mimeType" => "text/html",
2032 "href" => "https://peertube.-2d4c2d1630e3"
2036 "mimeType" => "text/html",
2037 "href" => "https://peertube.-2d4c2d16377-42"
2042 assert Transmogrifier.fix_url(object) == %{
2044 "url" => "https://peertube.-2d4c2d1630e3"
2047 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
2053 test "retunrs not modified object" do
2054 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
2058 describe "get_obj_helper/2" do
2059 test "returns nil when cannot normalize object" do
2060 assert capture_log(fn ->
2061 refute Transmogrifier.get_obj_helper("test-obj-id")
2062 end) =~ "Unsupported URI scheme"
2065 @tag capture_log: true
2066 test "returns {:ok, %Object{}} for success case" do
2067 assert {:ok, %Object{}} =
2068 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
2072 describe "fix_attachments/1" do
2073 test "returns not modified object" do
2074 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2075 assert Transmogrifier.fix_attachments(data) == data
2078 test "returns modified object when attachment is map" do
2079 assert Transmogrifier.fix_attachments(%{
2081 "mediaType" => "video/mp4",
2082 "url" => "https://peertube.moe/stat-480.mp4"
2087 "mediaType" => "video/mp4",
2089 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
2096 test "returns modified object when attachment is list" do
2097 assert Transmogrifier.fix_attachments(%{
2099 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
2100 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
2105 "mediaType" => "video/mp4",
2107 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2111 "mediaType" => "video/mp4",
2113 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
2121 describe "fix_emoji/1" do
2122 test "returns not modified object when object not contains tags" do
2123 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2124 assert Transmogrifier.fix_emoji(data) == data
2127 test "returns object with emoji when object contains list tags" do
2128 assert Transmogrifier.fix_emoji(%{
2130 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
2131 %{"type" => "Hashtag"}
2134 "emoji" => %{"bib" => "/test"},
2136 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
2137 %{"type" => "Hashtag"}
2142 test "returns object with emoji when object contains map tag" do
2143 assert Transmogrifier.fix_emoji(%{
2144 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
2146 "emoji" => %{"bib" => "/test"},
2147 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
2152 describe "set_replies/1" do
2153 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
2155 test "returns unmodified object if activity doesn't have self-replies" do
2156 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2157 assert Transmogrifier.set_replies(data) == data
2160 test "sets `replies` collection with a limited number of self-replies" do
2161 [user, another_user] = insert_list(2, :user)
2163 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
2165 {:ok, %{id: id2} = self_reply1} =
2166 CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
2168 {:ok, self_reply2} =
2169 CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
2171 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
2173 CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
2176 CommonAPI.post(user, %{
2177 "status" => "self-reply to self-reply",
2178 "in_reply_to_status_id" => id2
2182 CommonAPI.post(another_user, %{
2183 "status" => "another user's reply",
2184 "in_reply_to_status_id" => id1
2187 object = Object.normalize(activity)
2188 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
2190 assert %{"type" => "Collection", "items" => ^replies_uris} =
2191 Transmogrifier.set_replies(object.data)["replies"]
2195 test "take_emoji_tags/1" do
2196 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
2198 assert Transmogrifier.take_emoji_tags(user) == [
2200 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
2201 "id" => "https://example.org/firefox.png",
2202 "name" => ":firefox:",
2204 "updated" => "1970-01-01T00:00:00Z"