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.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") |> Poison.decode!()
30 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
31 object = Object.normalize(data["object"])
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") |> Poison.decode!()
39 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
40 object = Object.normalize(data["object"])
42 assert "test" in object.data["tag"]
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).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).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, 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, 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 capture_log(fn ->
150 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
151 end) =~ "[warn] Couldn't fetch \"https://404.site/whatever\", error: nil"
154 test "it does not work for deactivated users" do
155 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
157 insert(:user, ap_id: data["actor"], deactivated: true)
159 assert {:error, _} = Transmogrifier.handle_incoming(data)
162 test "it works for incoming notices" do
163 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
165 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
168 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
170 assert data["context"] ==
171 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
173 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
175 assert data["cc"] == [
176 "http://mastodon.example.org/users/admin/followers",
177 "http://localtesting.pleroma.lol/users/lain"
180 assert data["actor"] == "http://mastodon.example.org/users/admin"
182 object_data = Object.normalize(data["object"]).data
184 assert object_data["id"] ==
185 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
187 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
189 assert object_data["cc"] == [
190 "http://mastodon.example.org/users/admin/followers",
191 "http://localtesting.pleroma.lol/users/lain"
194 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
195 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
197 assert object_data["context"] ==
198 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
200 assert object_data["sensitive"] == true
202 user = User.get_cached_by_ap_id(object_data["actor"])
204 assert user.note_count == 1
207 test "it works for incoming notices without the sensitive property but an nsfw hashtag" do
208 data = File.read!("test/fixtures/mastodon-post-activity-nsfw.json") |> Poison.decode!()
210 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
212 object_data = Object.normalize(data["object"], false).data
214 assert object_data["sensitive"] == true
217 test "it works for incoming notices with hashtags" do
218 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
220 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
221 object = Object.normalize(data["object"])
223 assert Enum.at(object.data["tag"], 2) == "moo"
226 test "it works for incoming notices with contentMap" do
228 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
230 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
231 object = Object.normalize(data["object"])
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") |> Poison.decode!()
240 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
241 object = Object.normalize(data["object"])
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("to", nil)
279 |> Map.put("cc", nil)
283 |> Map.put("attributedTo", user.ap_id)
284 |> Map.put("to", nil)
285 |> Map.put("cc", nil)
286 |> Map.put("id", user.ap_id <> "/activities/12345678")
288 data = Map.put(data, "object", object)
290 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
292 assert !is_nil(data["to"])
293 assert !is_nil(data["cc"])
296 test "it strips internal likes" do
298 File.read!("test/fixtures/mastodon-post-activity.json")
303 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
304 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
306 "type" => "OrderedCollection"
309 object = Map.put(data["object"], "likes", likes)
310 data = Map.put(data, "object", object)
312 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
314 refute Map.has_key?(object.data, "likes")
317 test "it strips internal reactions" do
319 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
320 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
322 %{object: object} = Activity.get_by_id_with_object(activity.id)
323 assert Map.has_key?(object.data, "reactions")
324 assert Map.has_key?(object.data, "reaction_count")
326 object_data = Transmogrifier.strip_internal_fields(object.data)
327 refute Map.has_key?(object_data, "reactions")
328 refute Map.has_key?(object_data, "reaction_count")
331 test "it correctly processes messages with non-array to field" do
335 "@context" => "https://www.w3.org/ns/activitystreams",
336 "to" => "https://www.w3.org/ns/activitystreams#Public",
339 "content" => "blah blah blah",
341 "attributedTo" => user.ap_id,
344 "actor" => user.ap_id
347 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
349 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
352 test "it correctly processes messages with non-array cc field" do
356 "@context" => "https://www.w3.org/ns/activitystreams",
357 "to" => user.follower_address,
358 "cc" => "https://www.w3.org/ns/activitystreams#Public",
361 "content" => "blah blah blah",
363 "attributedTo" => user.ap_id,
366 "actor" => user.ap_id
369 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
371 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
372 assert [user.follower_address] == activity.data["to"]
375 test "it correctly processes messages with weirdness in address fields" do
379 "@context" => "https://www.w3.org/ns/activitystreams",
380 "to" => [nil, user.follower_address],
381 "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]],
386 "attributedTo" => user.ap_id,
389 "actor" => user.ap_id
392 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
394 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
395 assert [user.follower_address] == activity.data["to"]
399 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
400 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
401 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
405 "test/fixtures/mastodon-post-activity.json"
409 items = get_in(data, ["object", "replies", "first", "items"])
410 assert length(items) > 0
412 %{data: data, items: items}
415 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
419 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
421 {:ok, _activity} = Transmogrifier.handle_incoming(data)
424 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
425 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
429 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
431 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
433 {:ok, _activity} = Transmogrifier.handle_incoming(data)
435 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
439 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
440 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
441 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
446 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
449 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
452 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
454 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
456 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
458 Repo.delete(activity.object)
459 Repo.delete(activity)
461 %{federation_output: federation_output, replies_uris: replies_uris}
464 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
465 federation_output: federation_output,
466 replies_uris: replies_uris
468 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
470 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
472 for id <- replies_uris do
473 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
474 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
478 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
479 %{federation_output: federation_output} do
480 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
482 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
484 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
488 describe "reserialization" do
489 test "successfully reserializes a message with inReplyTo == nil" do
493 "@context" => "https://www.w3.org/ns/activitystreams",
494 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
498 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
503 "attributedTo" => user.ap_id
505 "actor" => user.ap_id
508 {:ok, activity} = Transmogrifier.handle_incoming(message)
510 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
513 test "successfully reserializes a message with AS2 objects in IR" do
517 "@context" => "https://www.w3.org/ns/activitystreams",
518 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
522 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
527 "attributedTo" => user.ap_id,
529 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
530 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
533 "actor" => user.ap_id
536 {:ok, activity} = Transmogrifier.handle_incoming(message)
538 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
542 describe "fix_in_reply_to/2" do
543 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
546 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
550 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
551 assert Transmogrifier.fix_in_reply_to(data) == data
554 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
555 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
558 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
560 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
561 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
564 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
566 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
567 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
570 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
572 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
573 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
575 object_with_reply = Map.put(data["object"], "inReplyTo", [])
576 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
577 assert modified_object["inReplyTo"] == []
580 @tag capture_log: true
581 test "returns modified object when allowed incoming reply", %{data: data} do
586 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
589 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
590 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
592 assert modified_object["inReplyTo"] ==
593 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
595 assert modified_object["context"] ==
596 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
600 describe "fix_attachments/1" do
601 test "returns not modified object" do
602 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
603 assert Transmogrifier.fix_attachments(data) == data
606 test "returns modified object when attachment is map" do
607 assert Transmogrifier.fix_attachments(%{
609 "mediaType" => "video/mp4",
610 "url" => "https://peertube.moe/stat-480.mp4"
615 "mediaType" => "video/mp4",
616 "type" => "Document",
619 "href" => "https://peertube.moe/stat-480.mp4",
620 "mediaType" => "video/mp4",
629 test "returns modified object when attachment is list" do
630 assert Transmogrifier.fix_attachments(%{
632 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
633 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
638 "mediaType" => "video/mp4",
639 "type" => "Document",
642 "href" => "https://pe.er/stat-480.mp4",
643 "mediaType" => "video/mp4",
649 "mediaType" => "video/mp4",
650 "type" => "Document",
653 "href" => "https://pe.er/stat-480.mp4",
654 "mediaType" => "video/mp4",
664 describe "fix_emoji/1" do
665 test "returns not modified object when object not contains tags" do
666 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
667 assert Transmogrifier.fix_emoji(data) == data
670 test "returns object with emoji when object contains list tags" do
671 assert Transmogrifier.fix_emoji(%{
673 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
674 %{"type" => "Hashtag"}
677 "emoji" => %{"bib" => "/test"},
679 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
680 %{"type" => "Hashtag"}
685 test "returns object with emoji when object contains map tag" do
686 assert Transmogrifier.fix_emoji(%{
687 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
689 "emoji" => %{"bib" => "/test"},
690 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
695 describe "set_replies/1" do
696 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
698 test "returns unmodified object if activity doesn't have self-replies" do
699 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
700 assert Transmogrifier.set_replies(data) == data
703 test "sets `replies` collection with a limited number of self-replies" do
704 [user, another_user] = insert_list(2, :user)
706 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
708 {:ok, %{id: id2} = self_reply1} =
709 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
712 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
714 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
715 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
718 CommonAPI.post(user, %{
719 status: "self-reply to self-reply",
720 in_reply_to_status_id: id2
724 CommonAPI.post(another_user, %{
725 status: "another user's reply",
726 in_reply_to_status_id: id1
729 object = Object.normalize(activity)
730 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
732 assert %{"type" => "Collection", "items" => ^replies_uris} =
733 Transmogrifier.set_replies(object.data)["replies"]
737 test "take_emoji_tags/1" do
738 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
740 assert Transmogrifier.take_emoji_tags(user) == [
742 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
743 "id" => "https://example.org/firefox.png",
744 "name" => ":firefox:",
746 "updated" => "1970-01-01T00:00:00Z"