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
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Transmogrifier
15 alias Pleroma.Web.CommonAPI
16 alias Pleroma.Web.OStatus
17 alias Pleroma.Web.Websub.WebsubClientSubscription
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 clear_config([:instance, :max_remote_account_fields])
30 describe "handle_incoming" do
31 test "it ignores an incoming notice if we already have it" do
32 activity = insert(:note_activity)
35 File.read!("test/fixtures/mastodon-post-activity.json")
37 |> Map.put("object", Object.normalize(activity).data)
39 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
41 assert activity == returned_activity
44 test "it fetches replied-to activities if we don't have them" do
46 File.read!("test/fixtures/mastodon-post-activity.json")
51 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
53 data = Map.put(data, "object", object)
54 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
55 returned_object = Object.normalize(returned_activity, false)
58 Activity.get_create_by_object_ap_id(
59 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
62 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
65 test "it does not fetch replied-to activities beyond max_replies_depth" do
67 File.read!("test/fixtures/mastodon-post-activity.json")
72 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
74 data = Map.put(data, "object", object)
76 with_mock Pleroma.Web.Federator,
77 allowed_incoming_reply_depth?: fn _ -> false end do
78 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
80 returned_object = Object.normalize(returned_activity, false)
82 refute Activity.get_create_by_object_ap_id(
83 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
86 assert returned_object.data["inReplyToAtomUri"] ==
87 "https://shitposter.club/notice/2827873"
91 test "it does not crash if the object in inReplyTo can't be fetched" do
93 File.read!("test/fixtures/mastodon-post-activity.json")
98 |> Map.put("inReplyTo", "https://404.site/whatever")
102 |> Map.put("object", object)
104 assert capture_log(fn ->
105 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
106 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
109 test "it works for incoming notices" do
110 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
112 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
115 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
117 assert data["context"] ==
118 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
120 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
122 assert data["cc"] == [
123 "http://mastodon.example.org/users/admin/followers",
124 "http://localtesting.pleroma.lol/users/lain"
127 assert data["actor"] == "http://mastodon.example.org/users/admin"
129 object_data = Object.normalize(data["object"]).data
131 assert object_data["id"] ==
132 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
134 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
136 assert object_data["cc"] == [
137 "http://mastodon.example.org/users/admin/followers",
138 "http://localtesting.pleroma.lol/users/lain"
141 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
142 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
144 assert object_data["context"] ==
145 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
147 assert object_data["sensitive"] == true
149 user = User.get_cached_by_ap_id(object_data["actor"])
151 assert user.info.note_count == 1
154 test "it works for incoming notices with hashtags" do
155 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
157 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
158 object = Object.normalize(data["object"])
160 assert Enum.at(object.data["tag"], 2) == "moo"
163 test "it works for incoming questions" do
164 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
166 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
168 object = Object.normalize(activity)
170 assert Enum.all?(object.data["oneOf"], fn choice ->
173 "Everyone knows that!",
174 "25 char limit is dumb",
175 "I can't even fit a funny"
180 test "it works for incoming listens" do
182 "@context" => "https://www.w3.org/ns/activitystreams",
183 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
186 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
187 "actor" => "http://mastodon.example.org/users/admin",
190 "id" => "http://mastodon.example.org/users/admin/listens/1234",
191 "attributedTo" => "http://mastodon.example.org/users/admin",
192 "title" => "lain radio episode 1",
194 "album" => "lain radio",
199 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
201 object = Object.normalize(activity)
203 assert object.data["title"] == "lain radio episode 1"
204 assert object.data["artist"] == "lain"
205 assert object.data["album"] == "lain radio"
206 assert object.data["length"] == 180_000
209 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
213 CommonAPI.post(user, %{
214 "status" => "suya...",
215 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
218 object = Object.normalize(activity)
221 File.read!("test/fixtures/mastodon-vote.json")
223 |> Kernel.put_in(["to"], user.ap_id)
224 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
225 |> Kernel.put_in(["object", "to"], user.ap_id)
227 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
228 answer_object = Object.normalize(activity)
229 assert answer_object.data["type"] == "Answer"
230 object = Object.get_by_ap_id(object.data["id"])
233 object.data["oneOf"],
235 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
241 test "it works for incoming notices with contentMap" do
243 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
245 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
246 object = Object.normalize(data["object"])
248 assert object.data["content"] ==
249 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
252 test "it works for incoming notices with to/cc not being an array (kroeg)" do
253 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
255 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
256 object = Object.normalize(data["object"])
258 assert object.data["content"] ==
259 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
262 test "it works for incoming announces with actor being inlined (kroeg)" do
263 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
265 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
267 assert data["actor"] == "https://puckipedia.com/"
270 test "it works for incoming notices with tag not being an array (kroeg)" do
271 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
273 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
274 object = Object.normalize(data["object"])
276 assert object.data["emoji"] == %{
277 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
280 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
282 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
283 object = Object.normalize(data["object"])
285 assert "test" in object.data["tag"]
288 test "it works for incoming notices with url not being a string (prismo)" do
289 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
291 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
292 object = Object.normalize(data["object"])
294 assert object.data["url"] == "https://prismo.news/posts/83"
297 test "it cleans up incoming notices which are not really DMs" do
299 other_user = insert(:user)
301 to = [user.ap_id, other_user.ap_id]
304 File.read!("test/fixtures/mastodon-post-activity.json")
314 data = Map.put(data, "object", object)
316 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
318 assert data["to"] == []
319 assert data["cc"] == to
321 object_data = Object.normalize(activity).data
323 assert object_data["to"] == []
324 assert object_data["cc"] == to
327 test "it works for incoming likes" do
329 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
332 File.read!("test/fixtures/mastodon-like.json")
334 |> Map.put("object", activity.data["object"])
336 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
338 assert data["actor"] == "http://mastodon.example.org/users/admin"
339 assert data["type"] == "Like"
340 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
341 assert data["object"] == activity.data["object"]
344 test "it works for incoming misskey likes, turning them into EmojiReactions" do
346 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
349 File.read!("test/fixtures/misskey-like.json")
351 |> Map.put("object", activity.data["object"])
353 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
355 assert data["actor"] == data["actor"]
356 assert data["type"] == "EmojiReaction"
357 assert data["id"] == data["id"]
358 assert data["object"] == activity.data["object"]
359 assert data["content"] == "🍮"
362 test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReactions" do
364 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
367 File.read!("test/fixtures/misskey-like.json")
369 |> Map.put("object", activity.data["object"])
370 |> Map.put("_misskey_reaction", "⭐")
372 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
374 assert data["actor"] == data["actor"]
375 assert data["type"] == "EmojiReaction"
376 assert data["id"] == data["id"]
377 assert data["object"] == activity.data["object"]
378 assert data["content"] == "⭐"
381 test "it works for incoming emoji reactions" do
383 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
386 File.read!("test/fixtures/emoji-reaction.json")
388 |> Map.put("object", activity.data["object"])
390 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
392 assert data["actor"] == "http://mastodon.example.org/users/admin"
393 assert data["type"] == "EmojiReaction"
394 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
395 assert data["object"] == activity.data["object"]
396 assert data["content"] == "👌"
399 test "it works for incoming emoji reaction undos" do
402 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
403 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
406 File.read!("test/fixtures/mastodon-undo-like.json")
408 |> Map.put("object", reaction_activity.data["id"])
409 |> Map.put("actor", user.ap_id)
411 {:ok, activity} = Transmogrifier.handle_incoming(data)
413 assert activity.actor == user.ap_id
414 assert activity.data["id"] == data["id"]
415 assert activity.data["type"] == "Undo"
418 test "it returns an error for incoming unlikes wihout a like activity" do
420 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
423 File.read!("test/fixtures/mastodon-undo-like.json")
425 |> Map.put("object", activity.data["object"])
427 assert Transmogrifier.handle_incoming(data) == :error
430 test "it works for incoming unlikes with an existing like activity" do
432 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
435 File.read!("test/fixtures/mastodon-like.json")
437 |> Map.put("object", activity.data["object"])
439 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
442 File.read!("test/fixtures/mastodon-undo-like.json")
444 |> Map.put("object", like_data)
445 |> Map.put("actor", like_data["actor"])
447 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
449 assert data["actor"] == "http://mastodon.example.org/users/admin"
450 assert data["type"] == "Undo"
451 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
452 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
455 test "it works for incoming unlikes with an existing like activity and a compact object" do
457 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
460 File.read!("test/fixtures/mastodon-like.json")
462 |> Map.put("object", activity.data["object"])
464 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
467 File.read!("test/fixtures/mastodon-undo-like.json")
469 |> Map.put("object", like_data["id"])
470 |> Map.put("actor", like_data["actor"])
472 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
474 assert data["actor"] == "http://mastodon.example.org/users/admin"
475 assert data["type"] == "Undo"
476 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
477 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
480 test "it works for incoming announces" do
481 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
483 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
485 assert data["actor"] == "http://mastodon.example.org/users/admin"
486 assert data["type"] == "Announce"
489 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
491 assert data["object"] ==
492 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
494 assert Activity.get_create_by_object_ap_id(data["object"])
497 test "it works for incoming announces with an existing activity" do
499 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
502 File.read!("test/fixtures/mastodon-announce.json")
504 |> Map.put("object", activity.data["object"])
506 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
508 assert data["actor"] == "http://mastodon.example.org/users/admin"
509 assert data["type"] == "Announce"
512 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
514 assert data["object"] == activity.data["object"]
516 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
519 test "it works for incoming announces with an inlined activity" do
521 File.read!("test/fixtures/mastodon-announce-private.json")
524 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
526 assert data["actor"] == "http://mastodon.example.org/users/admin"
527 assert data["type"] == "Announce"
530 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
532 object = Object.normalize(data["object"])
534 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
535 assert object.data["content"] == "this is a private toot"
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.info.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.Info.fields(user.info) == [
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.Info.fields(user.info) == [
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.Info.fields(user.info) == [
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.Info.fields(user.info) == []
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.info.locked == true
771 test "it works for incoming deletes" do
772 activity = insert(:note_activity)
775 File.read!("test/fixtures/mastodon-delete.json")
780 |> Map.put("id", activity.data["object"])
784 |> Map.put("object", object)
785 |> Map.put("actor", activity.data["actor"])
787 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
789 refute Activity.get_by_id(activity.id)
792 test "it fails for incoming deletes with spoofed origin" do
793 activity = insert(:note_activity)
796 File.read!("test/fixtures/mastodon-delete.json")
801 |> Map.put("id", activity.data["object"])
805 |> Map.put("object", object)
807 assert capture_log(fn ->
808 :error = Transmogrifier.handle_incoming(data)
810 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
812 assert Activity.get_by_id(activity.id)
815 test "it works for incoming user deletes" do
816 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
819 File.read!("test/fixtures/mastodon-delete-user.json")
822 {:ok, _} = Transmogrifier.handle_incoming(data)
823 ObanHelpers.perform_all()
825 refute User.get_cached_by_ap_id(ap_id)
828 test "it fails for incoming user deletes with spoofed origin" do
829 %{ap_id: ap_id} = insert(:user)
832 File.read!("test/fixtures/mastodon-delete-user.json")
834 |> Map.put("actor", ap_id)
836 assert :error == Transmogrifier.handle_incoming(data)
837 assert User.get_cached_by_ap_id(ap_id)
840 test "it works for incoming unannounces with an existing notice" do
842 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
845 File.read!("test/fixtures/mastodon-announce.json")
847 |> Map.put("object", activity.data["object"])
849 {:ok, %Activity{data: announce_data, local: false}} =
850 Transmogrifier.handle_incoming(announce_data)
853 File.read!("test/fixtures/mastodon-undo-announce.json")
855 |> Map.put("object", announce_data)
856 |> Map.put("actor", announce_data["actor"])
858 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
860 assert data["type"] == "Undo"
861 assert object_data = data["object"]
862 assert object_data["type"] == "Announce"
863 assert object_data["object"] == activity.data["object"]
865 assert object_data["id"] ==
866 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
869 test "it works for incomming unfollows with an existing follow" do
873 File.read!("test/fixtures/mastodon-follow-activity.json")
875 |> Map.put("object", user.ap_id)
877 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
880 File.read!("test/fixtures/mastodon-unfollow-activity.json")
882 |> Map.put("object", follow_data)
884 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
886 assert data["type"] == "Undo"
887 assert data["object"]["type"] == "Follow"
888 assert data["object"]["object"] == user.ap_id
889 assert data["actor"] == "http://mastodon.example.org/users/admin"
891 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
894 test "it works for incoming blocks" do
898 File.read!("test/fixtures/mastodon-block-activity.json")
900 |> Map.put("object", user.ap_id)
902 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
904 assert data["type"] == "Block"
905 assert data["object"] == user.ap_id
906 assert data["actor"] == "http://mastodon.example.org/users/admin"
908 blocker = User.get_cached_by_ap_id(data["actor"])
910 assert User.blocks?(blocker, user)
913 test "incoming blocks successfully tear down any follow relationship" do
914 blocker = insert(:user)
915 blocked = insert(:user)
918 File.read!("test/fixtures/mastodon-block-activity.json")
920 |> Map.put("object", blocked.ap_id)
921 |> Map.put("actor", blocker.ap_id)
923 {:ok, blocker} = User.follow(blocker, blocked)
924 {:ok, blocked} = User.follow(blocked, blocker)
926 assert User.following?(blocker, blocked)
927 assert User.following?(blocked, blocker)
929 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
931 assert data["type"] == "Block"
932 assert data["object"] == blocked.ap_id
933 assert data["actor"] == blocker.ap_id
935 blocker = User.get_cached_by_ap_id(data["actor"])
936 blocked = User.get_cached_by_ap_id(data["object"])
938 assert User.blocks?(blocker, blocked)
940 refute User.following?(blocker, blocked)
941 refute User.following?(blocked, blocker)
944 test "it works for incoming unblocks with an existing block" do
948 File.read!("test/fixtures/mastodon-block-activity.json")
950 |> Map.put("object", user.ap_id)
952 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
955 File.read!("test/fixtures/mastodon-unblock-activity.json")
957 |> Map.put("object", block_data)
959 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
960 assert data["type"] == "Undo"
961 assert data["object"]["type"] == "Block"
962 assert data["object"]["object"] == user.ap_id
963 assert data["actor"] == "http://mastodon.example.org/users/admin"
965 blocker = User.get_cached_by_ap_id(data["actor"])
967 refute User.blocks?(blocker, user)
970 test "it works for incoming accepts which were pre-accepted" do
971 follower = insert(:user)
972 followed = insert(:user)
974 {:ok, follower} = User.follow(follower, followed)
975 assert User.following?(follower, followed) == true
977 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
980 File.read!("test/fixtures/mastodon-accept-activity.json")
982 |> Map.put("actor", followed.ap_id)
985 accept_data["object"]
986 |> Map.put("actor", follower.ap_id)
987 |> Map.put("id", follow_activity.data["id"])
989 accept_data = Map.put(accept_data, "object", object)
991 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
992 refute activity.local
994 assert activity.data["object"] == follow_activity.data["id"]
996 follower = User.get_cached_by_id(follower.id)
998 assert User.following?(follower, followed) == true
1001 test "it works for incoming accepts which were orphaned" do
1002 follower = insert(:user)
1003 followed = insert(:user, %{info: %User.Info{locked: true}})
1005 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1008 File.read!("test/fixtures/mastodon-accept-activity.json")
1010 |> Map.put("actor", followed.ap_id)
1013 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1015 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1016 assert activity.data["object"] == follow_activity.data["id"]
1018 follower = User.get_cached_by_id(follower.id)
1020 assert User.following?(follower, followed) == true
1023 test "it works for incoming accepts which are referenced by IRI only" do
1024 follower = insert(:user)
1025 followed = insert(:user, %{info: %User.Info{locked: true}})
1027 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1030 File.read!("test/fixtures/mastodon-accept-activity.json")
1032 |> Map.put("actor", followed.ap_id)
1033 |> Map.put("object", follow_activity.data["id"])
1035 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1036 assert activity.data["object"] == follow_activity.data["id"]
1038 follower = User.get_cached_by_id(follower.id)
1040 assert User.following?(follower, followed) == true
1043 test "it fails for incoming accepts which cannot be correlated" do
1044 follower = insert(:user)
1045 followed = insert(:user, %{info: %User.Info{locked: true}})
1048 File.read!("test/fixtures/mastodon-accept-activity.json")
1050 |> Map.put("actor", followed.ap_id)
1053 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1055 :error = Transmogrifier.handle_incoming(accept_data)
1057 follower = User.get_cached_by_id(follower.id)
1059 refute User.following?(follower, followed) == true
1062 test "it fails for incoming rejects which cannot be correlated" do
1063 follower = insert(:user)
1064 followed = insert(:user, %{info: %User.Info{locked: true}})
1067 File.read!("test/fixtures/mastodon-reject-activity.json")
1069 |> Map.put("actor", followed.ap_id)
1072 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1074 :error = Transmogrifier.handle_incoming(accept_data)
1076 follower = User.get_cached_by_id(follower.id)
1078 refute User.following?(follower, followed) == true
1081 test "it works for incoming rejects which are orphaned" do
1082 follower = insert(:user)
1083 followed = insert(:user, %{info: %User.Info{locked: true}})
1085 {:ok, follower} = User.follow(follower, followed)
1086 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1088 assert User.following?(follower, followed) == true
1091 File.read!("test/fixtures/mastodon-reject-activity.json")
1093 |> Map.put("actor", followed.ap_id)
1096 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1098 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1099 refute activity.local
1101 follower = User.get_cached_by_id(follower.id)
1103 assert User.following?(follower, followed) == false
1106 test "it works for incoming rejects which are referenced by IRI only" do
1107 follower = insert(:user)
1108 followed = insert(:user, %{info: %User.Info{locked: true}})
1110 {:ok, follower} = User.follow(follower, followed)
1111 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1113 assert User.following?(follower, followed) == true
1116 File.read!("test/fixtures/mastodon-reject-activity.json")
1118 |> Map.put("actor", followed.ap_id)
1119 |> Map.put("object", follow_activity.data["id"])
1121 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1123 follower = User.get_cached_by_id(follower.id)
1125 assert User.following?(follower, followed) == false
1128 test "it rejects activities without a valid ID" do
1129 user = insert(:user)
1132 File.read!("test/fixtures/mastodon-follow-activity.json")
1134 |> Map.put("object", user.ap_id)
1135 |> Map.put("id", "")
1137 :error = Transmogrifier.handle_incoming(data)
1140 test "it remaps video URLs as attachments if necessary" do
1142 Fetcher.fetch_object_from_id(
1143 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1148 "mediaType" => "video/mp4",
1150 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1151 "mimeType" => "video/mp4",
1152 "size" => 5_015_880,
1156 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1157 "mediaType" => "video/mp4",
1164 assert object.data["url"] ==
1165 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1167 assert object.data["attachment"] == [attachment]
1170 test "it accepts Flag activities" do
1171 user = insert(:user)
1172 other_user = insert(:user)
1174 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1175 object = Object.normalize(activity)
1178 "@context" => "https://www.w3.org/ns/activitystreams",
1179 "cc" => [user.ap_id],
1180 "object" => [user.ap_id, object.data["id"]],
1182 "content" => "blocked AND reported!!!",
1183 "actor" => other_user.ap_id
1186 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1188 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1189 assert activity.data["content"] == "blocked AND reported!!!"
1190 assert activity.data["actor"] == other_user.ap_id
1191 assert activity.data["cc"] == [user.ap_id]
1195 describe "prepare outgoing" do
1196 test "it inlines private announced objects" do
1197 user = insert(:user)
1199 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1201 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1203 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1205 assert modified["object"]["content"] == "hey"
1206 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1209 test "it turns mentions into tags" do
1210 user = insert(:user)
1211 other_user = insert(:user)
1214 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1216 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1217 object = modified["object"]
1219 expected_mention = %{
1220 "href" => other_user.ap_id,
1221 "name" => "@#{other_user.nickname}",
1226 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1227 "type" => "Hashtag",
1231 assert Enum.member?(object["tag"], expected_tag)
1232 assert Enum.member?(object["tag"], expected_mention)
1235 test "it adds the sensitive property" do
1236 user = insert(:user)
1238 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1239 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1241 assert modified["object"]["sensitive"]
1244 test "it adds the json-ld context and the conversation property" do
1245 user = insert(:user)
1247 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1248 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1250 assert modified["@context"] ==
1251 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1253 assert modified["object"]["conversation"] == modified["context"]
1256 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1257 user = insert(:user)
1259 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1260 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1262 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1265 test "it translates ostatus IDs to external URLs" do
1266 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1267 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1269 user = insert(:user)
1271 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1272 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1274 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1277 test "it translates ostatus reply_to IDs to external URLs" do
1278 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1279 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1281 user = insert(:user)
1284 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1286 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1288 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1291 test "it strips internal hashtag data" do
1292 user = insert(:user)
1294 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1297 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1298 "type" => "Hashtag",
1302 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1304 assert modified["object"]["tag"] == [expected_tag]
1307 test "it strips internal fields" do
1308 user = insert(:user)
1310 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1312 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1314 assert length(modified["object"]["tag"]) == 2
1316 assert is_nil(modified["object"]["emoji"])
1317 assert is_nil(modified["object"]["like_count"])
1318 assert is_nil(modified["object"]["announcements"])
1319 assert is_nil(modified["object"]["announcement_count"])
1320 assert is_nil(modified["object"]["context_id"])
1323 test "it strips internal fields of article" do
1324 activity = insert(:article_activity)
1326 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1328 assert length(modified["object"]["tag"]) == 2
1330 assert is_nil(modified["object"]["emoji"])
1331 assert is_nil(modified["object"]["like_count"])
1332 assert is_nil(modified["object"]["announcements"])
1333 assert is_nil(modified["object"]["announcement_count"])
1334 assert is_nil(modified["object"]["context_id"])
1335 assert is_nil(modified["object"]["likes"])
1338 test "the directMessage flag is present" do
1339 user = insert(:user)
1340 other_user = insert(:user)
1342 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1344 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1346 assert modified["directMessage"] == false
1349 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1351 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1353 assert modified["directMessage"] == false
1356 CommonAPI.post(user, %{
1357 "status" => "@#{other_user.nickname} :moominmamma:",
1358 "visibility" => "direct"
1361 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1363 assert modified["directMessage"] == true
1366 test "it strips BCC field" do
1367 user = insert(:user)
1368 {:ok, list} = Pleroma.List.create("foo", user)
1371 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1373 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1375 assert is_nil(modified["bcc"])
1378 test "it can handle Listen activities" do
1379 listen_activity = insert(:listen)
1381 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1383 assert modified["type"] == "Listen"
1385 user = insert(:user)
1387 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1389 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1393 describe "user upgrade" do
1394 test "it upgrades a user to activitypub" do
1397 nickname: "rye@niu.moe",
1399 ap_id: "https://niu.moe/users/rye",
1400 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1403 user_two = insert(:user, %{following: [user.follower_address]})
1405 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1406 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1407 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1409 user = User.get_cached_by_id(user.id)
1410 assert user.info.note_count == 1
1412 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1413 ObanHelpers.perform_all()
1415 assert user.info.ap_enabled
1416 assert user.info.note_count == 1
1417 assert user.follower_address == "https://niu.moe/users/rye/followers"
1418 assert user.following_address == "https://niu.moe/users/rye/following"
1420 user = User.get_cached_by_id(user.id)
1421 assert user.info.note_count == 1
1423 activity = Activity.get_by_id(activity.id)
1424 assert user.follower_address in activity.recipients
1430 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1439 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1442 } = user.info.banner
1444 refute "..." in activity.recipients
1446 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1447 refute user.follower_address in unrelated_activity.recipients
1449 user_two = User.get_cached_by_id(user_two.id)
1450 assert user.follower_address in user_two.following
1451 refute "..." in user_two.following
1455 describe "maybe_retire_websub" do
1456 test "it deletes all websub client subscripitions with the user as topic" do
1457 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1458 {:ok, ws} = Repo.insert(subscription)
1460 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1461 {:ok, ws2} = Repo.insert(subscription)
1463 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1465 refute Repo.get(WebsubClientSubscription, ws.id)
1466 assert Repo.get(WebsubClientSubscription, ws2.id)
1470 describe "actor rewriting" do
1471 test "it fixes the actor URL property to be a proper URI" do
1473 "url" => %{"href" => "http://example.com"}
1476 rewritten = Transmogrifier.maybe_fix_user_object(data)
1477 assert rewritten["url"] == "http://example.com"
1481 describe "actor origin containment" do
1482 test "it rejects activities which reference objects with bogus origins" do
1484 "@context" => "https://www.w3.org/ns/activitystreams",
1485 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1486 "actor" => "http://mastodon.example.org/users/admin",
1487 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1488 "object" => "https://info.pleroma.site/activity.json",
1489 "type" => "Announce"
1492 :error = Transmogrifier.handle_incoming(data)
1495 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1497 "@context" => "https://www.w3.org/ns/activitystreams",
1498 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1499 "actor" => "http://mastodon.example.org/users/admin",
1500 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1501 "object" => "https://info.pleroma.site/activity2.json",
1502 "type" => "Announce"
1505 :error = Transmogrifier.handle_incoming(data)
1508 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1510 "@context" => "https://www.w3.org/ns/activitystreams",
1511 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1512 "actor" => "http://mastodon.example.org/users/admin",
1513 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1514 "object" => "https://info.pleroma.site/activity3.json",
1515 "type" => "Announce"
1518 :error = Transmogrifier.handle_incoming(data)
1522 describe "reserialization" do
1523 test "successfully reserializes a message with inReplyTo == nil" do
1524 user = insert(:user)
1527 "@context" => "https://www.w3.org/ns/activitystreams",
1528 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1532 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1537 "attributedTo" => user.ap_id
1539 "actor" => user.ap_id
1542 {:ok, activity} = Transmogrifier.handle_incoming(message)
1544 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1547 test "successfully reserializes a message with AS2 objects in IR" do
1548 user = insert(:user)
1551 "@context" => "https://www.w3.org/ns/activitystreams",
1552 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1556 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1561 "attributedTo" => user.ap_id,
1563 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1564 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1567 "actor" => user.ap_id
1570 {:ok, activity} = Transmogrifier.handle_incoming(message)
1572 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1576 test "Rewrites Answers to Notes" do
1577 user = insert(:user)
1579 {:ok, poll_activity} =
1580 CommonAPI.post(user, %{
1581 "status" => "suya...",
1582 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1585 poll_object = Object.normalize(poll_activity)
1586 # TODO: Replace with CommonAPI vote creation when implemented
1588 File.read!("test/fixtures/mastodon-vote.json")
1590 |> Kernel.put_in(["to"], user.ap_id)
1591 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1592 |> Kernel.put_in(["object", "to"], user.ap_id)
1594 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1595 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1597 assert data["object"]["type"] == "Note"
1600 describe "fix_explicit_addressing" do
1602 user = insert(:user)
1606 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1607 explicitly_mentioned_actors = [
1608 "https://pleroma.gold/users/user1",
1609 "https://pleroma.gold/user2"
1613 "actor" => user.ap_id,
1614 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1617 Enum.map(explicitly_mentioned_actors, fn href ->
1618 %{"type" => "Mention", "href" => href}
1622 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1623 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1624 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1625 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1628 test "does not move actor's follower collection to cc", %{user: user} do
1630 "actor" => user.ap_id,
1631 "to" => [user.follower_address],
1635 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1636 assert user.follower_address in fixed_object["to"]
1637 refute user.follower_address in fixed_object["cc"]
1640 test "removes recipient's follower collection from cc", %{user: user} do
1641 recipient = insert(:user)
1644 "actor" => user.ap_id,
1645 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1646 "cc" => [user.follower_address, recipient.follower_address]
1649 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1651 assert user.follower_address in fixed_object["cc"]
1652 refute recipient.follower_address in fixed_object["cc"]
1653 refute recipient.follower_address in fixed_object["to"]
1657 describe "fix_summary/1" do
1658 test "returns fixed object" do
1659 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1660 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1661 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1665 describe "fix_in_reply_to/2" do
1666 clear_config([:instance, :federation_incoming_replies_max_depth])
1669 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1673 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1674 assert Transmogrifier.fix_in_reply_to(data) == data
1677 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1678 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1681 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1683 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1684 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1685 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1688 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1690 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1691 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1692 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1695 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1697 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1698 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1699 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1701 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1702 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1703 assert modified_object["inReplyTo"] == []
1704 assert modified_object["inReplyToAtomUri"] == ""
1707 test "returns modified object when allowed incoming reply", %{data: data} do
1712 "https://shitposter.club/notice/2827873"
1715 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1716 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1718 assert modified_object["inReplyTo"] ==
1719 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1721 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1723 assert modified_object["conversation"] ==
1724 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1726 assert modified_object["context"] ==
1727 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1731 describe "fix_url/1" do
1732 test "fixes data for object when url is map" do
1736 "mimeType" => "video/mp4",
1737 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1741 assert Transmogrifier.fix_url(object) == %{
1742 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1746 test "fixes data for video object" do
1752 "mimeType" => "video/mp4",
1753 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1757 "mimeType" => "video/mp4",
1758 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1762 "mimeType" => "text/html",
1763 "href" => "https://peertube.-2d4c2d1630e3"
1767 "mimeType" => "text/html",
1768 "href" => "https://peertube.-2d4c2d16377-42"
1773 assert Transmogrifier.fix_url(object) == %{
1776 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1777 "mimeType" => "video/mp4",
1782 "url" => "https://peertube.-2d4c2d1630e3"
1786 test "fixes url for not Video object" do
1792 "mimeType" => "text/html",
1793 "href" => "https://peertube.-2d4c2d1630e3"
1797 "mimeType" => "text/html",
1798 "href" => "https://peertube.-2d4c2d16377-42"
1803 assert Transmogrifier.fix_url(object) == %{
1805 "url" => "https://peertube.-2d4c2d1630e3"
1808 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1814 test "retunrs not modified object" do
1815 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1819 describe "get_obj_helper/2" do
1820 test "returns nil when cannot normalize object" do
1821 refute Transmogrifier.get_obj_helper("test-obj-id")
1824 test "returns {:ok, %Object{}} for success case" do
1825 assert {:ok, %Object{}} =
1826 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1830 describe "fix_attachments/1" do
1831 test "returns not modified object" do
1832 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1833 assert Transmogrifier.fix_attachments(data) == data
1836 test "returns modified object when attachment is map" do
1837 assert Transmogrifier.fix_attachments(%{
1839 "mediaType" => "video/mp4",
1840 "url" => "https://peertube.moe/stat-480.mp4"
1845 "mediaType" => "video/mp4",
1848 "href" => "https://peertube.moe/stat-480.mp4",
1849 "mediaType" => "video/mp4",
1858 test "returns modified object when attachment is list" do
1859 assert Transmogrifier.fix_attachments(%{
1861 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1862 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1867 "mediaType" => "video/mp4",
1870 "href" => "https://pe.er/stat-480.mp4",
1871 "mediaType" => "video/mp4",
1877 "href" => "https://pe.er/stat-480.mp4",
1878 "mediaType" => "video/mp4",
1879 "mimeType" => "video/mp4",
1882 "href" => "https://pe.er/stat-480.mp4",
1883 "mediaType" => "video/mp4",
1893 describe "fix_emoji/1" do
1894 test "returns not modified object when object not contains tags" do
1895 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1896 assert Transmogrifier.fix_emoji(data) == data
1899 test "returns object with emoji when object contains list tags" do
1900 assert Transmogrifier.fix_emoji(%{
1902 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1903 %{"type" => "Hashtag"}
1906 "emoji" => %{"bib" => "/test"},
1908 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1909 %{"type" => "Hashtag"}
1914 test "returns object with emoji when object contains map tag" do
1915 assert Transmogrifier.fix_emoji(%{
1916 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1918 "emoji" => %{"bib" => "/test"},
1919 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}