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 announces" do
352 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
354 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
356 assert data["actor"] == "http://mastodon.example.org/users/admin"
357 assert data["type"] == "Announce"
360 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
362 assert data["object"] ==
363 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
365 assert Activity.get_create_by_object_ap_id(data["object"])
368 test "it works for incoming announces with an existing activity" do
370 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
373 File.read!("test/fixtures/mastodon-announce.json")
375 |> Map.put("object", activity.data["object"])
377 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
379 assert data["actor"] == "http://mastodon.example.org/users/admin"
380 assert data["type"] == "Announce"
383 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
385 assert data["object"] == activity.data["object"]
387 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
390 test "it does not clobber the addressing on announce activities" do
392 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
395 File.read!("test/fixtures/mastodon-announce.json")
397 |> Map.put("object", Object.normalize(activity).data["id"])
398 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
401 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
403 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
406 test "it ensures that as:Public activities make it to their followers collection" do
410 File.read!("test/fixtures/mastodon-post-activity.json")
412 |> Map.put("actor", user.ap_id)
413 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
418 |> Map.put("attributedTo", user.ap_id)
419 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
421 |> Map.put("id", user.ap_id <> "/activities/12345678")
423 data = Map.put(data, "object", object)
425 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
427 assert data["cc"] == [User.ap_followers(user)]
430 test "it ensures that address fields become lists" do
434 File.read!("test/fixtures/mastodon-post-activity.json")
436 |> Map.put("actor", user.ap_id)
437 |> Map.put("to", nil)
438 |> Map.put("cc", nil)
442 |> Map.put("attributedTo", user.ap_id)
443 |> Map.put("to", nil)
444 |> Map.put("cc", nil)
445 |> Map.put("id", user.ap_id <> "/activities/12345678")
447 data = Map.put(data, "object", object)
449 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
451 assert !is_nil(data["to"])
452 assert !is_nil(data["cc"])
455 test "it strips internal likes" do
457 File.read!("test/fixtures/mastodon-post-activity.json")
462 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
463 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
465 "type" => "OrderedCollection"
468 object = Map.put(data["object"], "likes", likes)
469 data = Map.put(data, "object", object)
471 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
473 refute Map.has_key?(object.data, "likes")
476 test "it works for incoming update activities" do
477 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
479 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
480 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
483 update_data["object"]
484 |> Map.put("actor", data["actor"])
485 |> Map.put("id", data["actor"])
489 |> Map.put("actor", data["actor"])
490 |> Map.put("object", object)
492 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
494 user = User.get_cached_by_ap_id(data["actor"])
495 assert user.name == "gargle"
497 assert user.avatar["url"] == [
500 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
504 assert user.info.banner["url"] == [
507 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
511 assert user.bio == "<p>Some bio</p>"
514 test "it works with custom profile fields" do
516 "test/fixtures/mastodon-post-activity.json"
519 |> Transmogrifier.handle_incoming()
521 user = User.get_cached_by_ap_id(activity.actor)
523 assert User.Info.fields(user.info) == [
524 %{"name" => "foo", "value" => "bar"},
525 %{"name" => "foo1", "value" => "bar1"}
528 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
531 update_data["object"]
532 |> Map.put("actor", user.ap_id)
533 |> Map.put("id", user.ap_id)
537 |> Map.put("actor", user.ap_id)
538 |> Map.put("object", object)
540 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
542 user = User.get_cached_by_ap_id(user.ap_id)
544 assert User.Info.fields(user.info) == [
545 %{"name" => "foo", "value" => "updated"},
546 %{"name" => "foo1", "value" => "updated"}
549 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
552 put_in(update_data, ["object", "attachment"], [
553 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
554 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
555 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
558 {:ok, _} = Transmogrifier.handle_incoming(update_data)
560 user = User.get_cached_by_ap_id(user.ap_id)
562 assert User.Info.fields(user.info) == [
563 %{"name" => "foo", "value" => "updated"},
564 %{"name" => "foo1", "value" => "updated"}
567 update_data = put_in(update_data, ["object", "attachment"], [])
569 {:ok, _} = Transmogrifier.handle_incoming(update_data)
571 user = User.get_cached_by_ap_id(user.ap_id)
573 assert User.Info.fields(user.info) == []
576 test "it works for incoming update activities which lock the account" do
577 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
579 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
580 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
583 update_data["object"]
584 |> Map.put("actor", data["actor"])
585 |> Map.put("id", data["actor"])
586 |> Map.put("manuallyApprovesFollowers", true)
590 |> Map.put("actor", data["actor"])
591 |> Map.put("object", object)
593 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
595 user = User.get_cached_by_ap_id(data["actor"])
596 assert user.info.locked == true
599 test "it works for incoming deletes" do
600 activity = insert(:note_activity)
603 File.read!("test/fixtures/mastodon-delete.json")
608 |> Map.put("id", activity.data["object"])
612 |> Map.put("object", object)
613 |> Map.put("actor", activity.data["actor"])
615 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
617 refute Activity.get_by_id(activity.id)
620 test "it fails for incoming deletes with spoofed origin" do
621 activity = insert(:note_activity)
624 File.read!("test/fixtures/mastodon-delete.json")
629 |> Map.put("id", activity.data["object"])
633 |> Map.put("object", object)
635 assert capture_log(fn ->
636 :error = Transmogrifier.handle_incoming(data)
638 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
640 assert Activity.get_by_id(activity.id)
643 test "it works for incoming user deletes" do
644 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
647 File.read!("test/fixtures/mastodon-delete-user.json")
650 {:ok, _} = Transmogrifier.handle_incoming(data)
652 refute User.get_cached_by_ap_id(ap_id)
655 test "it fails for incoming user deletes with spoofed origin" do
656 %{ap_id: ap_id} = insert(:user)
659 File.read!("test/fixtures/mastodon-delete-user.json")
661 |> Map.put("actor", ap_id)
663 assert :error == Transmogrifier.handle_incoming(data)
664 assert User.get_cached_by_ap_id(ap_id)
667 test "it works for incoming unannounces with an existing notice" do
669 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
672 File.read!("test/fixtures/mastodon-announce.json")
674 |> Map.put("object", activity.data["object"])
676 {:ok, %Activity{data: announce_data, local: false}} =
677 Transmogrifier.handle_incoming(announce_data)
680 File.read!("test/fixtures/mastodon-undo-announce.json")
682 |> Map.put("object", announce_data)
683 |> Map.put("actor", announce_data["actor"])
685 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
687 assert data["type"] == "Undo"
688 assert object_data = data["object"]
689 assert object_data["type"] == "Announce"
690 assert object_data["object"] == activity.data["object"]
692 assert object_data["id"] ==
693 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
696 test "it works for incomming unfollows with an existing follow" do
700 File.read!("test/fixtures/mastodon-follow-activity.json")
702 |> Map.put("object", user.ap_id)
704 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
707 File.read!("test/fixtures/mastodon-unfollow-activity.json")
709 |> Map.put("object", follow_data)
711 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
713 assert data["type"] == "Undo"
714 assert data["object"]["type"] == "Follow"
715 assert data["object"]["object"] == user.ap_id
716 assert data["actor"] == "http://mastodon.example.org/users/admin"
718 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
721 test "it works for incoming blocks" do
725 File.read!("test/fixtures/mastodon-block-activity.json")
727 |> Map.put("object", user.ap_id)
729 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
731 assert data["type"] == "Block"
732 assert data["object"] == user.ap_id
733 assert data["actor"] == "http://mastodon.example.org/users/admin"
735 blocker = User.get_cached_by_ap_id(data["actor"])
737 assert User.blocks?(blocker, user)
740 test "incoming blocks successfully tear down any follow relationship" do
741 blocker = insert(:user)
742 blocked = insert(:user)
745 File.read!("test/fixtures/mastodon-block-activity.json")
747 |> Map.put("object", blocked.ap_id)
748 |> Map.put("actor", blocker.ap_id)
750 {:ok, blocker} = User.follow(blocker, blocked)
751 {:ok, blocked} = User.follow(blocked, blocker)
753 assert User.following?(blocker, blocked)
754 assert User.following?(blocked, blocker)
756 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
758 assert data["type"] == "Block"
759 assert data["object"] == blocked.ap_id
760 assert data["actor"] == blocker.ap_id
762 blocker = User.get_cached_by_ap_id(data["actor"])
763 blocked = User.get_cached_by_ap_id(data["object"])
765 assert User.blocks?(blocker, blocked)
767 refute User.following?(blocker, blocked)
768 refute User.following?(blocked, blocker)
771 test "it works for incoming unblocks with an existing block" do
775 File.read!("test/fixtures/mastodon-block-activity.json")
777 |> Map.put("object", user.ap_id)
779 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
782 File.read!("test/fixtures/mastodon-unblock-activity.json")
784 |> Map.put("object", block_data)
786 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
787 assert data["type"] == "Undo"
788 assert data["object"]["type"] == "Block"
789 assert data["object"]["object"] == user.ap_id
790 assert data["actor"] == "http://mastodon.example.org/users/admin"
792 blocker = User.get_cached_by_ap_id(data["actor"])
794 refute User.blocks?(blocker, user)
797 test "it works for incoming accepts which were pre-accepted" do
798 follower = insert(:user)
799 followed = insert(:user)
801 {:ok, follower} = User.follow(follower, followed)
802 assert User.following?(follower, followed) == true
804 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
807 File.read!("test/fixtures/mastodon-accept-activity.json")
809 |> Map.put("actor", followed.ap_id)
812 accept_data["object"]
813 |> Map.put("actor", follower.ap_id)
814 |> Map.put("id", follow_activity.data["id"])
816 accept_data = Map.put(accept_data, "object", object)
818 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
819 refute activity.local
821 assert activity.data["object"] == follow_activity.data["id"]
823 follower = User.get_cached_by_id(follower.id)
825 assert User.following?(follower, followed) == true
828 test "it works for incoming accepts which were orphaned" do
829 follower = insert(:user)
830 followed = insert(:user, %{info: %User.Info{locked: true}})
832 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
835 File.read!("test/fixtures/mastodon-accept-activity.json")
837 |> Map.put("actor", followed.ap_id)
840 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
842 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
843 assert activity.data["object"] == follow_activity.data["id"]
845 follower = User.get_cached_by_id(follower.id)
847 assert User.following?(follower, followed) == true
850 test "it works for incoming accepts which are referenced by IRI only" do
851 follower = insert(:user)
852 followed = insert(:user, %{info: %User.Info{locked: true}})
854 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
857 File.read!("test/fixtures/mastodon-accept-activity.json")
859 |> Map.put("actor", followed.ap_id)
860 |> Map.put("object", follow_activity.data["id"])
862 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
863 assert activity.data["object"] == follow_activity.data["id"]
865 follower = User.get_cached_by_id(follower.id)
867 assert User.following?(follower, followed) == true
870 test "it fails for incoming accepts which cannot be correlated" do
871 follower = insert(:user)
872 followed = insert(:user, %{info: %User.Info{locked: true}})
875 File.read!("test/fixtures/mastodon-accept-activity.json")
877 |> Map.put("actor", followed.ap_id)
880 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
882 :error = Transmogrifier.handle_incoming(accept_data)
884 follower = User.get_cached_by_id(follower.id)
886 refute User.following?(follower, followed) == true
889 test "it fails for incoming rejects which cannot be correlated" do
890 follower = insert(:user)
891 followed = insert(:user, %{info: %User.Info{locked: true}})
894 File.read!("test/fixtures/mastodon-reject-activity.json")
896 |> Map.put("actor", followed.ap_id)
899 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
901 :error = Transmogrifier.handle_incoming(accept_data)
903 follower = User.get_cached_by_id(follower.id)
905 refute User.following?(follower, followed) == true
908 test "it works for incoming rejects which are orphaned" do
909 follower = insert(:user)
910 followed = insert(:user, %{info: %User.Info{locked: true}})
912 {:ok, follower} = User.follow(follower, followed)
913 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
915 assert User.following?(follower, followed) == true
918 File.read!("test/fixtures/mastodon-reject-activity.json")
920 |> Map.put("actor", followed.ap_id)
923 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
925 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
926 refute activity.local
928 follower = User.get_cached_by_id(follower.id)
930 assert User.following?(follower, followed) == false
933 test "it works for incoming rejects which are referenced by IRI only" 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)
946 |> Map.put("object", follow_activity.data["id"])
948 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
950 follower = User.get_cached_by_id(follower.id)
952 assert User.following?(follower, followed) == false
955 test "it rejects activities without a valid ID" do
959 File.read!("test/fixtures/mastodon-follow-activity.json")
961 |> Map.put("object", user.ap_id)
964 :error = Transmogrifier.handle_incoming(data)
967 test "it remaps video URLs as attachments if necessary" do
969 Fetcher.fetch_object_from_id(
970 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
975 "mediaType" => "video/mp4",
977 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
978 "mimeType" => "video/mp4",
983 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
984 "mediaType" => "video/mp4",
991 assert object.data["url"] ==
992 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
994 assert object.data["attachment"] == [attachment]
997 test "it accepts Flag activities" do
999 other_user = insert(:user)
1001 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1002 object = Object.normalize(activity)
1005 "@context" => "https://www.w3.org/ns/activitystreams",
1006 "cc" => [user.ap_id],
1007 "object" => [user.ap_id, object.data["id"]],
1009 "content" => "blocked AND reported!!!",
1010 "actor" => other_user.ap_id
1013 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1015 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1016 assert activity.data["content"] == "blocked AND reported!!!"
1017 assert activity.data["actor"] == other_user.ap_id
1018 assert activity.data["cc"] == [user.ap_id]
1022 describe "prepare outgoing" do
1023 test "it turns mentions into tags" do
1024 user = insert(:user)
1025 other_user = insert(:user)
1028 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1030 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1031 object = modified["object"]
1033 expected_mention = %{
1034 "href" => other_user.ap_id,
1035 "name" => "@#{other_user.nickname}",
1040 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1041 "type" => "Hashtag",
1045 assert Enum.member?(object["tag"], expected_tag)
1046 assert Enum.member?(object["tag"], expected_mention)
1049 test "it adds the sensitive property" do
1050 user = insert(:user)
1052 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1053 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1055 assert modified["object"]["sensitive"]
1058 test "it adds the json-ld context and the conversation property" do
1059 user = insert(:user)
1061 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1062 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1064 assert modified["@context"] ==
1065 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1067 assert modified["object"]["conversation"] == modified["context"]
1070 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1071 user = insert(:user)
1073 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1074 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1076 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1079 test "it translates ostatus IDs to external URLs" do
1080 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1081 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1083 user = insert(:user)
1085 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1086 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1088 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1091 test "it translates ostatus reply_to IDs to external URLs" do
1092 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1093 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1095 user = insert(:user)
1098 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1100 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1102 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1105 test "it strips internal hashtag data" do
1106 user = insert(:user)
1108 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1111 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1112 "type" => "Hashtag",
1116 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1118 assert modified["object"]["tag"] == [expected_tag]
1121 test "it strips internal fields" do
1122 user = insert(:user)
1124 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1126 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1128 assert length(modified["object"]["tag"]) == 2
1130 assert is_nil(modified["object"]["emoji"])
1131 assert is_nil(modified["object"]["like_count"])
1132 assert is_nil(modified["object"]["announcements"])
1133 assert is_nil(modified["object"]["announcement_count"])
1134 assert is_nil(modified["object"]["context_id"])
1137 test "it strips internal fields of article" do
1138 activity = insert(:article_activity)
1140 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1142 assert length(modified["object"]["tag"]) == 2
1144 assert is_nil(modified["object"]["emoji"])
1145 assert is_nil(modified["object"]["like_count"])
1146 assert is_nil(modified["object"]["announcements"])
1147 assert is_nil(modified["object"]["announcement_count"])
1148 assert is_nil(modified["object"]["context_id"])
1149 assert is_nil(modified["object"]["likes"])
1152 test "the directMessage flag is present" do
1153 user = insert(:user)
1154 other_user = insert(:user)
1156 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1158 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1160 assert modified["directMessage"] == false
1163 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1165 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1167 assert modified["directMessage"] == false
1170 CommonAPI.post(user, %{
1171 "status" => "@#{other_user.nickname} :moominmamma:",
1172 "visibility" => "direct"
1175 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1177 assert modified["directMessage"] == true
1180 test "it strips BCC field" do
1181 user = insert(:user)
1182 {:ok, list} = Pleroma.List.create("foo", user)
1185 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1187 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1189 assert is_nil(modified["bcc"])
1193 describe "user upgrade" do
1194 test "it upgrades a user to activitypub" do
1197 nickname: "rye@niu.moe",
1199 ap_id: "https://niu.moe/users/rye",
1200 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1203 user_two = insert(:user, %{following: [user.follower_address]})
1205 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1206 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1207 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1209 user = User.get_cached_by_id(user.id)
1210 assert user.info.note_count == 1
1212 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1213 assert user.info.ap_enabled
1214 assert user.info.note_count == 1
1215 assert user.follower_address == "https://niu.moe/users/rye/followers"
1216 assert user.following_address == "https://niu.moe/users/rye/following"
1218 user = User.get_cached_by_id(user.id)
1219 assert user.info.note_count == 1
1221 activity = Activity.get_by_id(activity.id)
1222 assert user.follower_address in activity.recipients
1228 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1237 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1240 } = user.info.banner
1242 refute "..." in activity.recipients
1244 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1245 refute user.follower_address in unrelated_activity.recipients
1247 user_two = User.get_cached_by_id(user_two.id)
1248 assert user.follower_address in user_two.following
1249 refute "..." in user_two.following
1253 describe "maybe_retire_websub" do
1254 test "it deletes all websub client subscripitions with the user as topic" do
1255 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1256 {:ok, ws} = Repo.insert(subscription)
1258 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1259 {:ok, ws2} = Repo.insert(subscription)
1261 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1263 refute Repo.get(WebsubClientSubscription, ws.id)
1264 assert Repo.get(WebsubClientSubscription, ws2.id)
1268 describe "actor rewriting" do
1269 test "it fixes the actor URL property to be a proper URI" do
1271 "url" => %{"href" => "http://example.com"}
1274 rewritten = Transmogrifier.maybe_fix_user_object(data)
1275 assert rewritten["url"] == "http://example.com"
1279 describe "actor origin containment" do
1280 test "it rejects activities which reference objects with bogus origins" do
1282 "@context" => "https://www.w3.org/ns/activitystreams",
1283 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1284 "actor" => "http://mastodon.example.org/users/admin",
1285 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1286 "object" => "https://info.pleroma.site/activity.json",
1287 "type" => "Announce"
1290 :error = Transmogrifier.handle_incoming(data)
1293 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1295 "@context" => "https://www.w3.org/ns/activitystreams",
1296 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1297 "actor" => "http://mastodon.example.org/users/admin",
1298 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1299 "object" => "https://info.pleroma.site/activity2.json",
1300 "type" => "Announce"
1303 :error = Transmogrifier.handle_incoming(data)
1306 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1308 "@context" => "https://www.w3.org/ns/activitystreams",
1309 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1310 "actor" => "http://mastodon.example.org/users/admin",
1311 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1312 "object" => "https://info.pleroma.site/activity3.json",
1313 "type" => "Announce"
1316 :error = Transmogrifier.handle_incoming(data)
1320 describe "reserialization" do
1321 test "successfully reserializes a message with inReplyTo == nil" do
1322 user = insert(:user)
1325 "@context" => "https://www.w3.org/ns/activitystreams",
1326 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1330 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1335 "attributedTo" => user.ap_id
1337 "actor" => user.ap_id
1340 {:ok, activity} = Transmogrifier.handle_incoming(message)
1342 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1345 test "successfully reserializes a message with AS2 objects in IR" do
1346 user = insert(:user)
1349 "@context" => "https://www.w3.org/ns/activitystreams",
1350 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1354 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1359 "attributedTo" => user.ap_id,
1361 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1362 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1365 "actor" => user.ap_id
1368 {:ok, activity} = Transmogrifier.handle_incoming(message)
1370 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1374 test "Rewrites Answers to Notes" do
1375 user = insert(:user)
1377 {:ok, poll_activity} =
1378 CommonAPI.post(user, %{
1379 "status" => "suya...",
1380 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1383 poll_object = Object.normalize(poll_activity)
1384 # TODO: Replace with CommonAPI vote creation when implemented
1386 File.read!("test/fixtures/mastodon-vote.json")
1388 |> Kernel.put_in(["to"], user.ap_id)
1389 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1390 |> Kernel.put_in(["object", "to"], user.ap_id)
1392 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1393 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1395 assert data["object"]["type"] == "Note"
1398 describe "fix_explicit_addressing" do
1400 user = insert(:user)
1404 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1405 explicitly_mentioned_actors = [
1406 "https://pleroma.gold/users/user1",
1407 "https://pleroma.gold/user2"
1411 "actor" => user.ap_id,
1412 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1415 Enum.map(explicitly_mentioned_actors, fn href ->
1416 %{"type" => "Mention", "href" => href}
1420 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1421 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1422 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1423 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1426 test "does not move actor's follower collection to cc", %{user: user} do
1428 "actor" => user.ap_id,
1429 "to" => [user.follower_address],
1433 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1434 assert user.follower_address in fixed_object["to"]
1435 refute user.follower_address in fixed_object["cc"]
1438 test "removes recipient's follower collection from cc", %{user: user} do
1439 recipient = insert(:user)
1442 "actor" => user.ap_id,
1443 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1444 "cc" => [user.follower_address, recipient.follower_address]
1447 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1449 assert user.follower_address in fixed_object["cc"]
1450 refute recipient.follower_address in fixed_object["cc"]
1451 refute recipient.follower_address in fixed_object["to"]