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 test "it fetches replied-to activities if we don't have them" do
44 File.read!("test/fixtures/mastodon-post-activity.json")
49 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
51 data = Map.put(data, "object", object)
52 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
53 returned_object = Object.normalize(returned_activity, false)
56 Activity.get_create_by_object_ap_id(
57 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
60 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
63 test "it does not fetch replied-to activities beyond max_replies_depth" do
65 File.read!("test/fixtures/mastodon-post-activity.json")
70 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
72 data = Map.put(data, "object", object)
74 with_mock Pleroma.Web.Federator,
75 allowed_incoming_reply_depth?: fn _ -> false end do
76 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
78 returned_object = Object.normalize(returned_activity, false)
80 refute Activity.get_create_by_object_ap_id(
81 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
84 assert returned_object.data["inReplyToAtomUri"] ==
85 "https://shitposter.club/notice/2827873"
89 test "it does not crash if the object in inReplyTo can't be fetched" do
91 File.read!("test/fixtures/mastodon-post-activity.json")
96 |> Map.put("inReplyTo", "https://404.site/whatever")
100 |> Map.put("object", object)
102 assert capture_log(fn ->
103 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
104 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
107 test "it works for incoming notices" do
108 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
113 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
115 assert data["context"] ==
116 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
118 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
120 assert data["cc"] == [
121 "http://mastodon.example.org/users/admin/followers",
122 "http://localtesting.pleroma.lol/users/lain"
125 assert data["actor"] == "http://mastodon.example.org/users/admin"
127 object_data = Object.normalize(data["object"]).data
129 assert object_data["id"] ==
130 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
132 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
134 assert object_data["cc"] == [
135 "http://mastodon.example.org/users/admin/followers",
136 "http://localtesting.pleroma.lol/users/lain"
139 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
140 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
142 assert object_data["context"] ==
143 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
145 assert object_data["sensitive"] == true
147 user = User.get_cached_by_ap_id(object_data["actor"])
149 assert user.note_count == 1
152 test "it works for incoming notices with hashtags" do
153 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
155 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
156 object = Object.normalize(data["object"])
158 assert Enum.at(object.data["tag"], 2) == "moo"
161 test "it works for incoming questions" do
162 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
164 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
166 object = Object.normalize(activity)
168 assert Enum.all?(object.data["oneOf"], fn choice ->
171 "Everyone knows that!",
172 "25 char limit is dumb",
173 "I can't even fit a funny"
178 test "it works for incoming listens" do
180 "@context" => "https://www.w3.org/ns/activitystreams",
181 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
184 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
185 "actor" => "http://mastodon.example.org/users/admin",
188 "id" => "http://mastodon.example.org/users/admin/listens/1234",
189 "attributedTo" => "http://mastodon.example.org/users/admin",
190 "title" => "lain radio episode 1",
192 "album" => "lain radio",
197 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
199 object = Object.normalize(activity)
201 assert object.data["title"] == "lain radio episode 1"
202 assert object.data["artist"] == "lain"
203 assert object.data["album"] == "lain radio"
204 assert object.data["length"] == 180_000
207 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
211 CommonAPI.post(user, %{
212 "status" => "suya...",
213 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
216 object = Object.normalize(activity)
219 File.read!("test/fixtures/mastodon-vote.json")
221 |> Kernel.put_in(["to"], user.ap_id)
222 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
223 |> Kernel.put_in(["object", "to"], user.ap_id)
225 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
226 answer_object = Object.normalize(activity)
227 assert answer_object.data["type"] == "Answer"
228 object = Object.get_by_ap_id(object.data["id"])
231 object.data["oneOf"],
233 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
239 test "it works for incoming notices with contentMap" do
241 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
243 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
244 object = Object.normalize(data["object"])
246 assert object.data["content"] ==
247 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
250 test "it works for incoming notices with to/cc not being an array (kroeg)" do
251 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
253 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
254 object = Object.normalize(data["object"])
256 assert object.data["content"] ==
257 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
260 test "it works for incoming announces with actor being inlined (kroeg)" do
261 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
263 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
265 assert data["actor"] == "https://puckipedia.com/"
268 test "it works for incoming notices with tag not being an array (kroeg)" do
269 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
271 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
272 object = Object.normalize(data["object"])
274 assert object.data["emoji"] == %{
275 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
278 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
280 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
281 object = Object.normalize(data["object"])
283 assert "test" in object.data["tag"]
286 test "it works for incoming notices with url not being a string (prismo)" do
287 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
289 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
290 object = Object.normalize(data["object"])
292 assert object.data["url"] == "https://prismo.news/posts/83"
295 test "it cleans up incoming notices which are not really DMs" do
297 other_user = insert(:user)
299 to = [user.ap_id, other_user.ap_id]
302 File.read!("test/fixtures/mastodon-post-activity.json")
312 data = Map.put(data, "object", object)
314 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
316 assert data["to"] == []
317 assert data["cc"] == to
319 object_data = Object.normalize(activity).data
321 assert object_data["to"] == []
322 assert object_data["cc"] == to
325 test "it works for incoming likes" do
327 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
330 File.read!("test/fixtures/mastodon-like.json")
332 |> Map.put("object", activity.data["object"])
334 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
336 assert data["actor"] == "http://mastodon.example.org/users/admin"
337 assert data["type"] == "Like"
338 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
339 assert data["object"] == activity.data["object"]
342 test "it works for incoming misskey likes, turning them into EmojiReactions" do
344 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
347 File.read!("test/fixtures/misskey-like.json")
349 |> Map.put("object", activity.data["object"])
351 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
353 assert data["actor"] == data["actor"]
354 assert data["type"] == "EmojiReaction"
355 assert data["id"] == data["id"]
356 assert data["object"] == activity.data["object"]
357 assert data["content"] == "🍮"
360 test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReactions" do
362 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
365 File.read!("test/fixtures/misskey-like.json")
367 |> Map.put("object", activity.data["object"])
368 |> Map.put("_misskey_reaction", "⭐")
370 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
372 assert data["actor"] == data["actor"]
373 assert data["type"] == "EmojiReaction"
374 assert data["id"] == data["id"]
375 assert data["object"] == activity.data["object"]
376 assert data["content"] == "⭐"
379 test "it works for incoming emoji reactions" do
381 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
384 File.read!("test/fixtures/emoji-reaction.json")
386 |> Map.put("object", activity.data["object"])
388 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
390 assert data["actor"] == "http://mastodon.example.org/users/admin"
391 assert data["type"] == "EmojiReaction"
392 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
393 assert data["object"] == activity.data["object"]
394 assert data["content"] == "👌"
397 test "it works for incoming emoji reaction undos" do
400 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
401 {:ok, reaction_activity, _object} = CommonAPI.react_with_emoji(activity.id, user, "👌")
404 File.read!("test/fixtures/mastodon-undo-like.json")
406 |> Map.put("object", reaction_activity.data["id"])
407 |> Map.put("actor", user.ap_id)
409 {:ok, activity} = Transmogrifier.handle_incoming(data)
411 assert activity.actor == user.ap_id
412 assert activity.data["id"] == data["id"]
413 assert activity.data["type"] == "Undo"
416 test "it returns an error for incoming unlikes wihout a like activity" do
418 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
421 File.read!("test/fixtures/mastodon-undo-like.json")
423 |> Map.put("object", activity.data["object"])
425 assert Transmogrifier.handle_incoming(data) == :error
428 test "it works for incoming unlikes with an existing like activity" do
430 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
433 File.read!("test/fixtures/mastodon-like.json")
435 |> Map.put("object", activity.data["object"])
437 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
440 File.read!("test/fixtures/mastodon-undo-like.json")
442 |> Map.put("object", like_data)
443 |> Map.put("actor", like_data["actor"])
445 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
447 assert data["actor"] == "http://mastodon.example.org/users/admin"
448 assert data["type"] == "Undo"
449 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
450 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
453 test "it works for incoming unlikes with an existing like activity and a compact object" do
455 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
458 File.read!("test/fixtures/mastodon-like.json")
460 |> Map.put("object", activity.data["object"])
462 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
465 File.read!("test/fixtures/mastodon-undo-like.json")
467 |> Map.put("object", like_data["id"])
468 |> Map.put("actor", like_data["actor"])
470 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
472 assert data["actor"] == "http://mastodon.example.org/users/admin"
473 assert data["type"] == "Undo"
474 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
475 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
478 test "it works for incoming announces" do
479 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
481 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
483 assert data["actor"] == "http://mastodon.example.org/users/admin"
484 assert data["type"] == "Announce"
487 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
489 assert data["object"] ==
490 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
492 assert Activity.get_create_by_object_ap_id(data["object"])
495 test "it works for incoming announces with an existing activity" do
497 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
500 File.read!("test/fixtures/mastodon-announce.json")
502 |> Map.put("object", activity.data["object"])
504 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
506 assert data["actor"] == "http://mastodon.example.org/users/admin"
507 assert data["type"] == "Announce"
510 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
512 assert data["object"] == activity.data["object"]
514 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
517 test "it works for incoming announces with an inlined activity" do
519 File.read!("test/fixtures/mastodon-announce-private.json")
522 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
524 assert data["actor"] == "http://mastodon.example.org/users/admin"
525 assert data["type"] == "Announce"
528 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
530 object = Object.normalize(data["object"])
532 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
533 assert object.data["content"] == "this is a private toot"
536 test "it rejects incoming announces with an inlined activity from another origin" do
538 File.read!("test/fixtures/bogus-mastodon-announce.json")
541 assert :error = Transmogrifier.handle_incoming(data)
544 test "it does not clobber the addressing on announce activities" do
546 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
549 File.read!("test/fixtures/mastodon-announce.json")
551 |> Map.put("object", Object.normalize(activity).data["id"])
552 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
555 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
557 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
560 test "it ensures that as:Public activities make it to their followers collection" do
564 File.read!("test/fixtures/mastodon-post-activity.json")
566 |> Map.put("actor", user.ap_id)
567 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
572 |> Map.put("attributedTo", user.ap_id)
573 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
575 |> Map.put("id", user.ap_id <> "/activities/12345678")
577 data = Map.put(data, "object", object)
579 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
581 assert data["cc"] == [User.ap_followers(user)]
584 test "it ensures that address fields become lists" do
588 File.read!("test/fixtures/mastodon-post-activity.json")
590 |> Map.put("actor", user.ap_id)
591 |> Map.put("to", nil)
592 |> Map.put("cc", nil)
596 |> Map.put("attributedTo", user.ap_id)
597 |> Map.put("to", nil)
598 |> Map.put("cc", nil)
599 |> Map.put("id", user.ap_id <> "/activities/12345678")
601 data = Map.put(data, "object", object)
603 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
605 assert !is_nil(data["to"])
606 assert !is_nil(data["cc"])
609 test "it strips internal likes" do
611 File.read!("test/fixtures/mastodon-post-activity.json")
616 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
617 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
619 "type" => "OrderedCollection"
622 object = Map.put(data["object"], "likes", likes)
623 data = Map.put(data, "object", object)
625 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
627 refute Map.has_key?(object.data, "likes")
630 test "it strips internal reactions" do
632 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
633 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
635 %{object: object} = Activity.get_by_id_with_object(activity.id)
636 assert Map.has_key?(object.data, "reactions")
637 assert Map.has_key?(object.data, "reaction_count")
639 object_data = Transmogrifier.strip_internal_fields(object.data)
640 refute Map.has_key?(object_data, "reactions")
641 refute Map.has_key?(object_data, "reaction_count")
644 test "it works for incoming update activities" do
645 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
647 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
648 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
651 update_data["object"]
652 |> Map.put("actor", data["actor"])
653 |> Map.put("id", data["actor"])
657 |> Map.put("actor", data["actor"])
658 |> Map.put("object", object)
660 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
662 assert data["id"] == update_data["id"]
664 user = User.get_cached_by_ap_id(data["actor"])
665 assert user.name == "gargle"
667 assert user.avatar["url"] == [
670 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
674 assert user.banner["url"] == [
677 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
681 assert user.bio == "<p>Some bio</p>"
684 test "it works with custom profile fields" do
686 "test/fixtures/mastodon-post-activity.json"
689 |> Transmogrifier.handle_incoming()
691 user = User.get_cached_by_ap_id(activity.actor)
693 assert User.fields(user) == [
694 %{"name" => "foo", "value" => "bar"},
695 %{"name" => "foo1", "value" => "bar1"}
698 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
701 update_data["object"]
702 |> Map.put("actor", user.ap_id)
703 |> Map.put("id", user.ap_id)
707 |> Map.put("actor", user.ap_id)
708 |> Map.put("object", object)
710 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
712 user = User.get_cached_by_ap_id(user.ap_id)
714 assert User.fields(user) == [
715 %{"name" => "foo", "value" => "updated"},
716 %{"name" => "foo1", "value" => "updated"}
719 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
722 put_in(update_data, ["object", "attachment"], [
723 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
724 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
725 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
728 {:ok, _} = Transmogrifier.handle_incoming(update_data)
730 user = User.get_cached_by_ap_id(user.ap_id)
732 assert User.fields(user) == [
733 %{"name" => "foo", "value" => "updated"},
734 %{"name" => "foo1", "value" => "updated"}
737 update_data = put_in(update_data, ["object", "attachment"], [])
739 {:ok, _} = Transmogrifier.handle_incoming(update_data)
741 user = User.get_cached_by_ap_id(user.ap_id)
743 assert User.fields(user) == []
746 test "it works for incoming update activities which lock the account" do
747 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
749 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
750 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
753 update_data["object"]
754 |> Map.put("actor", data["actor"])
755 |> Map.put("id", data["actor"])
756 |> Map.put("manuallyApprovesFollowers", true)
760 |> Map.put("actor", data["actor"])
761 |> Map.put("object", object)
763 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
765 user = User.get_cached_by_ap_id(data["actor"])
766 assert user.locked == true
769 test "it works for incoming deletes" do
770 activity = insert(:note_activity)
771 deleting_user = insert(:user)
774 File.read!("test/fixtures/mastodon-delete.json")
779 |> Map.put("id", activity.data["object"])
783 |> Map.put("object", object)
784 |> Map.put("actor", deleting_user.ap_id)
786 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
787 Transmogrifier.handle_incoming(data)
789 assert id == data["id"]
790 refute Activity.get_by_id(activity.id)
791 assert actor == deleting_user.ap_id
794 test "it fails for incoming deletes with spoofed origin" do
795 activity = insert(:note_activity)
798 File.read!("test/fixtures/mastodon-delete.json")
803 |> Map.put("id", activity.data["object"])
807 |> Map.put("object", object)
809 assert capture_log(fn ->
810 :error = Transmogrifier.handle_incoming(data)
812 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
814 assert Activity.get_by_id(activity.id)
817 test "it works for incoming user deletes" do
818 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
821 File.read!("test/fixtures/mastodon-delete-user.json")
824 {:ok, _} = Transmogrifier.handle_incoming(data)
825 ObanHelpers.perform_all()
827 refute User.get_cached_by_ap_id(ap_id)
830 test "it fails for incoming user deletes with spoofed origin" do
831 %{ap_id: ap_id} = insert(:user)
834 File.read!("test/fixtures/mastodon-delete-user.json")
836 |> Map.put("actor", ap_id)
838 assert capture_log(fn ->
839 assert :error == Transmogrifier.handle_incoming(data)
840 end) =~ "Object containment failed"
842 assert User.get_cached_by_ap_id(ap_id)
845 test "it works for incoming unannounces with an existing notice" do
847 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
850 File.read!("test/fixtures/mastodon-announce.json")
852 |> Map.put("object", activity.data["object"])
854 {:ok, %Activity{data: announce_data, local: false}} =
855 Transmogrifier.handle_incoming(announce_data)
858 File.read!("test/fixtures/mastodon-undo-announce.json")
860 |> Map.put("object", announce_data)
861 |> Map.put("actor", announce_data["actor"])
863 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
865 assert data["type"] == "Undo"
866 assert object_data = data["object"]
867 assert object_data["type"] == "Announce"
868 assert object_data["object"] == activity.data["object"]
870 assert object_data["id"] ==
871 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
874 test "it works for incomming unfollows with an existing follow" do
878 File.read!("test/fixtures/mastodon-follow-activity.json")
880 |> Map.put("object", user.ap_id)
882 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
885 File.read!("test/fixtures/mastodon-unfollow-activity.json")
887 |> Map.put("object", follow_data)
889 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
891 assert data["type"] == "Undo"
892 assert data["object"]["type"] == "Follow"
893 assert data["object"]["object"] == user.ap_id
894 assert data["actor"] == "http://mastodon.example.org/users/admin"
896 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
899 test "it works for incoming follows to locked account" do
900 pending_follower = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
901 user = insert(:user, locked: true)
904 File.read!("test/fixtures/mastodon-follow-activity.json")
906 |> Map.put("object", user.ap_id)
908 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
910 assert data["type"] == "Follow"
911 assert data["object"] == user.ap_id
912 assert data["state"] == "pending"
913 assert data["actor"] == "http://mastodon.example.org/users/admin"
915 assert [^pending_follower] = User.get_follow_requests(user)
918 test "it works for incoming blocks" do
922 File.read!("test/fixtures/mastodon-block-activity.json")
924 |> Map.put("object", user.ap_id)
926 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
928 assert data["type"] == "Block"
929 assert data["object"] == user.ap_id
930 assert data["actor"] == "http://mastodon.example.org/users/admin"
932 blocker = User.get_cached_by_ap_id(data["actor"])
934 assert User.blocks?(blocker, user)
937 test "incoming blocks successfully tear down any follow relationship" do
938 blocker = insert(:user)
939 blocked = insert(:user)
942 File.read!("test/fixtures/mastodon-block-activity.json")
944 |> Map.put("object", blocked.ap_id)
945 |> Map.put("actor", blocker.ap_id)
947 {:ok, blocker} = User.follow(blocker, blocked)
948 {:ok, blocked} = User.follow(blocked, blocker)
950 assert User.following?(blocker, blocked)
951 assert User.following?(blocked, blocker)
953 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
955 assert data["type"] == "Block"
956 assert data["object"] == blocked.ap_id
957 assert data["actor"] == blocker.ap_id
959 blocker = User.get_cached_by_ap_id(data["actor"])
960 blocked = User.get_cached_by_ap_id(data["object"])
962 assert User.blocks?(blocker, blocked)
964 refute User.following?(blocker, blocked)
965 refute User.following?(blocked, blocker)
968 test "it works for incoming unblocks with an existing block" do
972 File.read!("test/fixtures/mastodon-block-activity.json")
974 |> Map.put("object", user.ap_id)
976 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
979 File.read!("test/fixtures/mastodon-unblock-activity.json")
981 |> Map.put("object", block_data)
983 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
984 assert data["type"] == "Undo"
985 assert data["object"]["type"] == "Block"
986 assert data["object"]["object"] == user.ap_id
987 assert data["actor"] == "http://mastodon.example.org/users/admin"
989 blocker = User.get_cached_by_ap_id(data["actor"])
991 refute User.blocks?(blocker, user)
994 test "it works for incoming accepts which were pre-accepted" do
995 follower = insert(:user)
996 followed = insert(:user)
998 {:ok, follower} = User.follow(follower, followed)
999 assert User.following?(follower, followed) == true
1001 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1004 File.read!("test/fixtures/mastodon-accept-activity.json")
1006 |> Map.put("actor", followed.ap_id)
1009 accept_data["object"]
1010 |> Map.put("actor", follower.ap_id)
1011 |> Map.put("id", follow_activity.data["id"])
1013 accept_data = Map.put(accept_data, "object", object)
1015 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1016 refute activity.local
1018 assert activity.data["object"] == follow_activity.data["id"]
1020 assert activity.data["id"] == accept_data["id"]
1022 follower = User.get_cached_by_id(follower.id)
1024 assert User.following?(follower, followed) == true
1027 test "it works for incoming accepts which were orphaned" do
1028 follower = insert(:user)
1029 followed = insert(:user, locked: true)
1031 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1034 File.read!("test/fixtures/mastodon-accept-activity.json")
1036 |> Map.put("actor", followed.ap_id)
1039 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1041 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1042 assert activity.data["object"] == follow_activity.data["id"]
1044 follower = User.get_cached_by_id(follower.id)
1046 assert User.following?(follower, followed) == true
1049 test "it works for incoming accepts which are referenced by IRI only" do
1050 follower = insert(:user)
1051 followed = insert(:user, locked: true)
1053 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1056 File.read!("test/fixtures/mastodon-accept-activity.json")
1058 |> Map.put("actor", followed.ap_id)
1059 |> Map.put("object", follow_activity.data["id"])
1061 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
1062 assert activity.data["object"] == follow_activity.data["id"]
1064 follower = User.get_cached_by_id(follower.id)
1066 assert User.following?(follower, followed) == true
1069 test "it fails for incoming accepts which cannot be correlated" do
1070 follower = insert(:user)
1071 followed = insert(:user, locked: true)
1074 File.read!("test/fixtures/mastodon-accept-activity.json")
1076 |> Map.put("actor", followed.ap_id)
1079 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1081 :error = Transmogrifier.handle_incoming(accept_data)
1083 follower = User.get_cached_by_id(follower.id)
1085 refute User.following?(follower, followed) == true
1088 test "it fails for incoming rejects which cannot be correlated" do
1089 follower = insert(:user)
1090 followed = insert(:user, locked: true)
1093 File.read!("test/fixtures/mastodon-reject-activity.json")
1095 |> Map.put("actor", followed.ap_id)
1098 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1100 :error = Transmogrifier.handle_incoming(accept_data)
1102 follower = User.get_cached_by_id(follower.id)
1104 refute User.following?(follower, followed) == true
1107 test "it works for incoming rejects which are orphaned" do
1108 follower = insert(:user)
1109 followed = insert(:user, locked: true)
1111 {:ok, follower} = User.follow(follower, followed)
1112 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1114 assert User.following?(follower, followed) == true
1117 File.read!("test/fixtures/mastodon-reject-activity.json")
1119 |> Map.put("actor", followed.ap_id)
1122 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1124 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1125 refute activity.local
1126 assert activity.data["id"] == reject_data["id"]
1128 follower = User.get_cached_by_id(follower.id)
1130 assert User.following?(follower, followed) == false
1133 test "it works for incoming rejects which are referenced by IRI only" do
1134 follower = insert(:user)
1135 followed = insert(:user, locked: true)
1137 {:ok, follower} = User.follow(follower, followed)
1138 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1140 assert User.following?(follower, followed) == true
1143 File.read!("test/fixtures/mastodon-reject-activity.json")
1145 |> Map.put("actor", followed.ap_id)
1146 |> Map.put("object", follow_activity.data["id"])
1148 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1150 follower = User.get_cached_by_id(follower.id)
1152 assert User.following?(follower, followed) == false
1155 test "it rejects activities without a valid ID" do
1156 user = insert(:user)
1159 File.read!("test/fixtures/mastodon-follow-activity.json")
1161 |> Map.put("object", user.ap_id)
1162 |> Map.put("id", "")
1164 :error = Transmogrifier.handle_incoming(data)
1167 test "it remaps video URLs as attachments if necessary" do
1169 Fetcher.fetch_object_from_id(
1170 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1175 "mediaType" => "video/mp4",
1177 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1178 "mimeType" => "video/mp4",
1179 "size" => 5_015_880,
1183 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1184 "mediaType" => "video/mp4",
1191 assert object.data["url"] ==
1192 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1194 assert object.data["attachment"] == [attachment]
1197 test "it accepts Flag activities" do
1198 user = insert(:user)
1199 other_user = insert(:user)
1201 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1202 object = Object.normalize(activity)
1206 "id" => activity.data["id"],
1207 "content" => "test post",
1208 "published" => object.data["published"],
1209 "actor" => AccountView.render("show.json", %{user: user})
1213 "@context" => "https://www.w3.org/ns/activitystreams",
1214 "cc" => [user.ap_id],
1215 "object" => [user.ap_id, activity.data["id"]],
1217 "content" => "blocked AND reported!!!",
1218 "actor" => other_user.ap_id
1221 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1223 assert activity.data["object"] == [user.ap_id, note_obj]
1224 assert activity.data["content"] == "blocked AND reported!!!"
1225 assert activity.data["actor"] == other_user.ap_id
1226 assert activity.data["cc"] == [user.ap_id]
1229 test "it correctly processes messages with non-array to field" do
1230 user = insert(:user)
1233 "@context" => "https://www.w3.org/ns/activitystreams",
1234 "to" => "https://www.w3.org/ns/activitystreams#Public",
1237 "content" => "blah blah blah",
1239 "attributedTo" => user.ap_id,
1242 "actor" => user.ap_id
1245 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1247 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1250 test "it correctly processes messages with non-array cc field" do
1251 user = insert(:user)
1254 "@context" => "https://www.w3.org/ns/activitystreams",
1255 "to" => user.follower_address,
1256 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1259 "content" => "blah blah blah",
1261 "attributedTo" => user.ap_id,
1264 "actor" => user.ap_id
1267 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1269 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1270 assert [user.follower_address] == activity.data["to"]
1274 describe "prepare outgoing" do
1275 test "it inlines private announced objects" do
1276 user = insert(:user)
1278 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1280 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1282 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1284 assert modified["object"]["content"] == "hey"
1285 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1288 test "it turns mentions into tags" do
1289 user = insert(:user)
1290 other_user = insert(:user)
1293 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1295 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1296 object = modified["object"]
1298 expected_mention = %{
1299 "href" => other_user.ap_id,
1300 "name" => "@#{other_user.nickname}",
1305 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1306 "type" => "Hashtag",
1310 assert Enum.member?(object["tag"], expected_tag)
1311 assert Enum.member?(object["tag"], expected_mention)
1314 test "it adds the sensitive property" do
1315 user = insert(:user)
1317 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1318 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1320 assert modified["object"]["sensitive"]
1323 test "it adds the json-ld context and the conversation property" do
1324 user = insert(:user)
1326 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1327 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1329 assert modified["@context"] ==
1330 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1332 assert modified["object"]["conversation"] == modified["context"]
1335 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1336 user = insert(:user)
1338 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1339 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1341 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1344 test "it strips internal hashtag data" do
1345 user = insert(:user)
1347 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1350 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1351 "type" => "Hashtag",
1355 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1357 assert modified["object"]["tag"] == [expected_tag]
1360 test "it strips internal fields" do
1361 user = insert(:user)
1363 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1365 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1367 assert length(modified["object"]["tag"]) == 2
1369 assert is_nil(modified["object"]["emoji"])
1370 assert is_nil(modified["object"]["like_count"])
1371 assert is_nil(modified["object"]["announcements"])
1372 assert is_nil(modified["object"]["announcement_count"])
1373 assert is_nil(modified["object"]["context_id"])
1376 test "it strips internal fields of article" do
1377 activity = insert(:article_activity)
1379 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1381 assert length(modified["object"]["tag"]) == 2
1383 assert is_nil(modified["object"]["emoji"])
1384 assert is_nil(modified["object"]["like_count"])
1385 assert is_nil(modified["object"]["announcements"])
1386 assert is_nil(modified["object"]["announcement_count"])
1387 assert is_nil(modified["object"]["context_id"])
1388 assert is_nil(modified["object"]["likes"])
1391 test "the directMessage flag is present" do
1392 user = insert(:user)
1393 other_user = insert(:user)
1395 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1397 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1399 assert modified["directMessage"] == false
1402 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1404 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1406 assert modified["directMessage"] == false
1409 CommonAPI.post(user, %{
1410 "status" => "@#{other_user.nickname} :moominmamma:",
1411 "visibility" => "direct"
1414 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1416 assert modified["directMessage"] == true
1419 test "it strips BCC field" do
1420 user = insert(:user)
1421 {:ok, list} = Pleroma.List.create("foo", user)
1424 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1426 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1428 assert is_nil(modified["bcc"])
1431 test "it can handle Listen activities" do
1432 listen_activity = insert(:listen)
1434 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1436 assert modified["type"] == "Listen"
1438 user = insert(:user)
1440 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1442 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1446 describe "user upgrade" do
1447 test "it upgrades a user to activitypub" do
1450 nickname: "rye@niu.moe",
1452 ap_id: "https://niu.moe/users/rye",
1453 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1456 user_two = insert(:user)
1457 Pleroma.FollowingRelationship.follow(user_two, user, "accept")
1459 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1460 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1461 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1463 user = User.get_cached_by_id(user.id)
1464 assert user.note_count == 1
1466 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1467 ObanHelpers.perform_all()
1469 assert user.ap_enabled
1470 assert user.note_count == 1
1471 assert user.follower_address == "https://niu.moe/users/rye/followers"
1472 assert user.following_address == "https://niu.moe/users/rye/following"
1474 user = User.get_cached_by_id(user.id)
1475 assert user.note_count == 1
1477 activity = Activity.get_by_id(activity.id)
1478 assert user.follower_address in activity.recipients
1484 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1493 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1498 refute "..." in activity.recipients
1500 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1501 refute user.follower_address in unrelated_activity.recipients
1503 user_two = User.get_cached_by_id(user_two.id)
1504 assert User.following?(user_two, user)
1505 refute "..." in User.following(user_two)
1509 describe "actor rewriting" do
1510 test "it fixes the actor URL property to be a proper URI" do
1512 "url" => %{"href" => "http://example.com"}
1515 rewritten = Transmogrifier.maybe_fix_user_object(data)
1516 assert rewritten["url"] == "http://example.com"
1520 describe "actor origin containment" do
1521 test "it rejects activities which reference objects with bogus origins" do
1523 "@context" => "https://www.w3.org/ns/activitystreams",
1524 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1525 "actor" => "http://mastodon.example.org/users/admin",
1526 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1527 "object" => "https://info.pleroma.site/activity.json",
1528 "type" => "Announce"
1531 assert capture_log(fn ->
1532 :error = Transmogrifier.handle_incoming(data)
1533 end) =~ "Object containment failed"
1536 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1538 "@context" => "https://www.w3.org/ns/activitystreams",
1539 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1540 "actor" => "http://mastodon.example.org/users/admin",
1541 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1542 "object" => "https://info.pleroma.site/activity2.json",
1543 "type" => "Announce"
1546 assert capture_log(fn ->
1547 :error = Transmogrifier.handle_incoming(data)
1548 end) =~ "Object containment failed"
1551 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1553 "@context" => "https://www.w3.org/ns/activitystreams",
1554 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1555 "actor" => "http://mastodon.example.org/users/admin",
1556 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1557 "object" => "https://info.pleroma.site/activity3.json",
1558 "type" => "Announce"
1561 assert capture_log(fn ->
1562 :error = Transmogrifier.handle_incoming(data)
1563 end) =~ "Object containment failed"
1567 describe "reserialization" do
1568 test "successfully reserializes a message with inReplyTo == nil" do
1569 user = insert(:user)
1572 "@context" => "https://www.w3.org/ns/activitystreams",
1573 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1577 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1582 "attributedTo" => user.ap_id
1584 "actor" => user.ap_id
1587 {:ok, activity} = Transmogrifier.handle_incoming(message)
1589 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1592 test "successfully reserializes a message with AS2 objects in IR" do
1593 user = insert(:user)
1596 "@context" => "https://www.w3.org/ns/activitystreams",
1597 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1601 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1606 "attributedTo" => user.ap_id,
1608 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1609 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1612 "actor" => user.ap_id
1615 {:ok, activity} = Transmogrifier.handle_incoming(message)
1617 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1621 test "Rewrites Answers to Notes" do
1622 user = insert(:user)
1624 {:ok, poll_activity} =
1625 CommonAPI.post(user, %{
1626 "status" => "suya...",
1627 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1630 poll_object = Object.normalize(poll_activity)
1631 # TODO: Replace with CommonAPI vote creation when implemented
1633 File.read!("test/fixtures/mastodon-vote.json")
1635 |> Kernel.put_in(["to"], user.ap_id)
1636 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1637 |> Kernel.put_in(["object", "to"], user.ap_id)
1639 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1640 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1642 assert data["object"]["type"] == "Note"
1645 describe "fix_explicit_addressing" do
1647 user = insert(:user)
1651 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1652 explicitly_mentioned_actors = [
1653 "https://pleroma.gold/users/user1",
1654 "https://pleroma.gold/user2"
1658 "actor" => user.ap_id,
1659 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1662 Enum.map(explicitly_mentioned_actors, fn href ->
1663 %{"type" => "Mention", "href" => href}
1667 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1668 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1669 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1670 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1673 test "does not move actor's follower collection to cc", %{user: user} do
1675 "actor" => user.ap_id,
1676 "to" => [user.follower_address],
1680 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1681 assert user.follower_address in fixed_object["to"]
1682 refute user.follower_address in fixed_object["cc"]
1685 test "removes recipient's follower collection from cc", %{user: user} do
1686 recipient = insert(:user)
1689 "actor" => user.ap_id,
1690 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1691 "cc" => [user.follower_address, recipient.follower_address]
1694 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1696 assert user.follower_address in fixed_object["cc"]
1697 refute recipient.follower_address in fixed_object["cc"]
1698 refute recipient.follower_address in fixed_object["to"]
1702 describe "fix_summary/1" do
1703 test "returns fixed object" do
1704 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1705 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1706 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1710 describe "fix_in_reply_to/2" do
1711 clear_config([:instance, :federation_incoming_replies_max_depth])
1714 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1718 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1719 assert Transmogrifier.fix_in_reply_to(data) == data
1722 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1723 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1726 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1728 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1729 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1730 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1733 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1735 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1736 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1737 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1740 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1742 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1743 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1744 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1746 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1747 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1748 assert modified_object["inReplyTo"] == []
1749 assert modified_object["inReplyToAtomUri"] == ""
1752 test "returns modified object when allowed incoming reply", %{data: data} do
1757 "https://shitposter.club/notice/2827873"
1760 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1761 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1763 assert modified_object["inReplyTo"] ==
1764 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1766 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1768 assert modified_object["conversation"] ==
1769 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1771 assert modified_object["context"] ==
1772 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1776 describe "fix_url/1" do
1777 test "fixes data for object when url is map" do
1781 "mimeType" => "video/mp4",
1782 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1786 assert Transmogrifier.fix_url(object) == %{
1787 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1791 test "fixes data for video object" do
1797 "mimeType" => "video/mp4",
1798 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1802 "mimeType" => "video/mp4",
1803 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1807 "mimeType" => "text/html",
1808 "href" => "https://peertube.-2d4c2d1630e3"
1812 "mimeType" => "text/html",
1813 "href" => "https://peertube.-2d4c2d16377-42"
1818 assert Transmogrifier.fix_url(object) == %{
1821 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1822 "mimeType" => "video/mp4",
1827 "url" => "https://peertube.-2d4c2d1630e3"
1831 test "fixes url for not Video object" do
1837 "mimeType" => "text/html",
1838 "href" => "https://peertube.-2d4c2d1630e3"
1842 "mimeType" => "text/html",
1843 "href" => "https://peertube.-2d4c2d16377-42"
1848 assert Transmogrifier.fix_url(object) == %{
1850 "url" => "https://peertube.-2d4c2d1630e3"
1853 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1859 test "retunrs not modified object" do
1860 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1864 describe "get_obj_helper/2" do
1865 test "returns nil when cannot normalize object" do
1866 assert capture_log(fn ->
1867 refute Transmogrifier.get_obj_helper("test-obj-id")
1868 end) =~ "Unsupported URI scheme"
1871 test "returns {:ok, %Object{}} for success case" do
1872 assert {:ok, %Object{}} =
1873 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1877 describe "fix_attachments/1" do
1878 test "returns not modified object" do
1879 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1880 assert Transmogrifier.fix_attachments(data) == data
1883 test "returns modified object when attachment is map" do
1884 assert Transmogrifier.fix_attachments(%{
1886 "mediaType" => "video/mp4",
1887 "url" => "https://peertube.moe/stat-480.mp4"
1892 "mediaType" => "video/mp4",
1895 "href" => "https://peertube.moe/stat-480.mp4",
1896 "mediaType" => "video/mp4",
1905 test "returns modified object when attachment is list" do
1906 assert Transmogrifier.fix_attachments(%{
1908 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1909 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1914 "mediaType" => "video/mp4",
1917 "href" => "https://pe.er/stat-480.mp4",
1918 "mediaType" => "video/mp4",
1924 "href" => "https://pe.er/stat-480.mp4",
1925 "mediaType" => "video/mp4",
1926 "mimeType" => "video/mp4",
1929 "href" => "https://pe.er/stat-480.mp4",
1930 "mediaType" => "video/mp4",
1940 describe "fix_emoji/1" do
1941 test "returns not modified object when object not contains tags" do
1942 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1943 assert Transmogrifier.fix_emoji(data) == data
1946 test "returns object with emoji when object contains list tags" do
1947 assert Transmogrifier.fix_emoji(%{
1949 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1950 %{"type" => "Hashtag"}
1953 "emoji" => %{"bib" => "/test"},
1955 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1956 %{"type" => "Hashtag"}
1961 test "returns object with emoji when object contains map tag" do
1962 assert Transmogrifier.fix_emoji(%{
1963 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1965 "emoji" => %{"bib" => "/test"},
1966 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}