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
17 import ExUnit.CaptureLog
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 cleans up incoming notices which are not really DMs" do
48 other_user = insert(:user)
50 to = [user.ap_id, other_user.ap_id]
53 File.read!("test/fixtures/mastodon-post-activity.json")
63 data = Map.put(data, "object", object)
65 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
67 assert data["to"] == []
68 assert data["cc"] == to
70 object_data = Object.normalize(activity, fetch: false).data
72 assert object_data["to"] == []
73 assert object_data["cc"] == to
76 test "it ignores an incoming notice if we already have it" do
77 activity = insert(:note_activity)
80 File.read!("test/fixtures/mastodon-post-activity.json")
82 |> Map.put("object", Object.normalize(activity, fetch: false).data)
84 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
86 assert activity == returned_activity
89 @tag capture_log: true
90 test "it fetches reply-to activities if we don't have them" do
92 File.read!("test/fixtures/mastodon-post-activity.json")
97 |> Map.put("inReplyTo", "https://mstdn.io/users/mayuutann/statuses/99568293732299394")
99 data = Map.put(data, "object", object)
100 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
101 returned_object = Object.normalize(returned_activity, fetch: false)
104 Activity.get_create_by_object_ap_id(
105 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
108 assert returned_object.data["inReplyTo"] ==
109 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
112 test "it does not fetch reply-to activities beyond max replies depth limit" do
114 File.read!("test/fixtures/mastodon-post-activity.json")
119 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
121 data = Map.put(data, "object", object)
123 with_mock Pleroma.Web.Federator,
124 allowed_thread_distance?: fn _ -> false end do
125 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
127 returned_object = Object.normalize(returned_activity, fetch: false)
129 refute Activity.get_create_by_object_ap_id(
130 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
133 assert returned_object.data["inReplyTo"] == "https://shitposter.club/notice/2827873"
137 test "it does not crash if the object in inReplyTo can't be fetched" do
139 File.read!("test/fixtures/mastodon-post-activity.json")
144 |> Map.put("inReplyTo", "https://404.site/whatever")
148 |> Map.put("object", object)
150 assert capture_log(fn ->
151 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
152 end) =~ "[warn] Couldn't fetch \"https://404.site/whatever\", error: nil"
155 test "it does not work for deactivated users" do
156 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
158 insert(:user, ap_id: data["actor"], is_active: false)
160 assert {:error, _} = Transmogrifier.handle_incoming(data)
163 test "it works for incoming notices" do
164 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
166 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
169 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
171 assert data["context"] ==
172 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
174 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
176 assert data["cc"] == [
177 "http://mastodon.example.org/users/admin/followers",
178 "http://localtesting.pleroma.lol/users/lain"
181 assert data["actor"] == "http://mastodon.example.org/users/admin"
183 object_data = Object.normalize(data["object"], fetch: false).data
185 assert object_data["id"] ==
186 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
188 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
190 assert object_data["cc"] == [
191 "http://mastodon.example.org/users/admin/followers",
192 "http://localtesting.pleroma.lol/users/lain"
195 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
196 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
198 assert object_data["context"] ==
199 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
201 assert object_data["sensitive"] == true
203 user = User.get_cached_by_ap_id(object_data["actor"])
205 assert user.note_count == 1
208 test "it works for incoming notices without the sensitive property but an nsfw hashtag" do
209 data = File.read!("test/fixtures/mastodon-post-activity-nsfw.json") |> Jason.decode!()
211 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
213 object_data = Object.normalize(data["object"], fetch: false).data
215 assert object_data["sensitive"] == true
218 test "it works for incoming notices with hashtags" do
219 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Jason.decode!()
221 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
222 object = Object.normalize(data["object"], fetch: false)
224 assert Enum.at(Object.tags(object), 2) == "moo"
225 assert Object.hashtags(object) == ["moo"]
228 test "it works for incoming notices with contentMap" do
229 data = File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Jason.decode!()
231 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
232 object = Object.normalize(data["object"], fetch: false)
234 assert object.data["content"] ==
235 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
238 test "it works for incoming notices with to/cc not being an array (kroeg)" do
239 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
241 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
242 object = Object.normalize(data["object"], fetch: false)
244 assert object.data["content"] ==
245 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
248 test "it ensures that as:Public activities make it to their followers collection" do
252 File.read!("test/fixtures/mastodon-post-activity.json")
254 |> Map.put("actor", user.ap_id)
255 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
260 |> Map.put("attributedTo", user.ap_id)
261 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
263 |> Map.put("id", user.ap_id <> "/activities/12345678")
265 data = Map.put(data, "object", object)
267 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
269 assert data["cc"] == [User.ap_followers(user)]
272 test "it ensures that address fields become lists" do
276 File.read!("test/fixtures/mastodon-post-activity.json")
278 |> Map.put("actor", user.ap_id)
279 |> Map.put("to", nil)
280 |> Map.put("cc", nil)
284 |> Map.put("attributedTo", user.ap_id)
285 |> Map.put("to", nil)
286 |> Map.put("cc", nil)
287 |> Map.put("id", user.ap_id <> "/activities/12345678")
289 data = Map.put(data, "object", object)
291 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
293 assert !is_nil(data["to"])
294 assert !is_nil(data["cc"])
297 test "it strips internal likes" do
299 File.read!("test/fixtures/mastodon-post-activity.json")
304 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
305 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
307 "type" => "OrderedCollection"
310 object = Map.put(data["object"], "likes", likes)
311 data = Map.put(data, "object", object)
313 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
315 refute Map.has_key?(object.data, "likes")
318 test "it strips internal reactions" do
320 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
321 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
323 %{object: object} = Activity.get_by_id_with_object(activity.id)
324 assert Map.has_key?(object.data, "reactions")
325 assert Map.has_key?(object.data, "reaction_count")
327 object_data = Transmogrifier.strip_internal_fields(object.data)
328 refute Map.has_key?(object_data, "reactions")
329 refute Map.has_key?(object_data, "reaction_count")
332 test "it correctly processes messages with non-array to field" do
336 "@context" => "https://www.w3.org/ns/activitystreams",
337 "to" => "https://www.w3.org/ns/activitystreams#Public",
340 "content" => "blah blah blah",
342 "attributedTo" => user.ap_id,
345 "actor" => user.ap_id
348 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
350 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
353 test "it correctly processes messages with non-array cc field" do
357 "@context" => "https://www.w3.org/ns/activitystreams",
358 "to" => user.follower_address,
359 "cc" => "https://www.w3.org/ns/activitystreams#Public",
362 "content" => "blah blah blah",
364 "attributedTo" => user.ap_id,
367 "actor" => user.ap_id
370 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
372 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
373 assert [user.follower_address] == activity.data["to"]
376 test "it correctly processes messages with weirdness in address fields" do
380 "@context" => "https://www.w3.org/ns/activitystreams",
381 "to" => [nil, user.follower_address],
382 "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]],
387 "attributedTo" => user.ap_id,
390 "actor" => user.ap_id
393 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
395 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
396 assert [user.follower_address] == activity.data["to"]
400 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
401 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
402 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
406 "test/fixtures/mastodon-post-activity.json"
410 items = get_in(data, ["object", "replies", "first", "items"])
411 assert length(items) > 0
413 %{data: data, items: items}
416 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
420 clear_config([:instance, :federation_incoming_replies_max_depth], 10)
422 {:ok, _activity} = Transmogrifier.handle_incoming(data)
425 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
426 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
430 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
432 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
434 {:ok, _activity} = Transmogrifier.handle_incoming(data)
436 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
440 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
441 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
442 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
447 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
450 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
453 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
455 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
457 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
459 Repo.delete(activity.object)
460 Repo.delete(activity)
462 %{federation_output: federation_output, replies_uris: replies_uris}
465 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
466 federation_output: federation_output,
467 replies_uris: replies_uris
469 clear_config([:instance, :federation_incoming_replies_max_depth], 1)
471 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
473 for id <- replies_uris do
474 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
475 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
479 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
480 %{federation_output: federation_output} do
481 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
483 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
485 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
489 describe "reserialization" do
490 test "successfully reserializes a message with inReplyTo == nil" do
494 "@context" => "https://www.w3.org/ns/activitystreams",
495 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
499 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
504 "attributedTo" => user.ap_id
506 "actor" => user.ap_id
509 {:ok, activity} = Transmogrifier.handle_incoming(message)
511 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
514 test "successfully reserializes a message with AS2 objects in IR" do
518 "@context" => "https://www.w3.org/ns/activitystreams",
519 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
523 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
528 "attributedTo" => user.ap_id,
530 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
531 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
534 "actor" => user.ap_id
537 {:ok, activity} = Transmogrifier.handle_incoming(message)
539 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
543 describe "fix_in_reply_to/2" do
544 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
547 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
551 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
552 assert Transmogrifier.fix_in_reply_to(data) == data
555 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
556 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
559 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
561 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
562 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
565 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
567 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
568 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
571 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
573 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
574 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
576 object_with_reply = Map.put(data["object"], "inReplyTo", [])
577 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
578 assert modified_object["inReplyTo"] == []
581 @tag capture_log: true
582 test "returns modified object when allowed incoming reply", %{data: data} do
587 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
590 clear_config([:instance, :federation_incoming_replies_max_depth], 5)
591 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
593 assert modified_object["inReplyTo"] ==
594 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
596 assert modified_object["context"] ==
597 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
601 describe "fix_attachments/1" do
602 test "returns not modified object" do
603 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
604 assert Transmogrifier.fix_attachments(data) == data
607 test "returns modified object when attachment is map" do
608 assert Transmogrifier.fix_attachments(%{
610 "mediaType" => "video/mp4",
611 "url" => "https://peertube.moe/stat-480.mp4"
616 "mediaType" => "video/mp4",
617 "type" => "Document",
620 "href" => "https://peertube.moe/stat-480.mp4",
621 "mediaType" => "video/mp4",
630 test "returns modified object when attachment is list" do
631 assert Transmogrifier.fix_attachments(%{
633 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
634 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
639 "mediaType" => "video/mp4",
640 "type" => "Document",
643 "href" => "https://pe.er/stat-480.mp4",
644 "mediaType" => "video/mp4",
650 "mediaType" => "video/mp4",
651 "type" => "Document",
654 "href" => "https://pe.er/stat-480.mp4",
655 "mediaType" => "video/mp4",
665 describe "fix_emoji/1" do
666 test "returns not modified object when object not contains tags" do
667 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
668 assert Transmogrifier.fix_emoji(data) == data
671 test "returns object with emoji when object contains list tags" do
672 assert Transmogrifier.fix_emoji(%{
674 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
675 %{"type" => "Hashtag"}
678 "emoji" => %{"bib" => "/test"},
680 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
681 %{"type" => "Hashtag"}
686 test "returns object with emoji when object contains map tag" do
687 assert Transmogrifier.fix_emoji(%{
688 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
690 "emoji" => %{"bib" => "/test"},
691 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
696 describe "set_replies/1" do
697 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
699 test "returns unmodified object if activity doesn't have self-replies" do
700 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
701 assert Transmogrifier.set_replies(data) == data
704 test "sets `replies` collection with a limited number of self-replies" do
705 [user, another_user] = insert_list(2, :user)
707 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
709 {:ok, %{id: id2} = self_reply1} =
710 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
713 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
715 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
716 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
719 CommonAPI.post(user, %{
720 status: "self-reply to self-reply",
721 in_reply_to_status_id: id2
725 CommonAPI.post(another_user, %{
726 status: "another user's reply",
727 in_reply_to_status_id: id1
730 object = Object.normalize(activity, fetch: false)
731 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
733 assert %{"type" => "Collection", "items" => ^replies_uris} =
734 Transmogrifier.set_replies(object.data)["replies"]
738 test "take_emoji_tags/1" do
739 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
741 assert Transmogrifier.take_emoji_tags(user) == [
743 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
744 "id" => "https://example.org/firefox.png",
745 "name" => ":firefox:",
747 "updated" => "1970-01-01T00:00:00Z"