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 @tag capture_log: true
108 test "it does not crash if the object in inReplyTo can't be fetched" do
110 File.read!("test/fixtures/mastodon-post-activity.json")
115 |> Map.put("inReplyTo", "https://404.site/whatever")
119 |> Map.put("object", object)
121 assert {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
124 test "it does not work for deactivated users" do
125 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
127 insert(:user, ap_id: data["actor"], is_active: false)
129 assert {:error, _} = Transmogrifier.handle_incoming(data)
132 test "it works for incoming notices" do
133 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
135 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
138 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
140 assert data["context"] ==
141 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
143 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
145 assert data["cc"] == [
146 "http://localtesting.pleroma.lol/users/lain",
147 "http://mastodon.example.org/users/admin/followers"
150 assert data["actor"] == "http://mastodon.example.org/users/admin"
152 object_data = Object.normalize(data["object"], fetch: false).data
154 assert object_data["id"] ==
155 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
157 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
159 assert object_data["cc"] == [
160 "http://localtesting.pleroma.lol/users/lain",
161 "http://mastodon.example.org/users/admin/followers"
164 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
165 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
167 assert object_data["context"] ==
168 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
170 assert object_data["sensitive"] == true
172 user = User.get_cached_by_ap_id(object_data["actor"])
174 assert user.note_count == 1
177 test "it works for incoming notices without the sensitive property but an nsfw hashtag" do
178 data = File.read!("test/fixtures/mastodon-post-activity-nsfw.json") |> Jason.decode!()
180 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
182 object_data = Object.normalize(data["object"], fetch: false).data
184 assert object_data["sensitive"] == true
187 test "it works for incoming notices with hashtags" do
188 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Jason.decode!()
190 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
191 object = Object.normalize(data["object"], fetch: false)
195 "href" => "http://localtesting.pleroma.lol/users/lain",
196 "name" => "@lain@localtesting.pleroma.lol",
199 Enum.at(object.data["tag"], 0)
204 "href" => "http://mastodon.example.org/tags/moo",
208 Enum.at(object.data["tag"], 1)
211 assert "moo" == Enum.at(object.data["tag"], 2)
214 test "it works for incoming notices with contentMap" do
215 data = File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Jason.decode!()
217 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
218 object = Object.normalize(data["object"], fetch: false)
220 assert object.data["content"] ==
221 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
224 test "it works for incoming notices with a nil contentMap (firefish)" do
226 File.read!("test/fixtures/mastodon-post-activity-contentmap.json")
228 |> Map.put("contentMap", nil)
230 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
231 object = Object.normalize(data["object"], fetch: false)
233 assert object.data["content"] ==
234 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
237 test "it works for incoming notices with to/cc not being an array (kroeg)" do
238 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
240 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
241 object = Object.normalize(data["object"], fetch: false)
243 assert object.data["content"] ==
244 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
247 test "it ensures that as:Public activities make it to their followers collection" do
251 File.read!("test/fixtures/mastodon-post-activity.json")
253 |> Map.put("actor", user.ap_id)
254 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
259 |> Map.put("attributedTo", user.ap_id)
260 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
262 |> Map.put("id", user.ap_id <> "/activities/12345678")
264 data = Map.put(data, "object", object)
266 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
268 assert data["cc"] == [User.ap_followers(user)]
271 test "it ensures that address fields become lists" do
275 File.read!("test/fixtures/mastodon-post-activity.json")
277 |> Map.put("actor", user.ap_id)
278 |> Map.put("cc", nil)
282 |> Map.put("attributedTo", user.ap_id)
283 |> Map.put("cc", nil)
284 |> Map.put("id", user.ap_id <> "/activities/12345678")
286 data = Map.put(data, "object", object)
288 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
290 refute is_nil(data["cc"])
293 test "it strips internal likes" do
295 File.read!("test/fixtures/mastodon-post-activity.json")
300 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
301 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
303 "type" => "OrderedCollection"
306 object = Map.put(data["object"], "likes", likes)
307 data = Map.put(data, "object", object)
309 {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
311 object = Object.normalize(activity)
313 assert object.data["likes"] == []
316 test "it strips internal reactions" do
318 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
319 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
321 %{object: object} = Activity.get_by_id_with_object(activity.id)
322 assert Map.has_key?(object.data, "reactions")
323 assert Map.has_key?(object.data, "reaction_count")
325 object_data = Transmogrifier.strip_internal_fields(object.data)
326 refute Map.has_key?(object_data, "reactions")
327 refute Map.has_key?(object_data, "reaction_count")
330 test "it correctly processes messages with non-array to field" do
332 File.read!("test/fixtures/mastodon-post-activity.json")
334 |> Map.put("to", "https://www.w3.org/ns/activitystreams#Public")
335 |> put_in(["object", "to"], "https://www.w3.org/ns/activitystreams#Public")
337 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
340 "http://localtesting.pleroma.lol/users/lain",
341 "http://mastodon.example.org/users/admin/followers"
342 ] == activity.data["cc"]
344 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
347 test "it correctly processes messages with non-array cc field" do
349 File.read!("test/fixtures/mastodon-post-activity.json")
351 |> Map.put("cc", "http://mastodon.example.org/users/admin/followers")
352 |> put_in(["object", "cc"], "http://mastodon.example.org/users/admin/followers")
354 assert {:ok, activity} = Transmogrifier.handle_incoming(data)
356 assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
357 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
360 test "it correctly processes messages with weirdness in address fields" 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"]
374 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
375 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
376 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
380 "test/fixtures/mastodon-post-activity.json"
384 items = get_in(data, ["object", "replies", "first", "items"])
385 assert length(items) > 0
387 %{data: data, items: items}
390 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
394 clear_config([:instance, :federation_incoming_replies_max_depth], 10)
396 {:ok, activity} = Transmogrifier.handle_incoming(data)
398 object = Object.normalize(activity.data["object"])
400 assert object.data["replies"] == items
403 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
404 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
408 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
410 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
412 {:ok, _activity} = Transmogrifier.handle_incoming(data)
414 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
418 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
419 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
420 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
424 "type" => "Collection",
425 "items" => [Utils.generate_object_id(), Utils.generate_object_id()]
429 File.read!("test/fixtures/mastodon-post-activity.json")
431 |> Kernel.put_in(["object", "replies"], replies)
433 %{activity: activity}
436 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
439 clear_config([:instance, :federation_incoming_replies_max_depth], 1)
441 assert {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(activity)
442 object = Object.normalize(data["object"])
444 for id <- object.data["replies"] do
445 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
446 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
450 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
451 %{activity: activity} do
452 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
454 {:ok, _activity} = Transmogrifier.handle_incoming(activity)
456 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
460 describe "reserialization" do
461 test "successfully reserializes a message with inReplyTo == nil" do
465 "@context" => "https://www.w3.org/ns/activitystreams",
466 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
470 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
472 "id" => Utils.generate_object_id(),
476 "attributedTo" => user.ap_id
478 "actor" => user.ap_id
481 {:ok, activity} = Transmogrifier.handle_incoming(message)
483 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
486 test "successfully reserializes a message with AS2 objects in IR" do
490 "@context" => "https://www.w3.org/ns/activitystreams",
491 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
495 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
497 "id" => Utils.generate_object_id(),
501 "attributedTo" => user.ap_id,
503 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
504 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
507 "actor" => user.ap_id
510 {:ok, activity} = Transmogrifier.handle_incoming(message)
512 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
516 describe "fix_in_reply_to/2" do
517 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
520 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
524 test "returns not modified object when has no inReplyTo field", %{data: data} do
525 assert Transmogrifier.fix_in_reply_to(data) == data
528 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
529 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
532 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
534 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
535 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
538 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
540 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
541 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
544 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
546 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
547 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
549 object_with_reply = Map.put(data["object"], "inReplyTo", [])
550 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
551 assert modified_object["inReplyTo"] == []
554 @tag capture_log: true
555 test "returns modified object when allowed incoming reply", %{data: data} do
560 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
563 clear_config([:instance, :federation_incoming_replies_max_depth], 5)
564 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
566 assert modified_object["inReplyTo"] ==
567 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
569 assert modified_object["context"] ==
570 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
574 describe "fix_attachments/1" do
575 test "returns not modified object" do
576 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
577 assert Transmogrifier.fix_attachments(data) == data
580 test "returns modified object when attachment is map" do
581 assert Transmogrifier.fix_attachments(%{
583 "mediaType" => "video/mp4",
584 "url" => "https://peertube.moe/stat-480.mp4"
589 "mediaType" => "video/mp4",
590 "type" => "Document",
593 "href" => "https://peertube.moe/stat-480.mp4",
594 "mediaType" => "video/mp4",
603 test "returns modified object when attachment is list" do
604 assert Transmogrifier.fix_attachments(%{
606 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
607 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
612 "mediaType" => "video/mp4",
613 "type" => "Document",
616 "href" => "https://pe.er/stat-480.mp4",
617 "mediaType" => "video/mp4",
623 "mediaType" => "video/mp4",
624 "type" => "Document",
627 "href" => "https://pe.er/stat-480.mp4",
628 "mediaType" => "video/mp4",
638 describe "fix_emoji/1" do
639 test "returns not modified object when object not contains tags" do
640 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
641 assert Transmogrifier.fix_emoji(data) == data
644 test "returns object with emoji when object contains list tags" do
645 assert Transmogrifier.fix_emoji(%{
647 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
648 %{"type" => "Hashtag"}
651 "emoji" => %{"bib" => "/test"},
653 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
654 %{"type" => "Hashtag"}
659 test "returns object with emoji when object contains map tag" do
660 assert Transmogrifier.fix_emoji(%{
661 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
663 "emoji" => %{"bib" => "/test"},
664 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
669 describe "set_replies/1" do
670 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
672 test "returns unmodified object if activity doesn't have self-replies" do
673 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
674 assert Transmogrifier.set_replies(data) == data
677 test "sets `replies` collection with a limited number of self-replies" do
678 [user, another_user] = insert_list(2, :user)
680 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
682 {:ok, %{id: id2} = self_reply1} =
683 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
686 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
688 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
689 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
692 CommonAPI.post(user, %{
693 status: "self-reply to self-reply",
694 in_reply_to_status_id: id2
698 CommonAPI.post(another_user, %{
699 status: "another user's reply",
700 in_reply_to_status_id: id1
703 object = Object.normalize(activity, fetch: false)
704 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
706 assert %{"type" => "Collection", "items" => ^replies_uris} =
707 Transmogrifier.set_replies(object.data)["replies"]
711 test "take_emoji_tags/1" do
712 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
714 assert Transmogrifier.take_emoji_tags(user) == [
716 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
717 "id" => "https://example.org/firefox.png",
718 "name" => ":firefox:",
720 "updated" => "1970-01-01T00:00:00Z"
725 test "the standalone note uses its own ID when context is missing" do
726 insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
729 "test/fixtures/tesla_mock/mk.absturztau.be-93e7nm8wqg-activity.json"
733 {:ok, %Activity{} = modified} = Transmogrifier.handle_incoming(activity)
734 object = Object.normalize(modified, fetch: false)
736 assert object.data["context"] == object.data["id"]
737 assert modified.data["context"] == object.data["id"]
740 @tag capture_log: true
741 test "the reply note uses its parent's ID when context is missing and reply is unreachable" do
742 insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
745 "test/fixtures/tesla_mock/mk.absturztau.be-93e7nm8wqg-activity.json"
751 |> Map.put("inReplyTo", "https://404.site/object/went-to-buy-milk")
755 |> Map.put("object", object)
757 {:ok, %Activity{} = modified} = Transmogrifier.handle_incoming(activity)
758 object = Object.normalize(modified, fetch: false)
760 assert object.data["context"] == object.data["inReplyTo"]
761 assert modified.data["context"] == object.data["inReplyTo"]