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.Tests.ObanHelpers
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 setup do: clear_config([:instance, :max_remote_account_fields])
28 describe "handle_incoming" do
29 test "it works for incoming notices with tag not being an array (kroeg)" do
30 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
32 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
33 object = Object.normalize(data["object"])
35 assert object.data["emoji"] == %{
36 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
39 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
41 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
42 object = Object.normalize(data["object"])
44 assert "test" in object.data["tag"]
47 test "it cleans up incoming notices which are not really DMs" do
49 other_user = insert(:user)
51 to = [user.ap_id, other_user.ap_id]
54 File.read!("test/fixtures/mastodon-post-activity.json")
64 data = Map.put(data, "object", object)
66 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
68 assert data["to"] == []
69 assert data["cc"] == to
71 object_data = Object.normalize(activity).data
73 assert object_data["to"] == []
74 assert object_data["cc"] == to
77 test "it ignores an incoming notice if we already have it" do
78 activity = insert(:note_activity)
81 File.read!("test/fixtures/mastodon-post-activity.json")
83 |> Map.put("object", Object.normalize(activity).data)
85 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
87 assert activity == returned_activity
90 @tag capture_log: true
91 test "it fetches reply-to activities if we don't have them" do
93 File.read!("test/fixtures/mastodon-post-activity.json")
98 |> Map.put("inReplyTo", "https://mstdn.io/users/mayuutann/statuses/99568293732299394")
100 data = Map.put(data, "object", object)
101 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
102 returned_object = Object.normalize(returned_activity, false)
105 Activity.get_create_by_object_ap_id(
106 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
109 assert returned_object.data["inReplyTo"] ==
110 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
113 test "it does not fetch reply-to activities beyond max replies depth limit" do
115 File.read!("test/fixtures/mastodon-post-activity.json")
120 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
122 data = Map.put(data, "object", object)
124 with_mock Pleroma.Web.Federator,
125 allowed_thread_distance?: fn _ -> false end do
126 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
128 returned_object = Object.normalize(returned_activity, false)
130 refute Activity.get_create_by_object_ap_id(
131 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
134 assert returned_object.data["inReplyTo"] == "https://shitposter.club/notice/2827873"
138 test "it does not crash if the object in inReplyTo can't be fetched" do
140 File.read!("test/fixtures/mastodon-post-activity.json")
145 |> Map.put("inReplyTo", "https://404.site/whatever")
149 |> Map.put("object", object)
151 assert capture_log(fn ->
152 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
153 end) =~ "[warn] Couldn't fetch \"https://404.site/whatever\", error: nil"
156 test "it does not work for deactivated users" do
157 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
159 insert(:user, ap_id: data["actor"], deactivated: true)
161 assert {:error, _} = Transmogrifier.handle_incoming(data)
164 test "it works for incoming notices" do
165 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
167 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
170 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
172 assert data["context"] ==
173 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
175 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
177 assert data["cc"] == [
178 "http://mastodon.example.org/users/admin/followers",
179 "http://localtesting.pleroma.lol/users/lain"
182 assert data["actor"] == "http://mastodon.example.org/users/admin"
184 object_data = Object.normalize(data["object"]).data
186 assert object_data["id"] ==
187 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
189 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
191 assert object_data["cc"] == [
192 "http://mastodon.example.org/users/admin/followers",
193 "http://localtesting.pleroma.lol/users/lain"
196 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
197 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
199 assert object_data["context"] ==
200 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
202 assert object_data["sensitive"] == true
204 user = User.get_cached_by_ap_id(object_data["actor"])
206 assert user.note_count == 1
209 test "it works for incoming notices with hashtags" do
210 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
212 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
213 object = Object.normalize(data["object"])
215 assert Enum.at(object.data["tag"], 2) == "moo"
218 test "it works for incoming notices with contentMap" do
220 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
222 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
223 object = Object.normalize(data["object"])
225 assert object.data["content"] ==
226 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
229 test "it works for incoming notices with to/cc not being an array (kroeg)" do
230 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
232 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
233 object = Object.normalize(data["object"])
235 assert object.data["content"] ==
236 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
239 test "it ensures that as:Public activities make it to their followers collection" do
243 File.read!("test/fixtures/mastodon-post-activity.json")
245 |> Map.put("actor", user.ap_id)
246 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
251 |> Map.put("attributedTo", user.ap_id)
252 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
254 |> Map.put("id", user.ap_id <> "/activities/12345678")
256 data = Map.put(data, "object", object)
258 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
260 assert data["cc"] == [User.ap_followers(user)]
263 test "it ensures that address fields become lists" do
267 File.read!("test/fixtures/mastodon-post-activity.json")
269 |> Map.put("actor", user.ap_id)
270 |> Map.put("to", nil)
271 |> Map.put("cc", nil)
275 |> Map.put("attributedTo", user.ap_id)
276 |> Map.put("to", nil)
277 |> Map.put("cc", nil)
278 |> Map.put("id", user.ap_id <> "/activities/12345678")
280 data = Map.put(data, "object", object)
282 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
284 assert !is_nil(data["to"])
285 assert !is_nil(data["cc"])
288 test "it strips internal likes" do
290 File.read!("test/fixtures/mastodon-post-activity.json")
295 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
296 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
298 "type" => "OrderedCollection"
301 object = Map.put(data["object"], "likes", likes)
302 data = Map.put(data, "object", object)
304 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
306 refute Map.has_key?(object.data, "likes")
309 test "it strips internal reactions" do
311 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
312 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
314 %{object: object} = Activity.get_by_id_with_object(activity.id)
315 assert Map.has_key?(object.data, "reactions")
316 assert Map.has_key?(object.data, "reaction_count")
318 object_data = Transmogrifier.strip_internal_fields(object.data)
319 refute Map.has_key?(object_data, "reactions")
320 refute Map.has_key?(object_data, "reaction_count")
323 test "it works for incoming unfollows with an existing follow" do
327 File.read!("test/fixtures/mastodon-follow-activity.json")
329 |> Map.put("object", user.ap_id)
331 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
334 File.read!("test/fixtures/mastodon-unfollow-activity.json")
336 |> Map.put("object", follow_data)
338 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
340 assert data["type"] == "Undo"
341 assert data["object"]["type"] == "Follow"
342 assert data["object"]["object"] == user.ap_id
343 assert data["actor"] == "http://mastodon.example.org/users/admin"
345 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
348 test "it accepts Flag activities" do
350 other_user = insert(:user)
352 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
353 object = Object.normalize(activity)
357 "id" => activity.data["id"],
358 "content" => "test post",
359 "published" => object.data["published"],
360 "actor" => AccountView.render("show.json", %{user: user, skip_visibility_check: true})
364 "@context" => "https://www.w3.org/ns/activitystreams",
365 "cc" => [user.ap_id],
366 "object" => [user.ap_id, activity.data["id"]],
368 "content" => "blocked AND reported!!!",
369 "actor" => other_user.ap_id
372 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
374 assert activity.data["object"] == [user.ap_id, note_obj]
375 assert activity.data["content"] == "blocked AND reported!!!"
376 assert activity.data["actor"] == other_user.ap_id
377 assert activity.data["cc"] == [user.ap_id]
380 test "it correctly processes messages with non-array to field" do
384 "@context" => "https://www.w3.org/ns/activitystreams",
385 "to" => "https://www.w3.org/ns/activitystreams#Public",
388 "content" => "blah blah blah",
390 "attributedTo" => user.ap_id,
393 "actor" => user.ap_id
396 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
398 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
401 test "it correctly processes messages with non-array cc field" do
405 "@context" => "https://www.w3.org/ns/activitystreams",
406 "to" => user.follower_address,
407 "cc" => "https://www.w3.org/ns/activitystreams#Public",
410 "content" => "blah blah blah",
412 "attributedTo" => user.ap_id,
415 "actor" => user.ap_id
418 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
420 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
421 assert [user.follower_address] == activity.data["to"]
424 test "it correctly processes messages with weirdness in address fields" do
428 "@context" => "https://www.w3.org/ns/activitystreams",
429 "to" => [nil, user.follower_address],
430 "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]],
435 "attributedTo" => user.ap_id,
438 "actor" => user.ap_id
441 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
443 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
444 assert [user.follower_address] == activity.data["to"]
447 test "it accepts Move activities" do
448 old_user = insert(:user)
449 new_user = insert(:user)
452 "@context" => "https://www.w3.org/ns/activitystreams",
454 "actor" => old_user.ap_id,
455 "object" => old_user.ap_id,
456 "target" => new_user.ap_id
459 assert :error = Transmogrifier.handle_incoming(message)
461 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
463 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
464 assert activity.actor == old_user.ap_id
465 assert activity.data["actor"] == old_user.ap_id
466 assert activity.data["object"] == old_user.ap_id
467 assert activity.data["target"] == new_user.ap_id
468 assert activity.data["type"] == "Move"
472 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
473 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
474 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
478 "test/fixtures/mastodon-post-activity.json"
482 items = get_in(data, ["object", "replies", "first", "items"])
483 assert length(items) > 0
485 %{data: data, items: items}
488 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
492 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
494 {:ok, _activity} = Transmogrifier.handle_incoming(data)
497 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
498 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
502 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
504 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
506 {:ok, _activity} = Transmogrifier.handle_incoming(data)
508 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
512 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
513 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
514 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
519 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
522 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
525 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
527 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
529 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
531 Repo.delete(activity.object)
532 Repo.delete(activity)
534 %{federation_output: federation_output, replies_uris: replies_uris}
537 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
538 federation_output: federation_output,
539 replies_uris: replies_uris
541 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
543 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
545 for id <- replies_uris do
546 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
547 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
551 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
552 %{federation_output: federation_output} do
553 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
555 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
557 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
561 describe "prepare outgoing" do
562 test "it inlines private announced objects" do
565 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
567 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
569 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
571 assert modified["object"]["content"] == "hey"
572 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
575 test "it turns mentions into tags" do
577 other_user = insert(:user)
580 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
582 with_mock Pleroma.Notification,
583 get_notified_from_activity: fn _, _ -> [] end do
584 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
586 object = modified["object"]
588 expected_mention = %{
589 "href" => other_user.ap_id,
590 "name" => "@#{other_user.nickname}",
595 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
600 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
601 assert Enum.member?(object["tag"], expected_tag)
602 assert Enum.member?(object["tag"], expected_mention)
606 test "it adds the sensitive property" do
609 {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
610 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
612 assert modified["object"]["sensitive"]
615 test "it adds the json-ld context and the conversation property" do
618 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
619 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
621 assert modified["@context"] ==
622 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
624 assert modified["object"]["conversation"] == modified["context"]
627 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
630 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
631 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
633 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
636 test "it strips internal hashtag data" do
639 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
642 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
647 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
649 assert modified["object"]["tag"] == [expected_tag]
652 test "it strips internal fields" do
655 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu :firefox:"})
657 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
659 assert length(modified["object"]["tag"]) == 2
661 assert is_nil(modified["object"]["emoji"])
662 assert is_nil(modified["object"]["like_count"])
663 assert is_nil(modified["object"]["announcements"])
664 assert is_nil(modified["object"]["announcement_count"])
665 assert is_nil(modified["object"]["context_id"])
668 test "it strips internal fields of article" do
669 activity = insert(:article_activity)
671 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
673 assert length(modified["object"]["tag"]) == 2
675 assert is_nil(modified["object"]["emoji"])
676 assert is_nil(modified["object"]["like_count"])
677 assert is_nil(modified["object"]["announcements"])
678 assert is_nil(modified["object"]["announcement_count"])
679 assert is_nil(modified["object"]["context_id"])
680 assert is_nil(modified["object"]["likes"])
683 test "the directMessage flag is present" do
685 other_user = insert(:user)
687 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
689 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
691 assert modified["directMessage"] == false
693 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
695 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
697 assert modified["directMessage"] == false
700 CommonAPI.post(user, %{
701 status: "@#{other_user.nickname} :moominmamma:",
705 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
707 assert modified["directMessage"] == true
710 test "it strips BCC field" do
712 {:ok, list} = Pleroma.List.create("foo", user)
714 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
716 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
718 assert is_nil(modified["bcc"])
721 test "it can handle Listen activities" do
722 listen_activity = insert(:listen)
724 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
726 assert modified["type"] == "Listen"
730 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
732 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
736 describe "user upgrade" do
737 test "it upgrades a user to activitypub" do
740 nickname: "rye@niu.moe",
742 ap_id: "https://niu.moe/users/rye",
743 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
746 user_two = insert(:user)
747 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
749 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
750 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
751 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
753 user = User.get_cached_by_id(user.id)
754 assert user.note_count == 1
756 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
757 ObanHelpers.perform_all()
759 assert user.ap_enabled
760 assert user.note_count == 1
761 assert user.follower_address == "https://niu.moe/users/rye/followers"
762 assert user.following_address == "https://niu.moe/users/rye/following"
764 user = User.get_cached_by_id(user.id)
765 assert user.note_count == 1
767 activity = Activity.get_by_id(activity.id)
768 assert user.follower_address in activity.recipients
774 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
783 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
788 refute "..." in activity.recipients
790 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
791 refute user.follower_address in unrelated_activity.recipients
793 user_two = User.get_cached_by_id(user_two.id)
794 assert User.following?(user_two, user)
795 refute "..." in User.following(user_two)
799 describe "actor rewriting" do
800 test "it fixes the actor URL property to be a proper URI" do
802 "url" => %{"href" => "http://example.com"}
805 rewritten = Transmogrifier.maybe_fix_user_object(data)
806 assert rewritten["url"] == "http://example.com"
810 describe "actor origin containment" do
811 test "it rejects activities which reference objects with bogus origins" do
813 "@context" => "https://www.w3.org/ns/activitystreams",
814 "id" => "http://mastodon.example.org/users/admin/activities/1234",
815 "actor" => "http://mastodon.example.org/users/admin",
816 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
817 "object" => "https://info.pleroma.site/activity.json",
821 assert capture_log(fn ->
822 {:error, _} = Transmogrifier.handle_incoming(data)
823 end) =~ "Object containment failed"
826 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
828 "@context" => "https://www.w3.org/ns/activitystreams",
829 "id" => "http://mastodon.example.org/users/admin/activities/1234",
830 "actor" => "http://mastodon.example.org/users/admin",
831 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
832 "object" => "https://info.pleroma.site/activity2.json",
836 assert capture_log(fn ->
837 {:error, _} = Transmogrifier.handle_incoming(data)
838 end) =~ "Object containment failed"
841 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
843 "@context" => "https://www.w3.org/ns/activitystreams",
844 "id" => "http://mastodon.example.org/users/admin/activities/1234",
845 "actor" => "http://mastodon.example.org/users/admin",
846 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
847 "object" => "https://info.pleroma.site/activity3.json",
851 assert capture_log(fn ->
852 {:error, _} = Transmogrifier.handle_incoming(data)
853 end) =~ "Object containment failed"
857 describe "reserialization" do
858 test "successfully reserializes a message with inReplyTo == nil" do
862 "@context" => "https://www.w3.org/ns/activitystreams",
863 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
867 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
872 "attributedTo" => user.ap_id
874 "actor" => user.ap_id
877 {:ok, activity} = Transmogrifier.handle_incoming(message)
879 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
882 test "successfully reserializes a message with AS2 objects in IR" do
886 "@context" => "https://www.w3.org/ns/activitystreams",
887 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
891 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
896 "attributedTo" => user.ap_id,
898 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
899 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
902 "actor" => user.ap_id
905 {:ok, activity} = Transmogrifier.handle_incoming(message)
907 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
911 describe "fix_explicit_addressing" do
917 test "moves non-explicitly mentioned actors to cc", %{user: user} do
918 explicitly_mentioned_actors = [
919 "https://pleroma.gold/users/user1",
920 "https://pleroma.gold/user2"
924 "actor" => user.ap_id,
925 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
928 Enum.map(explicitly_mentioned_actors, fn href ->
929 %{"type" => "Mention", "href" => href}
933 fixed_object = Transmogrifier.fix_explicit_addressing(object)
934 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
935 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
936 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
939 test "does not move actor's follower collection to cc", %{user: user} do
941 "actor" => user.ap_id,
942 "to" => [user.follower_address],
946 fixed_object = Transmogrifier.fix_explicit_addressing(object)
947 assert user.follower_address in fixed_object["to"]
948 refute user.follower_address in fixed_object["cc"]
951 test "removes recipient's follower collection from cc", %{user: user} do
952 recipient = insert(:user)
955 "actor" => user.ap_id,
956 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
957 "cc" => [user.follower_address, recipient.follower_address]
960 fixed_object = Transmogrifier.fix_explicit_addressing(object)
962 assert user.follower_address in fixed_object["cc"]
963 refute recipient.follower_address in fixed_object["cc"]
964 refute recipient.follower_address in fixed_object["to"]
968 describe "fix_summary/1" do
969 test "returns fixed object" do
970 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
971 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
972 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
976 describe "fix_in_reply_to/2" do
977 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
980 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
984 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
985 assert Transmogrifier.fix_in_reply_to(data) == data
988 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
989 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
992 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
994 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
995 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
998 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1000 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1001 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1004 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1006 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1007 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1009 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1010 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1011 assert modified_object["inReplyTo"] == []
1014 @tag capture_log: true
1015 test "returns modified object when allowed incoming reply", %{data: data} do
1020 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
1023 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1024 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1026 assert modified_object["inReplyTo"] ==
1027 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
1029 assert modified_object["context"] ==
1030 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
1034 describe "fix_url/1" do
1035 test "fixes data for object when url is map" do
1039 "mimeType" => "video/mp4",
1040 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1044 assert Transmogrifier.fix_url(object) == %{
1045 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1049 test "returns non-modified object" do
1050 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1054 describe "get_obj_helper/2" do
1055 test "returns nil when cannot normalize object" do
1056 assert capture_log(fn ->
1057 refute Transmogrifier.get_obj_helper("test-obj-id")
1058 end) =~ "Unsupported URI scheme"
1061 @tag capture_log: true
1062 test "returns {:ok, %Object{}} for success case" do
1063 assert {:ok, %Object{}} =
1064 Transmogrifier.get_obj_helper(
1065 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
1070 describe "fix_attachments/1" do
1071 test "returns not modified object" do
1072 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1073 assert Transmogrifier.fix_attachments(data) == data
1076 test "returns modified object when attachment is map" do
1077 assert Transmogrifier.fix_attachments(%{
1079 "mediaType" => "video/mp4",
1080 "url" => "https://peertube.moe/stat-480.mp4"
1085 "mediaType" => "video/mp4",
1086 "type" => "Document",
1089 "href" => "https://peertube.moe/stat-480.mp4",
1090 "mediaType" => "video/mp4",
1099 test "returns modified object when attachment is list" do
1100 assert Transmogrifier.fix_attachments(%{
1102 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1103 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1108 "mediaType" => "video/mp4",
1109 "type" => "Document",
1112 "href" => "https://pe.er/stat-480.mp4",
1113 "mediaType" => "video/mp4",
1119 "mediaType" => "video/mp4",
1120 "type" => "Document",
1123 "href" => "https://pe.er/stat-480.mp4",
1124 "mediaType" => "video/mp4",
1134 describe "fix_emoji/1" do
1135 test "returns not modified object when object not contains tags" do
1136 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1137 assert Transmogrifier.fix_emoji(data) == data
1140 test "returns object with emoji when object contains list tags" do
1141 assert Transmogrifier.fix_emoji(%{
1143 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1144 %{"type" => "Hashtag"}
1147 "emoji" => %{"bib" => "/test"},
1149 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1150 %{"type" => "Hashtag"}
1155 test "returns object with emoji when object contains map tag" do
1156 assert Transmogrifier.fix_emoji(%{
1157 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1159 "emoji" => %{"bib" => "/test"},
1160 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1165 describe "set_replies/1" do
1166 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1168 test "returns unmodified object if activity doesn't have self-replies" do
1169 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1170 assert Transmogrifier.set_replies(data) == data
1173 test "sets `replies` collection with a limited number of self-replies" do
1174 [user, another_user] = insert_list(2, :user)
1176 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
1178 {:ok, %{id: id2} = self_reply1} =
1179 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
1181 {:ok, self_reply2} =
1182 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
1184 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1185 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
1188 CommonAPI.post(user, %{
1189 status: "self-reply to self-reply",
1190 in_reply_to_status_id: id2
1194 CommonAPI.post(another_user, %{
1195 status: "another user's reply",
1196 in_reply_to_status_id: id1
1199 object = Object.normalize(activity)
1200 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1202 assert %{"type" => "Collection", "items" => ^replies_uris} =
1203 Transmogrifier.set_replies(object.data)["replies"]
1207 test "take_emoji_tags/1" do
1208 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1210 assert Transmogrifier.take_emoji_tags(user) == [
1212 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1213 "id" => "https://example.org/firefox.png",
1214 "name" => ":firefox:",
1216 "updated" => "1970-01-01T00:00:00Z"