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"}
568 test "it works for incoming update activities which lock the account" do
569 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
571 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
572 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
575 update_data["object"]
576 |> Map.put("actor", data["actor"])
577 |> Map.put("id", data["actor"])
578 |> Map.put("manuallyApprovesFollowers", true)
582 |> Map.put("actor", data["actor"])
583 |> Map.put("object", object)
585 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
587 user = User.get_cached_by_ap_id(data["actor"])
588 assert user.info.locked == true
591 test "it works for incoming deletes" do
592 activity = insert(:note_activity)
595 File.read!("test/fixtures/mastodon-delete.json")
600 |> Map.put("id", activity.data["object"])
604 |> Map.put("object", object)
605 |> Map.put("actor", activity.data["actor"])
607 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
609 refute Activity.get_by_id(activity.id)
612 test "it fails for incoming deletes with spoofed origin" do
613 activity = insert(:note_activity)
616 File.read!("test/fixtures/mastodon-delete.json")
621 |> Map.put("id", activity.data["object"])
625 |> Map.put("object", object)
627 assert capture_log(fn ->
628 :error = Transmogrifier.handle_incoming(data)
630 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
632 assert Activity.get_by_id(activity.id)
635 test "it works for incoming user deletes" do
636 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
639 File.read!("test/fixtures/mastodon-delete-user.json")
642 {:ok, _} = Transmogrifier.handle_incoming(data)
644 refute User.get_cached_by_ap_id(ap_id)
647 test "it fails for incoming user deletes with spoofed origin" do
648 %{ap_id: ap_id} = insert(:user)
651 File.read!("test/fixtures/mastodon-delete-user.json")
653 |> Map.put("actor", ap_id)
655 assert :error == Transmogrifier.handle_incoming(data)
656 assert User.get_cached_by_ap_id(ap_id)
659 test "it works for incoming unannounces with an existing notice" do
661 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
664 File.read!("test/fixtures/mastodon-announce.json")
666 |> Map.put("object", activity.data["object"])
668 {:ok, %Activity{data: announce_data, local: false}} =
669 Transmogrifier.handle_incoming(announce_data)
672 File.read!("test/fixtures/mastodon-undo-announce.json")
674 |> Map.put("object", announce_data)
675 |> Map.put("actor", announce_data["actor"])
677 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
679 assert data["type"] == "Undo"
680 assert object_data = data["object"]
681 assert object_data["type"] == "Announce"
682 assert object_data["object"] == activity.data["object"]
684 assert object_data["id"] ==
685 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
688 test "it works for incomming unfollows with an existing follow" do
692 File.read!("test/fixtures/mastodon-follow-activity.json")
694 |> Map.put("object", user.ap_id)
696 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
699 File.read!("test/fixtures/mastodon-unfollow-activity.json")
701 |> Map.put("object", follow_data)
703 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
705 assert data["type"] == "Undo"
706 assert data["object"]["type"] == "Follow"
707 assert data["object"]["object"] == user.ap_id
708 assert data["actor"] == "http://mastodon.example.org/users/admin"
710 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
713 test "it works for incoming blocks" do
717 File.read!("test/fixtures/mastodon-block-activity.json")
719 |> Map.put("object", user.ap_id)
721 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
723 assert data["type"] == "Block"
724 assert data["object"] == user.ap_id
725 assert data["actor"] == "http://mastodon.example.org/users/admin"
727 blocker = User.get_cached_by_ap_id(data["actor"])
729 assert User.blocks?(blocker, user)
732 test "incoming blocks successfully tear down any follow relationship" do
733 blocker = insert(:user)
734 blocked = insert(:user)
737 File.read!("test/fixtures/mastodon-block-activity.json")
739 |> Map.put("object", blocked.ap_id)
740 |> Map.put("actor", blocker.ap_id)
742 {:ok, blocker} = User.follow(blocker, blocked)
743 {:ok, blocked} = User.follow(blocked, blocker)
745 assert User.following?(blocker, blocked)
746 assert User.following?(blocked, blocker)
748 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
750 assert data["type"] == "Block"
751 assert data["object"] == blocked.ap_id
752 assert data["actor"] == blocker.ap_id
754 blocker = User.get_cached_by_ap_id(data["actor"])
755 blocked = User.get_cached_by_ap_id(data["object"])
757 assert User.blocks?(blocker, blocked)
759 refute User.following?(blocker, blocked)
760 refute User.following?(blocked, blocker)
763 test "it works for incoming unblocks with an existing block" do
767 File.read!("test/fixtures/mastodon-block-activity.json")
769 |> Map.put("object", user.ap_id)
771 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
774 File.read!("test/fixtures/mastodon-unblock-activity.json")
776 |> Map.put("object", block_data)
778 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
779 assert data["type"] == "Undo"
780 assert data["object"]["type"] == "Block"
781 assert data["object"]["object"] == user.ap_id
782 assert data["actor"] == "http://mastodon.example.org/users/admin"
784 blocker = User.get_cached_by_ap_id(data["actor"])
786 refute User.blocks?(blocker, user)
789 test "it works for incoming accepts which were pre-accepted" do
790 follower = insert(:user)
791 followed = insert(:user)
793 {:ok, follower} = User.follow(follower, followed)
794 assert User.following?(follower, followed) == true
796 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
799 File.read!("test/fixtures/mastodon-accept-activity.json")
801 |> Map.put("actor", followed.ap_id)
804 accept_data["object"]
805 |> Map.put("actor", follower.ap_id)
806 |> Map.put("id", follow_activity.data["id"])
808 accept_data = Map.put(accept_data, "object", object)
810 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
811 refute activity.local
813 assert activity.data["object"] == follow_activity.data["id"]
815 follower = User.get_cached_by_id(follower.id)
817 assert User.following?(follower, followed) == true
820 test "it works for incoming accepts which were orphaned" do
821 follower = insert(:user)
822 followed = insert(:user, %{info: %User.Info{locked: true}})
824 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
827 File.read!("test/fixtures/mastodon-accept-activity.json")
829 |> Map.put("actor", followed.ap_id)
832 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
834 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
835 assert activity.data["object"] == follow_activity.data["id"]
837 follower = User.get_cached_by_id(follower.id)
839 assert User.following?(follower, followed) == true
842 test "it works for incoming accepts which are referenced by IRI only" do
843 follower = insert(:user)
844 followed = insert(:user, %{info: %User.Info{locked: true}})
846 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
849 File.read!("test/fixtures/mastodon-accept-activity.json")
851 |> Map.put("actor", followed.ap_id)
852 |> Map.put("object", follow_activity.data["id"])
854 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
855 assert activity.data["object"] == follow_activity.data["id"]
857 follower = User.get_cached_by_id(follower.id)
859 assert User.following?(follower, followed) == true
862 test "it fails for incoming accepts which cannot be correlated" do
863 follower = insert(:user)
864 followed = insert(:user, %{info: %User.Info{locked: true}})
867 File.read!("test/fixtures/mastodon-accept-activity.json")
869 |> Map.put("actor", followed.ap_id)
872 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
874 :error = Transmogrifier.handle_incoming(accept_data)
876 follower = User.get_cached_by_id(follower.id)
878 refute User.following?(follower, followed) == true
881 test "it fails for incoming rejects which cannot be correlated" do
882 follower = insert(:user)
883 followed = insert(:user, %{info: %User.Info{locked: true}})
886 File.read!("test/fixtures/mastodon-reject-activity.json")
888 |> Map.put("actor", followed.ap_id)
891 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
893 :error = Transmogrifier.handle_incoming(accept_data)
895 follower = User.get_cached_by_id(follower.id)
897 refute User.following?(follower, followed) == true
900 test "it works for incoming rejects which are orphaned" do
901 follower = insert(:user)
902 followed = insert(:user, %{info: %User.Info{locked: true}})
904 {:ok, follower} = User.follow(follower, followed)
905 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
907 assert User.following?(follower, followed) == true
910 File.read!("test/fixtures/mastodon-reject-activity.json")
912 |> Map.put("actor", followed.ap_id)
915 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
917 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
918 refute activity.local
920 follower = User.get_cached_by_id(follower.id)
922 assert User.following?(follower, followed) == false
925 test "it works for incoming rejects which are referenced by IRI only" do
926 follower = insert(:user)
927 followed = insert(:user, %{info: %User.Info{locked: true}})
929 {:ok, follower} = User.follow(follower, followed)
930 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
932 assert User.following?(follower, followed) == true
935 File.read!("test/fixtures/mastodon-reject-activity.json")
937 |> Map.put("actor", followed.ap_id)
938 |> Map.put("object", follow_activity.data["id"])
940 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
942 follower = User.get_cached_by_id(follower.id)
944 assert User.following?(follower, followed) == false
947 test "it rejects activities without a valid ID" do
951 File.read!("test/fixtures/mastodon-follow-activity.json")
953 |> Map.put("object", user.ap_id)
956 :error = Transmogrifier.handle_incoming(data)
959 test "it remaps video URLs as attachments if necessary" do
961 Fetcher.fetch_object_from_id(
962 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
967 "mediaType" => "video/mp4",
969 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
970 "mimeType" => "video/mp4",
975 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
976 "mediaType" => "video/mp4",
983 assert object.data["url"] ==
984 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
986 assert object.data["attachment"] == [attachment]
989 test "it accepts Flag activities" do
991 other_user = insert(:user)
993 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
994 object = Object.normalize(activity)
997 "@context" => "https://www.w3.org/ns/activitystreams",
998 "cc" => [user.ap_id],
999 "object" => [user.ap_id, object.data["id"]],
1001 "content" => "blocked AND reported!!!",
1002 "actor" => other_user.ap_id
1005 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1007 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1008 assert activity.data["content"] == "blocked AND reported!!!"
1009 assert activity.data["actor"] == other_user.ap_id
1010 assert activity.data["cc"] == [user.ap_id]
1014 describe "prepare outgoing" do
1015 test "it turns mentions into tags" do
1016 user = insert(:user)
1017 other_user = insert(:user)
1020 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1022 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1023 object = modified["object"]
1025 expected_mention = %{
1026 "href" => other_user.ap_id,
1027 "name" => "@#{other_user.nickname}",
1032 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1033 "type" => "Hashtag",
1037 assert Enum.member?(object["tag"], expected_tag)
1038 assert Enum.member?(object["tag"], expected_mention)
1041 test "it adds the sensitive property" do
1042 user = insert(:user)
1044 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1045 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1047 assert modified["object"]["sensitive"]
1050 test "it adds the json-ld context and the conversation property" do
1051 user = insert(:user)
1053 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1054 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1056 assert modified["@context"] ==
1057 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1059 assert modified["object"]["conversation"] == modified["context"]
1062 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1063 user = insert(:user)
1065 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1066 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1068 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1071 test "it translates ostatus IDs to external URLs" do
1072 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1073 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1075 user = insert(:user)
1077 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1078 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1080 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1083 test "it translates ostatus reply_to IDs to external URLs" do
1084 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1085 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1087 user = insert(:user)
1090 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1092 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1094 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1097 test "it strips internal hashtag data" do
1098 user = insert(:user)
1100 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1103 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1104 "type" => "Hashtag",
1108 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1110 assert modified["object"]["tag"] == [expected_tag]
1113 test "it strips internal fields" do
1114 user = insert(:user)
1116 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1118 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1120 assert length(modified["object"]["tag"]) == 2
1122 assert is_nil(modified["object"]["emoji"])
1123 assert is_nil(modified["object"]["like_count"])
1124 assert is_nil(modified["object"]["announcements"])
1125 assert is_nil(modified["object"]["announcement_count"])
1126 assert is_nil(modified["object"]["context_id"])
1129 test "it strips internal fields of article" do
1130 activity = insert(:article_activity)
1132 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1134 assert length(modified["object"]["tag"]) == 2
1136 assert is_nil(modified["object"]["emoji"])
1137 assert is_nil(modified["object"]["like_count"])
1138 assert is_nil(modified["object"]["announcements"])
1139 assert is_nil(modified["object"]["announcement_count"])
1140 assert is_nil(modified["object"]["context_id"])
1141 assert is_nil(modified["object"]["likes"])
1144 test "the directMessage flag is present" do
1145 user = insert(:user)
1146 other_user = insert(:user)
1148 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1150 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1152 assert modified["directMessage"] == false
1155 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1157 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1159 assert modified["directMessage"] == false
1162 CommonAPI.post(user, %{
1163 "status" => "@#{other_user.nickname} :moominmamma:",
1164 "visibility" => "direct"
1167 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1169 assert modified["directMessage"] == true
1172 test "it strips BCC field" do
1173 user = insert(:user)
1174 {:ok, list} = Pleroma.List.create("foo", user)
1177 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1179 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1181 assert is_nil(modified["bcc"])
1185 describe "user upgrade" do
1186 test "it upgrades a user to activitypub" do
1189 nickname: "rye@niu.moe",
1191 ap_id: "https://niu.moe/users/rye",
1192 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1195 user_two = insert(:user, %{following: [user.follower_address]})
1197 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1198 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1199 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1201 user = User.get_cached_by_id(user.id)
1202 assert user.info.note_count == 1
1204 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1205 assert user.info.ap_enabled
1206 assert user.info.note_count == 1
1207 assert user.follower_address == "https://niu.moe/users/rye/followers"
1208 assert user.following_address == "https://niu.moe/users/rye/following"
1210 user = User.get_cached_by_id(user.id)
1211 assert user.info.note_count == 1
1213 activity = Activity.get_by_id(activity.id)
1214 assert user.follower_address in activity.recipients
1220 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1229 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1232 } = user.info.banner
1234 refute "..." in activity.recipients
1236 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1237 refute user.follower_address in unrelated_activity.recipients
1239 user_two = User.get_cached_by_id(user_two.id)
1240 assert user.follower_address in user_two.following
1241 refute "..." in user_two.following
1245 describe "maybe_retire_websub" do
1246 test "it deletes all websub client subscripitions with the user as topic" do
1247 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1248 {:ok, ws} = Repo.insert(subscription)
1250 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1251 {:ok, ws2} = Repo.insert(subscription)
1253 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1255 refute Repo.get(WebsubClientSubscription, ws.id)
1256 assert Repo.get(WebsubClientSubscription, ws2.id)
1260 describe "actor rewriting" do
1261 test "it fixes the actor URL property to be a proper URI" do
1263 "url" => %{"href" => "http://example.com"}
1266 rewritten = Transmogrifier.maybe_fix_user_object(data)
1267 assert rewritten["url"] == "http://example.com"
1271 describe "actor origin containment" do
1272 test "it rejects activities which reference objects with bogus origins" do
1274 "@context" => "https://www.w3.org/ns/activitystreams",
1275 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1276 "actor" => "http://mastodon.example.org/users/admin",
1277 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1278 "object" => "https://info.pleroma.site/activity.json",
1279 "type" => "Announce"
1282 :error = Transmogrifier.handle_incoming(data)
1285 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1287 "@context" => "https://www.w3.org/ns/activitystreams",
1288 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1289 "actor" => "http://mastodon.example.org/users/admin",
1290 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1291 "object" => "https://info.pleroma.site/activity2.json",
1292 "type" => "Announce"
1295 :error = Transmogrifier.handle_incoming(data)
1298 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1300 "@context" => "https://www.w3.org/ns/activitystreams",
1301 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1302 "actor" => "http://mastodon.example.org/users/admin",
1303 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1304 "object" => "https://info.pleroma.site/activity3.json",
1305 "type" => "Announce"
1308 :error = Transmogrifier.handle_incoming(data)
1312 describe "reserialization" do
1313 test "successfully reserializes a message with inReplyTo == nil" do
1314 user = insert(:user)
1317 "@context" => "https://www.w3.org/ns/activitystreams",
1318 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1322 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1327 "attributedTo" => user.ap_id
1329 "actor" => user.ap_id
1332 {:ok, activity} = Transmogrifier.handle_incoming(message)
1334 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1337 test "successfully reserializes a message with AS2 objects in IR" do
1338 user = insert(:user)
1341 "@context" => "https://www.w3.org/ns/activitystreams",
1342 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1346 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1351 "attributedTo" => user.ap_id,
1353 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1354 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1357 "actor" => user.ap_id
1360 {:ok, activity} = Transmogrifier.handle_incoming(message)
1362 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1366 test "Rewrites Answers to Notes" do
1367 user = insert(:user)
1369 {:ok, poll_activity} =
1370 CommonAPI.post(user, %{
1371 "status" => "suya...",
1372 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1375 poll_object = Object.normalize(poll_activity)
1376 # TODO: Replace with CommonAPI vote creation when implemented
1378 File.read!("test/fixtures/mastodon-vote.json")
1380 |> Kernel.put_in(["to"], user.ap_id)
1381 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1382 |> Kernel.put_in(["object", "to"], user.ap_id)
1384 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1385 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1387 assert data["object"]["type"] == "Note"
1390 describe "fix_explicit_addressing" do
1392 user = insert(:user)
1396 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1397 explicitly_mentioned_actors = [
1398 "https://pleroma.gold/users/user1",
1399 "https://pleroma.gold/user2"
1403 "actor" => user.ap_id,
1404 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1407 Enum.map(explicitly_mentioned_actors, fn href ->
1408 %{"type" => "Mention", "href" => href}
1412 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1413 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1414 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1415 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1418 test "does not move actor's follower collection to cc", %{user: user} do
1420 "actor" => user.ap_id,
1421 "to" => [user.follower_address],
1425 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1426 assert user.follower_address in fixed_object["to"]
1427 refute user.follower_address in fixed_object["cc"]
1430 test "removes recipient's follower collection from cc", %{user: user} do
1431 recipient = insert(:user)
1434 "actor" => user.ap_id,
1435 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1436 "cc" => [user.follower_address, recipient.follower_address]
1439 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1441 assert user.follower_address in fixed_object["cc"]
1442 refute recipient.follower_address in fixed_object["cc"]
1443 refute recipient.follower_address in fixed_object["to"]