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") |> 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.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, 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 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") |> Jason.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") |> Jason.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"], fetch: false).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") |> Jason.decode!()
210 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
212 object_data = Object.normalize(data["object"], fetch: 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") |> Jason.decode!()
220 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
221 object = Object.normalize(data["object"], fetch: false)
223 assert Enum.at(object.data["tag"], 2) == "moo"
226 test "it works for incoming notices with contentMap" do
227 data = File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Jason.decode!()
229 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
230 object = Object.normalize(data["object"], fetch: false)
232 assert object.data["content"] ==
233 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
236 test "it works for incoming notices with to/cc not being an array (kroeg)" do
237 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
239 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
240 object = Object.normalize(data["object"], fetch: false)
242 assert object.data["content"] ==
243 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
246 test "it ensures that as:Public activities make it to their followers collection" do
250 File.read!("test/fixtures/mastodon-post-activity.json")
252 |> Map.put("actor", user.ap_id)
253 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
258 |> Map.put("attributedTo", user.ap_id)
259 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
261 |> Map.put("id", user.ap_id <> "/activities/12345678")
263 data = Map.put(data, "object", object)
265 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
267 assert data["cc"] == [User.ap_followers(user)]
270 test "it ensures that address fields become lists" do
274 File.read!("test/fixtures/mastodon-post-activity.json")
276 |> Map.put("actor", user.ap_id)
277 |> Map.put("to", nil)
278 |> Map.put("cc", nil)
282 |> Map.put("attributedTo", user.ap_id)
283 |> Map.put("to", nil)
284 |> Map.put("cc", nil)
285 |> Map.put("id", user.ap_id <> "/activities/12345678")
287 data = Map.put(data, "object", object)
289 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
291 assert !is_nil(data["to"])
292 assert !is_nil(data["cc"])
295 test "it strips internal likes" do
297 File.read!("test/fixtures/mastodon-post-activity.json")
302 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
303 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
305 "type" => "OrderedCollection"
308 object = Map.put(data["object"], "likes", likes)
309 data = Map.put(data, "object", object)
311 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
313 refute Map.has_key?(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
334 "@context" => "https://www.w3.org/ns/activitystreams",
335 "to" => "https://www.w3.org/ns/activitystreams#Public",
338 "content" => "blah blah blah",
340 "attributedTo" => user.ap_id,
343 "actor" => user.ap_id
346 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
348 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
351 test "it correctly processes messages with non-array cc field" do
355 "@context" => "https://www.w3.org/ns/activitystreams",
356 "to" => user.follower_address,
357 "cc" => "https://www.w3.org/ns/activitystreams#Public",
360 "content" => "blah blah blah",
362 "attributedTo" => user.ap_id,
365 "actor" => user.ap_id
368 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
370 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
371 assert [user.follower_address] == activity.data["to"]
374 test "it correctly processes messages with weirdness in address fields" do
378 "@context" => "https://www.w3.org/ns/activitystreams",
379 "to" => [nil, user.follower_address],
380 "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]],
385 "attributedTo" => user.ap_id,
388 "actor" => user.ap_id
391 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
393 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
394 assert [user.follower_address] == activity.data["to"]
398 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
399 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
400 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
404 "test/fixtures/mastodon-post-activity.json"
408 items = get_in(data, ["object", "replies", "first", "items"])
409 assert length(items) > 0
411 %{data: data, items: items}
414 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
418 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
420 {:ok, _activity} = Transmogrifier.handle_incoming(data)
423 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
424 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
428 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
430 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
432 {:ok, _activity} = Transmogrifier.handle_incoming(data)
434 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
438 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
439 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
440 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
445 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
448 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
451 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
453 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
455 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
457 Repo.delete(activity.object)
458 Repo.delete(activity)
460 %{federation_output: federation_output, replies_uris: replies_uris}
463 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
464 federation_output: federation_output,
465 replies_uris: replies_uris
467 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
469 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
471 for id <- replies_uris do
472 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
473 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
477 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
478 %{federation_output: federation_output} do
479 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
481 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
483 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
487 describe "reserialization" do
488 test "successfully reserializes a message with inReplyTo == nil" do
492 "@context" => "https://www.w3.org/ns/activitystreams",
493 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
497 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
502 "attributedTo" => user.ap_id
504 "actor" => user.ap_id
507 {:ok, activity} = Transmogrifier.handle_incoming(message)
509 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
512 test "successfully reserializes a message with AS2 objects in IR" do
516 "@context" => "https://www.w3.org/ns/activitystreams",
517 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
521 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
526 "attributedTo" => user.ap_id,
528 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
529 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
532 "actor" => user.ap_id
535 {:ok, activity} = Transmogrifier.handle_incoming(message)
537 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
541 describe "fix_in_reply_to/2" do
542 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
545 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
549 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
550 assert Transmogrifier.fix_in_reply_to(data) == data
553 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
554 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
557 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
559 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
560 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
563 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
565 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
566 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
569 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
571 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
572 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
574 object_with_reply = Map.put(data["object"], "inReplyTo", [])
575 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
576 assert modified_object["inReplyTo"] == []
579 @tag capture_log: true
580 test "returns modified object when allowed incoming reply", %{data: data} do
585 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
588 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
589 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
591 assert modified_object["inReplyTo"] ==
592 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
594 assert modified_object["context"] ==
595 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
599 describe "fix_attachments/1" do
600 test "returns not modified object" do
601 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
602 assert Transmogrifier.fix_attachments(data) == data
605 test "returns modified object when attachment is map" do
606 assert Transmogrifier.fix_attachments(%{
608 "mediaType" => "video/mp4",
609 "url" => "https://peertube.moe/stat-480.mp4"
614 "mediaType" => "video/mp4",
615 "type" => "Document",
618 "href" => "https://peertube.moe/stat-480.mp4",
619 "mediaType" => "video/mp4",
628 test "returns modified object when attachment is list" do
629 assert Transmogrifier.fix_attachments(%{
631 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
632 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
637 "mediaType" => "video/mp4",
638 "type" => "Document",
641 "href" => "https://pe.er/stat-480.mp4",
642 "mediaType" => "video/mp4",
648 "mediaType" => "video/mp4",
649 "type" => "Document",
652 "href" => "https://pe.er/stat-480.mp4",
653 "mediaType" => "video/mp4",
663 describe "fix_emoji/1" do
664 test "returns not modified object when object not contains tags" do
665 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
666 assert Transmogrifier.fix_emoji(data) == data
669 test "returns object with emoji when object contains list tags" do
670 assert Transmogrifier.fix_emoji(%{
672 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
673 %{"type" => "Hashtag"}
676 "emoji" => %{"bib" => "/test"},
678 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
679 %{"type" => "Hashtag"}
684 test "returns object with emoji when object contains map tag" do
685 assert Transmogrifier.fix_emoji(%{
686 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
688 "emoji" => %{"bib" => "/test"},
689 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
694 describe "set_replies/1" do
695 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
697 test "returns unmodified object if activity doesn't have self-replies" do
698 data = Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
699 assert Transmogrifier.set_replies(data) == data
702 test "sets `replies` collection with a limited number of self-replies" do
703 [user, another_user] = insert_list(2, :user)
705 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
707 {:ok, %{id: id2} = self_reply1} =
708 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
711 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
713 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
714 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
717 CommonAPI.post(user, %{
718 status: "self-reply to self-reply",
719 in_reply_to_status_id: id2
723 CommonAPI.post(another_user, %{
724 status: "another user's reply",
725 in_reply_to_status_id: id1
728 object = Object.normalize(activity, fetch: false)
729 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
731 assert %{"type" => "Collection", "items" => ^replies_uris} =
732 Transmogrifier.set_replies(object.data)["replies"]
736 test "take_emoji_tags/1" do
737 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
739 assert Transmogrifier.take_emoji_tags(user) == [
741 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
742 "id" => "https://example.org/firefox.png",
743 "name" => ":firefox:",
745 "updated" => "1970-01-01T00:00:00Z"