1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.CommonAPI
16 import Pleroma.Factory
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 setup do: clear_config([:instance, :max_remote_account_fields])
25 describe "handle_incoming" do
26 test "it works for incoming notices with tag not being an array (kroeg)" do
27 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Jason.decode!()
29 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
30 object = Object.normalize(data["object"], fetch: false)
32 assert object.data["emoji"] == %{
33 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
36 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Jason.decode!()
38 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
39 object = Object.normalize(data["object"], fetch: false)
41 assert "test" in Object.tags(object)
42 assert Object.hashtags(object) == ["test"]
45 test "it cleans up incoming notices which are not really DMs" do
47 other_user = insert(:user)
49 to = [user.ap_id, other_user.ap_id]
52 File.read!("test/fixtures/mastodon-post-activity.json")
62 data = Map.put(data, "object", object)
64 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
66 assert data["to"] == []
67 assert data["cc"] == to
69 object_data = Object.normalize(activity, fetch: false).data
71 assert object_data["to"] == []
72 assert object_data["cc"] == to
75 test "it ignores an incoming notice if we already have it" do
76 activity = insert(:note_activity)
79 File.read!("test/fixtures/mastodon-post-activity.json")
81 |> Map.put("object", Object.normalize(activity, fetch: false).data)
83 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
85 assert activity == returned_activity
88 @tag capture_log: true
89 test "it fetches reply-to activities if we don't have them" do
91 File.read!("test/fixtures/mastodon-post-activity.json")
96 |> Map.put("inReplyTo", "https://mstdn.io/users/mayuutann/statuses/99568293732299394")
98 data = Map.put(data, "object", object)
99 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
100 returned_object = Object.normalize(returned_activity, fetch: false)
103 Activity.get_create_by_object_ap_id(
104 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
107 assert returned_object.data["inReplyTo"] ==
108 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
111 test "it does not fetch reply-to activities beyond max replies depth limit" do
113 File.read!("test/fixtures/mastodon-post-activity.json")
118 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
120 data = Map.put(data, "object", object)
122 with_mock Pleroma.Web.Federator,
123 allowed_thread_distance?: fn _ -> false end do
124 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
126 returned_object = Object.normalize(returned_activity, fetch: false)
128 refute Activity.get_create_by_object_ap_id(
129 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
132 assert returned_object.data["inReplyTo"] == "https://shitposter.club/notice/2827873"
136 test "it does not crash if the object in inReplyTo can't be fetched" do
138 File.read!("test/fixtures/mastodon-post-activity.json")
143 |> Map.put("inReplyTo", "https://404.site/whatever")
147 |> Map.put("object", object)
149 assert {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
152 test "it does not work for deactivated users" do
153 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
155 insert(:user, ap_id: data["actor"], is_active: false)
157 assert {:error, _} = Transmogrifier.handle_incoming(data)
160 test "it works for incoming notices" do
161 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
163 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
166 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
168 assert data["context"] ==
169 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
171 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
173 assert data["cc"] == [
174 "http://localtesting.pleroma.lol/users/lain",
175 "http://mastodon.example.org/users/admin/followers"
178 assert data["actor"] == "http://mastodon.example.org/users/admin"
180 object_data = Object.normalize(data["object"], fetch: false).data
182 assert object_data["id"] ==
183 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
185 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
187 assert object_data["cc"] == [
188 "http://localtesting.pleroma.lol/users/lain",
189 "http://mastodon.example.org/users/admin/followers"
192 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
193 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
195 assert object_data["context"] ==
196 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
198 assert object_data["sensitive"] == true
200 user = User.get_cached_by_ap_id(object_data["actor"])
202 assert user.note_count == 1
205 test "it works for incoming notices without the sensitive property but an nsfw hashtag" do
206 data = File.read!("test/fixtures/mastodon-post-activity-nsfw.json") |> Jason.decode!()
208 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
210 object_data = Object.normalize(data["object"], fetch: false).data
212 assert object_data["sensitive"] == true
215 test "it works for incoming notices with hashtags" do
216 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Jason.decode!()
218 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
219 object = Object.normalize(data["object"], fetch: false)
223 "href" => "http://localtesting.pleroma.lol/users/lain",
224 "name" => "@lain@localtesting.pleroma.lol",
227 Enum.at(object.data["tag"], 0)
232 "href" => "http://mastodon.example.org/tags/moo",
236 Enum.at(object.data["tag"], 1)
239 assert "moo" == Enum.at(object.data["tag"], 2)
242 test "it works for incoming notices with contentMap" do
243 data = File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Jason.decode!()
245 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
246 object = Object.normalize(data["object"], fetch: false)
248 assert object.data["content"] ==
249 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
252 test "it works for incoming notices with to/cc not being an array (kroeg)" do
253 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
255 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
256 object = Object.normalize(data["object"], fetch: false)
258 assert object.data["content"] ==
259 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
262 test "it ensures that as:Public activities make it to their followers collection" do
266 File.read!("test/fixtures/mastodon-post-activity.json")
268 |> Map.put("actor", user.ap_id)
269 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
274 |> Map.put("attributedTo", user.ap_id)
275 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
277 |> Map.put("id", user.ap_id <> "/activities/12345678")
279 data = Map.put(data, "object", object)
281 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
283 assert data["cc"] == [User.ap_followers(user)]
286 test "it ensures that address fields become lists" do
290 File.read!("test/fixtures/mastodon-post-activity.json")
292 |> Map.put("actor", user.ap_id)
293 |> Map.put("cc", nil)
297 |> Map.put("attributedTo", user.ap_id)
298 |> Map.put("cc", nil)
299 |> Map.put("id", user.ap_id <> "/activities/12345678")
301 data = Map.put(data, "object", object)
303 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
305 refute is_nil(data["cc"])
308 test "it strips internal likes" do
310 File.read!("test/fixtures/mastodon-post-activity.json")
315 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
316 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
318 "type" => "OrderedCollection"
321 object = Map.put(data["object"], "likes", likes)
322 data = Map.put(data, "object", object)
324 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
326 refute Map.has_key?(object.data, "likes")
329 test "it strips internal reactions" do
331 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
332 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
334 %{object: object} = Activity.get_by_id_with_object(activity.id)
335 assert Map.has_key?(object.data, "reactions")
336 assert Map.has_key?(object.data, "reaction_count")
338 object_data = Transmogrifier.strip_internal_fields(object.data)
339 refute Map.has_key?(object_data, "reactions")
340 refute Map.has_key?(object_data, "reaction_count")
343 test "it correctly processes messages with non-array to field" do
345 File.read!("test/fixtures/mastodon-post-activity.json")
347 |> Map.put("to", "https://www.w3.org/ns/activitystreams#Public")
348 |> put_in(["object", "to"], "https://www.w3.org/ns/activitystreams#Public")
350 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
353 "http://localtesting.pleroma.lol/users/lain",
354 "http://mastodon.example.org/users/admin/followers"
355 ] == activity.data["cc"]
357 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
360 test "it correctly processes messages with non-array cc field" do
362 File.read!("test/fixtures/mastodon-post-activity.json")
364 |> Map.put("cc", "http://mastodon.example.org/users/admin/followers")
365 |> put_in(["object", "cc"], "http://mastodon.example.org/users/admin/followers")
367 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
369 assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
370 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
373 test "it correctly processes messages with weirdness in address fields" do
375 File.read!("test/fixtures/mastodon-post-activity.json")
377 |> Map.put("cc", ["http://mastodon.example.org/users/admin/followers", ["¿"]])
378 |> put_in(["object", "cc"], ["http://mastodon.example.org/users/admin/followers", ["¿"]])
380 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
382 assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
383 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
387 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
388 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
389 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
393 "test/fixtures/mastodon-post-activity.json"
397 items = get_in(data, ["object", "replies", "first", "items"])
398 assert length(items) > 0
400 %{data: data, items: items}
403 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
407 clear_config([:instance, :federation_incoming_replies_max_depth], 10)
409 {:ok, activity} = Transmogrifier.handle_incoming(data)
411 object = Object.normalize(activity.data["object"])
413 assert object.data["replies"] == items
416 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
417 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
421 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
423 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
425 {:ok, _activity} = Transmogrifier.handle_incoming(data)
427 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
431 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
432 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
433 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
437 "type" => "Collection",
439 Pleroma.Web.ActivityPub.Utils.generate_object_id(),
440 Pleroma.Web.ActivityPub.Utils.generate_object_id()
445 File.read!("test/fixtures/mastodon-post-activity.json")
447 |> Kernel.put_in(["object", "replies"], replies)
449 %{activity: activity}
452 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
455 clear_config([:instance, :federation_incoming_replies_max_depth], 1)
457 assert {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(activity)
458 object = Object.normalize(data["object"])
460 for id <- object.data["replies"] do
461 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
462 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
466 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
467 %{activity: activity} do
468 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
470 {:ok, _activity} = Transmogrifier.handle_incoming(activity)
472 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
476 describe "reserialization" do
477 test "successfully reserializes a message with inReplyTo == nil" do
481 "@context" => "https://www.w3.org/ns/activitystreams",
482 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
486 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
488 "id" => Utils.generate_object_id(),
492 "attributedTo" => user.ap_id
494 "actor" => user.ap_id
497 {:ok, activity} = Transmogrifier.handle_incoming(message)
499 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
502 test "successfully reserializes a message with AS2 objects in IR" do
506 "@context" => "https://www.w3.org/ns/activitystreams",
507 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
511 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
513 "id" => Utils.generate_object_id(),
517 "attributedTo" => user.ap_id,
519 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
520 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
523 "actor" => user.ap_id
526 {:ok, activity} = Transmogrifier.handle_incoming(message)
528 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
532 describe "fix_in_reply_to/2" do
533 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
536 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
540 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
541 assert Transmogrifier.fix_in_reply_to(data) == data
544 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
545 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
548 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
550 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
551 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
554 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
556 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
557 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
560 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
562 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
563 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
565 object_with_reply = Map.put(data["object"], "inReplyTo", [])
566 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
567 assert modified_object["inReplyTo"] == []
570 @tag capture_log: true
571 test "returns modified object when allowed incoming reply", %{data: data} do
576 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
579 clear_config([:instance, :federation_incoming_replies_max_depth], 5)
580 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
582 assert modified_object["inReplyTo"] ==
583 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
585 assert modified_object["context"] ==
586 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
590 describe "fix_attachments/1" do
591 test "returns not modified object" do
592 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
593 assert Transmogrifier.fix_attachments(data) == data
596 test "returns modified object when attachment is map" do
597 assert Transmogrifier.fix_attachments(%{
599 "mediaType" => "video/mp4",
600 "url" => "https://peertube.moe/stat-480.mp4"
605 "mediaType" => "video/mp4",
606 "type" => "Document",
609 "href" => "https://peertube.moe/stat-480.mp4",
610 "mediaType" => "video/mp4",
619 test "returns modified object when attachment is list" do
620 assert Transmogrifier.fix_attachments(%{
622 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
623 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
628 "mediaType" => "video/mp4",
629 "type" => "Document",
632 "href" => "https://pe.er/stat-480.mp4",
633 "mediaType" => "video/mp4",
639 "mediaType" => "video/mp4",
640 "type" => "Document",
643 "href" => "https://pe.er/stat-480.mp4",
644 "mediaType" => "video/mp4",
654 describe "fix_emoji/1" do
655 test "returns not modified object when object not contains tags" do
656 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
657 assert Transmogrifier.fix_emoji(data) == data
660 test "returns object with emoji when object contains list tags" do
661 assert Transmogrifier.fix_emoji(%{
663 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
664 %{"type" => "Hashtag"}
667 "emoji" => %{"bib" => "/test"},
669 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
670 %{"type" => "Hashtag"}
675 test "returns object with emoji when object contains map tag" do
676 assert Transmogrifier.fix_emoji(%{
677 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
679 "emoji" => %{"bib" => "/test"},
680 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
685 describe "set_replies/1" do
686 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
688 test "returns unmodified object if activity doesn't have self-replies" do
689 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
690 assert Transmogrifier.set_replies(data) == data
693 test "sets `replies` collection with a limited number of self-replies" do
694 [user, another_user] = insert_list(2, :user)
696 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
698 {:ok, %{id: id2} = self_reply1} =
699 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
702 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
704 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
705 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
708 CommonAPI.post(user, %{
709 status: "self-reply to self-reply",
710 in_reply_to_status_id: id2
714 CommonAPI.post(another_user, %{
715 status: "another user's reply",
716 in_reply_to_status_id: id1
719 object = Object.normalize(activity, fetch: false)
720 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
722 assert %{"type" => "Collection", "items" => ^replies_uris} =
723 Transmogrifier.set_replies(object.data)["replies"]
727 test "take_emoji_tags/1" do
728 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
730 assert Transmogrifier.take_emoji_tags(user) == [
732 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
733 "id" => "https://example.org/firefox.png",
734 "name" => ":firefox:",
736 "updated" => "1970-01-01T00:00:00Z"