1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
9 alias Pleroma.Object.Fetcher
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.AdminAPI.AccountView
15 alias Pleroma.Web.CommonAPI
18 import Pleroma.Factory
19 import ExUnit.CaptureLog
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 clear_config([:instance, :max_remote_account_fields])
28 describe "handle_incoming" do
29 test "it ignores an incoming notice if we already have it" do
30 activity = insert(:note_activity)
33 File.read!("test/fixtures/mastodon-post-activity.json")
35 |> Map.put("object", Object.normalize(activity).data)
37 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
39 assert activity == returned_activity
42 @tag capture_log: true
43 test "it fetches replied-to activities if we don't have them" do
45 File.read!("test/fixtures/mastodon-post-activity.json")
50 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
52 data = Map.put(data, "object", object)
53 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 returned_object = Object.normalize(returned_activity, false)
57 Activity.get_create_by_object_ap_id(
58 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
61 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
64 test "it does not fetch replied-to activities beyond max_replies_depth" do
66 File.read!("test/fixtures/mastodon-post-activity.json")
71 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
73 data = Map.put(data, "object", object)
75 with_mock Pleroma.Web.Federator,
76 allowed_incoming_reply_depth?: fn _ -> false end do
77 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
79 returned_object = Object.normalize(returned_activity, false)
81 refute Activity.get_create_by_object_ap_id(
82 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
85 assert returned_object.data["inReplyToAtomUri"] ==
86 "https://shitposter.club/notice/2827873"
90 test "it does not crash if the object in inReplyTo can't be fetched" do
92 File.read!("test/fixtures/mastodon-post-activity.json")
97 |> Map.put("inReplyTo", "https://404.site/whatever")
101 |> Map.put("object", object)
103 assert capture_log(fn ->
104 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
105 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
108 test "it works for incoming notices" do
109 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
111 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
114 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
116 assert data["context"] ==
117 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
119 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
121 assert data["cc"] == [
122 "http://mastodon.example.org/users/admin/followers",
123 "http://localtesting.pleroma.lol/users/lain"
126 assert data["actor"] == "http://mastodon.example.org/users/admin"
128 object_data = Object.normalize(data["object"]).data
130 assert object_data["id"] ==
131 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
133 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
135 assert object_data["cc"] == [
136 "http://mastodon.example.org/users/admin/followers",
137 "http://localtesting.pleroma.lol/users/lain"
140 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
141 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
143 assert object_data["context"] ==
144 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
146 assert object_data["sensitive"] == true
148 user = User.get_cached_by_ap_id(object_data["actor"])
150 assert user.note_count == 1
153 test "it works for incoming notices with hashtags" do
154 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
156 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
157 object = Object.normalize(data["object"])
159 assert Enum.at(object.data["tag"], 2) == "moo"
162 test "it works for incoming questions" do
163 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
165 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
167 object = Object.normalize(activity)
169 assert Enum.all?(object.data["oneOf"], fn choice ->
172 "Everyone knows that!",
173 "25 char limit is dumb",
174 "I can't even fit a funny"
179 test "it works for incoming listens" do
181 "@context" => "https://www.w3.org/ns/activitystreams",
182 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
185 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
186 "actor" => "http://mastodon.example.org/users/admin",
189 "id" => "http://mastodon.example.org/users/admin/listens/1234",
190 "attributedTo" => "http://mastodon.example.org/users/admin",
191 "title" => "lain radio episode 1",
193 "album" => "lain radio",
198 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
200 object = Object.normalize(activity)
202 assert object.data["title"] == "lain radio episode 1"
203 assert object.data["artist"] == "lain"
204 assert object.data["album"] == "lain radio"
205 assert object.data["length"] == 180_000
208 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
212 CommonAPI.post(user, %{
213 "status" => "suya...",
214 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
217 object = Object.normalize(activity)
220 File.read!("test/fixtures/mastodon-vote.json")
222 |> Kernel.put_in(["to"], user.ap_id)
223 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
224 |> Kernel.put_in(["object", "to"], user.ap_id)
226 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
227 answer_object = Object.normalize(activity)
228 assert answer_object.data["type"] == "Answer"
229 object = Object.get_by_ap_id(object.data["id"])
232 object.data["oneOf"],
234 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
240 test "it works for incoming notices with contentMap" do
242 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
244 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
245 object = Object.normalize(data["object"])
247 assert object.data["content"] ==
248 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
251 test "it works for incoming notices with to/cc not being an array (kroeg)" do
252 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
254 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
255 object = Object.normalize(data["object"])
257 assert object.data["content"] ==
258 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
261 test "it works for incoming announces with actor being inlined (kroeg)" do
262 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
264 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
266 assert data["actor"] == "https://puckipedia.com/"
269 test "it works for incoming notices with tag not being an array (kroeg)" do
270 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
272 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
273 object = Object.normalize(data["object"])
275 assert object.data["emoji"] == %{
276 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
279 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
281 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
282 object = Object.normalize(data["object"])
284 assert "test" in object.data["tag"]
287 test "it works for incoming notices with url not being a string (prismo)" do
288 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
290 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
291 object = Object.normalize(data["object"])
293 assert object.data["url"] == "https://prismo.news/posts/83"
296 test "it cleans up incoming notices which are not really DMs" do
298 other_user = insert(:user)
300 to = [user.ap_id, other_user.ap_id]
303 File.read!("test/fixtures/mastodon-post-activity.json")
313 data = Map.put(data, "object", object)
315 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
317 assert data["to"] == []
318 assert data["cc"] == to
320 object_data = Object.normalize(activity).data
322 assert object_data["to"] == []
323 assert object_data["cc"] == to
326 test "it works for incoming likes" do
328 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
331 File.read!("test/fixtures/mastodon-like.json")
333 |> Map.put("object", activity.data["object"])
335 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
337 assert data["actor"] == "http://mastodon.example.org/users/admin"
338 assert data["type"] == "Like"
339 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
340 assert data["object"] == activity.data["object"]
343 test "it works for incoming misskey likes, turning them into EmojiReactions" do
345 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
348 File.read!("test/fixtures/misskey-like.json")
350 |> Map.put("object", activity.data["object"])
352 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
354 assert data["actor"] == data["actor"]
355 assert data["type"] == "EmojiReaction"
356 assert data["id"] == data["id"]
357 assert data["object"] == activity.data["object"]
358 assert data["content"] == "🍮"
361 test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReactions" do
363 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
366 File.read!("test/fixtures/misskey-like.json")
368 |> Map.put("object", activity.data["object"])
369 |> Map.put("_misskey_reaction", "⭐")
371 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
373 assert data["actor"] == data["actor"]
374 assert data["type"] == "EmojiReaction"
375 assert data["id"] == data["id"]
376 assert data["object"] == activity.data["object"]
377 assert data["content"] == "⭐"
380 test "it works for incoming emoji reactions" do
382 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
385 File.read!("test/fixtures/emoji-reaction.json")
387 |> Map.put("object", activity.data["object"])
389 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
391 assert data["actor"] == "http://mastodon.example.org/users/admin"
392 assert data["type"] == "EmojiReaction"
393 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
394 assert data["object"] == activity.data["object"]
395 assert data["content"] == "👌"
398 test "it works for incoming emoji reaction undos" do
401 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
402 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
405 File.read!("test/fixtures/mastodon-undo-like.json")
407 |> Map.put("object", reaction_activity.data["id"])
408 |> Map.put("actor", user.ap_id)
410 {:ok, activity} = Transmogrifier.handle_incoming(data)
412 assert activity.actor == user.ap_id
413 assert activity.data["id"] == data["id"]
414 assert activity.data["type"] == "Undo"
417 test "it returns an error for incoming unlikes wihout a like activity" do
419 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
422 File.read!("test/fixtures/mastodon-undo-like.json")
424 |> Map.put("object", activity.data["object"])
426 assert Transmogrifier.handle_incoming(data) == :error
429 test "it works for incoming unlikes with an existing like activity" do
431 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
434 File.read!("test/fixtures/mastodon-like.json")
436 |> Map.put("object", activity.data["object"])
438 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
441 File.read!("test/fixtures/mastodon-undo-like.json")
443 |> Map.put("object", like_data)
444 |> Map.put("actor", like_data["actor"])
446 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
448 assert data["actor"] == "http://mastodon.example.org/users/admin"
449 assert data["type"] == "Undo"
450 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
451 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
454 test "it works for incoming unlikes with an existing like activity and a compact object" do
456 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
459 File.read!("test/fixtures/mastodon-like.json")
461 |> Map.put("object", activity.data["object"])
463 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
466 File.read!("test/fixtures/mastodon-undo-like.json")
468 |> Map.put("object", like_data["id"])
469 |> Map.put("actor", like_data["actor"])
471 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
473 assert data["actor"] == "http://mastodon.example.org/users/admin"
474 assert data["type"] == "Undo"
475 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
476 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
479 test "it works for incoming announces" do
480 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
482 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
484 assert data["actor"] == "http://mastodon.example.org/users/admin"
485 assert data["type"] == "Announce"
488 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
490 assert data["object"] ==
491 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
493 assert Activity.get_create_by_object_ap_id(data["object"])
496 test "it works for incoming announces with an existing activity" do
498 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
501 File.read!("test/fixtures/mastodon-announce.json")
503 |> Map.put("object", activity.data["object"])
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"] == activity.data["object"]
515 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
518 test "it works for incoming announces with an inlined activity" do
520 File.read!("test/fixtures/mastodon-announce-private.json")
523 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
525 assert data["actor"] == "http://mastodon.example.org/users/admin"
526 assert data["type"] == "Announce"
529 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
531 object = Object.normalize(data["object"])
533 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
534 assert object.data["content"] == "this is a private toot"
537 @tag capture_log: true
538 test "it rejects incoming announces with an inlined activity from another origin" do
540 File.read!("test/fixtures/bogus-mastodon-announce.json")
543 assert :error = Transmogrifier.handle_incoming(data)
546 test "it does not clobber the addressing on announce activities" do
548 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
551 File.read!("test/fixtures/mastodon-announce.json")
553 |> Map.put("object", Object.normalize(activity).data["id"])
554 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
557 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
559 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
562 test "it ensures that as:Public activities make it to their followers collection" do
566 File.read!("test/fixtures/mastodon-post-activity.json")
568 |> Map.put("actor", user.ap_id)
569 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
574 |> Map.put("attributedTo", user.ap_id)
575 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
577 |> Map.put("id", user.ap_id <> "/activities/12345678")
579 data = Map.put(data, "object", object)
581 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
583 assert data["cc"] == [User.ap_followers(user)]
586 test "it ensures that address fields become lists" do
590 File.read!("test/fixtures/mastodon-post-activity.json")
592 |> Map.put("actor", user.ap_id)
593 |> Map.put("to", nil)
594 |> Map.put("cc", nil)
598 |> Map.put("attributedTo", user.ap_id)
599 |> Map.put("to", nil)
600 |> Map.put("cc", nil)
601 |> Map.put("id", user.ap_id <> "/activities/12345678")
603 data = Map.put(data, "object", object)
605 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
607 assert !is_nil(data["to"])
608 assert !is_nil(data["cc"])
611 test "it strips internal likes" do
613 File.read!("test/fixtures/mastodon-post-activity.json")
618 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
619 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
621 "type" => "OrderedCollection"
624 object = Map.put(data["object"], "likes", likes)
625 data = Map.put(data, "object", object)
627 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
629 refute Map.has_key?(object.data, "likes")
632 test "it strips internal reactions" do
634 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
635 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
637 %{object: object} = Activity.get_by_id_with_object(activity.id)
638 assert Map.has_key?(object.data, "reactions")
639 assert Map.has_key?(object.data, "reaction_count")
641 object_data = Transmogrifier.strip_internal_fields(object.data)
642 refute Map.has_key?(object_data, "reactions")
643 refute Map.has_key?(object_data, "reaction_count")
646 test "it works for incoming update activities" do
647 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
649 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
650 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
653 update_data["object"]
654 |> Map.put("actor", data["actor"])
655 |> Map.put("id", data["actor"])
659 |> Map.put("actor", data["actor"])
660 |> Map.put("object", object)
662 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
664 assert data["id"] == update_data["id"]
666 user = User.get_cached_by_ap_id(data["actor"])
667 assert user.name == "gargle"
669 assert user.avatar["url"] == [
672 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
676 assert user.banner["url"] == [
679 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
683 assert user.bio == "<p>Some bio</p>"
686 test "it works with alsoKnownAs" do
687 {:ok, %Activity{data: %{"actor" => actor}}} =
688 "test/fixtures/mastodon-post-activity.json"
691 |> Transmogrifier.handle_incoming()
693 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
696 "test/fixtures/mastodon-update.json"
699 |> Map.put("actor", actor)
700 |> Map.update!("object", fn object ->
702 |> Map.put("actor", actor)
703 |> Map.put("id", actor)
704 |> Map.put("alsoKnownAs", [
705 "http://mastodon.example.org/users/foo",
706 "http://example.org/users/bar"
709 |> Transmogrifier.handle_incoming()
711 assert User.get_cached_by_ap_id(actor).also_known_as == [
712 "http://mastodon.example.org/users/foo",
713 "http://example.org/users/bar"
717 test "it works with custom profile fields" do
719 "test/fixtures/mastodon-post-activity.json"
722 |> Transmogrifier.handle_incoming()
724 user = User.get_cached_by_ap_id(activity.actor)
726 assert User.fields(user) == [
727 %{"name" => "foo", "value" => "bar"},
728 %{"name" => "foo1", "value" => "bar1"}
731 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
734 update_data["object"]
735 |> Map.put("actor", user.ap_id)
736 |> Map.put("id", user.ap_id)
740 |> Map.put("actor", user.ap_id)
741 |> Map.put("object", object)
743 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
745 user = User.get_cached_by_ap_id(user.ap_id)
747 assert User.fields(user) == [
748 %{"name" => "foo", "value" => "updated"},
749 %{"name" => "foo1", "value" => "updated"}
752 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
755 put_in(update_data, ["object", "attachment"], [
756 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
757 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
758 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
761 {:ok, _} = Transmogrifier.handle_incoming(update_data)
763 user = User.get_cached_by_ap_id(user.ap_id)
765 assert User.fields(user) == [
766 %{"name" => "foo", "value" => "updated"},
767 %{"name" => "foo1", "value" => "updated"}
770 update_data = put_in(update_data, ["object", "attachment"], [])
772 {:ok, _} = Transmogrifier.handle_incoming(update_data)
774 user = User.get_cached_by_ap_id(user.ap_id)
776 assert User.fields(user) == []
779 test "it works for incoming update activities which lock the account" do
780 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
782 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
783 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
786 update_data["object"]
787 |> Map.put("actor", data["actor"])
788 |> Map.put("id", data["actor"])
789 |> Map.put("manuallyApprovesFollowers", true)
793 |> Map.put("actor", data["actor"])
794 |> Map.put("object", object)
796 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
798 user = User.get_cached_by_ap_id(data["actor"])
799 assert user.locked == true
802 test "it works for incoming deletes" do
803 activity = insert(:note_activity)
804 deleting_user = insert(:user)
807 File.read!("test/fixtures/mastodon-delete.json")
812 |> Map.put("id", activity.data["object"])
816 |> Map.put("object", object)
817 |> Map.put("actor", deleting_user.ap_id)
819 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
820 Transmogrifier.handle_incoming(data)
822 assert id == data["id"]
823 refute Activity.get_by_id(activity.id)
824 assert actor == deleting_user.ap_id
827 test "it fails for incoming deletes with spoofed origin" do
828 activity = insert(:note_activity)
831 File.read!("test/fixtures/mastodon-delete.json")
836 |> Map.put("id", activity.data["object"])
840 |> Map.put("object", object)
842 assert capture_log(fn ->
843 :error = Transmogrifier.handle_incoming(data)
845 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
847 assert Activity.get_by_id(activity.id)
850 @tag capture_log: true
851 test "it works for incoming user deletes" do
852 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
855 File.read!("test/fixtures/mastodon-delete-user.json")
858 {:ok, _} = Transmogrifier.handle_incoming(data)
859 ObanHelpers.perform_all()
861 refute User.get_cached_by_ap_id(ap_id)
864 test "it fails for incoming user deletes with spoofed origin" do
865 %{ap_id: ap_id} = insert(:user)
868 File.read!("test/fixtures/mastodon-delete-user.json")
870 |> Map.put("actor", ap_id)
872 assert capture_log(fn ->
873 assert :error == Transmogrifier.handle_incoming(data)
874 end) =~ "Object containment failed"
876 assert User.get_cached_by_ap_id(ap_id)
879 test "it works for incoming unannounces with an existing notice" do
881 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
884 File.read!("test/fixtures/mastodon-announce.json")
886 |> Map.put("object", activity.data["object"])
888 {:ok, %Activity{data: announce_data, local: false}} =
889 Transmogrifier.handle_incoming(announce_data)
892 File.read!("test/fixtures/mastodon-undo-announce.json")
894 |> Map.put("object", announce_data)
895 |> Map.put("actor", announce_data["actor"])
897 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
899 assert data["type"] == "Undo"
900 assert object_data = data["object"]
901 assert object_data["type"] == "Announce"
902 assert object_data["object"] == activity.data["object"]
904 assert object_data["id"] ==
905 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
908 test "it works for incomming unfollows with an existing follow" do
912 File.read!("test/fixtures/mastodon-follow-activity.json")
914 |> Map.put("object", user.ap_id)
916 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
919 File.read!("test/fixtures/mastodon-unfollow-activity.json")
921 |> Map.put("object", follow_data)
923 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
925 assert data["type"] == "Undo"
926 assert data["object"]["type"] == "Follow"
927 assert data["object"]["object"] == user.ap_id
928 assert data["actor"] == "http://mastodon.example.org/users/admin"
930 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
933 test "it works for incoming follows to locked account" do
934 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
935 user = insert(:user, locked: true)
938 File.read!("test/fixtures/mastodon-follow-activity.json")
940 |> Map.put("object", user.ap_id)
942 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
944 assert data["type"] == "Follow"
945 assert data["object"] == user.ap_id
946 assert data["state"] == "pending"
947 assert data["actor"] == "http://mastodon.example.org/users/admin"
949 assert [^pending_follower] = User.get_follow_requests(user)
952 test "it works for incoming blocks" do
956 File.read!("test/fixtures/mastodon-block-activity.json")
958 |> Map.put("object", user.ap_id)
960 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
962 assert data["type"] == "Block"
963 assert data["object"] == user.ap_id
964 assert data["actor"] == "http://mastodon.example.org/users/admin"
966 blocker = User.get_cached_by_ap_id(data["actor"])
968 assert User.blocks?(blocker, user)
971 test "incoming blocks successfully tear down any follow relationship" do
972 blocker = insert(:user)
973 blocked = insert(:user)
976 File.read!("test/fixtures/mastodon-block-activity.json")
978 |> Map.put("object", blocked.ap_id)
979 |> Map.put("actor", blocker.ap_id)
981 {:ok, blocker} = User.follow(blocker, blocked)
982 {:ok, blocked} = User.follow(blocked, blocker)
984 assert User.following?(blocker, blocked)
985 assert User.following?(blocked, blocker)
987 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
989 assert data["type"] == "Block"
990 assert data["object"] == blocked.ap_id
991 assert data["actor"] == blocker.ap_id
993 blocker = User.get_cached_by_ap_id(data["actor"])
994 blocked = User.get_cached_by_ap_id(data["object"])
996 assert User.blocks?(blocker, blocked)
998 refute User.following?(blocker, blocked)
999 refute User.following?(blocked, blocker)
1002 test "it works for incoming unblocks with an existing block" do
1003 user = insert(:user)
1006 File.read!("test/fixtures/mastodon-block-activity.json")
1008 |> Map.put("object", user.ap_id)
1010 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
1013 File.read!("test/fixtures/mastodon-unblock-activity.json")
1015 |> Map.put("object", block_data)
1017 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
1018 assert data["type"] == "Undo"
1019 assert data["object"]["type"] == "Block"
1020 assert data["object"]["object"] == user.ap_id
1021 assert data["actor"] == "http://mastodon.example.org/users/admin"
1023 blocker = User.get_cached_by_ap_id(data["actor"])
1025 refute User.blocks?(blocker, user)
1028 test "it works for incoming accepts which were pre-accepted" do
1029 follower = insert(:user)
1030 followed = insert(:user)
1032 {:ok, follower} = User.follow(follower, followed)
1033 assert User.following?(follower, followed) == true
1035 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1038 File.read!("test/fixtures/mastodon-accept-activity.json")
1040 |> Map.put("actor", followed.ap_id)
1043 accept_data["object"]
1044 |> Map.put("actor", follower.ap_id)
1045 |> Map.put("id", follow_activity.data["id"])
1047 accept_data = Map.put(accept_data, "object", object)
1049 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1050 refute activity.local
1052 assert activity.data["object"] == follow_activity.data["id"]
1054 assert activity.data["id"] == accept_data["id"]
1056 follower = User.get_cached_by_id(follower.id)
1058 assert User.following?(follower, followed) == true
1061 test "it works for incoming accepts which were orphaned" do
1062 follower = insert(:user)
1063 followed = insert(:user, locked: true)
1065 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1068 File.read!("test/fixtures/mastodon-accept-activity.json")
1070 |> Map.put("actor", followed.ap_id)
1073 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1075 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1076 assert activity.data["object"] == follow_activity.data["id"]
1078 follower = User.get_cached_by_id(follower.id)
1080 assert User.following?(follower, followed) == true
1083 test "it works for incoming accepts which are referenced by IRI only" do
1084 follower = insert(:user)
1085 followed = insert(:user, locked: true)
1087 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1090 File.read!("test/fixtures/mastodon-accept-activity.json")
1092 |> Map.put("actor", followed.ap_id)
1093 |> Map.put("object", follow_activity.data["id"])
1095 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1096 assert activity.data["object"] == follow_activity.data["id"]
1098 follower = User.get_cached_by_id(follower.id)
1100 assert User.following?(follower, followed) == true
1103 test "it fails for incoming accepts which cannot be correlated" do
1104 follower = insert(:user)
1105 followed = insert(:user, locked: true)
1108 File.read!("test/fixtures/mastodon-accept-activity.json")
1110 |> Map.put("actor", followed.ap_id)
1113 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1115 :error = Transmogrifier.handle_incoming(accept_data)
1117 follower = User.get_cached_by_id(follower.id)
1119 refute User.following?(follower, followed) == true
1122 test "it fails for incoming rejects which cannot be correlated" do
1123 follower = insert(:user)
1124 followed = insert(:user, locked: true)
1127 File.read!("test/fixtures/mastodon-reject-activity.json")
1129 |> Map.put("actor", followed.ap_id)
1132 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1134 :error = Transmogrifier.handle_incoming(accept_data)
1136 follower = User.get_cached_by_id(follower.id)
1138 refute User.following?(follower, followed) == true
1141 test "it works for incoming rejects which are orphaned" do
1142 follower = insert(:user)
1143 followed = insert(:user, locked: true)
1145 {:ok, follower} = User.follow(follower, followed)
1146 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1148 assert User.following?(follower, followed) == true
1151 File.read!("test/fixtures/mastodon-reject-activity.json")
1153 |> Map.put("actor", followed.ap_id)
1156 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1158 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1159 refute activity.local
1160 assert activity.data["id"] == reject_data["id"]
1162 follower = User.get_cached_by_id(follower.id)
1164 assert User.following?(follower, followed) == false
1167 test "it works for incoming rejects which are referenced by IRI only" do
1168 follower = insert(:user)
1169 followed = insert(:user, locked: true)
1171 {:ok, follower} = User.follow(follower, followed)
1172 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1174 assert User.following?(follower, followed) == true
1177 File.read!("test/fixtures/mastodon-reject-activity.json")
1179 |> Map.put("actor", followed.ap_id)
1180 |> Map.put("object", follow_activity.data["id"])
1182 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1184 follower = User.get_cached_by_id(follower.id)
1186 assert User.following?(follower, followed) == false
1189 test "it rejects activities without a valid ID" do
1190 user = insert(:user)
1193 File.read!("test/fixtures/mastodon-follow-activity.json")
1195 |> Map.put("object", user.ap_id)
1196 |> Map.put("id", "")
1198 :error = Transmogrifier.handle_incoming(data)
1201 test "it remaps video URLs as attachments if necessary" do
1203 Fetcher.fetch_object_from_id(
1204 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1209 "mediaType" => "video/mp4",
1211 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1212 "mimeType" => "video/mp4",
1213 "size" => 5_015_880,
1217 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1218 "mediaType" => "video/mp4",
1225 assert object.data["url"] ==
1226 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1228 assert object.data["attachment"] == [attachment]
1231 test "it accepts Flag activities" do
1232 user = insert(:user)
1233 other_user = insert(:user)
1235 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1236 object = Object.normalize(activity)
1240 "id" => activity.data["id"],
1241 "content" => "test post",
1242 "published" => object.data["published"],
1243 "actor" => AccountView.render("show.json", %{user: user})
1247 "@context" => "https://www.w3.org/ns/activitystreams",
1248 "cc" => [user.ap_id],
1249 "object" => [user.ap_id, activity.data["id"]],
1251 "content" => "blocked AND reported!!!",
1252 "actor" => other_user.ap_id
1255 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1257 assert activity.data["object"] == [user.ap_id, note_obj]
1258 assert activity.data["content"] == "blocked AND reported!!!"
1259 assert activity.data["actor"] == other_user.ap_id
1260 assert activity.data["cc"] == [user.ap_id]
1263 test "it correctly processes messages with non-array to field" do
1264 user = insert(:user)
1267 "@context" => "https://www.w3.org/ns/activitystreams",
1268 "to" => "https://www.w3.org/ns/activitystreams#Public",
1271 "content" => "blah blah blah",
1273 "attributedTo" => user.ap_id,
1276 "actor" => user.ap_id
1279 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1281 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1284 test "it correctly processes messages with non-array cc field" do
1285 user = insert(:user)
1288 "@context" => "https://www.w3.org/ns/activitystreams",
1289 "to" => user.follower_address,
1290 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1293 "content" => "blah blah blah",
1295 "attributedTo" => user.ap_id,
1298 "actor" => user.ap_id
1301 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1303 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1304 assert [user.follower_address] == activity.data["to"]
1307 test "it accepts Move activities" do
1308 old_user = insert(:user)
1309 new_user = insert(:user)
1312 "@context" => "https://www.w3.org/ns/activitystreams",
1314 "actor" => old_user.ap_id,
1315 "object" => old_user.ap_id,
1316 "target" => new_user.ap_id
1319 assert :error = Transmogrifier.handle_incoming(message)
1321 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
1323 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
1324 assert activity.actor == old_user.ap_id
1325 assert activity.data["actor"] == old_user.ap_id
1326 assert activity.data["object"] == old_user.ap_id
1327 assert activity.data["target"] == new_user.ap_id
1328 assert activity.data["type"] == "Move"
1332 describe "prepare outgoing" do
1333 test "it inlines private announced objects" do
1334 user = insert(:user)
1336 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1338 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1340 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1342 assert modified["object"]["content"] == "hey"
1343 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1346 test "it turns mentions into tags" do
1347 user = insert(:user)
1348 other_user = insert(:user)
1351 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1353 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1354 object = modified["object"]
1356 expected_mention = %{
1357 "href" => other_user.ap_id,
1358 "name" => "@#{other_user.nickname}",
1363 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1364 "type" => "Hashtag",
1368 assert Enum.member?(object["tag"], expected_tag)
1369 assert Enum.member?(object["tag"], expected_mention)
1372 test "it adds the sensitive property" do
1373 user = insert(:user)
1375 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1376 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1378 assert modified["object"]["sensitive"]
1381 test "it adds the json-ld context and the conversation property" do
1382 user = insert(:user)
1384 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1385 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1387 assert modified["@context"] ==
1388 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1390 assert modified["object"]["conversation"] == modified["context"]
1393 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1394 user = insert(:user)
1396 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1397 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1399 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1402 test "it strips internal hashtag data" do
1403 user = insert(:user)
1405 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1408 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1409 "type" => "Hashtag",
1413 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1415 assert modified["object"]["tag"] == [expected_tag]
1418 test "it strips internal fields" do
1419 user = insert(:user)
1421 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1423 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1425 assert length(modified["object"]["tag"]) == 2
1427 assert is_nil(modified["object"]["emoji"])
1428 assert is_nil(modified["object"]["like_count"])
1429 assert is_nil(modified["object"]["announcements"])
1430 assert is_nil(modified["object"]["announcement_count"])
1431 assert is_nil(modified["object"]["context_id"])
1434 test "it strips internal fields of article" do
1435 activity = insert(:article_activity)
1437 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1439 assert length(modified["object"]["tag"]) == 2
1441 assert is_nil(modified["object"]["emoji"])
1442 assert is_nil(modified["object"]["like_count"])
1443 assert is_nil(modified["object"]["announcements"])
1444 assert is_nil(modified["object"]["announcement_count"])
1445 assert is_nil(modified["object"]["context_id"])
1446 assert is_nil(modified["object"]["likes"])
1449 test "the directMessage flag is present" do
1450 user = insert(:user)
1451 other_user = insert(:user)
1453 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1455 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1457 assert modified["directMessage"] == false
1460 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1462 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1464 assert modified["directMessage"] == false
1467 CommonAPI.post(user, %{
1468 "status" => "@#{other_user.nickname} :moominmamma:",
1469 "visibility" => "direct"
1472 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1474 assert modified["directMessage"] == true
1477 test "it strips BCC field" do
1478 user = insert(:user)
1479 {:ok, list} = Pleroma.List.create("foo", user)
1482 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1484 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1486 assert is_nil(modified["bcc"])
1489 test "it can handle Listen activities" do
1490 listen_activity = insert(:listen)
1492 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1494 assert modified["type"] == "Listen"
1496 user = insert(:user)
1498 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1500 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1504 describe "user upgrade" do
1505 test "it upgrades a user to activitypub" do
1508 nickname: "rye@niu.moe",
1510 ap_id: "https://niu.moe/users/rye",
1511 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1514 user_two = insert(:user)
1515 Pleroma.FollowingRelationship.follow(user_two, user, "accept")
1517 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1518 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1519 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1521 user = User.get_cached_by_id(user.id)
1522 assert user.note_count == 1
1524 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1525 ObanHelpers.perform_all()
1527 assert user.ap_enabled
1528 assert user.note_count == 1
1529 assert user.follower_address == "https://niu.moe/users/rye/followers"
1530 assert user.following_address == "https://niu.moe/users/rye/following"
1532 user = User.get_cached_by_id(user.id)
1533 assert user.note_count == 1
1535 activity = Activity.get_by_id(activity.id)
1536 assert user.follower_address in activity.recipients
1542 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1551 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1556 refute "..." in activity.recipients
1558 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1559 refute user.follower_address in unrelated_activity.recipients
1561 user_two = User.get_cached_by_id(user_two.id)
1562 assert User.following?(user_two, user)
1563 refute "..." in User.following(user_two)
1567 describe "actor rewriting" do
1568 test "it fixes the actor URL property to be a proper URI" do
1570 "url" => %{"href" => "http://example.com"}
1573 rewritten = Transmogrifier.maybe_fix_user_object(data)
1574 assert rewritten["url"] == "http://example.com"
1578 describe "actor origin containment" do
1579 test "it rejects activities which reference objects with bogus origins" do
1581 "@context" => "https://www.w3.org/ns/activitystreams",
1582 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1583 "actor" => "http://mastodon.example.org/users/admin",
1584 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1585 "object" => "https://info.pleroma.site/activity.json",
1586 "type" => "Announce"
1589 assert capture_log(fn ->
1590 :error = Transmogrifier.handle_incoming(data)
1591 end) =~ "Object containment failed"
1594 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1596 "@context" => "https://www.w3.org/ns/activitystreams",
1597 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1598 "actor" => "http://mastodon.example.org/users/admin",
1599 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1600 "object" => "https://info.pleroma.site/activity2.json",
1601 "type" => "Announce"
1604 assert capture_log(fn ->
1605 :error = Transmogrifier.handle_incoming(data)
1606 end) =~ "Object containment failed"
1609 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1611 "@context" => "https://www.w3.org/ns/activitystreams",
1612 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1613 "actor" => "http://mastodon.example.org/users/admin",
1614 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1615 "object" => "https://info.pleroma.site/activity3.json",
1616 "type" => "Announce"
1619 assert capture_log(fn ->
1620 :error = Transmogrifier.handle_incoming(data)
1621 end) =~ "Object containment failed"
1625 describe "reserialization" do
1626 test "successfully reserializes a message with inReplyTo == nil" do
1627 user = insert(:user)
1630 "@context" => "https://www.w3.org/ns/activitystreams",
1631 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1635 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1640 "attributedTo" => user.ap_id
1642 "actor" => user.ap_id
1645 {:ok, activity} = Transmogrifier.handle_incoming(message)
1647 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1650 test "successfully reserializes a message with AS2 objects in IR" do
1651 user = insert(:user)
1654 "@context" => "https://www.w3.org/ns/activitystreams",
1655 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1659 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1664 "attributedTo" => user.ap_id,
1666 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1667 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1670 "actor" => user.ap_id
1673 {:ok, activity} = Transmogrifier.handle_incoming(message)
1675 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1679 test "Rewrites Answers to Notes" do
1680 user = insert(:user)
1682 {:ok, poll_activity} =
1683 CommonAPI.post(user, %{
1684 "status" => "suya...",
1685 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1688 poll_object = Object.normalize(poll_activity)
1689 # TODO: Replace with CommonAPI vote creation when implemented
1691 File.read!("test/fixtures/mastodon-vote.json")
1693 |> Kernel.put_in(["to"], user.ap_id)
1694 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1695 |> Kernel.put_in(["object", "to"], user.ap_id)
1697 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1698 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1700 assert data["object"]["type"] == "Note"
1703 describe "fix_explicit_addressing" do
1705 user = insert(:user)
1709 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1710 explicitly_mentioned_actors = [
1711 "https://pleroma.gold/users/user1",
1712 "https://pleroma.gold/user2"
1716 "actor" => user.ap_id,
1717 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1720 Enum.map(explicitly_mentioned_actors, fn href ->
1721 %{"type" => "Mention", "href" => href}
1725 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1726 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1727 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1728 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1731 test "does not move actor's follower collection to cc", %{user: user} do
1733 "actor" => user.ap_id,
1734 "to" => [user.follower_address],
1738 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1739 assert user.follower_address in fixed_object["to"]
1740 refute user.follower_address in fixed_object["cc"]
1743 test "removes recipient's follower collection from cc", %{user: user} do
1744 recipient = insert(:user)
1747 "actor" => user.ap_id,
1748 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1749 "cc" => [user.follower_address, recipient.follower_address]
1752 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1754 assert user.follower_address in fixed_object["cc"]
1755 refute recipient.follower_address in fixed_object["cc"]
1756 refute recipient.follower_address in fixed_object["to"]
1760 describe "fix_summary/1" do
1761 test "returns fixed object" do
1762 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1763 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1764 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1768 describe "fix_in_reply_to/2" do
1769 clear_config([:instance, :federation_incoming_replies_max_depth])
1772 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1776 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1777 assert Transmogrifier.fix_in_reply_to(data) == data
1780 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1781 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1784 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1786 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1787 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1788 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1791 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1793 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1794 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1795 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1798 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1800 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1801 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1802 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1804 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1805 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1806 assert modified_object["inReplyTo"] == []
1807 assert modified_object["inReplyToAtomUri"] == ""
1810 @tag capture_log: true
1811 test "returns modified object when allowed incoming reply", %{data: data} do
1816 "https://shitposter.club/notice/2827873"
1819 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1820 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1822 assert modified_object["inReplyTo"] ==
1823 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1825 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1827 assert modified_object["conversation"] ==
1828 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1830 assert modified_object["context"] ==
1831 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1835 describe "fix_url/1" do
1836 test "fixes data for object when url is map" do
1840 "mimeType" => "video/mp4",
1841 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1845 assert Transmogrifier.fix_url(object) == %{
1846 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1850 test "fixes data for video object" do
1856 "mimeType" => "video/mp4",
1857 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1861 "mimeType" => "video/mp4",
1862 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1866 "mimeType" => "text/html",
1867 "href" => "https://peertube.-2d4c2d1630e3"
1871 "mimeType" => "text/html",
1872 "href" => "https://peertube.-2d4c2d16377-42"
1877 assert Transmogrifier.fix_url(object) == %{
1880 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1881 "mimeType" => "video/mp4",
1886 "url" => "https://peertube.-2d4c2d1630e3"
1890 test "fixes url for not Video object" do
1896 "mimeType" => "text/html",
1897 "href" => "https://peertube.-2d4c2d1630e3"
1901 "mimeType" => "text/html",
1902 "href" => "https://peertube.-2d4c2d16377-42"
1907 assert Transmogrifier.fix_url(object) == %{
1909 "url" => "https://peertube.-2d4c2d1630e3"
1912 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1918 test "retunrs not modified object" do
1919 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1923 describe "get_obj_helper/2" do
1924 test "returns nil when cannot normalize object" do
1925 assert capture_log(fn ->
1926 refute Transmogrifier.get_obj_helper("test-obj-id")
1927 end) =~ "Unsupported URI scheme"
1930 @tag capture_log: true
1931 test "returns {:ok, %Object{}} for success case" do
1932 assert {:ok, %Object{}} =
1933 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1937 describe "fix_attachments/1" do
1938 test "returns not modified object" do
1939 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1940 assert Transmogrifier.fix_attachments(data) == data
1943 test "returns modified object when attachment is map" do
1944 assert Transmogrifier.fix_attachments(%{
1946 "mediaType" => "video/mp4",
1947 "url" => "https://peertube.moe/stat-480.mp4"
1952 "mediaType" => "video/mp4",
1955 "href" => "https://peertube.moe/stat-480.mp4",
1956 "mediaType" => "video/mp4",
1965 test "returns modified object when attachment is list" do
1966 assert Transmogrifier.fix_attachments(%{
1968 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1969 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1974 "mediaType" => "video/mp4",
1977 "href" => "https://pe.er/stat-480.mp4",
1978 "mediaType" => "video/mp4",
1984 "href" => "https://pe.er/stat-480.mp4",
1985 "mediaType" => "video/mp4",
1986 "mimeType" => "video/mp4",
1989 "href" => "https://pe.er/stat-480.mp4",
1990 "mediaType" => "video/mp4",
2000 describe "fix_emoji/1" do
2001 test "returns not modified object when object not contains tags" do
2002 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
2003 assert Transmogrifier.fix_emoji(data) == data
2006 test "returns object with emoji when object contains list tags" do
2007 assert Transmogrifier.fix_emoji(%{
2009 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
2010 %{"type" => "Hashtag"}
2013 "emoji" => %{"bib" => "/test"},
2015 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
2016 %{"type" => "Hashtag"}
2021 test "returns object with emoji when object contains map tag" do
2022 assert Transmogrifier.fix_emoji(%{
2023 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
2025 "emoji" => %{"bib" => "/test"},
2026 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}