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.Transmogrifier
15 alias Pleroma.Web.AdminAPI.AccountView
16 alias Pleroma.Web.CommonAPI
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 setup do: clear_config([:instance, :max_remote_account_fields])
29 describe "handle_incoming" do
30 test "it works for incoming notices with tag not being an array (kroeg)" do
31 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
33 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
34 object = Object.normalize(data["object"])
36 assert object.data["emoji"] == %{
37 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
40 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
42 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
43 object = Object.normalize(data["object"])
45 assert "test" in object.data["tag"]
48 test "it works for incoming notices with url not being a string (prismo)" do
49 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
51 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
52 object = Object.normalize(data["object"])
54 assert object.data["url"] == "https://prismo.news/posts/83"
57 test "it cleans up incoming notices which are not really DMs" do
59 other_user = insert(:user)
61 to = [user.ap_id, other_user.ap_id]
64 File.read!("test/fixtures/mastodon-post-activity.json")
74 data = Map.put(data, "object", object)
76 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
78 assert data["to"] == []
79 assert data["cc"] == to
81 object_data = Object.normalize(activity).data
83 assert object_data["to"] == []
84 assert object_data["cc"] == to
87 test "it ignores an incoming notice if we already have it" do
88 activity = insert(:note_activity)
91 File.read!("test/fixtures/mastodon-post-activity.json")
93 |> Map.put("object", Object.normalize(activity).data)
95 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
97 assert activity == returned_activity
100 @tag capture_log: true
101 test "it fetches reply-to activities if we don't have them" do
103 File.read!("test/fixtures/mastodon-post-activity.json")
108 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
110 data = Map.put(data, "object", object)
111 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
112 returned_object = Object.normalize(returned_activity, false)
115 Activity.get_create_by_object_ap_id(
116 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
119 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
122 test "it does not fetch reply-to activities beyond max replies depth limit" do
124 File.read!("test/fixtures/mastodon-post-activity.json")
129 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
131 data = Map.put(data, "object", object)
133 with_mock Pleroma.Web.Federator,
134 allowed_thread_distance?: fn _ -> false end do
135 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
137 returned_object = Object.normalize(returned_activity, false)
139 refute Activity.get_create_by_object_ap_id(
140 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
143 assert returned_object.data["inReplyToAtomUri"] ==
144 "https://shitposter.club/notice/2827873"
148 test "it does not crash if the object in inReplyTo can't be fetched" do
150 File.read!("test/fixtures/mastodon-post-activity.json")
155 |> Map.put("inReplyTo", "https://404.site/whatever")
159 |> Map.put("object", object)
161 assert capture_log(fn ->
162 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
163 end) =~ "[warn] Couldn't fetch \"https://404.site/whatever\", error: nil"
166 test "it does not work for deactivated users" do
167 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
169 insert(:user, ap_id: data["actor"], deactivated: true)
171 assert {:error, _} = Transmogrifier.handle_incoming(data)
174 test "it works for incoming notices" do
175 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
177 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
180 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
182 assert data["context"] ==
183 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
185 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
187 assert data["cc"] == [
188 "http://mastodon.example.org/users/admin/followers",
189 "http://localtesting.pleroma.lol/users/lain"
192 assert data["actor"] == "http://mastodon.example.org/users/admin"
194 object_data = Object.normalize(data["object"]).data
196 assert object_data["id"] ==
197 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
199 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
201 assert object_data["cc"] == [
202 "http://mastodon.example.org/users/admin/followers",
203 "http://localtesting.pleroma.lol/users/lain"
206 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
207 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
209 assert object_data["context"] ==
210 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
212 assert object_data["sensitive"] == true
214 user = User.get_cached_by_ap_id(object_data["actor"])
216 assert user.note_count == 1
219 test "it works for incoming notices with hashtags" do
220 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
222 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
223 object = Object.normalize(data["object"])
225 assert Enum.at(object.data["tag"], 2) == "moo"
228 test "it works for incoming listens" do
230 "@context" => "https://www.w3.org/ns/activitystreams",
231 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
234 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
235 "actor" => "http://mastodon.example.org/users/admin",
238 "id" => "http://mastodon.example.org/users/admin/listens/1234",
239 "attributedTo" => "http://mastodon.example.org/users/admin",
240 "title" => "lain radio episode 1",
242 "album" => "lain radio",
247 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
249 object = Object.normalize(activity)
251 assert object.data["title"] == "lain radio episode 1"
252 assert object.data["artist"] == "lain"
253 assert object.data["album"] == "lain radio"
254 assert object.data["length"] == 180_000
257 test "it works for incoming notices with contentMap" do
259 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
261 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
262 object = Object.normalize(data["object"])
264 assert object.data["content"] ==
265 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
268 test "it works for incoming notices with to/cc not being an array (kroeg)" do
269 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
271 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
272 object = Object.normalize(data["object"])
274 assert object.data["content"] ==
275 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
278 test "it ensures that as:Public activities make it to their followers collection" do
282 File.read!("test/fixtures/mastodon-post-activity.json")
284 |> Map.put("actor", user.ap_id)
285 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
290 |> Map.put("attributedTo", user.ap_id)
291 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
293 |> Map.put("id", user.ap_id <> "/activities/12345678")
295 data = Map.put(data, "object", object)
297 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
299 assert data["cc"] == [User.ap_followers(user)]
302 test "it ensures that address fields become lists" do
306 File.read!("test/fixtures/mastodon-post-activity.json")
308 |> Map.put("actor", user.ap_id)
309 |> Map.put("to", nil)
310 |> Map.put("cc", nil)
314 |> Map.put("attributedTo", user.ap_id)
315 |> Map.put("to", nil)
316 |> Map.put("cc", nil)
317 |> Map.put("id", user.ap_id <> "/activities/12345678")
319 data = Map.put(data, "object", object)
321 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
323 assert !is_nil(data["to"])
324 assert !is_nil(data["cc"])
327 test "it strips internal likes" do
329 File.read!("test/fixtures/mastodon-post-activity.json")
334 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
335 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
337 "type" => "OrderedCollection"
340 object = Map.put(data["object"], "likes", likes)
341 data = Map.put(data, "object", object)
343 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
345 refute Map.has_key?(object.data, "likes")
348 test "it strips internal reactions" do
350 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
351 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
353 %{object: object} = Activity.get_by_id_with_object(activity.id)
354 assert Map.has_key?(object.data, "reactions")
355 assert Map.has_key?(object.data, "reaction_count")
357 object_data = Transmogrifier.strip_internal_fields(object.data)
358 refute Map.has_key?(object_data, "reactions")
359 refute Map.has_key?(object_data, "reaction_count")
362 test "it works for incoming unfollows with an existing follow" do
366 File.read!("test/fixtures/mastodon-follow-activity.json")
368 |> Map.put("object", user.ap_id)
370 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
373 File.read!("test/fixtures/mastodon-unfollow-activity.json")
375 |> Map.put("object", follow_data)
377 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
379 assert data["type"] == "Undo"
380 assert data["object"]["type"] == "Follow"
381 assert data["object"]["object"] == user.ap_id
382 assert data["actor"] == "http://mastodon.example.org/users/admin"
384 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
387 test "it works for incoming follows to locked account" do
388 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
389 user = insert(:user, locked: true)
392 File.read!("test/fixtures/mastodon-follow-activity.json")
394 |> Map.put("object", user.ap_id)
396 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
398 assert data["type"] == "Follow"
399 assert data["object"] == user.ap_id
400 assert data["state"] == "pending"
401 assert data["actor"] == "http://mastodon.example.org/users/admin"
403 assert [^pending_follower] = User.get_follow_requests(user)
406 test "it fails for incoming rejects which cannot be correlated" do
407 follower = insert(:user)
408 followed = insert(:user, locked: true)
411 File.read!("test/fixtures/mastodon-reject-activity.json")
413 |> Map.put("actor", followed.ap_id)
416 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
418 :error = Transmogrifier.handle_incoming(accept_data)
420 follower = User.get_cached_by_id(follower.id)
422 refute User.following?(follower, followed) == true
425 test "it works for incoming rejects which are orphaned" do
426 follower = insert(:user)
427 followed = insert(:user, locked: true)
429 {:ok, follower} = User.follow(follower, followed)
430 {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, followed)
432 assert User.following?(follower, followed) == true
435 File.read!("test/fixtures/mastodon-reject-activity.json")
437 |> Map.put("actor", followed.ap_id)
440 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
442 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
443 refute activity.local
444 assert activity.data["id"] == reject_data["id"]
446 follower = User.get_cached_by_id(follower.id)
448 assert User.following?(follower, followed) == false
451 test "it works for incoming rejects which are referenced by IRI only" do
452 follower = insert(:user)
453 followed = insert(:user, locked: true)
455 {:ok, follower} = User.follow(follower, followed)
456 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
458 assert User.following?(follower, followed) == true
461 File.read!("test/fixtures/mastodon-reject-activity.json")
463 |> Map.put("actor", followed.ap_id)
464 |> Map.put("object", follow_activity.data["id"])
466 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
468 follower = User.get_cached_by_id(follower.id)
470 assert User.following?(follower, followed) == false
473 test "it rejects activities without a valid ID" do
477 File.read!("test/fixtures/mastodon-follow-activity.json")
479 |> Map.put("object", user.ap_id)
482 :error = Transmogrifier.handle_incoming(data)
485 test "skip converting the content when it is nil" do
486 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
488 {:ok, object} = Fetcher.fetch_and_contain_remote_object_from_id(object_id)
491 Pleroma.Web.ActivityPub.Transmogrifier.fix_object(Map.merge(object, %{"content" => nil}))
493 assert result["content"] == nil
496 test "it converts content of object to html" do
497 object_id = "https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe"
499 {:ok, %{"content" => content_markdown}} =
500 Fetcher.fetch_and_contain_remote_object_from_id(object_id)
502 {:ok, %Pleroma.Object{data: %{"content" => content}} = object} =
503 Fetcher.fetch_object_from_id(object_id)
505 assert content_markdown ==
506 "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..."
509 "<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>"
511 assert object.data["mediaType"] == "text/html"
514 test "it remaps video URLs as attachments if necessary" do
516 Fetcher.fetch_object_from_id(
517 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
520 assert object.data["url"] ==
521 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
523 assert object.data["attachment"] == [
526 "mediaType" => "video/mp4",
530 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
531 "mediaType" => "video/mp4",
539 Fetcher.fetch_object_from_id(
540 "https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206"
543 assert object.data["attachment"] == [
546 "mediaType" => "video/mp4",
550 "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4",
551 "mediaType" => "video/mp4",
558 assert object.data["url"] ==
559 "https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206"
562 test "it accepts Flag activities" do
564 other_user = insert(:user)
566 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
567 object = Object.normalize(activity)
571 "id" => activity.data["id"],
572 "content" => "test post",
573 "published" => object.data["published"],
574 "actor" => AccountView.render("show.json", %{user: user, skip_visibility_check: true})
578 "@context" => "https://www.w3.org/ns/activitystreams",
579 "cc" => [user.ap_id],
580 "object" => [user.ap_id, activity.data["id"]],
582 "content" => "blocked AND reported!!!",
583 "actor" => other_user.ap_id
586 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
588 assert activity.data["object"] == [user.ap_id, note_obj]
589 assert activity.data["content"] == "blocked AND reported!!!"
590 assert activity.data["actor"] == other_user.ap_id
591 assert activity.data["cc"] == [user.ap_id]
594 test "it correctly processes messages with non-array to field" do
598 "@context" => "https://www.w3.org/ns/activitystreams",
599 "to" => "https://www.w3.org/ns/activitystreams#Public",
602 "content" => "blah blah blah",
604 "attributedTo" => user.ap_id,
607 "actor" => user.ap_id
610 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
612 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
615 test "it correctly processes messages with non-array cc field" do
619 "@context" => "https://www.w3.org/ns/activitystreams",
620 "to" => user.follower_address,
621 "cc" => "https://www.w3.org/ns/activitystreams#Public",
624 "content" => "blah blah blah",
626 "attributedTo" => user.ap_id,
629 "actor" => user.ap_id
632 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
634 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
635 assert [user.follower_address] == activity.data["to"]
638 test "it correctly processes messages with weirdness in address fields" do
642 "@context" => "https://www.w3.org/ns/activitystreams",
643 "to" => [nil, user.follower_address],
644 "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]],
649 "attributedTo" => user.ap_id,
652 "actor" => user.ap_id
655 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
657 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
658 assert [user.follower_address] == activity.data["to"]
661 test "it accepts Move activities" do
662 old_user = insert(:user)
663 new_user = insert(:user)
666 "@context" => "https://www.w3.org/ns/activitystreams",
668 "actor" => old_user.ap_id,
669 "object" => old_user.ap_id,
670 "target" => new_user.ap_id
673 assert :error = Transmogrifier.handle_incoming(message)
675 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
677 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
678 assert activity.actor == old_user.ap_id
679 assert activity.data["actor"] == old_user.ap_id
680 assert activity.data["object"] == old_user.ap_id
681 assert activity.data["target"] == new_user.ap_id
682 assert activity.data["type"] == "Move"
686 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
687 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
688 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
692 "test/fixtures/mastodon-post-activity.json"
696 items = get_in(data, ["object", "replies", "first", "items"])
697 assert length(items) > 0
699 %{data: data, items: items}
702 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
706 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
708 {:ok, _activity} = Transmogrifier.handle_incoming(data)
711 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
712 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
716 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
718 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
720 {:ok, _activity} = Transmogrifier.handle_incoming(data)
722 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
726 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
727 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
728 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
733 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
736 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
739 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
741 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
743 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
745 Repo.delete(activity.object)
746 Repo.delete(activity)
748 %{federation_output: federation_output, replies_uris: replies_uris}
751 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
752 federation_output: federation_output,
753 replies_uris: replies_uris
755 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
757 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
759 for id <- replies_uris do
760 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
761 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
765 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
766 %{federation_output: federation_output} do
767 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
769 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
771 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
775 describe "prepare outgoing" do
776 test "it inlines private announced objects" do
779 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
781 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
783 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
785 assert modified["object"]["content"] == "hey"
786 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
789 test "it turns mentions into tags" do
791 other_user = insert(:user)
794 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
796 with_mock Pleroma.Notification,
797 get_notified_from_activity: fn _, _ -> [] end do
798 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
800 object = modified["object"]
802 expected_mention = %{
803 "href" => other_user.ap_id,
804 "name" => "@#{other_user.nickname}",
809 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
814 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
815 assert Enum.member?(object["tag"], expected_tag)
816 assert Enum.member?(object["tag"], expected_mention)
820 test "it adds the sensitive property" do
823 {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
824 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
826 assert modified["object"]["sensitive"]
829 test "it adds the json-ld context and the conversation property" do
832 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
833 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
835 assert modified["@context"] ==
836 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
838 assert modified["object"]["conversation"] == modified["context"]
841 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
844 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
845 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
847 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
850 test "it strips internal hashtag data" do
853 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
856 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
861 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
863 assert modified["object"]["tag"] == [expected_tag]
866 test "it strips internal fields" do
869 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu :firefox:"})
871 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
873 assert length(modified["object"]["tag"]) == 2
875 assert is_nil(modified["object"]["emoji"])
876 assert is_nil(modified["object"]["like_count"])
877 assert is_nil(modified["object"]["announcements"])
878 assert is_nil(modified["object"]["announcement_count"])
879 assert is_nil(modified["object"]["context_id"])
882 test "it strips internal fields of article" do
883 activity = insert(:article_activity)
885 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
887 assert length(modified["object"]["tag"]) == 2
889 assert is_nil(modified["object"]["emoji"])
890 assert is_nil(modified["object"]["like_count"])
891 assert is_nil(modified["object"]["announcements"])
892 assert is_nil(modified["object"]["announcement_count"])
893 assert is_nil(modified["object"]["context_id"])
894 assert is_nil(modified["object"]["likes"])
897 test "the directMessage flag is present" do
899 other_user = insert(:user)
901 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
903 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
905 assert modified["directMessage"] == false
907 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
909 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
911 assert modified["directMessage"] == false
914 CommonAPI.post(user, %{
915 status: "@#{other_user.nickname} :moominmamma:",
919 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
921 assert modified["directMessage"] == true
924 test "it strips BCC field" do
926 {:ok, list} = Pleroma.List.create("foo", user)
928 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
930 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
932 assert is_nil(modified["bcc"])
935 test "it can handle Listen activities" do
936 listen_activity = insert(:listen)
938 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
940 assert modified["type"] == "Listen"
944 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
946 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
950 describe "user upgrade" do
951 test "it upgrades a user to activitypub" do
954 nickname: "rye@niu.moe",
956 ap_id: "https://niu.moe/users/rye",
957 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
960 user_two = insert(:user)
961 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
963 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
964 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
965 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
967 user = User.get_cached_by_id(user.id)
968 assert user.note_count == 1
970 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
971 ObanHelpers.perform_all()
973 assert user.ap_enabled
974 assert user.note_count == 1
975 assert user.follower_address == "https://niu.moe/users/rye/followers"
976 assert user.following_address == "https://niu.moe/users/rye/following"
978 user = User.get_cached_by_id(user.id)
979 assert user.note_count == 1
981 activity = Activity.get_by_id(activity.id)
982 assert user.follower_address in activity.recipients
988 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
997 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1002 refute "..." in activity.recipients
1004 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1005 refute user.follower_address in unrelated_activity.recipients
1007 user_two = User.get_cached_by_id(user_two.id)
1008 assert User.following?(user_two, user)
1009 refute "..." in User.following(user_two)
1013 describe "actor rewriting" do
1014 test "it fixes the actor URL property to be a proper URI" do
1016 "url" => %{"href" => "http://example.com"}
1019 rewritten = Transmogrifier.maybe_fix_user_object(data)
1020 assert rewritten["url"] == "http://example.com"
1024 describe "actor origin containment" do
1025 test "it rejects activities which reference objects with bogus origins" do
1027 "@context" => "https://www.w3.org/ns/activitystreams",
1028 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1029 "actor" => "http://mastodon.example.org/users/admin",
1030 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1031 "object" => "https://info.pleroma.site/activity.json",
1032 "type" => "Announce"
1035 assert capture_log(fn ->
1036 {:error, _} = Transmogrifier.handle_incoming(data)
1037 end) =~ "Object containment failed"
1040 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1042 "@context" => "https://www.w3.org/ns/activitystreams",
1043 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1044 "actor" => "http://mastodon.example.org/users/admin",
1045 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1046 "object" => "https://info.pleroma.site/activity2.json",
1047 "type" => "Announce"
1050 assert capture_log(fn ->
1051 {:error, _} = Transmogrifier.handle_incoming(data)
1052 end) =~ "Object containment failed"
1055 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1057 "@context" => "https://www.w3.org/ns/activitystreams",
1058 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1059 "actor" => "http://mastodon.example.org/users/admin",
1060 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1061 "object" => "https://info.pleroma.site/activity3.json",
1062 "type" => "Announce"
1065 assert capture_log(fn ->
1066 {:error, _} = Transmogrifier.handle_incoming(data)
1067 end) =~ "Object containment failed"
1071 describe "reserialization" do
1072 test "successfully reserializes a message with inReplyTo == nil" do
1073 user = insert(:user)
1076 "@context" => "https://www.w3.org/ns/activitystreams",
1077 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1081 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1086 "attributedTo" => user.ap_id
1088 "actor" => user.ap_id
1091 {:ok, activity} = Transmogrifier.handle_incoming(message)
1093 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1096 test "successfully reserializes a message with AS2 objects in IR" do
1097 user = insert(:user)
1100 "@context" => "https://www.w3.org/ns/activitystreams",
1101 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1105 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1110 "attributedTo" => user.ap_id,
1112 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1113 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1116 "actor" => user.ap_id
1119 {:ok, activity} = Transmogrifier.handle_incoming(message)
1121 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1125 describe "fix_explicit_addressing" do
1127 user = insert(:user)
1131 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1132 explicitly_mentioned_actors = [
1133 "https://pleroma.gold/users/user1",
1134 "https://pleroma.gold/user2"
1138 "actor" => user.ap_id,
1139 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1142 Enum.map(explicitly_mentioned_actors, fn href ->
1143 %{"type" => "Mention", "href" => href}
1147 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1148 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1149 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1150 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1153 test "does not move actor's follower collection to cc", %{user: user} do
1155 "actor" => user.ap_id,
1156 "to" => [user.follower_address],
1160 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1161 assert user.follower_address in fixed_object["to"]
1162 refute user.follower_address in fixed_object["cc"]
1165 test "removes recipient's follower collection from cc", %{user: user} do
1166 recipient = insert(:user)
1169 "actor" => user.ap_id,
1170 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1171 "cc" => [user.follower_address, recipient.follower_address]
1174 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1176 assert user.follower_address in fixed_object["cc"]
1177 refute recipient.follower_address in fixed_object["cc"]
1178 refute recipient.follower_address in fixed_object["to"]
1182 describe "fix_summary/1" do
1183 test "returns fixed object" do
1184 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1185 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1186 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1190 describe "fix_in_reply_to/2" do
1191 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
1194 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1198 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1199 assert Transmogrifier.fix_in_reply_to(data) == data
1202 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1203 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1206 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1208 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1209 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1210 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1213 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1215 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1216 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1217 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1220 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1222 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1223 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1224 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1226 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1227 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1228 assert modified_object["inReplyTo"] == []
1229 assert modified_object["inReplyToAtomUri"] == ""
1232 @tag capture_log: true
1233 test "returns modified object when allowed incoming reply", %{data: data} do
1238 "https://shitposter.club/notice/2827873"
1241 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1242 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1244 assert modified_object["inReplyTo"] ==
1245 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1247 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1249 assert modified_object["context"] ==
1250 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1254 describe "fix_url/1" do
1255 test "fixes data for object when url is map" do
1259 "mimeType" => "video/mp4",
1260 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1264 assert Transmogrifier.fix_url(object) == %{
1265 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1269 test "fixes data for video object" do
1275 "mimeType" => "video/mp4",
1276 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1280 "mimeType" => "video/mp4",
1281 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1285 "mimeType" => "text/html",
1286 "href" => "https://peertube.-2d4c2d1630e3"
1290 "mimeType" => "text/html",
1291 "href" => "https://peertube.-2d4c2d16377-42"
1296 assert Transmogrifier.fix_url(object) == %{
1299 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1300 "mimeType" => "video/mp4",
1305 "url" => "https://peertube.-2d4c2d1630e3"
1309 test "fixes url for not Video object" do
1315 "mimeType" => "text/html",
1316 "href" => "https://peertube.-2d4c2d1630e3"
1320 "mimeType" => "text/html",
1321 "href" => "https://peertube.-2d4c2d16377-42"
1326 assert Transmogrifier.fix_url(object) == %{
1328 "url" => "https://peertube.-2d4c2d1630e3"
1331 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1337 test "retunrs not modified object" do
1338 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1342 describe "get_obj_helper/2" do
1343 test "returns nil when cannot normalize object" do
1344 assert capture_log(fn ->
1345 refute Transmogrifier.get_obj_helper("test-obj-id")
1346 end) =~ "Unsupported URI scheme"
1349 @tag capture_log: true
1350 test "returns {:ok, %Object{}} for success case" do
1351 assert {:ok, %Object{}} =
1352 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1356 describe "fix_attachments/1" do
1357 test "returns not modified object" do
1358 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1359 assert Transmogrifier.fix_attachments(data) == data
1362 test "returns modified object when attachment is map" do
1363 assert Transmogrifier.fix_attachments(%{
1365 "mediaType" => "video/mp4",
1366 "url" => "https://peertube.moe/stat-480.mp4"
1371 "mediaType" => "video/mp4",
1372 "type" => "Document",
1375 "href" => "https://peertube.moe/stat-480.mp4",
1376 "mediaType" => "video/mp4",
1385 test "returns modified object when attachment is list" do
1386 assert Transmogrifier.fix_attachments(%{
1388 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1389 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1394 "mediaType" => "video/mp4",
1395 "type" => "Document",
1398 "href" => "https://pe.er/stat-480.mp4",
1399 "mediaType" => "video/mp4",
1405 "mediaType" => "video/mp4",
1406 "type" => "Document",
1409 "href" => "https://pe.er/stat-480.mp4",
1410 "mediaType" => "video/mp4",
1420 describe "fix_emoji/1" do
1421 test "returns not modified object when object not contains tags" do
1422 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1423 assert Transmogrifier.fix_emoji(data) == data
1426 test "returns object with emoji when object contains list tags" do
1427 assert Transmogrifier.fix_emoji(%{
1429 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1430 %{"type" => "Hashtag"}
1433 "emoji" => %{"bib" => "/test"},
1435 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1436 %{"type" => "Hashtag"}
1441 test "returns object with emoji when object contains map tag" do
1442 assert Transmogrifier.fix_emoji(%{
1443 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1445 "emoji" => %{"bib" => "/test"},
1446 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1451 describe "set_replies/1" do
1452 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1454 test "returns unmodified object if activity doesn't have self-replies" do
1455 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1456 assert Transmogrifier.set_replies(data) == data
1459 test "sets `replies` collection with a limited number of self-replies" do
1460 [user, another_user] = insert_list(2, :user)
1462 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
1464 {:ok, %{id: id2} = self_reply1} =
1465 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
1467 {:ok, self_reply2} =
1468 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
1470 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1471 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
1474 CommonAPI.post(user, %{
1475 status: "self-reply to self-reply",
1476 in_reply_to_status_id: id2
1480 CommonAPI.post(another_user, %{
1481 status: "another user's reply",
1482 in_reply_to_status_id: id1
1485 object = Object.normalize(activity)
1486 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1488 assert %{"type" => "Collection", "items" => ^replies_uris} =
1489 Transmogrifier.set_replies(object.data)["replies"]
1493 test "take_emoji_tags/1" do
1494 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1496 assert Transmogrifier.take_emoji_tags(user) == [
1498 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1499 "id" => "https://example.org/firefox.png",
1500 "name" => ":firefox:",
1502 "updated" => "1970-01-01T00:00:00Z"