1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do
6 use Oban.Testing, repo: Pleroma.Repo
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.ActivityPub.Utils
14 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 setup do: clear_config([:instance, :max_remote_account_fields])
26 describe "handle_incoming" do
27 test "it works for incoming notices with tag not being an array (kroeg)" do
28 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Jason.decode!()
30 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
31 object = Object.normalize(data["object"], fetch: false)
33 assert object.data["emoji"] == %{
34 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
37 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Jason.decode!()
39 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
40 object = Object.normalize(data["object"], fetch: false)
42 assert "test" in Object.tags(object)
43 assert Object.hashtags(object) == ["test"]
46 test "it ignores an incoming notice if we already have it" do
47 activity = insert(:note_activity)
50 File.read!("test/fixtures/mastodon-post-activity.json")
52 |> Map.put("object", Object.normalize(activity, fetch: false).data)
54 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
56 assert activity == returned_activity
59 @tag capture_log: true
60 test "it fetches reply-to activities if we don't have them" do
62 File.read!("test/fixtures/mastodon-post-activity.json")
67 |> Map.put("inReplyTo", "https://mstdn.io/users/mayuutann/statuses/99568293732299394")
69 data = Map.put(data, "object", object)
70 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
71 returned_object = Object.normalize(returned_activity, fetch: false)
74 Activity.get_create_by_object_ap_id(
75 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
78 assert returned_object.data["inReplyTo"] ==
79 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
82 test "it does not fetch reply-to activities beyond max replies depth limit" do
84 File.read!("test/fixtures/mastodon-post-activity.json")
89 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
91 data = Map.put(data, "object", object)
93 with_mock Pleroma.Web.Federator,
94 allowed_thread_distance?: fn _ -> false end do
95 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
97 returned_object = Object.normalize(returned_activity, fetch: false)
99 refute Activity.get_create_by_object_ap_id(
100 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
103 assert returned_object.data["inReplyTo"] == "https://shitposter.club/notice/2827873"
107 test "it does not crash if the object in inReplyTo can't be fetched" do
109 File.read!("test/fixtures/mastodon-post-activity.json")
114 |> Map.put("inReplyTo", "https://404.site/whatever")
118 |> Map.put("object", object)
120 assert {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
123 test "it does not work for deactivated users" do
124 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
126 insert(:user, ap_id: data["actor"], is_active: false)
128 assert {:error, _} = Transmogrifier.handle_incoming(data)
131 test "it works for incoming notices" do
132 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
134 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
137 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
139 assert data["context"] ==
140 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
142 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
144 assert data["cc"] == [
145 "http://localtesting.pleroma.lol/users/lain",
146 "http://mastodon.example.org/users/admin/followers"
149 assert data["actor"] == "http://mastodon.example.org/users/admin"
151 object_data = Object.normalize(data["object"], fetch: false).data
153 assert object_data["id"] ==
154 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
156 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
158 assert object_data["cc"] == [
159 "http://localtesting.pleroma.lol/users/lain",
160 "http://mastodon.example.org/users/admin/followers"
163 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
164 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
166 assert object_data["context"] ==
167 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
169 assert object_data["sensitive"] == true
171 user = User.get_cached_by_ap_id(object_data["actor"])
173 assert user.note_count == 1
176 test "it works for incoming notices without the sensitive property but an nsfw hashtag" do
177 data = File.read!("test/fixtures/mastodon-post-activity-nsfw.json") |> Jason.decode!()
179 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
181 object_data = Object.normalize(data["object"], fetch: false).data
183 assert object_data["sensitive"] == true
186 test "it works for incoming notices with hashtags" do
187 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Jason.decode!()
189 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
190 object = Object.normalize(data["object"], fetch: false)
194 "href" => "http://localtesting.pleroma.lol/users/lain",
195 "name" => "@lain@localtesting.pleroma.lol",
198 Enum.at(object.data["tag"], 0)
203 "href" => "http://mastodon.example.org/tags/moo",
207 Enum.at(object.data["tag"], 1)
210 assert "moo" == Enum.at(object.data["tag"], 2)
213 test "it works for incoming notices with contentMap" do
214 data = File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Jason.decode!()
216 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
217 object = Object.normalize(data["object"], fetch: false)
219 assert object.data["content"] ==
220 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
223 test "it works for incoming notices with to/cc not being an array (kroeg)" do
224 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
226 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
227 object = Object.normalize(data["object"], fetch: false)
229 assert object.data["content"] ==
230 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
233 test "it ensures that as:Public activities make it to their followers collection" do
237 File.read!("test/fixtures/mastodon-post-activity.json")
239 |> Map.put("actor", user.ap_id)
240 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
245 |> Map.put("attributedTo", user.ap_id)
246 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
248 |> Map.put("id", user.ap_id <> "/activities/12345678")
250 data = Map.put(data, "object", object)
252 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
254 assert data["cc"] == [User.ap_followers(user)]
257 test "it ensures that address fields become lists" do
261 File.read!("test/fixtures/mastodon-post-activity.json")
263 |> Map.put("actor", user.ap_id)
264 |> Map.put("cc", nil)
268 |> Map.put("attributedTo", user.ap_id)
269 |> Map.put("cc", nil)
270 |> Map.put("id", user.ap_id <> "/activities/12345678")
272 data = Map.put(data, "object", object)
274 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
276 refute is_nil(data["cc"])
279 test "it strips internal likes" do
281 File.read!("test/fixtures/mastodon-post-activity.json")
286 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
287 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
289 "type" => "OrderedCollection"
292 object = Map.put(data["object"], "likes", likes)
293 data = Map.put(data, "object", object)
295 {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
297 object = Object.normalize(activity)
299 assert object.data["likes"] == []
302 test "it strips internal reactions" do
304 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
305 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
307 %{object: object} = Activity.get_by_id_with_object(activity.id)
308 assert Map.has_key?(object.data, "reactions")
309 assert Map.has_key?(object.data, "reaction_count")
311 object_data = Transmogrifier.strip_internal_fields(object.data)
312 refute Map.has_key?(object_data, "reactions")
313 refute Map.has_key?(object_data, "reaction_count")
316 test "it correctly processes messages with non-array to field" do
318 File.read!("test/fixtures/mastodon-post-activity.json")
320 |> Map.put("to", "https://www.w3.org/ns/activitystreams#Public")
321 |> put_in(["object", "to"], "https://www.w3.org/ns/activitystreams#Public")
323 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
326 "http://localtesting.pleroma.lol/users/lain",
327 "http://mastodon.example.org/users/admin/followers"
328 ] == activity.data["cc"]
330 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
333 test "it correctly processes messages with non-array cc field" do
335 File.read!("test/fixtures/mastodon-post-activity.json")
337 |> Map.put("cc", "http://mastodon.example.org/users/admin/followers")
338 |> put_in(["object", "cc"], "http://mastodon.example.org/users/admin/followers")
340 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
342 assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
343 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
346 test "it correctly processes messages with weirdness in address fields" do
348 File.read!("test/fixtures/mastodon-post-activity.json")
350 |> Map.put("cc", ["http://mastodon.example.org/users/admin/followers", ["¿"]])
351 |> put_in(["object", "cc"], ["http://mastodon.example.org/users/admin/followers", ["¿"]])
353 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
355 assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
356 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
360 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
361 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
362 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
366 "test/fixtures/mastodon-post-activity.json"
370 items = get_in(data, ["object", "replies", "first", "items"])
371 assert length(items) > 0
373 %{data: data, items: items}
376 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
380 clear_config([:instance, :federation_incoming_replies_max_depth], 10)
382 {:ok, activity} = Transmogrifier.handle_incoming(data)
384 object = Object.normalize(activity.data["object"])
386 assert object.data["replies"] == items
389 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
390 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
394 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
396 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
398 {:ok, _activity} = Transmogrifier.handle_incoming(data)
400 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
404 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
405 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
406 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
410 "type" => "Collection",
411 "items" => [Utils.generate_object_id(), Utils.generate_object_id()]
415 File.read!("test/fixtures/mastodon-post-activity.json")
417 |> Kernel.put_in(["object", "replies"], replies)
419 %{activity: activity}
422 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
425 clear_config([:instance, :federation_incoming_replies_max_depth], 1)
427 assert {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(activity)
428 object = Object.normalize(data["object"])
430 for id <- object.data["replies"] do
431 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
432 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
436 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
437 %{activity: activity} do
438 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
440 {:ok, _activity} = Transmogrifier.handle_incoming(activity)
442 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
446 describe "reserialization" do
447 test "successfully reserializes a message with inReplyTo == nil" do
451 "@context" => "https://www.w3.org/ns/activitystreams",
452 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
456 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
458 "id" => Utils.generate_object_id(),
462 "attributedTo" => user.ap_id
464 "actor" => user.ap_id
467 {:ok, activity} = Transmogrifier.handle_incoming(message)
469 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
472 test "successfully reserializes a message with AS2 objects in IR" do
476 "@context" => "https://www.w3.org/ns/activitystreams",
477 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
481 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
483 "id" => Utils.generate_object_id(),
487 "attributedTo" => user.ap_id,
489 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
490 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
493 "actor" => user.ap_id
496 {:ok, activity} = Transmogrifier.handle_incoming(message)
498 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
502 describe "fix_in_reply_to/2" do
503 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
506 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
510 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
511 assert Transmogrifier.fix_in_reply_to(data) == data
514 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
515 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
518 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
520 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
521 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
524 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
526 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
527 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
530 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
532 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
533 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
535 object_with_reply = Map.put(data["object"], "inReplyTo", [])
536 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
537 assert modified_object["inReplyTo"] == []
540 @tag capture_log: true
541 test "returns modified object when allowed incoming reply", %{data: data} do
546 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
549 clear_config([:instance, :federation_incoming_replies_max_depth], 5)
550 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
552 assert modified_object["inReplyTo"] ==
553 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
555 assert modified_object["context"] ==
556 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
560 describe "fix_attachments/1" do
561 test "returns not modified object" do
562 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
563 assert Transmogrifier.fix_attachments(data) == data
566 test "returns modified object when attachment is map" do
567 assert Transmogrifier.fix_attachments(%{
569 "mediaType" => "video/mp4",
570 "url" => "https://peertube.moe/stat-480.mp4"
575 "mediaType" => "video/mp4",
576 "type" => "Document",
579 "href" => "https://peertube.moe/stat-480.mp4",
580 "mediaType" => "video/mp4",
589 test "returns modified object when attachment is list" do
590 assert Transmogrifier.fix_attachments(%{
592 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
593 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
598 "mediaType" => "video/mp4",
599 "type" => "Document",
602 "href" => "https://pe.er/stat-480.mp4",
603 "mediaType" => "video/mp4",
609 "mediaType" => "video/mp4",
610 "type" => "Document",
613 "href" => "https://pe.er/stat-480.mp4",
614 "mediaType" => "video/mp4",
624 describe "fix_emoji/1" do
625 test "returns not modified object when object not contains tags" do
626 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
627 assert Transmogrifier.fix_emoji(data) == data
630 test "returns object with emoji when object contains list tags" do
631 assert Transmogrifier.fix_emoji(%{
633 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
634 %{"type" => "Hashtag"}
637 "emoji" => %{"bib" => "/test"},
639 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
640 %{"type" => "Hashtag"}
645 test "returns object with emoji when object contains map tag" do
646 assert Transmogrifier.fix_emoji(%{
647 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
649 "emoji" => %{"bib" => "/test"},
650 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
655 describe "set_replies/1" do
656 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
658 test "returns unmodified object if activity doesn't have self-replies" do
659 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
660 assert Transmogrifier.set_replies(data) == data
663 test "sets `replies` collection with a limited number of self-replies" do
664 [user, another_user] = insert_list(2, :user)
666 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
668 {:ok, %{id: id2} = self_reply1} =
669 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
672 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
674 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
675 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
678 CommonAPI.post(user, %{
679 status: "self-reply to self-reply",
680 in_reply_to_status_id: id2
684 CommonAPI.post(another_user, %{
685 status: "another user's reply",
686 in_reply_to_status_id: id1
689 object = Object.normalize(activity, fetch: false)
690 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
692 assert %{"type" => "Collection", "items" => ^replies_uris} =
693 Transmogrifier.set_replies(object.data)["replies"]
697 test "take_emoji_tags/1" do
698 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
700 assert Transmogrifier.take_emoji_tags(user) == [
702 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
703 "id" => "https://example.org/firefox.png",
704 "name" => ":firefox:",
706 "updated" => "1970-01-01T00:00:00Z"
711 test "the standalone note uses its own ID when context is missing" do
712 insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
715 "test/fixtures/tesla_mock/mk.absturztau.be-93e7nm8wqg-activity.json"
719 {:ok, %Activity{} = modified} = Transmogrifier.handle_incoming(activity)
720 object = Object.normalize(modified, fetch: false)
722 assert object.data["context"] == object.data["id"]
723 assert modified.data["context"] == object.data["id"]
726 test "the reply note uses its parent's ID when context is missing and reply is unreachable" do
727 insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
730 "test/fixtures/tesla_mock/mk.absturztau.be-93e7nm8wqg-activity.json"
736 |> Map.put("inReplyTo", "https://404.site/object/went-to-buy-milk")
740 |> Map.put("object", object)
742 {:ok, %Activity{} = modified} = Transmogrifier.handle_incoming(activity)
743 object = Object.normalize(modified, fetch: false)
745 assert object.data["context"] == object.data["inReplyTo"]
746 assert modified.data["context"] == object.data["inReplyTo"]