1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 clear_config([:instance, :max_remote_account_fields])
29 describe "handle_incoming" do
30 test "it ignores an incoming notice if we already have it" do
31 activity = insert(:note_activity)
34 File.read!("test/fixtures/mastodon-post-activity.json")
36 |> Map.put("object", Object.normalize(activity).data)
38 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
40 assert activity == returned_activity
43 test "it fetches replied-to activities if we don't have them" do
45 File.read!("test/fixtures/mastodon-post-activity.json")
50 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
52 data = Map.put(data, "object", object)
53 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 returned_object = Object.normalize(returned_activity, false)
57 Activity.get_create_by_object_ap_id(
58 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
61 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
64 test "it does not fetch replied-to activities beyond max_replies_depth" do
66 File.read!("test/fixtures/mastodon-post-activity.json")
71 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
73 data = Map.put(data, "object", object)
75 with_mock Pleroma.Web.Federator,
76 allowed_incoming_reply_depth?: fn _ -> false end do
77 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
79 returned_object = Object.normalize(returned_activity, false)
81 refute Activity.get_create_by_object_ap_id(
82 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
85 assert returned_object.data["inReplyToAtomUri"] ==
86 "https://shitposter.club/notice/2827873"
90 test "it does not crash if the object in inReplyTo can't be fetched" do
92 File.read!("test/fixtures/mastodon-post-activity.json")
97 |> Map.put("inReplyTo", "https://404.site/whatever")
101 |> Map.put("object", object)
103 assert capture_log(fn ->
104 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
105 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
108 test "it works for incoming notices" do
109 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
111 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
114 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
116 assert data["context"] ==
117 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
119 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
121 assert data["cc"] == [
122 "http://mastodon.example.org/users/admin/followers",
123 "http://localtesting.pleroma.lol/users/lain"
126 assert data["actor"] == "http://mastodon.example.org/users/admin"
128 object_data = Object.normalize(data["object"]).data
130 assert object_data["id"] ==
131 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
133 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
135 assert object_data["cc"] == [
136 "http://mastodon.example.org/users/admin/followers",
137 "http://localtesting.pleroma.lol/users/lain"
140 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
141 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
143 assert object_data["context"] ==
144 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
146 assert object_data["sensitive"] == true
148 user = User.get_cached_by_ap_id(object_data["actor"])
150 assert user.info.note_count == 1
153 test "it works for incoming notices with hashtags" do
154 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
156 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
157 object = Object.normalize(data["object"])
159 assert Enum.at(object.data["tag"], 2) == "moo"
162 test "it works for incoming questions" do
163 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
165 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
167 object = Object.normalize(activity)
169 assert Enum.all?(object.data["oneOf"], fn choice ->
172 "Everyone knows that!",
173 "25 char limit is dumb",
174 "I can't even fit a funny"
179 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
183 CommonAPI.post(user, %{
184 "status" => "suya...",
185 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
188 object = Object.normalize(activity)
191 File.read!("test/fixtures/mastodon-vote.json")
193 |> Kernel.put_in(["to"], user.ap_id)
194 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
195 |> Kernel.put_in(["object", "to"], user.ap_id)
197 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
198 answer_object = Object.normalize(activity)
199 assert answer_object.data["type"] == "Answer"
200 object = Object.get_by_ap_id(object.data["id"])
203 object.data["oneOf"],
205 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
211 test "it works for incoming notices with contentMap" do
213 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
215 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
216 object = Object.normalize(data["object"])
218 assert object.data["content"] ==
219 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
222 test "it works for incoming notices with to/cc not being an array (kroeg)" do
223 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
225 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
226 object = Object.normalize(data["object"])
228 assert object.data["content"] ==
229 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
232 test "it works for incoming announces with actor being inlined (kroeg)" do
233 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
235 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
237 assert data["actor"] == "https://puckipedia.com/"
240 test "it works for incoming notices with tag not being an array (kroeg)" do
241 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
243 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
244 object = Object.normalize(data["object"])
246 assert object.data["emoji"] == %{
247 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
250 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
252 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
253 object = Object.normalize(data["object"])
255 assert "test" in object.data["tag"]
258 test "it works for incoming notices with url not being a string (prismo)" do
259 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
261 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
262 object = Object.normalize(data["object"])
264 assert object.data["url"] == "https://prismo.news/posts/83"
267 test "it cleans up incoming notices which are not really DMs" do
269 other_user = insert(:user)
271 to = [user.ap_id, other_user.ap_id]
274 File.read!("test/fixtures/mastodon-post-activity.json")
284 data = Map.put(data, "object", object)
286 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
288 assert data["to"] == []
289 assert data["cc"] == to
291 object_data = Object.normalize(activity).data
293 assert object_data["to"] == []
294 assert object_data["cc"] == to
297 test "it works for incoming likes" do
299 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
302 File.read!("test/fixtures/mastodon-like.json")
304 |> Map.put("object", activity.data["object"])
306 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
308 assert data["actor"] == "http://mastodon.example.org/users/admin"
309 assert data["type"] == "Like"
310 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
311 assert data["object"] == activity.data["object"]
314 test "it returns an error for incoming unlikes wihout a like activity" do
316 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
319 File.read!("test/fixtures/mastodon-undo-like.json")
321 |> Map.put("object", activity.data["object"])
323 assert Transmogrifier.handle_incoming(data) == :error
326 test "it works for incoming unlikes with an existing like activity" do
328 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
331 File.read!("test/fixtures/mastodon-like.json")
333 |> Map.put("object", activity.data["object"])
335 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
338 File.read!("test/fixtures/mastodon-undo-like.json")
340 |> Map.put("object", like_data)
341 |> Map.put("actor", like_data["actor"])
343 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
345 assert data["actor"] == "http://mastodon.example.org/users/admin"
346 assert data["type"] == "Undo"
347 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
348 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
351 test "it works for incoming unlikes with an existing like activity and a compact object" do
353 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
356 File.read!("test/fixtures/mastodon-like.json")
358 |> Map.put("object", activity.data["object"])
360 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
363 File.read!("test/fixtures/mastodon-undo-like.json")
365 |> Map.put("object", like_data["id"])
366 |> Map.put("actor", like_data["actor"])
368 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
370 assert data["actor"] == "http://mastodon.example.org/users/admin"
371 assert data["type"] == "Undo"
372 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
373 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
376 test "it works for incoming announces" do
377 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
379 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
381 assert data["actor"] == "http://mastodon.example.org/users/admin"
382 assert data["type"] == "Announce"
385 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
387 assert data["object"] ==
388 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
390 assert Activity.get_create_by_object_ap_id(data["object"])
393 test "it works for incoming announces with an existing activity" do
395 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
398 File.read!("test/fixtures/mastodon-announce.json")
400 |> Map.put("object", activity.data["object"])
402 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
404 assert data["actor"] == "http://mastodon.example.org/users/admin"
405 assert data["type"] == "Announce"
408 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
410 assert data["object"] == activity.data["object"]
412 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
415 test "it does not clobber the addressing on announce activities" do
417 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
420 File.read!("test/fixtures/mastodon-announce.json")
422 |> Map.put("object", Object.normalize(activity).data["id"])
423 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
426 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
428 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
431 test "it ensures that as:Public activities make it to their followers collection" do
435 File.read!("test/fixtures/mastodon-post-activity.json")
437 |> Map.put("actor", user.ap_id)
438 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
443 |> Map.put("attributedTo", user.ap_id)
444 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
446 |> Map.put("id", user.ap_id <> "/activities/12345678")
448 data = Map.put(data, "object", object)
450 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
452 assert data["cc"] == [User.ap_followers(user)]
455 test "it ensures that address fields become lists" do
459 File.read!("test/fixtures/mastodon-post-activity.json")
461 |> Map.put("actor", user.ap_id)
462 |> Map.put("to", nil)
463 |> Map.put("cc", nil)
467 |> Map.put("attributedTo", user.ap_id)
468 |> Map.put("to", nil)
469 |> Map.put("cc", nil)
470 |> Map.put("id", user.ap_id <> "/activities/12345678")
472 data = Map.put(data, "object", object)
474 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
476 assert !is_nil(data["to"])
477 assert !is_nil(data["cc"])
480 test "it strips internal likes" do
482 File.read!("test/fixtures/mastodon-post-activity.json")
487 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
488 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
490 "type" => "OrderedCollection"
493 object = Map.put(data["object"], "likes", likes)
494 data = Map.put(data, "object", object)
496 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
498 refute Map.has_key?(object.data, "likes")
501 test "it works for incoming update activities" do
502 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
504 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
505 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
508 update_data["object"]
509 |> Map.put("actor", data["actor"])
510 |> Map.put("id", data["actor"])
514 |> Map.put("actor", data["actor"])
515 |> Map.put("object", object)
517 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
519 user = User.get_cached_by_ap_id(data["actor"])
520 assert user.name == "gargle"
522 assert user.avatar["url"] == [
525 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
529 assert user.info.banner["url"] == [
532 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
536 assert user.bio == "<p>Some bio</p>"
539 test "it works with custom profile fields" do
541 "test/fixtures/mastodon-post-activity.json"
544 |> Transmogrifier.handle_incoming()
546 user = User.get_cached_by_ap_id(activity.actor)
548 assert User.Info.fields(user.info) == [
549 %{"name" => "foo", "value" => "bar"},
550 %{"name" => "foo1", "value" => "bar1"}
553 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
556 update_data["object"]
557 |> Map.put("actor", user.ap_id)
558 |> Map.put("id", user.ap_id)
562 |> Map.put("actor", user.ap_id)
563 |> Map.put("object", object)
565 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
567 user = User.get_cached_by_ap_id(user.ap_id)
569 assert User.Info.fields(user.info) == [
570 %{"name" => "foo", "value" => "updated"},
571 %{"name" => "foo1", "value" => "updated"}
574 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
577 put_in(update_data, ["object", "attachment"], [
578 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
579 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
580 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
583 {:ok, _} = Transmogrifier.handle_incoming(update_data)
585 user = User.get_cached_by_ap_id(user.ap_id)
587 assert User.Info.fields(user.info) == [
588 %{"name" => "foo", "value" => "updated"},
589 %{"name" => "foo1", "value" => "updated"}
592 update_data = put_in(update_data, ["object", "attachment"], [])
594 {:ok, _} = Transmogrifier.handle_incoming(update_data)
596 user = User.get_cached_by_ap_id(user.ap_id)
598 assert User.Info.fields(user.info) == []
601 test "it works for incoming update activities which lock the account" do
602 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
604 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
605 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
608 update_data["object"]
609 |> Map.put("actor", data["actor"])
610 |> Map.put("id", data["actor"])
611 |> Map.put("manuallyApprovesFollowers", true)
615 |> Map.put("actor", data["actor"])
616 |> Map.put("object", object)
618 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
620 user = User.get_cached_by_ap_id(data["actor"])
621 assert user.info.locked == true
624 test "it works for incoming deletes" do
625 activity = insert(:note_activity)
628 File.read!("test/fixtures/mastodon-delete.json")
633 |> Map.put("id", activity.data["object"])
637 |> Map.put("object", object)
638 |> Map.put("actor", activity.data["actor"])
640 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
642 refute Activity.get_by_id(activity.id)
645 test "it fails for incoming deletes with spoofed origin" do
646 activity = insert(:note_activity)
649 File.read!("test/fixtures/mastodon-delete.json")
654 |> Map.put("id", activity.data["object"])
658 |> Map.put("object", object)
660 assert capture_log(fn ->
661 :error = Transmogrifier.handle_incoming(data)
663 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
665 assert Activity.get_by_id(activity.id)
668 test "it works for incoming user deletes" do
669 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
672 File.read!("test/fixtures/mastodon-delete-user.json")
675 {:ok, _} = Transmogrifier.handle_incoming(data)
677 refute User.get_cached_by_ap_id(ap_id)
680 test "it fails for incoming user deletes with spoofed origin" do
681 %{ap_id: ap_id} = insert(:user)
684 File.read!("test/fixtures/mastodon-delete-user.json")
686 |> Map.put("actor", ap_id)
688 assert :error == Transmogrifier.handle_incoming(data)
689 assert User.get_cached_by_ap_id(ap_id)
692 test "it works for incoming unannounces with an existing notice" do
694 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
697 File.read!("test/fixtures/mastodon-announce.json")
699 |> Map.put("object", activity.data["object"])
701 {:ok, %Activity{data: announce_data, local: false}} =
702 Transmogrifier.handle_incoming(announce_data)
705 File.read!("test/fixtures/mastodon-undo-announce.json")
707 |> Map.put("object", announce_data)
708 |> Map.put("actor", announce_data["actor"])
710 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
712 assert data["type"] == "Undo"
713 assert object_data = data["object"]
714 assert object_data["type"] == "Announce"
715 assert object_data["object"] == activity.data["object"]
717 assert object_data["id"] ==
718 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
721 test "it works for incomming unfollows with an existing follow" do
725 File.read!("test/fixtures/mastodon-follow-activity.json")
727 |> Map.put("object", user.ap_id)
729 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
732 File.read!("test/fixtures/mastodon-unfollow-activity.json")
734 |> Map.put("object", follow_data)
736 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
738 assert data["type"] == "Undo"
739 assert data["object"]["type"] == "Follow"
740 assert data["object"]["object"] == user.ap_id
741 assert data["actor"] == "http://mastodon.example.org/users/admin"
743 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
746 test "it works for incoming blocks" do
750 File.read!("test/fixtures/mastodon-block-activity.json")
752 |> Map.put("object", user.ap_id)
754 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
756 assert data["type"] == "Block"
757 assert data["object"] == user.ap_id
758 assert data["actor"] == "http://mastodon.example.org/users/admin"
760 blocker = User.get_cached_by_ap_id(data["actor"])
762 assert User.blocks?(blocker, user)
765 test "incoming blocks successfully tear down any follow relationship" do
766 blocker = insert(:user)
767 blocked = insert(:user)
770 File.read!("test/fixtures/mastodon-block-activity.json")
772 |> Map.put("object", blocked.ap_id)
773 |> Map.put("actor", blocker.ap_id)
775 {:ok, blocker} = User.follow(blocker, blocked)
776 {:ok, blocked} = User.follow(blocked, blocker)
778 assert User.following?(blocker, blocked)
779 assert User.following?(blocked, blocker)
781 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
783 assert data["type"] == "Block"
784 assert data["object"] == blocked.ap_id
785 assert data["actor"] == blocker.ap_id
787 blocker = User.get_cached_by_ap_id(data["actor"])
788 blocked = User.get_cached_by_ap_id(data["object"])
790 assert User.blocks?(blocker, blocked)
792 refute User.following?(blocker, blocked)
793 refute User.following?(blocked, blocker)
796 test "it works for incoming unblocks with an existing block" do
800 File.read!("test/fixtures/mastodon-block-activity.json")
802 |> Map.put("object", user.ap_id)
804 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
807 File.read!("test/fixtures/mastodon-unblock-activity.json")
809 |> Map.put("object", block_data)
811 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
812 assert data["type"] == "Undo"
813 assert data["object"]["type"] == "Block"
814 assert data["object"]["object"] == user.ap_id
815 assert data["actor"] == "http://mastodon.example.org/users/admin"
817 blocker = User.get_cached_by_ap_id(data["actor"])
819 refute User.blocks?(blocker, user)
822 test "it works for incoming accepts which were pre-accepted" do
823 follower = insert(:user)
824 followed = insert(:user)
826 {:ok, follower} = User.follow(follower, followed)
827 assert User.following?(follower, followed) == true
829 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
832 File.read!("test/fixtures/mastodon-accept-activity.json")
834 |> Map.put("actor", followed.ap_id)
837 accept_data["object"]
838 |> Map.put("actor", follower.ap_id)
839 |> Map.put("id", follow_activity.data["id"])
841 accept_data = Map.put(accept_data, "object", object)
843 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
844 refute activity.local
846 assert activity.data["object"] == follow_activity.data["id"]
848 follower = User.get_cached_by_id(follower.id)
850 assert User.following?(follower, followed) == true
853 test "it works for incoming accepts which were orphaned" do
854 follower = insert(:user)
855 followed = insert(:user, %{info: %User.Info{locked: true}})
857 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
860 File.read!("test/fixtures/mastodon-accept-activity.json")
862 |> Map.put("actor", followed.ap_id)
865 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
867 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
868 assert activity.data["object"] == follow_activity.data["id"]
870 follower = User.get_cached_by_id(follower.id)
872 assert User.following?(follower, followed) == true
875 test "it works for incoming accepts which are referenced by IRI only" do
876 follower = insert(:user)
877 followed = insert(:user, %{info: %User.Info{locked: true}})
879 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
882 File.read!("test/fixtures/mastodon-accept-activity.json")
884 |> Map.put("actor", followed.ap_id)
885 |> Map.put("object", follow_activity.data["id"])
887 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
888 assert activity.data["object"] == follow_activity.data["id"]
890 follower = User.get_cached_by_id(follower.id)
892 assert User.following?(follower, followed) == true
895 test "it fails for incoming accepts which cannot be correlated" do
896 follower = insert(:user)
897 followed = insert(:user, %{info: %User.Info{locked: true}})
900 File.read!("test/fixtures/mastodon-accept-activity.json")
902 |> Map.put("actor", followed.ap_id)
905 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
907 :error = Transmogrifier.handle_incoming(accept_data)
909 follower = User.get_cached_by_id(follower.id)
911 refute User.following?(follower, followed) == true
914 test "it fails for incoming rejects which cannot be correlated" do
915 follower = insert(:user)
916 followed = insert(:user, %{info: %User.Info{locked: true}})
919 File.read!("test/fixtures/mastodon-reject-activity.json")
921 |> Map.put("actor", followed.ap_id)
924 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
926 :error = Transmogrifier.handle_incoming(accept_data)
928 follower = User.get_cached_by_id(follower.id)
930 refute User.following?(follower, followed) == true
933 test "it works for incoming rejects which are orphaned" do
934 follower = insert(:user)
935 followed = insert(:user, %{info: %User.Info{locked: true}})
937 {:ok, follower} = User.follow(follower, followed)
938 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
940 assert User.following?(follower, followed) == true
943 File.read!("test/fixtures/mastodon-reject-activity.json")
945 |> Map.put("actor", followed.ap_id)
948 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
950 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
951 refute activity.local
953 follower = User.get_cached_by_id(follower.id)
955 assert User.following?(follower, followed) == false
958 test "it works for incoming rejects which are referenced by IRI only" do
959 follower = insert(:user)
960 followed = insert(:user, %{info: %User.Info{locked: true}})
962 {:ok, follower} = User.follow(follower, followed)
963 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
965 assert User.following?(follower, followed) == true
968 File.read!("test/fixtures/mastodon-reject-activity.json")
970 |> Map.put("actor", followed.ap_id)
971 |> Map.put("object", follow_activity.data["id"])
973 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
975 follower = User.get_cached_by_id(follower.id)
977 assert User.following?(follower, followed) == false
980 test "it rejects activities without a valid ID" do
984 File.read!("test/fixtures/mastodon-follow-activity.json")
986 |> Map.put("object", user.ap_id)
989 :error = Transmogrifier.handle_incoming(data)
992 test "it remaps video URLs as attachments if necessary" do
994 Fetcher.fetch_object_from_id(
995 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1000 "mediaType" => "video/mp4",
1002 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1003 "mimeType" => "video/mp4",
1004 "size" => 5_015_880,
1008 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1009 "mediaType" => "video/mp4",
1016 assert object.data["url"] ==
1017 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1019 assert object.data["attachment"] == [attachment]
1022 test "it accepts Flag activities" do
1023 user = insert(:user)
1024 other_user = insert(:user)
1026 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1027 object = Object.normalize(activity)
1030 "@context" => "https://www.w3.org/ns/activitystreams",
1031 "cc" => [user.ap_id],
1032 "object" => [user.ap_id, object.data["id"]],
1034 "content" => "blocked AND reported!!!",
1035 "actor" => other_user.ap_id
1038 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1040 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1041 assert activity.data["content"] == "blocked AND reported!!!"
1042 assert activity.data["actor"] == other_user.ap_id
1043 assert activity.data["cc"] == [user.ap_id]
1047 describe "prepare outgoing" do
1048 test "it turns mentions into tags" do
1049 user = insert(:user)
1050 other_user = insert(:user)
1053 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1055 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1056 object = modified["object"]
1058 expected_mention = %{
1059 "href" => other_user.ap_id,
1060 "name" => "@#{other_user.nickname}",
1065 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1066 "type" => "Hashtag",
1070 assert Enum.member?(object["tag"], expected_tag)
1071 assert Enum.member?(object["tag"], expected_mention)
1074 test "it adds the sensitive property" do
1075 user = insert(:user)
1077 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1078 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1080 assert modified["object"]["sensitive"]
1083 test "it adds the json-ld context and the conversation property" do
1084 user = insert(:user)
1086 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1087 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1089 assert modified["@context"] ==
1090 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1092 assert modified["object"]["conversation"] == modified["context"]
1095 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1096 user = insert(:user)
1098 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1099 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1101 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1104 test "it translates ostatus IDs to external URLs" do
1105 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1106 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1108 user = insert(:user)
1110 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1111 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1113 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1116 test "it translates ostatus reply_to IDs to external URLs" do
1117 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1118 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1120 user = insert(:user)
1123 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1125 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1127 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1130 test "it strips internal hashtag data" do
1131 user = insert(:user)
1133 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1136 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1137 "type" => "Hashtag",
1141 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1143 assert modified["object"]["tag"] == [expected_tag]
1146 test "it strips internal fields" do
1147 user = insert(:user)
1149 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1151 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1153 assert length(modified["object"]["tag"]) == 2
1155 assert is_nil(modified["object"]["emoji"])
1156 assert is_nil(modified["object"]["like_count"])
1157 assert is_nil(modified["object"]["announcements"])
1158 assert is_nil(modified["object"]["announcement_count"])
1159 assert is_nil(modified["object"]["context_id"])
1162 test "it strips internal fields of article" do
1163 activity = insert(:article_activity)
1165 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1167 assert length(modified["object"]["tag"]) == 2
1169 assert is_nil(modified["object"]["emoji"])
1170 assert is_nil(modified["object"]["like_count"])
1171 assert is_nil(modified["object"]["announcements"])
1172 assert is_nil(modified["object"]["announcement_count"])
1173 assert is_nil(modified["object"]["context_id"])
1174 assert is_nil(modified["object"]["likes"])
1177 test "the directMessage flag is present" do
1178 user = insert(:user)
1179 other_user = insert(:user)
1181 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1183 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1185 assert modified["directMessage"] == false
1188 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1190 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1192 assert modified["directMessage"] == false
1195 CommonAPI.post(user, %{
1196 "status" => "@#{other_user.nickname} :moominmamma:",
1197 "visibility" => "direct"
1200 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1202 assert modified["directMessage"] == true
1205 test "it strips BCC field" do
1206 user = insert(:user)
1207 {:ok, list} = Pleroma.List.create("foo", user)
1210 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1212 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1214 assert is_nil(modified["bcc"])
1218 describe "user upgrade" do
1219 test "it upgrades a user to activitypub" do
1222 nickname: "rye@niu.moe",
1224 ap_id: "https://niu.moe/users/rye",
1225 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1228 user_two = insert(:user, %{following: [user.follower_address]})
1230 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1231 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1232 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1234 user = User.get_cached_by_id(user.id)
1235 assert user.info.note_count == 1
1237 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1238 assert user.info.ap_enabled
1239 assert user.info.note_count == 1
1240 assert user.follower_address == "https://niu.moe/users/rye/followers"
1241 assert user.following_address == "https://niu.moe/users/rye/following"
1243 user = User.get_cached_by_id(user.id)
1244 assert user.info.note_count == 1
1246 activity = Activity.get_by_id(activity.id)
1247 assert user.follower_address in activity.recipients
1253 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1262 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1265 } = user.info.banner
1267 refute "..." in activity.recipients
1269 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1270 refute user.follower_address in unrelated_activity.recipients
1272 user_two = User.get_cached_by_id(user_two.id)
1273 assert user.follower_address in user_two.following
1274 refute "..." in user_two.following
1278 describe "maybe_retire_websub" do
1279 test "it deletes all websub client subscripitions with the user as topic" do
1280 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1281 {:ok, ws} = Repo.insert(subscription)
1283 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1284 {:ok, ws2} = Repo.insert(subscription)
1286 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1288 refute Repo.get(WebsubClientSubscription, ws.id)
1289 assert Repo.get(WebsubClientSubscription, ws2.id)
1293 describe "actor rewriting" do
1294 test "it fixes the actor URL property to be a proper URI" do
1296 "url" => %{"href" => "http://example.com"}
1299 rewritten = Transmogrifier.maybe_fix_user_object(data)
1300 assert rewritten["url"] == "http://example.com"
1304 describe "actor origin containment" do
1305 test "it rejects activities which reference objects with bogus origins" do
1307 "@context" => "https://www.w3.org/ns/activitystreams",
1308 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1309 "actor" => "http://mastodon.example.org/users/admin",
1310 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1311 "object" => "https://info.pleroma.site/activity.json",
1312 "type" => "Announce"
1315 :error = Transmogrifier.handle_incoming(data)
1318 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1320 "@context" => "https://www.w3.org/ns/activitystreams",
1321 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1322 "actor" => "http://mastodon.example.org/users/admin",
1323 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1324 "object" => "https://info.pleroma.site/activity2.json",
1325 "type" => "Announce"
1328 :error = Transmogrifier.handle_incoming(data)
1331 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1333 "@context" => "https://www.w3.org/ns/activitystreams",
1334 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1335 "actor" => "http://mastodon.example.org/users/admin",
1336 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1337 "object" => "https://info.pleroma.site/activity3.json",
1338 "type" => "Announce"
1341 :error = Transmogrifier.handle_incoming(data)
1345 describe "reserialization" do
1346 test "successfully reserializes a message with inReplyTo == nil" do
1347 user = insert(:user)
1350 "@context" => "https://www.w3.org/ns/activitystreams",
1351 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1355 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1360 "attributedTo" => user.ap_id
1362 "actor" => user.ap_id
1365 {:ok, activity} = Transmogrifier.handle_incoming(message)
1367 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1370 test "successfully reserializes a message with AS2 objects in IR" do
1371 user = insert(:user)
1374 "@context" => "https://www.w3.org/ns/activitystreams",
1375 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1379 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1384 "attributedTo" => user.ap_id,
1386 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1387 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1390 "actor" => user.ap_id
1393 {:ok, activity} = Transmogrifier.handle_incoming(message)
1395 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1399 test "Rewrites Answers to Notes" do
1400 user = insert(:user)
1402 {:ok, poll_activity} =
1403 CommonAPI.post(user, %{
1404 "status" => "suya...",
1405 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1408 poll_object = Object.normalize(poll_activity)
1409 # TODO: Replace with CommonAPI vote creation when implemented
1411 File.read!("test/fixtures/mastodon-vote.json")
1413 |> Kernel.put_in(["to"], user.ap_id)
1414 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1415 |> Kernel.put_in(["object", "to"], user.ap_id)
1417 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1418 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1420 assert data["object"]["type"] == "Note"
1423 describe "fix_explicit_addressing" do
1425 user = insert(:user)
1429 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1430 explicitly_mentioned_actors = [
1431 "https://pleroma.gold/users/user1",
1432 "https://pleroma.gold/user2"
1436 "actor" => user.ap_id,
1437 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1440 Enum.map(explicitly_mentioned_actors, fn href ->
1441 %{"type" => "Mention", "href" => href}
1445 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1446 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1447 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1448 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1451 test "does not move actor's follower collection to cc", %{user: user} do
1453 "actor" => user.ap_id,
1454 "to" => [user.follower_address],
1458 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1459 assert user.follower_address in fixed_object["to"]
1460 refute user.follower_address in fixed_object["cc"]
1463 test "removes recipient's follower collection from cc", %{user: user} do
1464 recipient = insert(:user)
1467 "actor" => user.ap_id,
1468 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1469 "cc" => [user.follower_address, recipient.follower_address]
1472 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1474 assert user.follower_address in fixed_object["cc"]
1475 refute recipient.follower_address in fixed_object["cc"]
1476 refute recipient.follower_address in fixed_object["to"]