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 test "it fetches replied-to activities if we don't have them" do
44 File.read!("test/fixtures/mastodon-post-activity.json")
49 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
51 data = Map.put(data, "object", object)
52 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
53 returned_object = Object.normalize(returned_activity, false)
56 Activity.get_create_by_object_ap_id(
57 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
60 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
63 test "it does not fetch replied-to activities beyond max_replies_depth" do
65 File.read!("test/fixtures/mastodon-post-activity.json")
70 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
72 data = Map.put(data, "object", object)
74 with_mock Pleroma.Web.Federator,
75 allowed_incoming_reply_depth?: fn _ -> false end do
76 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
78 returned_object = Object.normalize(returned_activity, false)
80 refute Activity.get_create_by_object_ap_id(
81 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
84 assert returned_object.data["inReplyToAtomUri"] ==
85 "https://shitposter.club/notice/2827873"
89 test "it does not crash if the object in inReplyTo can't be fetched" do
91 File.read!("test/fixtures/mastodon-post-activity.json")
96 |> Map.put("inReplyTo", "https://404.site/whatever")
100 |> Map.put("object", object)
102 assert capture_log(fn ->
103 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
104 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
107 test "it works for incoming notices" do
108 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
113 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
115 assert data["context"] ==
116 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
118 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
120 assert data["cc"] == [
121 "http://mastodon.example.org/users/admin/followers",
122 "http://localtesting.pleroma.lol/users/lain"
125 assert data["actor"] == "http://mastodon.example.org/users/admin"
127 object_data = Object.normalize(data["object"]).data
129 assert object_data["id"] ==
130 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
132 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
134 assert object_data["cc"] == [
135 "http://mastodon.example.org/users/admin/followers",
136 "http://localtesting.pleroma.lol/users/lain"
139 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
140 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
142 assert object_data["context"] ==
143 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
145 assert object_data["sensitive"] == true
147 user = User.get_cached_by_ap_id(object_data["actor"])
149 assert user.note_count == 1
152 test "it works for incoming notices with hashtags" do
153 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
155 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
156 object = Object.normalize(data["object"])
158 assert Enum.at(object.data["tag"], 2) == "moo"
161 test "it works for incoming questions" do
162 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
164 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
166 object = Object.normalize(activity)
168 assert Enum.all?(object.data["oneOf"], fn choice ->
171 "Everyone knows that!",
172 "25 char limit is dumb",
173 "I can't even fit a funny"
178 test "it works for incoming listens" do
180 "@context" => "https://www.w3.org/ns/activitystreams",
181 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
184 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
185 "actor" => "http://mastodon.example.org/users/admin",
188 "id" => "http://mastodon.example.org/users/admin/listens/1234",
189 "attributedTo" => "http://mastodon.example.org/users/admin",
190 "title" => "lain radio episode 1",
192 "album" => "lain radio",
197 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
199 object = Object.normalize(activity)
201 assert object.data["title"] == "lain radio episode 1"
202 assert object.data["artist"] == "lain"
203 assert object.data["album"] == "lain radio"
204 assert object.data["length"] == 180_000
207 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
211 CommonAPI.post(user, %{
212 "status" => "suya...",
213 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
216 object = Object.normalize(activity)
219 File.read!("test/fixtures/mastodon-vote.json")
221 |> Kernel.put_in(["to"], user.ap_id)
222 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
223 |> Kernel.put_in(["object", "to"], user.ap_id)
225 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
226 answer_object = Object.normalize(activity)
227 assert answer_object.data["type"] == "Answer"
228 object = Object.get_by_ap_id(object.data["id"])
231 object.data["oneOf"],
233 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
239 test "it works for incoming notices with contentMap" do
241 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
243 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
244 object = Object.normalize(data["object"])
246 assert object.data["content"] ==
247 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
250 test "it works for incoming notices with to/cc not being an array (kroeg)" do
251 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
253 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
254 object = Object.normalize(data["object"])
256 assert object.data["content"] ==
257 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
260 test "it works for incoming announces with actor being inlined (kroeg)" do
261 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
263 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
265 assert data["actor"] == "https://puckipedia.com/"
268 test "it works for incoming notices with tag not being an array (kroeg)" do
269 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
271 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
272 object = Object.normalize(data["object"])
274 assert object.data["emoji"] == %{
275 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
278 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
280 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
281 object = Object.normalize(data["object"])
283 assert "test" in object.data["tag"]
286 test "it works for incoming notices with url not being a string (prismo)" do
287 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
289 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
290 object = Object.normalize(data["object"])
292 assert object.data["url"] == "https://prismo.news/posts/83"
295 test "it cleans up incoming notices which are not really DMs" do
297 other_user = insert(:user)
299 to = [user.ap_id, other_user.ap_id]
302 File.read!("test/fixtures/mastodon-post-activity.json")
312 data = Map.put(data, "object", object)
314 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
316 assert data["to"] == []
317 assert data["cc"] == to
319 object_data = Object.normalize(activity).data
321 assert object_data["to"] == []
322 assert object_data["cc"] == to
325 test "it works for incoming likes" do
327 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
330 File.read!("test/fixtures/mastodon-like.json")
332 |> Map.put("object", activity.data["object"])
334 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
336 assert data["actor"] == "http://mastodon.example.org/users/admin"
337 assert data["type"] == "Like"
338 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
339 assert data["object"] == activity.data["object"]
342 test "it returns an error for incoming unlikes wihout a like activity" do
344 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
347 File.read!("test/fixtures/mastodon-undo-like.json")
349 |> Map.put("object", activity.data["object"])
351 assert Transmogrifier.handle_incoming(data) == :error
354 test "it works for incoming unlikes with an existing like activity" do
356 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
359 File.read!("test/fixtures/mastodon-like.json")
361 |> Map.put("object", activity.data["object"])
363 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
366 File.read!("test/fixtures/mastodon-undo-like.json")
368 |> Map.put("object", like_data)
369 |> Map.put("actor", like_data["actor"])
371 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
373 assert data["actor"] == "http://mastodon.example.org/users/admin"
374 assert data["type"] == "Undo"
375 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
376 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
379 test "it works for incoming unlikes with an existing like activity and a compact object" do
381 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
384 File.read!("test/fixtures/mastodon-like.json")
386 |> Map.put("object", activity.data["object"])
388 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
391 File.read!("test/fixtures/mastodon-undo-like.json")
393 |> Map.put("object", like_data["id"])
394 |> Map.put("actor", like_data["actor"])
396 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
398 assert data["actor"] == "http://mastodon.example.org/users/admin"
399 assert data["type"] == "Undo"
400 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
401 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
404 test "it works for incoming announces" do
405 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
407 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
409 assert data["actor"] == "http://mastodon.example.org/users/admin"
410 assert data["type"] == "Announce"
413 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
415 assert data["object"] ==
416 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
418 assert Activity.get_create_by_object_ap_id(data["object"])
421 test "it works for incoming announces with an existing activity" do
423 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
426 File.read!("test/fixtures/mastodon-announce.json")
428 |> Map.put("object", activity.data["object"])
430 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
432 assert data["actor"] == "http://mastodon.example.org/users/admin"
433 assert data["type"] == "Announce"
436 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
438 assert data["object"] == activity.data["object"]
440 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
443 test "it works for incoming announces with an inlined activity" do
445 File.read!("test/fixtures/mastodon-announce-private.json")
448 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
450 assert data["actor"] == "http://mastodon.example.org/users/admin"
451 assert data["type"] == "Announce"
454 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
456 object = Object.normalize(data["object"])
458 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
459 assert object.data["content"] == "this is a private toot"
462 test "it rejects incoming announces with an inlined activity from another origin" do
464 File.read!("test/fixtures/bogus-mastodon-announce.json")
467 assert :error = Transmogrifier.handle_incoming(data)
470 test "it does not clobber the addressing on announce activities" do
472 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
475 File.read!("test/fixtures/mastodon-announce.json")
477 |> Map.put("object", Object.normalize(activity).data["id"])
478 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
481 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
483 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
486 test "it ensures that as:Public activities make it to their followers collection" do
490 File.read!("test/fixtures/mastodon-post-activity.json")
492 |> Map.put("actor", user.ap_id)
493 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
498 |> Map.put("attributedTo", user.ap_id)
499 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
501 |> Map.put("id", user.ap_id <> "/activities/12345678")
503 data = Map.put(data, "object", object)
505 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
507 assert data["cc"] == [User.ap_followers(user)]
510 test "it ensures that address fields become lists" do
514 File.read!("test/fixtures/mastodon-post-activity.json")
516 |> Map.put("actor", user.ap_id)
517 |> Map.put("to", nil)
518 |> Map.put("cc", nil)
522 |> Map.put("attributedTo", user.ap_id)
523 |> Map.put("to", nil)
524 |> Map.put("cc", nil)
525 |> Map.put("id", user.ap_id <> "/activities/12345678")
527 data = Map.put(data, "object", object)
529 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
531 assert !is_nil(data["to"])
532 assert !is_nil(data["cc"])
535 test "it strips internal likes" do
537 File.read!("test/fixtures/mastodon-post-activity.json")
542 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
543 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
545 "type" => "OrderedCollection"
548 object = Map.put(data["object"], "likes", likes)
549 data = Map.put(data, "object", object)
551 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
553 refute Map.has_key?(object.data, "likes")
556 test "it works for incoming update activities" do
557 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
559 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
560 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
563 update_data["object"]
564 |> Map.put("actor", data["actor"])
565 |> Map.put("id", data["actor"])
569 |> Map.put("actor", data["actor"])
570 |> Map.put("object", object)
572 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
574 assert data["id"] == update_data["id"]
576 user = User.get_cached_by_ap_id(data["actor"])
577 assert user.name == "gargle"
579 assert user.avatar["url"] == [
582 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
586 assert user.banner["url"] == [
589 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
593 assert user.bio == "<p>Some bio</p>"
596 test "it works with custom profile fields" do
598 "test/fixtures/mastodon-post-activity.json"
601 |> Transmogrifier.handle_incoming()
603 user = User.get_cached_by_ap_id(activity.actor)
605 assert User.fields(user) == [
606 %{"name" => "foo", "value" => "bar"},
607 %{"name" => "foo1", "value" => "bar1"}
610 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
613 update_data["object"]
614 |> Map.put("actor", user.ap_id)
615 |> Map.put("id", user.ap_id)
619 |> Map.put("actor", user.ap_id)
620 |> Map.put("object", object)
622 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
624 user = User.get_cached_by_ap_id(user.ap_id)
626 assert User.fields(user) == [
627 %{"name" => "foo", "value" => "updated"},
628 %{"name" => "foo1", "value" => "updated"}
631 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
634 put_in(update_data, ["object", "attachment"], [
635 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
636 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
637 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
640 {:ok, _} = Transmogrifier.handle_incoming(update_data)
642 user = User.get_cached_by_ap_id(user.ap_id)
644 assert User.fields(user) == [
645 %{"name" => "foo", "value" => "updated"},
646 %{"name" => "foo1", "value" => "updated"}
649 update_data = put_in(update_data, ["object", "attachment"], [])
651 {:ok, _} = Transmogrifier.handle_incoming(update_data)
653 user = User.get_cached_by_ap_id(user.ap_id)
655 assert User.fields(user) == []
658 test "it works for incoming update activities which lock the account" do
659 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
661 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
662 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
665 update_data["object"]
666 |> Map.put("actor", data["actor"])
667 |> Map.put("id", data["actor"])
668 |> Map.put("manuallyApprovesFollowers", true)
672 |> Map.put("actor", data["actor"])
673 |> Map.put("object", object)
675 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
677 user = User.get_cached_by_ap_id(data["actor"])
678 assert user.locked == true
681 test "it works for incoming deletes" do
682 activity = insert(:note_activity)
683 deleting_user = insert(:user)
686 File.read!("test/fixtures/mastodon-delete.json")
691 |> Map.put("id", activity.data["object"])
695 |> Map.put("object", object)
696 |> Map.put("actor", deleting_user.ap_id)
698 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
699 Transmogrifier.handle_incoming(data)
701 assert id == data["id"]
702 refute Activity.get_by_id(activity.id)
703 assert actor == deleting_user.ap_id
706 test "it fails for incoming deletes with spoofed origin" do
707 activity = insert(:note_activity)
710 File.read!("test/fixtures/mastodon-delete.json")
715 |> Map.put("id", activity.data["object"])
719 |> Map.put("object", object)
721 assert capture_log(fn ->
722 :error = Transmogrifier.handle_incoming(data)
724 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
726 assert Activity.get_by_id(activity.id)
729 test "it works for incoming user deletes" do
730 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
733 File.read!("test/fixtures/mastodon-delete-user.json")
736 {:ok, _} = Transmogrifier.handle_incoming(data)
737 ObanHelpers.perform_all()
739 refute User.get_cached_by_ap_id(ap_id)
742 test "it fails for incoming user deletes with spoofed origin" do
743 %{ap_id: ap_id} = insert(:user)
746 File.read!("test/fixtures/mastodon-delete-user.json")
748 |> Map.put("actor", ap_id)
750 assert capture_log(fn ->
751 assert :error == Transmogrifier.handle_incoming(data)
752 end) =~ "Object containment failed"
754 assert User.get_cached_by_ap_id(ap_id)
757 test "it works for incoming unannounces with an existing notice" do
759 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
762 File.read!("test/fixtures/mastodon-announce.json")
764 |> Map.put("object", activity.data["object"])
766 {:ok, %Activity{data: announce_data, local: false}} =
767 Transmogrifier.handle_incoming(announce_data)
770 File.read!("test/fixtures/mastodon-undo-announce.json")
772 |> Map.put("object", announce_data)
773 |> Map.put("actor", announce_data["actor"])
775 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
777 assert data["type"] == "Undo"
778 assert object_data = data["object"]
779 assert object_data["type"] == "Announce"
780 assert object_data["object"] == activity.data["object"]
782 assert object_data["id"] ==
783 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
786 test "it works for incomming unfollows with an existing follow" do
790 File.read!("test/fixtures/mastodon-follow-activity.json")
792 |> Map.put("object", user.ap_id)
794 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
797 File.read!("test/fixtures/mastodon-unfollow-activity.json")
799 |> Map.put("object", follow_data)
801 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
803 assert data["type"] == "Undo"
804 assert data["object"]["type"] == "Follow"
805 assert data["object"]["object"] == user.ap_id
806 assert data["actor"] == "http://mastodon.example.org/users/admin"
808 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
811 test "it works for incoming follows to locked account" do
812 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
813 user = insert(:user, locked: true)
816 File.read!("test/fixtures/mastodon-follow-activity.json")
818 |> Map.put("object", user.ap_id)
820 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
822 assert data["type"] == "Follow"
823 assert data["object"] == user.ap_id
824 assert data["state"] == "pending"
825 assert data["actor"] == "http://mastodon.example.org/users/admin"
827 assert [^pending_follower] = User.get_follow_requests(user)
830 test "it works for incoming blocks" do
834 File.read!("test/fixtures/mastodon-block-activity.json")
836 |> Map.put("object", user.ap_id)
838 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
840 assert data["type"] == "Block"
841 assert data["object"] == user.ap_id
842 assert data["actor"] == "http://mastodon.example.org/users/admin"
844 blocker = User.get_cached_by_ap_id(data["actor"])
846 assert User.blocks?(blocker, user)
849 test "incoming blocks successfully tear down any follow relationship" do
850 blocker = insert(:user)
851 blocked = insert(:user)
854 File.read!("test/fixtures/mastodon-block-activity.json")
856 |> Map.put("object", blocked.ap_id)
857 |> Map.put("actor", blocker.ap_id)
859 {:ok, blocker} = User.follow(blocker, blocked)
860 {:ok, blocked} = User.follow(blocked, blocker)
862 assert User.following?(blocker, blocked)
863 assert User.following?(blocked, blocker)
865 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
867 assert data["type"] == "Block"
868 assert data["object"] == blocked.ap_id
869 assert data["actor"] == blocker.ap_id
871 blocker = User.get_cached_by_ap_id(data["actor"])
872 blocked = User.get_cached_by_ap_id(data["object"])
874 assert User.blocks?(blocker, blocked)
876 refute User.following?(blocker, blocked)
877 refute User.following?(blocked, blocker)
880 test "it works for incoming unblocks with an existing block" do
884 File.read!("test/fixtures/mastodon-block-activity.json")
886 |> Map.put("object", user.ap_id)
888 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
891 File.read!("test/fixtures/mastodon-unblock-activity.json")
893 |> Map.put("object", block_data)
895 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
896 assert data["type"] == "Undo"
897 assert data["object"]["type"] == "Block"
898 assert data["object"]["object"] == user.ap_id
899 assert data["actor"] == "http://mastodon.example.org/users/admin"
901 blocker = User.get_cached_by_ap_id(data["actor"])
903 refute User.blocks?(blocker, user)
906 test "it works for incoming accepts which were pre-accepted" do
907 follower = insert(:user)
908 followed = insert(:user)
910 {:ok, follower} = User.follow(follower, followed)
911 assert User.following?(follower, followed) == true
913 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
916 File.read!("test/fixtures/mastodon-accept-activity.json")
918 |> Map.put("actor", followed.ap_id)
921 accept_data["object"]
922 |> Map.put("actor", follower.ap_id)
923 |> Map.put("id", follow_activity.data["id"])
925 accept_data = Map.put(accept_data, "object", object)
927 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
928 refute activity.local
930 assert activity.data["object"] == follow_activity.data["id"]
932 assert activity.data["id"] == accept_data["id"]
934 follower = User.get_cached_by_id(follower.id)
936 assert User.following?(follower, followed) == true
939 test "it works for incoming accepts which were orphaned" do
940 follower = insert(:user)
941 followed = insert(:user, locked: true)
943 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
946 File.read!("test/fixtures/mastodon-accept-activity.json")
948 |> Map.put("actor", followed.ap_id)
951 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
953 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
954 assert activity.data["object"] == follow_activity.data["id"]
956 follower = User.get_cached_by_id(follower.id)
958 assert User.following?(follower, followed) == true
961 test "it works for incoming accepts which are referenced by IRI only" do
962 follower = insert(:user)
963 followed = insert(:user, locked: true)
965 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
968 File.read!("test/fixtures/mastodon-accept-activity.json")
970 |> Map.put("actor", followed.ap_id)
971 |> Map.put("object", follow_activity.data["id"])
973 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
974 assert activity.data["object"] == follow_activity.data["id"]
976 follower = User.get_cached_by_id(follower.id)
978 assert User.following?(follower, followed) == true
981 test "it fails for incoming accepts which cannot be correlated" do
982 follower = insert(:user)
983 followed = insert(:user, locked: true)
986 File.read!("test/fixtures/mastodon-accept-activity.json")
988 |> Map.put("actor", followed.ap_id)
991 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
993 :error = Transmogrifier.handle_incoming(accept_data)
995 follower = User.get_cached_by_id(follower.id)
997 refute User.following?(follower, followed) == true
1000 test "it fails for incoming rejects which cannot be correlated" do
1001 follower = insert(:user)
1002 followed = insert(:user, locked: true)
1005 File.read!("test/fixtures/mastodon-reject-activity.json")
1007 |> Map.put("actor", followed.ap_id)
1010 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1012 :error = Transmogrifier.handle_incoming(accept_data)
1014 follower = User.get_cached_by_id(follower.id)
1016 refute User.following?(follower, followed) == true
1019 test "it works for incoming rejects which are orphaned" do
1020 follower = insert(:user)
1021 followed = insert(:user, locked: true)
1023 {:ok, follower} = User.follow(follower, followed)
1024 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1026 assert User.following?(follower, followed) == true
1029 File.read!("test/fixtures/mastodon-reject-activity.json")
1031 |> Map.put("actor", followed.ap_id)
1034 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1036 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1037 refute activity.local
1038 assert activity.data["id"] == reject_data["id"]
1040 follower = User.get_cached_by_id(follower.id)
1042 assert User.following?(follower, followed) == false
1045 test "it works for incoming rejects which are referenced by IRI only" do
1046 follower = insert(:user)
1047 followed = insert(:user, locked: true)
1049 {:ok, follower} = User.follow(follower, followed)
1050 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1052 assert User.following?(follower, followed) == true
1055 File.read!("test/fixtures/mastodon-reject-activity.json")
1057 |> Map.put("actor", followed.ap_id)
1058 |> Map.put("object", follow_activity.data["id"])
1060 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1062 follower = User.get_cached_by_id(follower.id)
1064 assert User.following?(follower, followed) == false
1067 test "it rejects activities without a valid ID" do
1068 user = insert(:user)
1071 File.read!("test/fixtures/mastodon-follow-activity.json")
1073 |> Map.put("object", user.ap_id)
1074 |> Map.put("id", "")
1076 :error = Transmogrifier.handle_incoming(data)
1079 test "it remaps video URLs as attachments if necessary" do
1081 Fetcher.fetch_object_from_id(
1082 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1087 "mediaType" => "video/mp4",
1089 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1090 "mimeType" => "video/mp4",
1091 "size" => 5_015_880,
1095 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1096 "mediaType" => "video/mp4",
1103 assert object.data["url"] ==
1104 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1106 assert object.data["attachment"] == [attachment]
1109 test "it accepts Flag activities" do
1110 user = insert(:user)
1111 other_user = insert(:user)
1113 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1114 object = Object.normalize(activity)
1118 "id" => activity.data["id"],
1119 "content" => "test post",
1120 "published" => object.data["published"],
1121 "actor" => AccountView.render("show.json", %{user: user})
1125 "@context" => "https://www.w3.org/ns/activitystreams",
1126 "cc" => [user.ap_id],
1127 "object" => [user.ap_id, activity.data["id"]],
1129 "content" => "blocked AND reported!!!",
1130 "actor" => other_user.ap_id
1133 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1135 assert activity.data["object"] == [user.ap_id, note_obj]
1136 assert activity.data["content"] == "blocked AND reported!!!"
1137 assert activity.data["actor"] == other_user.ap_id
1138 assert activity.data["cc"] == [user.ap_id]
1141 test "it correctly processes messages with non-array to field" do
1142 user = insert(:user)
1145 "@context" => "https://www.w3.org/ns/activitystreams",
1146 "to" => "https://www.w3.org/ns/activitystreams#Public",
1149 "content" => "blah blah blah",
1151 "attributedTo" => user.ap_id,
1154 "actor" => user.ap_id
1157 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1159 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1162 test "it correctly processes messages with non-array cc field" do
1163 user = insert(:user)
1166 "@context" => "https://www.w3.org/ns/activitystreams",
1167 "to" => user.follower_address,
1168 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1171 "content" => "blah blah blah",
1173 "attributedTo" => user.ap_id,
1176 "actor" => user.ap_id
1179 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1181 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1182 assert [user.follower_address] == activity.data["to"]
1186 describe "prepare outgoing" do
1187 test "it inlines private announced objects" do
1188 user = insert(:user)
1190 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1192 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1194 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1196 assert modified["object"]["content"] == "hey"
1197 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1200 test "it turns mentions into tags" do
1201 user = insert(:user)
1202 other_user = insert(:user)
1205 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1207 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1208 object = modified["object"]
1210 expected_mention = %{
1211 "href" => other_user.ap_id,
1212 "name" => "@#{other_user.nickname}",
1217 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1218 "type" => "Hashtag",
1222 assert Enum.member?(object["tag"], expected_tag)
1223 assert Enum.member?(object["tag"], expected_mention)
1226 test "it adds the sensitive property" do
1227 user = insert(:user)
1229 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1230 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1232 assert modified["object"]["sensitive"]
1235 test "it adds the json-ld context and the conversation property" do
1236 user = insert(:user)
1238 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1239 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1241 assert modified["@context"] ==
1242 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1244 assert modified["object"]["conversation"] == modified["context"]
1247 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1248 user = insert(:user)
1250 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1251 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1253 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1256 test "it strips internal hashtag data" do
1257 user = insert(:user)
1259 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1262 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1263 "type" => "Hashtag",
1267 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1269 assert modified["object"]["tag"] == [expected_tag]
1272 test "it strips internal fields" do
1273 user = insert(:user)
1275 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1277 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1279 assert length(modified["object"]["tag"]) == 2
1281 assert is_nil(modified["object"]["emoji"])
1282 assert is_nil(modified["object"]["like_count"])
1283 assert is_nil(modified["object"]["announcements"])
1284 assert is_nil(modified["object"]["announcement_count"])
1285 assert is_nil(modified["object"]["context_id"])
1288 test "it strips internal fields of article" do
1289 activity = insert(:article_activity)
1291 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1293 assert length(modified["object"]["tag"]) == 2
1295 assert is_nil(modified["object"]["emoji"])
1296 assert is_nil(modified["object"]["like_count"])
1297 assert is_nil(modified["object"]["announcements"])
1298 assert is_nil(modified["object"]["announcement_count"])
1299 assert is_nil(modified["object"]["context_id"])
1300 assert is_nil(modified["object"]["likes"])
1303 test "the directMessage flag is present" do
1304 user = insert(:user)
1305 other_user = insert(:user)
1307 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1309 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1311 assert modified["directMessage"] == false
1314 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1316 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1318 assert modified["directMessage"] == false
1321 CommonAPI.post(user, %{
1322 "status" => "@#{other_user.nickname} :moominmamma:",
1323 "visibility" => "direct"
1326 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1328 assert modified["directMessage"] == true
1331 test "it strips BCC field" do
1332 user = insert(:user)
1333 {:ok, list} = Pleroma.List.create("foo", user)
1336 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1338 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1340 assert is_nil(modified["bcc"])
1343 test "it can handle Listen activities" do
1344 listen_activity = insert(:listen)
1346 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1348 assert modified["type"] == "Listen"
1350 user = insert(:user)
1352 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1354 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1358 describe "user upgrade" do
1359 test "it upgrades a user to activitypub" do
1362 nickname: "rye@niu.moe",
1364 ap_id: "https://niu.moe/users/rye",
1365 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1368 user_two = insert(:user)
1369 Pleroma.FollowingRelationship.follow(user_two, user, "accept")
1371 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1372 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1373 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1375 user = User.get_cached_by_id(user.id)
1376 assert user.note_count == 1
1378 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1379 ObanHelpers.perform_all()
1381 assert user.ap_enabled
1382 assert user.note_count == 1
1383 assert user.follower_address == "https://niu.moe/users/rye/followers"
1384 assert user.following_address == "https://niu.moe/users/rye/following"
1386 user = User.get_cached_by_id(user.id)
1387 assert user.note_count == 1
1389 activity = Activity.get_by_id(activity.id)
1390 assert user.follower_address in activity.recipients
1396 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1405 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1410 refute "..." in activity.recipients
1412 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1413 refute user.follower_address in unrelated_activity.recipients
1415 user_two = User.get_cached_by_id(user_two.id)
1416 assert User.following?(user_two, user)
1417 refute "..." in User.following(user_two)
1421 describe "actor rewriting" do
1422 test "it fixes the actor URL property to be a proper URI" do
1424 "url" => %{"href" => "http://example.com"}
1427 rewritten = Transmogrifier.maybe_fix_user_object(data)
1428 assert rewritten["url"] == "http://example.com"
1432 describe "actor origin containment" do
1433 test "it rejects activities which reference objects with bogus origins" do
1435 "@context" => "https://www.w3.org/ns/activitystreams",
1436 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1437 "actor" => "http://mastodon.example.org/users/admin",
1438 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1439 "object" => "https://info.pleroma.site/activity.json",
1440 "type" => "Announce"
1443 assert capture_log(fn ->
1444 :error = Transmogrifier.handle_incoming(data)
1445 end) =~ "Object containment failed"
1448 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1450 "@context" => "https://www.w3.org/ns/activitystreams",
1451 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1452 "actor" => "http://mastodon.example.org/users/admin",
1453 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1454 "object" => "https://info.pleroma.site/activity2.json",
1455 "type" => "Announce"
1458 assert capture_log(fn ->
1459 :error = Transmogrifier.handle_incoming(data)
1460 end) =~ "Object containment failed"
1463 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1465 "@context" => "https://www.w3.org/ns/activitystreams",
1466 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1467 "actor" => "http://mastodon.example.org/users/admin",
1468 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1469 "object" => "https://info.pleroma.site/activity3.json",
1470 "type" => "Announce"
1473 assert capture_log(fn ->
1474 :error = Transmogrifier.handle_incoming(data)
1475 end) =~ "Object containment failed"
1479 describe "reserialization" do
1480 test "successfully reserializes a message with inReplyTo == nil" do
1481 user = insert(:user)
1484 "@context" => "https://www.w3.org/ns/activitystreams",
1485 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1489 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1494 "attributedTo" => user.ap_id
1496 "actor" => user.ap_id
1499 {:ok, activity} = Transmogrifier.handle_incoming(message)
1501 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1504 test "successfully reserializes a message with AS2 objects in IR" do
1505 user = insert(:user)
1508 "@context" => "https://www.w3.org/ns/activitystreams",
1509 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1513 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1518 "attributedTo" => user.ap_id,
1520 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1521 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1524 "actor" => user.ap_id
1527 {:ok, activity} = Transmogrifier.handle_incoming(message)
1529 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1533 test "Rewrites Answers to Notes" do
1534 user = insert(:user)
1536 {:ok, poll_activity} =
1537 CommonAPI.post(user, %{
1538 "status" => "suya...",
1539 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1542 poll_object = Object.normalize(poll_activity)
1543 # TODO: Replace with CommonAPI vote creation when implemented
1545 File.read!("test/fixtures/mastodon-vote.json")
1547 |> Kernel.put_in(["to"], user.ap_id)
1548 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1549 |> Kernel.put_in(["object", "to"], user.ap_id)
1551 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1552 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1554 assert data["object"]["type"] == "Note"
1557 describe "fix_explicit_addressing" do
1559 user = insert(:user)
1563 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1564 explicitly_mentioned_actors = [
1565 "https://pleroma.gold/users/user1",
1566 "https://pleroma.gold/user2"
1570 "actor" => user.ap_id,
1571 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1574 Enum.map(explicitly_mentioned_actors, fn href ->
1575 %{"type" => "Mention", "href" => href}
1579 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1580 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1581 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1582 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1585 test "does not move actor's follower collection to cc", %{user: user} do
1587 "actor" => user.ap_id,
1588 "to" => [user.follower_address],
1592 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1593 assert user.follower_address in fixed_object["to"]
1594 refute user.follower_address in fixed_object["cc"]
1597 test "removes recipient's follower collection from cc", %{user: user} do
1598 recipient = insert(:user)
1601 "actor" => user.ap_id,
1602 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1603 "cc" => [user.follower_address, recipient.follower_address]
1606 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1608 assert user.follower_address in fixed_object["cc"]
1609 refute recipient.follower_address in fixed_object["cc"]
1610 refute recipient.follower_address in fixed_object["to"]
1614 describe "fix_summary/1" do
1615 test "returns fixed object" do
1616 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1617 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1618 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1622 describe "fix_in_reply_to/2" do
1623 clear_config([:instance, :federation_incoming_replies_max_depth])
1626 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1630 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1631 assert Transmogrifier.fix_in_reply_to(data) == data
1634 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1635 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1638 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1640 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1641 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1642 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1645 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1647 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1648 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1649 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1652 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1654 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1655 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1656 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1658 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1659 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1660 assert modified_object["inReplyTo"] == []
1661 assert modified_object["inReplyToAtomUri"] == ""
1664 test "returns modified object when allowed incoming reply", %{data: data} do
1669 "https://shitposter.club/notice/2827873"
1672 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1673 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1675 assert modified_object["inReplyTo"] ==
1676 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1678 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1680 assert modified_object["conversation"] ==
1681 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1683 assert modified_object["context"] ==
1684 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1688 describe "fix_url/1" do
1689 test "fixes data for object when url is map" do
1693 "mimeType" => "video/mp4",
1694 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1698 assert Transmogrifier.fix_url(object) == %{
1699 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1703 test "fixes data for video object" do
1709 "mimeType" => "video/mp4",
1710 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1714 "mimeType" => "video/mp4",
1715 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1719 "mimeType" => "text/html",
1720 "href" => "https://peertube.-2d4c2d1630e3"
1724 "mimeType" => "text/html",
1725 "href" => "https://peertube.-2d4c2d16377-42"
1730 assert Transmogrifier.fix_url(object) == %{
1733 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1734 "mimeType" => "video/mp4",
1739 "url" => "https://peertube.-2d4c2d1630e3"
1743 test "fixes url for not Video object" do
1749 "mimeType" => "text/html",
1750 "href" => "https://peertube.-2d4c2d1630e3"
1754 "mimeType" => "text/html",
1755 "href" => "https://peertube.-2d4c2d16377-42"
1760 assert Transmogrifier.fix_url(object) == %{
1762 "url" => "https://peertube.-2d4c2d1630e3"
1765 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1771 test "retunrs not modified object" do
1772 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1776 describe "get_obj_helper/2" do
1777 test "returns nil when cannot normalize object" do
1778 assert capture_log(fn ->
1779 refute Transmogrifier.get_obj_helper("test-obj-id")
1780 end) =~ "Unsupported URI scheme"
1783 test "returns {:ok, %Object{}} for success case" do
1784 assert {:ok, %Object{}} =
1785 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1789 describe "fix_attachments/1" do
1790 test "returns not modified object" do
1791 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1792 assert Transmogrifier.fix_attachments(data) == data
1795 test "returns modified object when attachment is map" do
1796 assert Transmogrifier.fix_attachments(%{
1798 "mediaType" => "video/mp4",
1799 "url" => "https://peertube.moe/stat-480.mp4"
1804 "mediaType" => "video/mp4",
1807 "href" => "https://peertube.moe/stat-480.mp4",
1808 "mediaType" => "video/mp4",
1817 test "returns modified object when attachment is list" do
1818 assert Transmogrifier.fix_attachments(%{
1820 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1821 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1826 "mediaType" => "video/mp4",
1829 "href" => "https://pe.er/stat-480.mp4",
1830 "mediaType" => "video/mp4",
1836 "href" => "https://pe.er/stat-480.mp4",
1837 "mediaType" => "video/mp4",
1838 "mimeType" => "video/mp4",
1841 "href" => "https://pe.er/stat-480.mp4",
1842 "mediaType" => "video/mp4",
1852 describe "fix_emoji/1" do
1853 test "returns not modified object when object not contains tags" do
1854 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1855 assert Transmogrifier.fix_emoji(data) == data
1858 test "returns object with emoji when object contains list tags" do
1859 assert Transmogrifier.fix_emoji(%{
1861 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1862 %{"type" => "Hashtag"}
1865 "emoji" => %{"bib" => "/test"},
1867 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1868 %{"type" => "Hashtag"}
1873 test "returns object with emoji when object contains map tag" do
1874 assert Transmogrifier.fix_emoji(%{
1875 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1877 "emoji" => %{"bib" => "/test"},
1878 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}