1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Object.Fetcher
12 alias Pleroma.Tests.ObanHelpers
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 setup do: clear_config([:instance, :max_remote_account_fields])
30 describe "handle_incoming" do
31 test "it works for incoming notices with tag not being an array (kroeg)" do
32 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
34 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
35 object = Object.normalize(data["object"])
37 assert object.data["emoji"] == %{
38 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
41 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
43 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
44 object = Object.normalize(data["object"])
46 assert "test" in object.data["tag"]
49 test "it works for incoming notices with url not being a string (prismo)" do
50 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
52 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
53 object = Object.normalize(data["object"])
55 assert object.data["url"] == "https://prismo.news/posts/83"
58 test "it cleans up incoming notices which are not really DMs" do
60 other_user = insert(:user)
62 to = [user.ap_id, other_user.ap_id]
65 File.read!("test/fixtures/mastodon-post-activity.json")
75 data = Map.put(data, "object", object)
77 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
79 assert data["to"] == []
80 assert data["cc"] == to
82 object_data = Object.normalize(activity).data
84 assert object_data["to"] == []
85 assert object_data["cc"] == to
88 test "it ignores an incoming notice if we already have it" do
89 activity = insert(:note_activity)
92 File.read!("test/fixtures/mastodon-post-activity.json")
94 |> Map.put("object", Object.normalize(activity).data)
96 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
98 assert activity == returned_activity
101 @tag capture_log: true
102 test "it fetches reply-to activities if we don't have them" do
104 File.read!("test/fixtures/mastodon-post-activity.json")
109 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
111 data = Map.put(data, "object", object)
112 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
113 returned_object = Object.normalize(returned_activity, false)
116 Activity.get_create_by_object_ap_id(
117 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
120 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
123 test "it does not fetch reply-to activities beyond max replies depth limit" do
125 File.read!("test/fixtures/mastodon-post-activity.json")
130 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
132 data = Map.put(data, "object", object)
134 with_mock Pleroma.Web.Federator,
135 allowed_thread_distance?: fn _ -> false end do
136 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
138 returned_object = Object.normalize(returned_activity, false)
140 refute Activity.get_create_by_object_ap_id(
141 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
144 assert returned_object.data["inReplyToAtomUri"] ==
145 "https://shitposter.club/notice/2827873"
149 test "it does not crash if the object in inReplyTo can't be fetched" do
151 File.read!("test/fixtures/mastodon-post-activity.json")
156 |> Map.put("inReplyTo", "https://404.site/whatever")
160 |> Map.put("object", object)
162 assert capture_log(fn ->
163 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
164 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
167 test "it works for incoming notices" do
168 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
170 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
173 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
175 assert data["context"] ==
176 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
178 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
180 assert data["cc"] == [
181 "http://mastodon.example.org/users/admin/followers",
182 "http://localtesting.pleroma.lol/users/lain"
185 assert data["actor"] == "http://mastodon.example.org/users/admin"
187 object_data = Object.normalize(data["object"]).data
189 assert object_data["id"] ==
190 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
192 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
194 assert object_data["cc"] == [
195 "http://mastodon.example.org/users/admin/followers",
196 "http://localtesting.pleroma.lol/users/lain"
199 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
200 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
202 assert object_data["context"] ==
203 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
205 assert object_data["sensitive"] == true
207 user = User.get_cached_by_ap_id(object_data["actor"])
209 assert user.note_count == 1
212 test "it works for incoming notices with hashtags" do
213 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
215 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
216 object = Object.normalize(data["object"])
218 assert Enum.at(object.data["tag"], 2) == "moo"
221 test "it works for incoming questions" do
222 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
224 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
226 object = Object.normalize(activity)
228 assert Enum.all?(object.data["oneOf"], fn choice ->
231 "Everyone knows that!",
232 "25 char limit is dumb",
233 "I can't even fit a funny"
238 test "it works for incoming listens" do
240 "@context" => "https://www.w3.org/ns/activitystreams",
241 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
244 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
245 "actor" => "http://mastodon.example.org/users/admin",
248 "id" => "http://mastodon.example.org/users/admin/listens/1234",
249 "attributedTo" => "http://mastodon.example.org/users/admin",
250 "title" => "lain radio episode 1",
252 "album" => "lain radio",
257 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
259 object = Object.normalize(activity)
261 assert object.data["title"] == "lain radio episode 1"
262 assert object.data["artist"] == "lain"
263 assert object.data["album"] == "lain radio"
264 assert object.data["length"] == 180_000
267 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
271 CommonAPI.post(user, %{
273 poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
276 object = Object.normalize(activity)
279 File.read!("test/fixtures/mastodon-vote.json")
281 |> Kernel.put_in(["to"], user.ap_id)
282 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
283 |> Kernel.put_in(["object", "to"], user.ap_id)
285 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
286 answer_object = Object.normalize(activity)
287 assert answer_object.data["type"] == "Answer"
288 object = Object.get_by_ap_id(object.data["id"])
291 object.data["oneOf"],
293 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
299 test "it works for incoming notices with contentMap" do
301 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
303 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
304 object = Object.normalize(data["object"])
306 assert object.data["content"] ==
307 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
310 test "it works for incoming notices with to/cc not being an array (kroeg)" do
311 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
313 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
314 object = Object.normalize(data["object"])
316 assert object.data["content"] ==
317 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
320 test "it ensures that as:Public activities make it to their followers collection" do
324 File.read!("test/fixtures/mastodon-post-activity.json")
326 |> Map.put("actor", user.ap_id)
327 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
332 |> Map.put("attributedTo", user.ap_id)
333 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
335 |> Map.put("id", user.ap_id <> "/activities/12345678")
337 data = Map.put(data, "object", object)
339 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
341 assert data["cc"] == [User.ap_followers(user)]
344 test "it ensures that address fields become lists" do
348 File.read!("test/fixtures/mastodon-post-activity.json")
350 |> Map.put("actor", user.ap_id)
351 |> Map.put("to", nil)
352 |> Map.put("cc", nil)
356 |> Map.put("attributedTo", user.ap_id)
357 |> Map.put("to", nil)
358 |> Map.put("cc", nil)
359 |> Map.put("id", user.ap_id <> "/activities/12345678")
361 data = Map.put(data, "object", object)
363 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
365 assert !is_nil(data["to"])
366 assert !is_nil(data["cc"])
369 test "it strips internal likes" do
371 File.read!("test/fixtures/mastodon-post-activity.json")
376 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
377 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
379 "type" => "OrderedCollection"
382 object = Map.put(data["object"], "likes", likes)
383 data = Map.put(data, "object", object)
385 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
387 refute Map.has_key?(object.data, "likes")
390 test "it strips internal reactions" do
392 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
393 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
395 %{object: object} = Activity.get_by_id_with_object(activity.id)
396 assert Map.has_key?(object.data, "reactions")
397 assert Map.has_key?(object.data, "reaction_count")
399 object_data = Transmogrifier.strip_internal_fields(object.data)
400 refute Map.has_key?(object_data, "reactions")
401 refute Map.has_key?(object_data, "reaction_count")
404 test "it works for incoming update activities" do
405 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
407 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
408 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
411 update_data["object"]
412 |> Map.put("actor", data["actor"])
413 |> Map.put("id", data["actor"])
417 |> Map.put("actor", data["actor"])
418 |> Map.put("object", object)
420 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
422 assert data["id"] == update_data["id"]
424 user = User.get_cached_by_ap_id(data["actor"])
425 assert user.name == "gargle"
427 assert user.avatar["url"] == [
430 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
434 assert user.banner["url"] == [
437 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
441 assert user.bio == "<p>Some bio</p>"
444 test "it works with alsoKnownAs" do
445 {:ok, %Activity{data: %{"actor" => actor}}} =
446 "test/fixtures/mastodon-post-activity.json"
449 |> Transmogrifier.handle_incoming()
451 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
454 "test/fixtures/mastodon-update.json"
457 |> Map.put("actor", actor)
458 |> Map.update!("object", fn object ->
460 |> Map.put("actor", actor)
461 |> Map.put("id", actor)
462 |> Map.put("alsoKnownAs", [
463 "http://mastodon.example.org/users/foo",
464 "http://example.org/users/bar"
467 |> Transmogrifier.handle_incoming()
469 assert User.get_cached_by_ap_id(actor).also_known_as == [
470 "http://mastodon.example.org/users/foo",
471 "http://example.org/users/bar"
475 test "it works with custom profile fields" do
477 "test/fixtures/mastodon-post-activity.json"
480 |> Transmogrifier.handle_incoming()
482 user = User.get_cached_by_ap_id(activity.actor)
484 assert user.fields == [
485 %{"name" => "foo", "value" => "bar"},
486 %{"name" => "foo1", "value" => "bar1"}
489 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
492 update_data["object"]
493 |> Map.put("actor", user.ap_id)
494 |> Map.put("id", user.ap_id)
498 |> Map.put("actor", user.ap_id)
499 |> Map.put("object", object)
501 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
503 user = User.get_cached_by_ap_id(user.ap_id)
505 assert user.fields == [
506 %{"name" => "foo", "value" => "updated"},
507 %{"name" => "foo1", "value" => "updated"}
510 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
513 put_in(update_data, ["object", "attachment"], [
514 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
515 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
516 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
519 {:ok, _} = Transmogrifier.handle_incoming(update_data)
521 user = User.get_cached_by_ap_id(user.ap_id)
523 assert user.fields == [
524 %{"name" => "foo", "value" => "updated"},
525 %{"name" => "foo1", "value" => "updated"}
528 update_data = put_in(update_data, ["object", "attachment"], [])
530 {:ok, _} = Transmogrifier.handle_incoming(update_data)
532 user = User.get_cached_by_ap_id(user.ap_id)
534 assert user.fields == []
537 test "it works for incoming update activities which lock the account" do
538 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
540 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
541 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
544 update_data["object"]
545 |> Map.put("actor", data["actor"])
546 |> Map.put("id", data["actor"])
547 |> Map.put("manuallyApprovesFollowers", true)
551 |> Map.put("actor", data["actor"])
552 |> Map.put("object", object)
554 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
556 user = User.get_cached_by_ap_id(data["actor"])
557 assert user.locked == true
560 test "it works for incomming unfollows with an existing follow" do
564 File.read!("test/fixtures/mastodon-follow-activity.json")
566 |> Map.put("object", user.ap_id)
568 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
571 File.read!("test/fixtures/mastodon-unfollow-activity.json")
573 |> Map.put("object", follow_data)
575 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
577 assert data["type"] == "Undo"
578 assert data["object"]["type"] == "Follow"
579 assert data["object"]["object"] == user.ap_id
580 assert data["actor"] == "http://mastodon.example.org/users/admin"
582 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
585 test "it works for incoming follows to locked account" do
586 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
587 user = insert(:user, locked: true)
590 File.read!("test/fixtures/mastodon-follow-activity.json")
592 |> Map.put("object", user.ap_id)
594 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
596 assert data["type"] == "Follow"
597 assert data["object"] == user.ap_id
598 assert data["state"] == "pending"
599 assert data["actor"] == "http://mastodon.example.org/users/admin"
601 assert [^pending_follower] = User.get_follow_requests(user)
604 test "it works for incoming blocks" do
608 File.read!("test/fixtures/mastodon-block-activity.json")
610 |> Map.put("object", user.ap_id)
612 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
614 assert data["type"] == "Block"
615 assert data["object"] == user.ap_id
616 assert data["actor"] == "http://mastodon.example.org/users/admin"
618 blocker = User.get_cached_by_ap_id(data["actor"])
620 assert User.blocks?(blocker, user)
623 test "incoming blocks successfully tear down any follow relationship" do
624 blocker = insert(:user)
625 blocked = insert(:user)
628 File.read!("test/fixtures/mastodon-block-activity.json")
630 |> Map.put("object", blocked.ap_id)
631 |> Map.put("actor", blocker.ap_id)
633 {:ok, blocker} = User.follow(blocker, blocked)
634 {:ok, blocked} = User.follow(blocked, blocker)
636 assert User.following?(blocker, blocked)
637 assert User.following?(blocked, blocker)
639 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
641 assert data["type"] == "Block"
642 assert data["object"] == blocked.ap_id
643 assert data["actor"] == blocker.ap_id
645 blocker = User.get_cached_by_ap_id(data["actor"])
646 blocked = User.get_cached_by_ap_id(data["object"])
648 assert User.blocks?(blocker, blocked)
650 refute User.following?(blocker, blocked)
651 refute User.following?(blocked, blocker)
654 test "it works for incoming accepts which were pre-accepted" do
655 follower = insert(:user)
656 followed = insert(:user)
658 {:ok, follower} = User.follow(follower, followed)
659 assert User.following?(follower, followed) == true
661 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
664 File.read!("test/fixtures/mastodon-accept-activity.json")
666 |> Map.put("actor", followed.ap_id)
669 accept_data["object"]
670 |> Map.put("actor", follower.ap_id)
671 |> Map.put("id", follow_activity.data["id"])
673 accept_data = Map.put(accept_data, "object", object)
675 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
676 refute activity.local
678 assert activity.data["object"] == follow_activity.data["id"]
680 assert activity.data["id"] == accept_data["id"]
682 follower = User.get_cached_by_id(follower.id)
684 assert User.following?(follower, followed) == true
687 test "it works for incoming accepts which were orphaned" do
688 follower = insert(:user)
689 followed = insert(:user, locked: true)
691 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
694 File.read!("test/fixtures/mastodon-accept-activity.json")
696 |> Map.put("actor", followed.ap_id)
699 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
701 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
702 assert activity.data["object"] == follow_activity.data["id"]
704 follower = User.get_cached_by_id(follower.id)
706 assert User.following?(follower, followed) == true
709 test "it works for incoming accepts which are referenced by IRI only" do
710 follower = insert(:user)
711 followed = insert(:user, locked: true)
713 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
716 File.read!("test/fixtures/mastodon-accept-activity.json")
718 |> Map.put("actor", followed.ap_id)
719 |> Map.put("object", follow_activity.data["id"])
721 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
722 assert activity.data["object"] == follow_activity.data["id"]
724 follower = User.get_cached_by_id(follower.id)
726 assert User.following?(follower, followed) == true
728 follower = User.get_by_id(follower.id)
729 assert follower.following_count == 1
731 followed = User.get_by_id(followed.id)
732 assert followed.follower_count == 1
735 test "it fails for incoming accepts which cannot be correlated" do
736 follower = insert(:user)
737 followed = insert(:user, locked: true)
740 File.read!("test/fixtures/mastodon-accept-activity.json")
742 |> Map.put("actor", followed.ap_id)
745 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
747 :error = Transmogrifier.handle_incoming(accept_data)
749 follower = User.get_cached_by_id(follower.id)
751 refute User.following?(follower, followed) == true
754 test "it fails for incoming rejects which cannot be correlated" do
755 follower = insert(:user)
756 followed = insert(:user, locked: true)
759 File.read!("test/fixtures/mastodon-reject-activity.json")
761 |> Map.put("actor", followed.ap_id)
764 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
766 :error = Transmogrifier.handle_incoming(accept_data)
768 follower = User.get_cached_by_id(follower.id)
770 refute User.following?(follower, followed) == true
773 test "it works for incoming rejects which are orphaned" do
774 follower = insert(:user)
775 followed = insert(:user, locked: true)
777 {:ok, follower} = User.follow(follower, followed)
778 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
780 assert User.following?(follower, followed) == true
783 File.read!("test/fixtures/mastodon-reject-activity.json")
785 |> Map.put("actor", followed.ap_id)
788 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
790 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
791 refute activity.local
792 assert activity.data["id"] == reject_data["id"]
794 follower = User.get_cached_by_id(follower.id)
796 assert User.following?(follower, followed) == false
799 test "it works for incoming rejects which are referenced by IRI only" do
800 follower = insert(:user)
801 followed = insert(:user, locked: true)
803 {:ok, follower} = User.follow(follower, followed)
804 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
806 assert User.following?(follower, followed) == true
809 File.read!("test/fixtures/mastodon-reject-activity.json")
811 |> Map.put("actor", followed.ap_id)
812 |> Map.put("object", follow_activity.data["id"])
814 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
816 follower = User.get_cached_by_id(follower.id)
818 assert User.following?(follower, followed) == false
821 test "it rejects activities without a valid ID" do
825 File.read!("test/fixtures/mastodon-follow-activity.json")
827 |> Map.put("object", user.ap_id)
830 :error = Transmogrifier.handle_incoming(data)
833 test "skip converting the content when it is nil" do
834 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
836 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
839 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
841 assert result["content"] == nil
844 test "it converts content of object to html" do
845 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
847 {:ok, %{"content" => content_markdown}} =
848 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
850 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
851 Fetcher.fetch_object_from_id(object_id)
853 assert content_markdown ==
854 "Support this and our other Michigan!/usr/group videos and meetings. Learn more at http://mug.org/membership\n\nTwenty Years in Jail: FreeBSD's Jails, Then and Now\n\nJails started as a limited virtualization system, but over the last two years they've..."
857 "<p>Support this and our other Michigan!/usr/group videos and meetings. Learn more at <a href=\"http://mug.org/membership\">http://mug.org/membership</a></p><p>Twenty Years in Jail: FreeBSD’s Jails, Then and Now</p><p>Jails started as a limited virtualization system, but over the last two years they’ve…</p>"
859 assert object.data["mediaType"] == "text/html"
862 test "it remaps video URLs as attachments if necessary" do
864 Fetcher.fetch_object_from_id(
865 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
870 "mediaType" => "video/mp4",
874 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
875 "mediaType" => "video/mp4"
880 assert object.data["url"] ==
881 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
883 assert object.data["attachment"] == [attachment]
886 test "it accepts Flag activities" do
888 other_user = insert(:user)
890 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
891 object = Object.normalize(activity)
895 "id" => activity.data["id"],
896 "content" => "test post",
897 "published" => object.data["published"],
898 "actor" => AccountView.render("show.json", %{user: user})
902 "@context" => "https://www.w3.org/ns/activitystreams",
903 "cc" => [user.ap_id],
904 "object" => [user.ap_id, activity.data["id"]],
906 "content" => "blocked AND reported!!!",
907 "actor" => other_user.ap_id
910 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
912 assert activity.data["object"] == [user.ap_id, note_obj]
913 assert activity.data["content"] == "blocked AND reported!!!"
914 assert activity.data["actor"] == other_user.ap_id
915 assert activity.data["cc"] == [user.ap_id]
918 test "it correctly processes messages with non-array to field" do
922 "@context" => "https://www.w3.org/ns/activitystreams",
923 "to" => "https://www.w3.org/ns/activitystreams#Public",
926 "content" => "blah blah blah",
928 "attributedTo" => user.ap_id,
931 "actor" => user.ap_id
934 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
936 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
939 test "it correctly processes messages with non-array cc field" do
943 "@context" => "https://www.w3.org/ns/activitystreams",
944 "to" => user.follower_address,
945 "cc" => "https://www.w3.org/ns/activitystreams#Public",
948 "content" => "blah blah blah",
950 "attributedTo" => user.ap_id,
953 "actor" => user.ap_id
956 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
958 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
959 assert [user.follower_address] == activity.data["to"]
962 test "it accepts Move activities" do
963 old_user = insert(:user)
964 new_user = insert(:user)
967 "@context" => "https://www.w3.org/ns/activitystreams",
969 "actor" => old_user.ap_id,
970 "object" => old_user.ap_id,
971 "target" => new_user.ap_id
974 assert :error = Transmogrifier.handle_incoming(message)
976 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
978 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
979 assert activity.actor == old_user.ap_id
980 assert activity.data["actor"] == old_user.ap_id
981 assert activity.data["object"] == old_user.ap_id
982 assert activity.data["target"] == new_user.ap_id
983 assert activity.data["type"] == "Move"
987 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
988 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
989 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
993 "test/fixtures/mastodon-post-activity.json"
997 items = get_in(data, ["object", "replies", "first", "items"])
998 assert length(items) > 0
1000 %{data: data, items: items}
1003 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1007 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
1009 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1012 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1013 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1017 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1019 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1021 {:ok, _activity} = Transmogrifier.handle_incoming(data)
1023 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1027 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
1028 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
1029 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1032 user = insert(:user)
1034 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
1037 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
1040 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
1042 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
1044 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
1046 Repo.delete(activity.object)
1047 Repo.delete(activity)
1049 %{federation_output: federation_output, replies_uris: replies_uris}
1052 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
1053 federation_output: federation_output,
1054 replies_uris: replies_uris
1056 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
1058 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1060 for id <- replies_uris do
1061 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
1062 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
1066 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
1067 %{federation_output: federation_output} do
1068 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1070 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
1072 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
1076 describe "prepare outgoing" do
1077 test "it inlines private announced objects" do
1078 user = insert(:user)
1080 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
1082 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
1084 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1086 assert modified["object"]["content"] == "hey"
1087 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1090 test "it turns mentions into tags" do
1091 user = insert(:user)
1092 other_user = insert(:user)
1095 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
1097 with_mock Pleroma.Notification,
1098 get_notified_from_activity: fn _, _ -> [] end do
1099 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1101 object = modified["object"]
1103 expected_mention = %{
1104 "href" => other_user.ap_id,
1105 "name" => "@#{other_user.nickname}",
1110 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1111 "type" => "Hashtag",
1115 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
1116 assert Enum.member?(object["tag"], expected_tag)
1117 assert Enum.member?(object["tag"], expected_mention)
1121 test "it adds the sensitive property" do
1122 user = insert(:user)
1124 {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
1125 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1127 assert modified["object"]["sensitive"]
1130 test "it adds the json-ld context and the conversation property" do
1131 user = insert(:user)
1133 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
1134 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1136 assert modified["@context"] ==
1137 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1139 assert modified["object"]["conversation"] == modified["context"]
1142 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1143 user = insert(:user)
1145 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
1146 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1148 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1151 test "it strips internal hashtag data" do
1152 user = insert(:user)
1154 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
1157 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1158 "type" => "Hashtag",
1162 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1164 assert modified["object"]["tag"] == [expected_tag]
1167 test "it strips internal fields" do
1168 user = insert(:user)
1170 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu :firefox:"})
1172 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1174 assert length(modified["object"]["tag"]) == 2
1176 assert is_nil(modified["object"]["emoji"])
1177 assert is_nil(modified["object"]["like_count"])
1178 assert is_nil(modified["object"]["announcements"])
1179 assert is_nil(modified["object"]["announcement_count"])
1180 assert is_nil(modified["object"]["context_id"])
1183 test "it strips internal fields of article" do
1184 activity = insert(:article_activity)
1186 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1188 assert length(modified["object"]["tag"]) == 2
1190 assert is_nil(modified["object"]["emoji"])
1191 assert is_nil(modified["object"]["like_count"])
1192 assert is_nil(modified["object"]["announcements"])
1193 assert is_nil(modified["object"]["announcement_count"])
1194 assert is_nil(modified["object"]["context_id"])
1195 assert is_nil(modified["object"]["likes"])
1198 test "the directMessage flag is present" do
1199 user = insert(:user)
1200 other_user = insert(:user)
1202 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
1204 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1206 assert modified["directMessage"] == false
1208 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
1210 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1212 assert modified["directMessage"] == false
1215 CommonAPI.post(user, %{
1216 status: "@#{other_user.nickname} :moominmamma:",
1217 visibility: "direct"
1220 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1222 assert modified["directMessage"] == true
1225 test "it strips BCC field" do
1226 user = insert(:user)
1227 {:ok, list} = Pleroma.List.create("foo", user)
1229 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1231 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1233 assert is_nil(modified["bcc"])
1236 test "it can handle Listen activities" do
1237 listen_activity = insert(:listen)
1239 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1241 assert modified["type"] == "Listen"
1243 user = insert(:user)
1245 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1247 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1251 describe "user upgrade" do
1252 test "it upgrades a user to activitypub" do
1255 nickname: "rye@niu.moe",
1257 ap_id: "https://niu.moe/users/rye",
1258 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1261 user_two = insert(:user)
1262 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
1264 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1265 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
1266 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1268 user = User.get_cached_by_id(user.id)
1269 assert user.note_count == 1
1271 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1272 ObanHelpers.perform_all()
1274 assert user.ap_enabled
1275 assert user.note_count == 1
1276 assert user.follower_address == "https://niu.moe/users/rye/followers"
1277 assert user.following_address == "https://niu.moe/users/rye/following"
1279 user = User.get_cached_by_id(user.id)
1280 assert user.note_count == 1
1282 activity = Activity.get_by_id(activity.id)
1283 assert user.follower_address in activity.recipients
1289 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1298 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1303 refute "..." in activity.recipients
1305 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1306 refute user.follower_address in unrelated_activity.recipients
1308 user_two = User.get_cached_by_id(user_two.id)
1309 assert User.following?(user_two, user)
1310 refute "..." in User.following(user_two)
1314 describe "actor rewriting" do
1315 test "it fixes the actor URL property to be a proper URI" do
1317 "url" => %{"href" => "http://example.com"}
1320 rewritten = Transmogrifier.maybe_fix_user_object(data)
1321 assert rewritten["url"] == "http://example.com"
1325 describe "actor origin containment" do
1326 test "it rejects activities which reference objects with bogus origins" do
1328 "@context" => "https://www.w3.org/ns/activitystreams",
1329 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1330 "actor" => "http://mastodon.example.org/users/admin",
1331 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1332 "object" => "https://info.pleroma.site/activity.json",
1333 "type" => "Announce"
1336 assert capture_log(fn ->
1337 {:error, _} = Transmogrifier.handle_incoming(data)
1338 end) =~ "Object containment failed"
1341 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1343 "@context" => "https://www.w3.org/ns/activitystreams",
1344 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1345 "actor" => "http://mastodon.example.org/users/admin",
1346 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1347 "object" => "https://info.pleroma.site/activity2.json",
1348 "type" => "Announce"
1351 assert capture_log(fn ->
1352 {:error, _} = Transmogrifier.handle_incoming(data)
1353 end) =~ "Object containment failed"
1356 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1358 "@context" => "https://www.w3.org/ns/activitystreams",
1359 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1360 "actor" => "http://mastodon.example.org/users/admin",
1361 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1362 "object" => "https://info.pleroma.site/activity3.json",
1363 "type" => "Announce"
1366 assert capture_log(fn ->
1367 {:error, _} = Transmogrifier.handle_incoming(data)
1368 end) =~ "Object containment failed"
1372 describe "reserialization" do
1373 test "successfully reserializes a message with inReplyTo == nil" do
1374 user = insert(:user)
1377 "@context" => "https://www.w3.org/ns/activitystreams",
1378 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1382 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1387 "attributedTo" => user.ap_id
1389 "actor" => user.ap_id
1392 {:ok, activity} = Transmogrifier.handle_incoming(message)
1394 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1397 test "successfully reserializes a message with AS2 objects in IR" do
1398 user = insert(:user)
1401 "@context" => "https://www.w3.org/ns/activitystreams",
1402 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1406 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1411 "attributedTo" => user.ap_id,
1413 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1414 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1417 "actor" => user.ap_id
1420 {:ok, activity} = Transmogrifier.handle_incoming(message)
1422 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1426 test "Rewrites Answers to Notes" do
1427 user = insert(:user)
1429 {:ok, poll_activity} =
1430 CommonAPI.post(user, %{
1432 poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
1435 poll_object = Object.normalize(poll_activity)
1436 # TODO: Replace with CommonAPI vote creation when implemented
1438 File.read!("test/fixtures/mastodon-vote.json")
1440 |> Kernel.put_in(["to"], user.ap_id)
1441 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1442 |> Kernel.put_in(["object", "to"], user.ap_id)
1444 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1445 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1447 assert data["object"]["type"] == "Note"
1450 describe "fix_explicit_addressing" do
1452 user = insert(:user)
1456 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1457 explicitly_mentioned_actors = [
1458 "https://pleroma.gold/users/user1",
1459 "https://pleroma.gold/user2"
1463 "actor" => user.ap_id,
1464 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1467 Enum.map(explicitly_mentioned_actors, fn href ->
1468 %{"type" => "Mention", "href" => href}
1472 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1473 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1474 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1475 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1478 test "does not move actor's follower collection to cc", %{user: user} do
1480 "actor" => user.ap_id,
1481 "to" => [user.follower_address],
1485 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1486 assert user.follower_address in fixed_object["to"]
1487 refute user.follower_address in fixed_object["cc"]
1490 test "removes recipient's follower collection from cc", %{user: user} do
1491 recipient = insert(:user)
1494 "actor" => user.ap_id,
1495 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1496 "cc" => [user.follower_address, recipient.follower_address]
1499 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1501 assert user.follower_address in fixed_object["cc"]
1502 refute recipient.follower_address in fixed_object["cc"]
1503 refute recipient.follower_address in fixed_object["to"]
1507 describe "fix_summary/1" do
1508 test "returns fixed object" do
1509 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1510 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1511 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1515 describe "fix_in_reply_to/2" do
1516 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1519 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1523 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1524 assert Transmogrifier.fix_in_reply_to(data) == data
1527 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1528 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1531 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1533 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1534 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1535 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1538 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1540 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1541 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1542 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1545 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1547 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1548 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1549 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1551 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1552 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1553 assert modified_object["inReplyTo"] == []
1554 assert modified_object["inReplyToAtomUri"] == ""
1557 @tag capture_log: true
1558 test "returns modified object when allowed incoming reply", %{data: data} do
1563 "https://shitposter.club/notice/2827873"
1566 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1567 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1569 assert modified_object["inReplyTo"] ==
1570 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1572 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1574 assert modified_object["context"] ==
1575 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1579 describe "fix_url/1" do
1580 test "fixes data for object when url is map" do
1584 "mimeType" => "video/mp4",
1585 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1589 assert Transmogrifier.fix_url(object) == %{
1590 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1594 test "fixes data for video object" do
1600 "mimeType" => "video/mp4",
1601 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1605 "mimeType" => "video/mp4",
1606 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1610 "mimeType" => "text/html",
1611 "href" => "https://peertube.-2d4c2d1630e3"
1615 "mimeType" => "text/html",
1616 "href" => "https://peertube.-2d4c2d16377-42"
1621 assert Transmogrifier.fix_url(object) == %{
1624 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1625 "mimeType" => "video/mp4",
1630 "url" => "https://peertube.-2d4c2d1630e3"
1634 test "fixes url for not Video object" do
1640 "mimeType" => "text/html",
1641 "href" => "https://peertube.-2d4c2d1630e3"
1645 "mimeType" => "text/html",
1646 "href" => "https://peertube.-2d4c2d16377-42"
1651 assert Transmogrifier.fix_url(object) == %{
1653 "url" => "https://peertube.-2d4c2d1630e3"
1656 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1662 test "retunrs not modified object" do
1663 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1667 describe "get_obj_helper/2" do
1668 test "returns nil when cannot normalize object" do
1669 assert capture_log(fn ->
1670 refute Transmogrifier.get_obj_helper("test-obj-id")
1671 end) =~ "Unsupported URI scheme"
1674 @tag capture_log: true
1675 test "returns {:ok, %Object{}} for success case" do
1676 assert {:ok, %Object{}} =
1677 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1681 describe "fix_attachments/1" do
1682 test "returns not modified object" do
1683 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1684 assert Transmogrifier.fix_attachments(data) == data
1687 test "returns modified object when attachment is map" do
1688 assert Transmogrifier.fix_attachments(%{
1690 "mediaType" => "video/mp4",
1691 "url" => "https://peertube.moe/stat-480.mp4"
1696 "mediaType" => "video/mp4",
1698 %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
1705 test "returns modified object when attachment is list" do
1706 assert Transmogrifier.fix_attachments(%{
1708 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1709 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1714 "mediaType" => "video/mp4",
1716 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1720 "mediaType" => "video/mp4",
1722 %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
1730 describe "fix_emoji/1" do
1731 test "returns not modified object when object not contains tags" do
1732 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1733 assert Transmogrifier.fix_emoji(data) == data
1736 test "returns object with emoji when object contains list tags" do
1737 assert Transmogrifier.fix_emoji(%{
1739 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1740 %{"type" => "Hashtag"}
1743 "emoji" => %{"bib" => "/test"},
1745 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1746 %{"type" => "Hashtag"}
1751 test "returns object with emoji when object contains map tag" do
1752 assert Transmogrifier.fix_emoji(%{
1753 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1755 "emoji" => %{"bib" => "/test"},
1756 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1761 describe "set_replies/1" do
1762 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1764 test "returns unmodified object if activity doesn't have self-replies" do
1765 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1766 assert Transmogrifier.set_replies(data) == data
1769 test "sets `replies` collection with a limited number of self-replies" do
1770 [user, another_user] = insert_list(2, :user)
1772 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
1774 {:ok, %{id: id2} = self_reply1} =
1775 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
1777 {:ok, self_reply2} =
1778 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
1780 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1781 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
1784 CommonAPI.post(user, %{
1785 status: "self-reply to self-reply",
1786 in_reply_to_status_id: id2
1790 CommonAPI.post(another_user, %{
1791 status: "another user's reply",
1792 in_reply_to_status_id: id1
1795 object = Object.normalize(activity)
1796 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1798 assert %{"type" => "Collection", "items" => ^replies_uris} =
1799 Transmogrifier.set_replies(object.data)["replies"]
1803 test "take_emoji_tags/1" do
1804 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1806 assert Transmogrifier.take_emoji_tags(user) == [
1808 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1809 "id" => "https://example.org/firefox.png",
1810 "name" => ":firefox:",
1812 "updated" => "1970-01-01T00:00:00Z"