1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
9 alias Pleroma.Object.Fetcher
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.AdminAPI.AccountView
15 alias Pleroma.Web.CommonAPI
18 import Pleroma.Factory
19 import ExUnit.CaptureLog
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 clear_config([:instance, :max_remote_account_fields])
28 describe "handle_incoming" do
29 test "it ignores an incoming notice if we already have it" do
30 activity = insert(:note_activity)
33 File.read!("test/fixtures/mastodon-post-activity.json")
35 |> Map.put("object", Object.normalize(activity).data)
37 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
39 assert activity == returned_activity
42 @tag capture_log: true
43 test "it fetches replied-to activities if we don't have them" do
45 File.read!("test/fixtures/mastodon-post-activity.json")
50 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
52 data = Map.put(data, "object", object)
53 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 returned_object = Object.normalize(returned_activity, false)
57 Activity.get_create_by_object_ap_id(
58 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
61 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
64 test "it does not fetch replied-to activities beyond max_replies_depth" do
66 File.read!("test/fixtures/mastodon-post-activity.json")
71 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
73 data = Map.put(data, "object", object)
75 with_mock Pleroma.Web.Federator,
76 allowed_incoming_reply_depth?: fn _ -> false end do
77 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
79 returned_object = Object.normalize(returned_activity, false)
81 refute Activity.get_create_by_object_ap_id(
82 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
85 assert returned_object.data["inReplyToAtomUri"] ==
86 "https://shitposter.club/notice/2827873"
90 test "it does not crash if the object in inReplyTo can't be fetched" do
92 File.read!("test/fixtures/mastodon-post-activity.json")
97 |> Map.put("inReplyTo", "https://404.site/whatever")
101 |> Map.put("object", object)
103 assert capture_log(fn ->
104 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
105 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
108 test "it works for incoming notices" do
109 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
111 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
114 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
116 assert data["context"] ==
117 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
119 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
121 assert data["cc"] == [
122 "http://mastodon.example.org/users/admin/followers",
123 "http://localtesting.pleroma.lol/users/lain"
126 assert data["actor"] == "http://mastodon.example.org/users/admin"
128 object_data = Object.normalize(data["object"]).data
130 assert object_data["id"] ==
131 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
133 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
135 assert object_data["cc"] == [
136 "http://mastodon.example.org/users/admin/followers",
137 "http://localtesting.pleroma.lol/users/lain"
140 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
141 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
143 assert object_data["context"] ==
144 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
146 assert object_data["sensitive"] == true
148 user = User.get_cached_by_ap_id(object_data["actor"])
150 assert user.note_count == 1
153 test "it works for incoming notices with hashtags" do
154 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
156 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
157 object = Object.normalize(data["object"])
159 assert Enum.at(object.data["tag"], 2) == "moo"
162 test "it works for incoming questions" do
163 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
165 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
167 object = Object.normalize(activity)
169 assert Enum.all?(object.data["oneOf"], fn choice ->
172 "Everyone knows that!",
173 "25 char limit is dumb",
174 "I can't even fit a funny"
179 test "it works for incoming listens" do
181 "@context" => "https://www.w3.org/ns/activitystreams",
182 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
185 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
186 "actor" => "http://mastodon.example.org/users/admin",
189 "id" => "http://mastodon.example.org/users/admin/listens/1234",
190 "attributedTo" => "http://mastodon.example.org/users/admin",
191 "title" => "lain radio episode 1",
193 "album" => "lain radio",
198 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
200 object = Object.normalize(activity)
202 assert object.data["title"] == "lain radio episode 1"
203 assert object.data["artist"] == "lain"
204 assert object.data["album"] == "lain radio"
205 assert object.data["length"] == 180_000
208 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
212 CommonAPI.post(user, %{
213 "status" => "suya...",
214 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
217 object = Object.normalize(activity)
220 File.read!("test/fixtures/mastodon-vote.json")
222 |> Kernel.put_in(["to"], user.ap_id)
223 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
224 |> Kernel.put_in(["object", "to"], user.ap_id)
226 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
227 answer_object = Object.normalize(activity)
228 assert answer_object.data["type"] == "Answer"
229 object = Object.get_by_ap_id(object.data["id"])
232 object.data["oneOf"],
234 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
240 test "it works for incoming notices with contentMap" do
242 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
244 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
245 object = Object.normalize(data["object"])
247 assert object.data["content"] ==
248 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
251 test "it works for incoming notices with to/cc not being an array (kroeg)" do
252 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
254 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
255 object = Object.normalize(data["object"])
257 assert object.data["content"] ==
258 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
261 test "it works for incoming announces with actor being inlined (kroeg)" do
262 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
264 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
266 assert data["actor"] == "https://puckipedia.com/"
269 test "it works for incoming notices with tag not being an array (kroeg)" do
270 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
272 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
273 object = Object.normalize(data["object"])
275 assert object.data["emoji"] == %{
276 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
279 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
281 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
282 object = Object.normalize(data["object"])
284 assert "test" in object.data["tag"]
287 test "it works for incoming notices with url not being a string (prismo)" do
288 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
290 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
291 object = Object.normalize(data["object"])
293 assert object.data["url"] == "https://prismo.news/posts/83"
296 test "it cleans up incoming notices which are not really DMs" do
298 other_user = insert(:user)
300 to = [user.ap_id, other_user.ap_id]
303 File.read!("test/fixtures/mastodon-post-activity.json")
313 data = Map.put(data, "object", object)
315 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
317 assert data["to"] == []
318 assert data["cc"] == to
320 object_data = Object.normalize(activity).data
322 assert object_data["to"] == []
323 assert object_data["cc"] == to
326 test "it works for incoming likes" do
328 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
331 File.read!("test/fixtures/mastodon-like.json")
333 |> Map.put("object", activity.data["object"])
335 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
337 assert data["actor"] == "http://mastodon.example.org/users/admin"
338 assert data["type"] == "Like"
339 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
340 assert data["object"] == activity.data["object"]
343 test "it works for incoming misskey likes, turning them into EmojiReactions" do
345 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
348 File.read!("test/fixtures/misskey-like.json")
350 |> Map.put("object", activity.data["object"])
352 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
354 assert data["actor"] == data["actor"]
355 assert data["type"] == "EmojiReaction"
356 assert data["id"] == data["id"]
357 assert data["object"] == activity.data["object"]
358 assert data["content"] == "🍮"
361 test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReactions" do
363 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
366 File.read!("test/fixtures/misskey-like.json")
368 |> Map.put("object", activity.data["object"])
369 |> Map.put("_misskey_reaction", "⭐")
371 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
373 assert data["actor"] == data["actor"]
374 assert data["type"] == "EmojiReaction"
375 assert data["id"] == data["id"]
376 assert data["object"] == activity.data["object"]
377 assert data["content"] == "⭐"
380 test "it works for incoming emoji reactions" do
382 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
385 File.read!("test/fixtures/emoji-reaction.json")
387 |> Map.put("object", activity.data["object"])
389 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
391 assert data["actor"] == "http://mastodon.example.org/users/admin"
392 assert data["type"] == "EmojiReaction"
393 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
394 assert data["object"] == activity.data["object"]
395 assert data["content"] == "👌"
398 test "it works for incoming emoji reaction undos" do
401 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
402 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
405 File.read!("test/fixtures/mastodon-undo-like.json")
407 |> Map.put("object", reaction_activity.data["id"])
408 |> Map.put("actor", user.ap_id)
410 {:ok, activity} = Transmogrifier.handle_incoming(data)
412 assert activity.actor == user.ap_id
413 assert activity.data["id"] == data["id"]
414 assert activity.data["type"] == "Undo"
417 test "it returns an error for incoming unlikes wihout a like activity" do
419 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
422 File.read!("test/fixtures/mastodon-undo-like.json")
424 |> Map.put("object", activity.data["object"])
426 assert Transmogrifier.handle_incoming(data) == :error
429 test "it works for incoming unlikes with an existing like activity" do
431 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
434 File.read!("test/fixtures/mastodon-like.json")
436 |> Map.put("object", activity.data["object"])
438 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
441 File.read!("test/fixtures/mastodon-undo-like.json")
443 |> Map.put("object", like_data)
444 |> Map.put("actor", like_data["actor"])
446 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
448 assert data["actor"] == "http://mastodon.example.org/users/admin"
449 assert data["type"] == "Undo"
450 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
451 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
454 test "it works for incoming unlikes with an existing like activity and a compact object" do
456 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
459 File.read!("test/fixtures/mastodon-like.json")
461 |> Map.put("object", activity.data["object"])
463 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
466 File.read!("test/fixtures/mastodon-undo-like.json")
468 |> Map.put("object", like_data["id"])
469 |> Map.put("actor", like_data["actor"])
471 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
473 assert data["actor"] == "http://mastodon.example.org/users/admin"
474 assert data["type"] == "Undo"
475 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
476 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
479 test "it works for incoming announces" do
480 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
482 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
484 assert data["actor"] == "http://mastodon.example.org/users/admin"
485 assert data["type"] == "Announce"
488 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
490 assert data["object"] ==
491 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
493 assert Activity.get_create_by_object_ap_id(data["object"])
496 test "it works for incoming announces with an existing activity" do
498 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
501 File.read!("test/fixtures/mastodon-announce.json")
503 |> Map.put("object", activity.data["object"])
505 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
507 assert data["actor"] == "http://mastodon.example.org/users/admin"
508 assert data["type"] == "Announce"
511 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
513 assert data["object"] == activity.data["object"]
515 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
518 test "it works for incoming announces with an inlined activity" do
520 File.read!("test/fixtures/mastodon-announce-private.json")
523 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
525 assert data["actor"] == "http://mastodon.example.org/users/admin"
526 assert data["type"] == "Announce"
529 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
531 object = Object.normalize(data["object"])
533 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
534 assert object.data["content"] == "this is a private toot"
537 @tag capture_log: true
538 test "it rejects incoming announces with an inlined activity from another origin" do
540 File.read!("test/fixtures/bogus-mastodon-announce.json")
543 assert :error = Transmogrifier.handle_incoming(data)
546 test "it does not clobber the addressing on announce activities" do
548 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
551 File.read!("test/fixtures/mastodon-announce.json")
553 |> Map.put("object", Object.normalize(activity).data["id"])
554 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
557 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
559 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
562 test "it ensures that as:Public activities make it to their followers collection" do
566 File.read!("test/fixtures/mastodon-post-activity.json")
568 |> Map.put("actor", user.ap_id)
569 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
574 |> Map.put("attributedTo", user.ap_id)
575 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
577 |> Map.put("id", user.ap_id <> "/activities/12345678")
579 data = Map.put(data, "object", object)
581 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
583 assert data["cc"] == [User.ap_followers(user)]
586 test "it ensures that address fields become lists" do
590 File.read!("test/fixtures/mastodon-post-activity.json")
592 |> Map.put("actor", user.ap_id)
593 |> Map.put("to", nil)
594 |> Map.put("cc", nil)
598 |> Map.put("attributedTo", user.ap_id)
599 |> Map.put("to", nil)
600 |> Map.put("cc", nil)
601 |> Map.put("id", user.ap_id <> "/activities/12345678")
603 data = Map.put(data, "object", object)
605 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
607 assert !is_nil(data["to"])
608 assert !is_nil(data["cc"])
611 test "it strips internal likes" do
613 File.read!("test/fixtures/mastodon-post-activity.json")
618 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
619 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
621 "type" => "OrderedCollection"
624 object = Map.put(data["object"], "likes", likes)
625 data = Map.put(data, "object", object)
627 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
629 refute Map.has_key?(object.data, "likes")
632 test "it strips internal reactions" do
634 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
635 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
637 %{object: object} = Activity.get_by_id_with_object(activity.id)
638 assert Map.has_key?(object.data, "reactions")
639 assert Map.has_key?(object.data, "reaction_count")
641 object_data = Transmogrifier.strip_internal_fields(object.data)
642 refute Map.has_key?(object_data, "reactions")
643 refute Map.has_key?(object_data, "reaction_count")
646 test "it works for incoming update activities" do
647 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
649 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
650 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
653 update_data["object"]
654 |> Map.put("actor", data["actor"])
655 |> Map.put("id", data["actor"])
659 |> Map.put("actor", data["actor"])
660 |> Map.put("object", object)
662 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
664 assert data["id"] == update_data["id"]
666 user = User.get_cached_by_ap_id(data["actor"])
667 assert user.name == "gargle"
669 assert user.avatar["url"] == [
672 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
676 assert user.banner["url"] == [
679 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
683 assert user.bio == "<p>Some bio</p>"
686 test "it works with custom profile fields" do
688 "test/fixtures/mastodon-post-activity.json"
691 |> Transmogrifier.handle_incoming()
693 user = User.get_cached_by_ap_id(activity.actor)
695 assert User.fields(user) == [
696 %{"name" => "foo", "value" => "bar"},
697 %{"name" => "foo1", "value" => "bar1"}
700 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
703 update_data["object"]
704 |> Map.put("actor", user.ap_id)
705 |> Map.put("id", user.ap_id)
709 |> Map.put("actor", user.ap_id)
710 |> Map.put("object", object)
712 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
714 user = User.get_cached_by_ap_id(user.ap_id)
716 assert User.fields(user) == [
717 %{"name" => "foo", "value" => "updated"},
718 %{"name" => "foo1", "value" => "updated"}
721 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
724 put_in(update_data, ["object", "attachment"], [
725 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
726 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
727 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
730 {:ok, _} = Transmogrifier.handle_incoming(update_data)
732 user = User.get_cached_by_ap_id(user.ap_id)
734 assert User.fields(user) == [
735 %{"name" => "foo", "value" => "updated"},
736 %{"name" => "foo1", "value" => "updated"}
739 update_data = put_in(update_data, ["object", "attachment"], [])
741 {:ok, _} = Transmogrifier.handle_incoming(update_data)
743 user = User.get_cached_by_ap_id(user.ap_id)
745 assert User.fields(user) == []
748 test "it works for incoming update activities which lock the account" do
749 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
751 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
752 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
755 update_data["object"]
756 |> Map.put("actor", data["actor"])
757 |> Map.put("id", data["actor"])
758 |> Map.put("manuallyApprovesFollowers", true)
762 |> Map.put("actor", data["actor"])
763 |> Map.put("object", object)
765 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
767 user = User.get_cached_by_ap_id(data["actor"])
768 assert user.locked == true
771 test "it works for incoming deletes" do
772 activity = insert(:note_activity)
773 deleting_user = insert(:user)
776 File.read!("test/fixtures/mastodon-delete.json")
781 |> Map.put("id", activity.data["object"])
785 |> Map.put("object", object)
786 |> Map.put("actor", deleting_user.ap_id)
788 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
789 Transmogrifier.handle_incoming(data)
791 assert id == data["id"]
792 refute Activity.get_by_id(activity.id)
793 assert actor == deleting_user.ap_id
796 test "it fails for incoming deletes with spoofed origin" do
797 activity = insert(:note_activity)
800 File.read!("test/fixtures/mastodon-delete.json")
805 |> Map.put("id", activity.data["object"])
809 |> Map.put("object", object)
811 assert capture_log(fn ->
812 :error = Transmogrifier.handle_incoming(data)
814 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
816 assert Activity.get_by_id(activity.id)
819 @tag capture_log: true
820 test "it works for incoming user deletes" do
821 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
824 File.read!("test/fixtures/mastodon-delete-user.json")
827 {:ok, _} = Transmogrifier.handle_incoming(data)
828 ObanHelpers.perform_all()
830 refute User.get_cached_by_ap_id(ap_id)
833 test "it fails for incoming user deletes with spoofed origin" do
834 %{ap_id: ap_id} = insert(:user)
837 File.read!("test/fixtures/mastodon-delete-user.json")
839 |> Map.put("actor", ap_id)
841 assert capture_log(fn ->
842 assert :error == Transmogrifier.handle_incoming(data)
843 end) =~ "Object containment failed"
845 assert User.get_cached_by_ap_id(ap_id)
848 test "it works for incoming unannounces with an existing notice" do
850 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
853 File.read!("test/fixtures/mastodon-announce.json")
855 |> Map.put("object", activity.data["object"])
857 {:ok, %Activity{data: announce_data, local: false}} =
858 Transmogrifier.handle_incoming(announce_data)
861 File.read!("test/fixtures/mastodon-undo-announce.json")
863 |> Map.put("object", announce_data)
864 |> Map.put("actor", announce_data["actor"])
866 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
868 assert data["type"] == "Undo"
869 assert object_data = data["object"]
870 assert object_data["type"] == "Announce"
871 assert object_data["object"] == activity.data["object"]
873 assert object_data["id"] ==
874 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
877 test "it works for incomming unfollows with an existing follow" do
881 File.read!("test/fixtures/mastodon-follow-activity.json")
883 |> Map.put("object", user.ap_id)
885 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
888 File.read!("test/fixtures/mastodon-unfollow-activity.json")
890 |> Map.put("object", follow_data)
892 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
894 assert data["type"] == "Undo"
895 assert data["object"]["type"] == "Follow"
896 assert data["object"]["object"] == user.ap_id
897 assert data["actor"] == "http://mastodon.example.org/users/admin"
899 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
902 test "it works for incoming follows to locked account" do
903 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
904 user = insert(:user, locked: true)
907 File.read!("test/fixtures/mastodon-follow-activity.json")
909 |> Map.put("object", user.ap_id)
911 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
913 assert data["type"] == "Follow"
914 assert data["object"] == user.ap_id
915 assert data["state"] == "pending"
916 assert data["actor"] == "http://mastodon.example.org/users/admin"
918 assert [^pending_follower] = User.get_follow_requests(user)
921 test "it works for incoming blocks" do
925 File.read!("test/fixtures/mastodon-block-activity.json")
927 |> Map.put("object", user.ap_id)
929 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
931 assert data["type"] == "Block"
932 assert data["object"] == user.ap_id
933 assert data["actor"] == "http://mastodon.example.org/users/admin"
935 blocker = User.get_cached_by_ap_id(data["actor"])
937 assert User.blocks?(blocker, user)
940 test "incoming blocks successfully tear down any follow relationship" do
941 blocker = insert(:user)
942 blocked = insert(:user)
945 File.read!("test/fixtures/mastodon-block-activity.json")
947 |> Map.put("object", blocked.ap_id)
948 |> Map.put("actor", blocker.ap_id)
950 {:ok, blocker} = User.follow(blocker, blocked)
951 {:ok, blocked} = User.follow(blocked, blocker)
953 assert User.following?(blocker, blocked)
954 assert User.following?(blocked, blocker)
956 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
958 assert data["type"] == "Block"
959 assert data["object"] == blocked.ap_id
960 assert data["actor"] == blocker.ap_id
962 blocker = User.get_cached_by_ap_id(data["actor"])
963 blocked = User.get_cached_by_ap_id(data["object"])
965 assert User.blocks?(blocker, blocked)
967 refute User.following?(blocker, blocked)
968 refute User.following?(blocked, blocker)
971 test "it works for incoming unblocks with an existing block" do
975 File.read!("test/fixtures/mastodon-block-activity.json")
977 |> Map.put("object", user.ap_id)
979 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
982 File.read!("test/fixtures/mastodon-unblock-activity.json")
984 |> Map.put("object", block_data)
986 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
987 assert data["type"] == "Undo"
988 assert data["object"]["type"] == "Block"
989 assert data["object"]["object"] == user.ap_id
990 assert data["actor"] == "http://mastodon.example.org/users/admin"
992 blocker = User.get_cached_by_ap_id(data["actor"])
994 refute User.blocks?(blocker, user)
997 test "it works for incoming accepts which were pre-accepted" do
998 follower = insert(:user)
999 followed = insert(:user)
1001 {:ok, follower} = User.follow(follower, followed)
1002 assert User.following?(follower, followed) == true
1004 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1007 File.read!("test/fixtures/mastodon-accept-activity.json")
1009 |> Map.put("actor", followed.ap_id)
1012 accept_data["object"]
1013 |> Map.put("actor", follower.ap_id)
1014 |> Map.put("id", follow_activity.data["id"])
1016 accept_data = Map.put(accept_data, "object", object)
1018 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1019 refute activity.local
1021 assert activity.data["object"] == follow_activity.data["id"]
1023 assert activity.data["id"] == accept_data["id"]
1025 follower = User.get_cached_by_id(follower.id)
1027 assert User.following?(follower, followed) == true
1030 test "it works for incoming accepts which were orphaned" do
1031 follower = insert(:user)
1032 followed = insert(:user, locked: true)
1034 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1037 File.read!("test/fixtures/mastodon-accept-activity.json")
1039 |> Map.put("actor", followed.ap_id)
1042 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1044 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1045 assert activity.data["object"] == follow_activity.data["id"]
1047 follower = User.get_cached_by_id(follower.id)
1049 assert User.following?(follower, followed) == true
1052 test "it works for incoming accepts which are referenced by IRI only" do
1053 follower = insert(:user)
1054 followed = insert(:user, locked: true)
1056 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1059 File.read!("test/fixtures/mastodon-accept-activity.json")
1061 |> Map.put("actor", followed.ap_id)
1062 |> Map.put("object", follow_activity.data["id"])
1064 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1065 assert activity.data["object"] == follow_activity.data["id"]
1067 follower = User.get_cached_by_id(follower.id)
1069 assert User.following?(follower, followed) == true
1072 test "it fails for incoming accepts which cannot be correlated" do
1073 follower = insert(:user)
1074 followed = insert(:user, locked: true)
1077 File.read!("test/fixtures/mastodon-accept-activity.json")
1079 |> Map.put("actor", followed.ap_id)
1082 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1084 :error = Transmogrifier.handle_incoming(accept_data)
1086 follower = User.get_cached_by_id(follower.id)
1088 refute User.following?(follower, followed) == true
1091 test "it fails for incoming rejects which cannot be correlated" do
1092 follower = insert(:user)
1093 followed = insert(:user, locked: true)
1096 File.read!("test/fixtures/mastodon-reject-activity.json")
1098 |> Map.put("actor", followed.ap_id)
1101 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1103 :error = Transmogrifier.handle_incoming(accept_data)
1105 follower = User.get_cached_by_id(follower.id)
1107 refute User.following?(follower, followed) == true
1110 test "it works for incoming rejects which are orphaned" do
1111 follower = insert(:user)
1112 followed = insert(:user, locked: true)
1114 {:ok, follower} = User.follow(follower, followed)
1115 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1117 assert User.following?(follower, followed) == true
1120 File.read!("test/fixtures/mastodon-reject-activity.json")
1122 |> Map.put("actor", followed.ap_id)
1125 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1127 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1128 refute activity.local
1129 assert activity.data["id"] == reject_data["id"]
1131 follower = User.get_cached_by_id(follower.id)
1133 assert User.following?(follower, followed) == false
1136 test "it works for incoming rejects which are referenced by IRI only" do
1137 follower = insert(:user)
1138 followed = insert(:user, locked: true)
1140 {:ok, follower} = User.follow(follower, followed)
1141 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1143 assert User.following?(follower, followed) == true
1146 File.read!("test/fixtures/mastodon-reject-activity.json")
1148 |> Map.put("actor", followed.ap_id)
1149 |> Map.put("object", follow_activity.data["id"])
1151 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1153 follower = User.get_cached_by_id(follower.id)
1155 assert User.following?(follower, followed) == false
1158 test "it rejects activities without a valid ID" do
1159 user = insert(:user)
1162 File.read!("test/fixtures/mastodon-follow-activity.json")
1164 |> Map.put("object", user.ap_id)
1165 |> Map.put("id", "")
1167 :error = Transmogrifier.handle_incoming(data)
1170 test "it remaps video URLs as attachments if necessary" do
1172 Fetcher.fetch_object_from_id(
1173 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1178 "mediaType" => "video/mp4",
1180 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1181 "mimeType" => "video/mp4",
1182 "size" => 5_015_880,
1186 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1187 "mediaType" => "video/mp4",
1194 assert object.data["url"] ==
1195 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1197 assert object.data["attachment"] == [attachment]
1200 test "it accepts Flag activities" do
1201 user = insert(:user)
1202 other_user = insert(:user)
1204 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1205 object = Object.normalize(activity)
1209 "id" => activity.data["id"],
1210 "content" => "test post",
1211 "published" => object.data["published"],
1212 "actor" => AccountView.render("show.json", %{user: user})
1216 "@context" => "https://www.w3.org/ns/activitystreams",
1217 "cc" => [user.ap_id],
1218 "object" => [user.ap_id, activity.data["id"]],
1220 "content" => "blocked AND reported!!!",
1221 "actor" => other_user.ap_id
1224 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1226 assert activity.data["object"] == [user.ap_id, note_obj]
1227 assert activity.data["content"] == "blocked AND reported!!!"
1228 assert activity.data["actor"] == other_user.ap_id
1229 assert activity.data["cc"] == [user.ap_id]
1232 test "it correctly processes messages with non-array to field" do
1233 user = insert(:user)
1236 "@context" => "https://www.w3.org/ns/activitystreams",
1237 "to" => "https://www.w3.org/ns/activitystreams#Public",
1240 "content" => "blah blah blah",
1242 "attributedTo" => user.ap_id,
1245 "actor" => user.ap_id
1248 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1250 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1253 test "it correctly processes messages with non-array cc field" do
1254 user = insert(:user)
1257 "@context" => "https://www.w3.org/ns/activitystreams",
1258 "to" => user.follower_address,
1259 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1262 "content" => "blah blah blah",
1264 "attributedTo" => user.ap_id,
1267 "actor" => user.ap_id
1270 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1272 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1273 assert [user.follower_address] == activity.data["to"]
1277 describe "prepare outgoing" do
1278 test "it inlines private announced objects" do
1279 user = insert(:user)
1281 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1283 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1285 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1287 assert modified["object"]["content"] == "hey"
1288 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1291 test "it turns mentions into tags" do
1292 user = insert(:user)
1293 other_user = insert(:user)
1296 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1298 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1299 object = modified["object"]
1301 expected_mention = %{
1302 "href" => other_user.ap_id,
1303 "name" => "@#{other_user.nickname}",
1308 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1309 "type" => "Hashtag",
1313 assert Enum.member?(object["tag"], expected_tag)
1314 assert Enum.member?(object["tag"], expected_mention)
1317 test "it adds the sensitive property" do
1318 user = insert(:user)
1320 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1321 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1323 assert modified["object"]["sensitive"]
1326 test "it adds the json-ld context and the conversation property" do
1327 user = insert(:user)
1329 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1330 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1332 assert modified["@context"] ==
1333 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1335 assert modified["object"]["conversation"] == modified["context"]
1338 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1339 user = insert(:user)
1341 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1342 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1344 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1347 test "it strips internal hashtag data" do
1348 user = insert(:user)
1350 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1353 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1354 "type" => "Hashtag",
1358 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1360 assert modified["object"]["tag"] == [expected_tag]
1363 test "it strips internal fields" do
1364 user = insert(:user)
1366 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1368 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1370 assert length(modified["object"]["tag"]) == 2
1372 assert is_nil(modified["object"]["emoji"])
1373 assert is_nil(modified["object"]["like_count"])
1374 assert is_nil(modified["object"]["announcements"])
1375 assert is_nil(modified["object"]["announcement_count"])
1376 assert is_nil(modified["object"]["context_id"])
1379 test "it strips internal fields of article" do
1380 activity = insert(:article_activity)
1382 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1384 assert length(modified["object"]["tag"]) == 2
1386 assert is_nil(modified["object"]["emoji"])
1387 assert is_nil(modified["object"]["like_count"])
1388 assert is_nil(modified["object"]["announcements"])
1389 assert is_nil(modified["object"]["announcement_count"])
1390 assert is_nil(modified["object"]["context_id"])
1391 assert is_nil(modified["object"]["likes"])
1394 test "the directMessage flag is present" do
1395 user = insert(:user)
1396 other_user = insert(:user)
1398 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1400 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1402 assert modified["directMessage"] == false
1405 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1407 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1409 assert modified["directMessage"] == false
1412 CommonAPI.post(user, %{
1413 "status" => "@#{other_user.nickname} :moominmamma:",
1414 "visibility" => "direct"
1417 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1419 assert modified["directMessage"] == true
1422 test "it strips BCC field" do
1423 user = insert(:user)
1424 {:ok, list} = Pleroma.List.create("foo", user)
1427 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1429 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1431 assert is_nil(modified["bcc"])
1434 test "it can handle Listen activities" do
1435 listen_activity = insert(:listen)
1437 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1439 assert modified["type"] == "Listen"
1441 user = insert(:user)
1443 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1445 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1449 describe "user upgrade" do
1450 test "it upgrades a user to activitypub" do
1453 nickname: "rye@niu.moe",
1455 ap_id: "https://niu.moe/users/rye",
1456 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1459 user_two = insert(:user)
1460 Pleroma.FollowingRelationship.follow(user_two, user, "accept")
1462 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1463 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1464 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1466 user = User.get_cached_by_id(user.id)
1467 assert user.note_count == 1
1469 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1470 ObanHelpers.perform_all()
1472 assert user.ap_enabled
1473 assert user.note_count == 1
1474 assert user.follower_address == "https://niu.moe/users/rye/followers"
1475 assert user.following_address == "https://niu.moe/users/rye/following"
1477 user = User.get_cached_by_id(user.id)
1478 assert user.note_count == 1
1480 activity = Activity.get_by_id(activity.id)
1481 assert user.follower_address in activity.recipients
1487 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1496 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1501 refute "..." in activity.recipients
1503 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1504 refute user.follower_address in unrelated_activity.recipients
1506 user_two = User.get_cached_by_id(user_two.id)
1507 assert User.following?(user_two, user)
1508 refute "..." in User.following(user_two)
1512 describe "actor rewriting" do
1513 test "it fixes the actor URL property to be a proper URI" do
1515 "url" => %{"href" => "http://example.com"}
1518 rewritten = Transmogrifier.maybe_fix_user_object(data)
1519 assert rewritten["url"] == "http://example.com"
1523 describe "actor origin containment" do
1524 test "it rejects activities which reference objects with bogus origins" do
1526 "@context" => "https://www.w3.org/ns/activitystreams",
1527 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1528 "actor" => "http://mastodon.example.org/users/admin",
1529 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1530 "object" => "https://info.pleroma.site/activity.json",
1531 "type" => "Announce"
1534 assert capture_log(fn ->
1535 :error = Transmogrifier.handle_incoming(data)
1536 end) =~ "Object containment failed"
1539 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1541 "@context" => "https://www.w3.org/ns/activitystreams",
1542 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1543 "actor" => "http://mastodon.example.org/users/admin",
1544 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1545 "object" => "https://info.pleroma.site/activity2.json",
1546 "type" => "Announce"
1549 assert capture_log(fn ->
1550 :error = Transmogrifier.handle_incoming(data)
1551 end) =~ "Object containment failed"
1554 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1556 "@context" => "https://www.w3.org/ns/activitystreams",
1557 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1558 "actor" => "http://mastodon.example.org/users/admin",
1559 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1560 "object" => "https://info.pleroma.site/activity3.json",
1561 "type" => "Announce"
1564 assert capture_log(fn ->
1565 :error = Transmogrifier.handle_incoming(data)
1566 end) =~ "Object containment failed"
1570 describe "reserialization" do
1571 test "successfully reserializes a message with inReplyTo == nil" do
1572 user = insert(:user)
1575 "@context" => "https://www.w3.org/ns/activitystreams",
1576 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1580 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1585 "attributedTo" => user.ap_id
1587 "actor" => user.ap_id
1590 {:ok, activity} = Transmogrifier.handle_incoming(message)
1592 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1595 test "successfully reserializes a message with AS2 objects in IR" do
1596 user = insert(:user)
1599 "@context" => "https://www.w3.org/ns/activitystreams",
1600 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1604 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1609 "attributedTo" => user.ap_id,
1611 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1612 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1615 "actor" => user.ap_id
1618 {:ok, activity} = Transmogrifier.handle_incoming(message)
1620 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1624 test "Rewrites Answers to Notes" do
1625 user = insert(:user)
1627 {:ok, poll_activity} =
1628 CommonAPI.post(user, %{
1629 "status" => "suya...",
1630 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1633 poll_object = Object.normalize(poll_activity)
1634 # TODO: Replace with CommonAPI vote creation when implemented
1636 File.read!("test/fixtures/mastodon-vote.json")
1638 |> Kernel.put_in(["to"], user.ap_id)
1639 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1640 |> Kernel.put_in(["object", "to"], user.ap_id)
1642 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1643 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1645 assert data["object"]["type"] == "Note"
1648 describe "fix_explicit_addressing" do
1650 user = insert(:user)
1654 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1655 explicitly_mentioned_actors = [
1656 "https://pleroma.gold/users/user1",
1657 "https://pleroma.gold/user2"
1661 "actor" => user.ap_id,
1662 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1665 Enum.map(explicitly_mentioned_actors, fn href ->
1666 %{"type" => "Mention", "href" => href}
1670 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1671 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1672 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1673 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1676 test "does not move actor's follower collection to cc", %{user: user} do
1678 "actor" => user.ap_id,
1679 "to" => [user.follower_address],
1683 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1684 assert user.follower_address in fixed_object["to"]
1685 refute user.follower_address in fixed_object["cc"]
1688 test "removes recipient's follower collection from cc", %{user: user} do
1689 recipient = insert(:user)
1692 "actor" => user.ap_id,
1693 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1694 "cc" => [user.follower_address, recipient.follower_address]
1697 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1699 assert user.follower_address in fixed_object["cc"]
1700 refute recipient.follower_address in fixed_object["cc"]
1701 refute recipient.follower_address in fixed_object["to"]
1705 describe "fix_summary/1" do
1706 test "returns fixed object" do
1707 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1708 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1709 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1713 describe "fix_in_reply_to/2" do
1714 clear_config([:instance, :federation_incoming_replies_max_depth])
1717 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1721 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1722 assert Transmogrifier.fix_in_reply_to(data) == data
1725 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1726 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1729 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1731 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1732 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1733 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1736 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1738 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1739 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1740 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1743 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1745 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1746 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1747 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1749 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1750 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1751 assert modified_object["inReplyTo"] == []
1752 assert modified_object["inReplyToAtomUri"] == ""
1755 @tag capture_log: true
1756 test "returns modified object when allowed incoming reply", %{data: data} do
1761 "https://shitposter.club/notice/2827873"
1764 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1765 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1767 assert modified_object["inReplyTo"] ==
1768 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1770 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1772 assert modified_object["conversation"] ==
1773 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1775 assert modified_object["context"] ==
1776 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1780 describe "fix_url/1" do
1781 test "fixes data for object when url is map" do
1785 "mimeType" => "video/mp4",
1786 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1790 assert Transmogrifier.fix_url(object) == %{
1791 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1795 test "fixes data for video object" do
1801 "mimeType" => "video/mp4",
1802 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1806 "mimeType" => "video/mp4",
1807 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1811 "mimeType" => "text/html",
1812 "href" => "https://peertube.-2d4c2d1630e3"
1816 "mimeType" => "text/html",
1817 "href" => "https://peertube.-2d4c2d16377-42"
1822 assert Transmogrifier.fix_url(object) == %{
1825 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1826 "mimeType" => "video/mp4",
1831 "url" => "https://peertube.-2d4c2d1630e3"
1835 test "fixes url for not Video object" do
1841 "mimeType" => "text/html",
1842 "href" => "https://peertube.-2d4c2d1630e3"
1846 "mimeType" => "text/html",
1847 "href" => "https://peertube.-2d4c2d16377-42"
1852 assert Transmogrifier.fix_url(object) == %{
1854 "url" => "https://peertube.-2d4c2d1630e3"
1857 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1863 test "retunrs not modified object" do
1864 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1868 describe "get_obj_helper/2" do
1869 test "returns nil when cannot normalize object" do
1870 assert capture_log(fn ->
1871 refute Transmogrifier.get_obj_helper("test-obj-id")
1872 end) =~ "Unsupported URI scheme"
1875 @tag capture_log: true
1876 test "returns {:ok, %Object{}} for success case" do
1877 assert {:ok, %Object{}} =
1878 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1882 describe "fix_attachments/1" do
1883 test "returns not modified object" do
1884 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1885 assert Transmogrifier.fix_attachments(data) == data
1888 test "returns modified object when attachment is map" do
1889 assert Transmogrifier.fix_attachments(%{
1891 "mediaType" => "video/mp4",
1892 "url" => "https://peertube.moe/stat-480.mp4"
1897 "mediaType" => "video/mp4",
1900 "href" => "https://peertube.moe/stat-480.mp4",
1901 "mediaType" => "video/mp4",
1910 test "returns modified object when attachment is list" do
1911 assert Transmogrifier.fix_attachments(%{
1913 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1914 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1919 "mediaType" => "video/mp4",
1922 "href" => "https://pe.er/stat-480.mp4",
1923 "mediaType" => "video/mp4",
1929 "href" => "https://pe.er/stat-480.mp4",
1930 "mediaType" => "video/mp4",
1931 "mimeType" => "video/mp4",
1934 "href" => "https://pe.er/stat-480.mp4",
1935 "mediaType" => "video/mp4",
1945 describe "fix_emoji/1" do
1946 test "returns not modified object when object not contains tags" do
1947 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1948 assert Transmogrifier.fix_emoji(data) == data
1951 test "returns object with emoji when object contains list tags" do
1952 assert Transmogrifier.fix_emoji(%{
1954 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1955 %{"type" => "Hashtag"}
1958 "emoji" => %{"bib" => "/test"},
1960 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1961 %{"type" => "Hashtag"}
1966 test "returns object with emoji when object contains map tag" do
1967 assert Transmogrifier.fix_emoji(%{
1968 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1970 "emoji" => %{"bib" => "/test"},
1971 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}