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.ActivityPub.Utils
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
18 import Pleroma.Factory
19 alias Pleroma.Web.CommonAPI
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 describe "handle_incoming" do
27 test "it ignores an incoming notice if we already have it" do
28 activity = insert(:note_activity)
31 File.read!("test/fixtures/mastodon-post-activity.json")
33 |> Map.put("object", activity.data["object"])
35 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
37 assert activity == returned_activity
40 test "it fetches replied-to activities if we don't have them" do
42 File.read!("test/fixtures/mastodon-post-activity.json")
47 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
51 |> Map.put("object", object)
53 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 returned_object = Object.normalize(returned_activity.data["object"])
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 works for incoming notices" do
65 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
67 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
70 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
72 assert data["context"] ==
73 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
75 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
77 assert data["cc"] == [
78 "http://mastodon.example.org/users/admin/followers",
79 "http://localtesting.pleroma.lol/users/lain"
82 assert data["actor"] == "http://mastodon.example.org/users/admin"
84 object = Object.normalize(data["object"]).data
85 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
87 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
89 assert object["cc"] == [
90 "http://mastodon.example.org/users/admin/followers",
91 "http://localtesting.pleroma.lol/users/lain"
94 assert object["actor"] == "http://mastodon.example.org/users/admin"
95 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
97 assert object["context"] ==
98 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
100 assert object["sensitive"] == true
102 user = User.get_cached_by_ap_id(object["actor"])
104 assert user.info.note_count == 1
107 test "it works for incoming notices with hashtags" do
108 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
111 object = Object.normalize(data["object"])
113 assert Enum.at(object.data["tag"], 2) == "moo"
116 test "it works for incoming notices with contentMap" do
118 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
120 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
121 object = Object.normalize(data["object"])
123 assert object.data["content"] ==
124 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
127 test "it works for incoming notices with to/cc not being an array (kroeg)" do
128 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
130 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
131 object = Object.normalize(data["object"])
133 assert object.data["content"] ==
134 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
137 test "it works for incoming announces with actor being inlined (kroeg)" do
138 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
140 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
142 assert data["actor"] == "https://puckipedia.com/"
145 test "it works for incoming notices with tag not being an array (kroeg)" do
146 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
148 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
149 object = Object.normalize(data["object"])
151 assert object.data["emoji"] == %{
152 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
155 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
157 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
158 object = Object.normalize(data["object"])
160 assert "test" in object.data["tag"]
163 test "it works for incoming notices with url not being a string (prismo)" do
164 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
166 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
167 object = Object.normalize(data["object"])
169 assert object.data["url"] == "https://prismo.news/posts/83"
172 test "it cleans up incoming notices which are not really DMs" do
174 other_user = insert(:user)
176 to = [user.ap_id, other_user.ap_id]
179 File.read!("test/fixtures/mastodon-post-activity.json")
189 data = Map.put(data, "object", object)
191 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
193 assert data["to"] == []
194 assert data["cc"] == to
196 object_data = Object.normalize(activity).data
198 assert object_data["to"] == []
199 assert object_data["cc"] == to
202 test "it works for incoming follow requests" do
206 File.read!("test/fixtures/mastodon-follow-activity.json")
208 |> Map.put("object", user.ap_id)
210 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
212 assert data["actor"] == "http://mastodon.example.org/users/admin"
213 assert data["type"] == "Follow"
214 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
215 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
218 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
219 Pleroma.Config.put([:user, :deny_follow_blocked], true)
222 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
224 {:ok, user} = User.block(user, target)
227 File.read!("test/fixtures/mastodon-follow-activity.json")
229 |> Map.put("object", user.ap_id)
231 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
233 %Activity{} = activity = Activity.get_by_ap_id(id)
235 assert activity.data["state"] == "reject"
238 test "it works for incoming follow requests from hubzilla" do
242 File.read!("test/fixtures/hubzilla-follow-activity.json")
244 |> Map.put("object", user.ap_id)
245 |> Utils.normalize_params()
247 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
249 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
250 assert data["type"] == "Follow"
251 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
252 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
255 test "it works for incoming likes" do
257 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
260 File.read!("test/fixtures/mastodon-like.json")
262 |> Map.put("object", activity.data["object"])
264 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
266 assert data["actor"] == "http://mastodon.example.org/users/admin"
267 assert data["type"] == "Like"
268 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
269 assert data["object"] == activity.data["object"]
272 test "it returns an error for incoming unlikes wihout a like activity" do
274 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
277 File.read!("test/fixtures/mastodon-undo-like.json")
279 |> Map.put("object", activity.data["object"])
281 assert Transmogrifier.handle_incoming(data) == :error
284 test "it works for incoming unlikes with an existing like activity" do
286 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
289 File.read!("test/fixtures/mastodon-like.json")
291 |> Map.put("object", activity.data["object"])
293 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
296 File.read!("test/fixtures/mastodon-undo-like.json")
298 |> Map.put("object", like_data)
299 |> Map.put("actor", like_data["actor"])
301 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
303 assert data["actor"] == "http://mastodon.example.org/users/admin"
304 assert data["type"] == "Undo"
305 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
306 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
309 test "it works for incoming announces" do
310 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
312 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
314 assert data["actor"] == "http://mastodon.example.org/users/admin"
315 assert data["type"] == "Announce"
318 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
320 assert data["object"] ==
321 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
323 assert Activity.get_create_by_object_ap_id(data["object"])
326 test "it works for incoming announces with an existing activity" do
328 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
331 File.read!("test/fixtures/mastodon-announce.json")
333 |> Map.put("object", activity.data["object"])
335 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
337 assert data["actor"] == "http://mastodon.example.org/users/admin"
338 assert data["type"] == "Announce"
341 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
343 assert data["object"] == activity.data["object"]
345 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
348 test "it does not clobber the addressing on announce activities" do
350 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
353 File.read!("test/fixtures/mastodon-announce.json")
355 |> Map.put("object", Object.normalize(activity).data["id"])
356 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
359 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
361 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
364 test "it ensures that as:Public activities make it to their followers collection" do
368 File.read!("test/fixtures/mastodon-post-activity.json")
370 |> Map.put("actor", user.ap_id)
371 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
376 |> Map.put("attributedTo", user.ap_id)
377 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
380 data = Map.put(data, "object", object)
382 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
384 assert data["cc"] == [User.ap_followers(user)]
387 test "it ensures that address fields become lists" do
391 File.read!("test/fixtures/mastodon-post-activity.json")
393 |> Map.put("actor", user.ap_id)
394 |> Map.put("to", nil)
395 |> Map.put("cc", nil)
399 |> Map.put("attributedTo", user.ap_id)
400 |> Map.put("to", nil)
401 |> Map.put("cc", nil)
403 data = Map.put(data, "object", object)
405 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
407 assert !is_nil(data["to"])
408 assert !is_nil(data["cc"])
411 test "it works for incoming update activities" do
412 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
414 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
415 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
418 update_data["object"]
419 |> Map.put("actor", data["actor"])
420 |> Map.put("id", data["actor"])
424 |> Map.put("actor", data["actor"])
425 |> Map.put("object", object)
427 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
429 user = User.get_cached_by_ap_id(data["actor"])
430 assert user.name == "gargle"
432 assert user.avatar["url"] == [
435 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
439 assert user.info.banner["url"] == [
442 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
446 assert user.bio == "<p>Some bio</p>"
449 test "it works for incoming update activities which lock the account" do
450 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
452 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
453 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
456 update_data["object"]
457 |> Map.put("actor", data["actor"])
458 |> Map.put("id", data["actor"])
459 |> Map.put("manuallyApprovesFollowers", true)
463 |> Map.put("actor", data["actor"])
464 |> Map.put("object", object)
466 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
468 user = User.get_cached_by_ap_id(data["actor"])
469 assert user.info.locked == true
472 test "it works for incoming deletes" do
473 activity = insert(:note_activity)
476 File.read!("test/fixtures/mastodon-delete.json")
481 |> Map.put("id", activity.data["object"])
485 |> Map.put("object", object)
486 |> Map.put("actor", activity.data["actor"])
488 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
490 refute Activity.get_by_id(activity.id)
493 test "it fails for incoming deletes with spoofed origin" do
494 activity = insert(:note_activity)
497 File.read!("test/fixtures/mastodon-delete.json")
502 |> Map.put("id", activity.data["object"])
506 |> Map.put("object", object)
508 :error = Transmogrifier.handle_incoming(data)
510 assert Activity.get_by_id(activity.id)
513 test "it works for incoming unannounces with an existing notice" do
515 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
518 File.read!("test/fixtures/mastodon-announce.json")
520 |> Map.put("object", activity.data["object"])
522 {:ok, %Activity{data: announce_data, local: false}} =
523 Transmogrifier.handle_incoming(announce_data)
526 File.read!("test/fixtures/mastodon-undo-announce.json")
528 |> Map.put("object", announce_data)
529 |> Map.put("actor", announce_data["actor"])
531 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
533 assert data["type"] == "Undo"
534 assert data["object"]["type"] == "Announce"
535 assert data["object"]["object"] == activity.data["object"]
537 assert data["object"]["id"] ==
538 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
541 test "it works for incomming unfollows with an existing follow" do
545 File.read!("test/fixtures/mastodon-follow-activity.json")
547 |> Map.put("object", user.ap_id)
549 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
552 File.read!("test/fixtures/mastodon-unfollow-activity.json")
554 |> Map.put("object", follow_data)
556 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
558 assert data["type"] == "Undo"
559 assert data["object"]["type"] == "Follow"
560 assert data["object"]["object"] == user.ap_id
561 assert data["actor"] == "http://mastodon.example.org/users/admin"
563 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
566 test "it works for incoming blocks" do
570 File.read!("test/fixtures/mastodon-block-activity.json")
572 |> Map.put("object", user.ap_id)
574 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
576 assert data["type"] == "Block"
577 assert data["object"] == user.ap_id
578 assert data["actor"] == "http://mastodon.example.org/users/admin"
580 blocker = User.get_cached_by_ap_id(data["actor"])
582 assert User.blocks?(blocker, user)
585 test "incoming blocks successfully tear down any follow relationship" do
586 blocker = insert(:user)
587 blocked = insert(:user)
590 File.read!("test/fixtures/mastodon-block-activity.json")
592 |> Map.put("object", blocked.ap_id)
593 |> Map.put("actor", blocker.ap_id)
595 {:ok, blocker} = User.follow(blocker, blocked)
596 {:ok, blocked} = User.follow(blocked, blocker)
598 assert User.following?(blocker, blocked)
599 assert User.following?(blocked, blocker)
601 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
603 assert data["type"] == "Block"
604 assert data["object"] == blocked.ap_id
605 assert data["actor"] == blocker.ap_id
607 blocker = User.get_cached_by_ap_id(data["actor"])
608 blocked = User.get_cached_by_ap_id(data["object"])
610 assert User.blocks?(blocker, blocked)
612 refute User.following?(blocker, blocked)
613 refute User.following?(blocked, blocker)
616 test "it works for incoming unblocks with an existing block" do
620 File.read!("test/fixtures/mastodon-block-activity.json")
622 |> Map.put("object", user.ap_id)
624 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
627 File.read!("test/fixtures/mastodon-unblock-activity.json")
629 |> Map.put("object", block_data)
631 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
632 assert data["type"] == "Undo"
633 assert data["object"]["type"] == "Block"
634 assert data["object"]["object"] == user.ap_id
635 assert data["actor"] == "http://mastodon.example.org/users/admin"
637 blocker = User.get_cached_by_ap_id(data["actor"])
639 refute User.blocks?(blocker, user)
642 test "it works for incoming accepts which were pre-accepted" do
643 follower = insert(:user)
644 followed = insert(:user)
646 {:ok, follower} = User.follow(follower, followed)
647 assert User.following?(follower, followed) == true
649 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
652 File.read!("test/fixtures/mastodon-accept-activity.json")
654 |> Map.put("actor", followed.ap_id)
657 accept_data["object"]
658 |> Map.put("actor", follower.ap_id)
659 |> Map.put("id", follow_activity.data["id"])
661 accept_data = Map.put(accept_data, "object", object)
663 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
664 refute activity.local
666 assert activity.data["object"] == follow_activity.data["id"]
668 follower = User.get_cached_by_id(follower.id)
670 assert User.following?(follower, followed) == true
673 test "it works for incoming accepts which were orphaned" do
674 follower = insert(:user)
675 followed = insert(:user, %{info: %User.Info{locked: true}})
677 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
680 File.read!("test/fixtures/mastodon-accept-activity.json")
682 |> Map.put("actor", followed.ap_id)
685 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
687 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
688 assert activity.data["object"] == follow_activity.data["id"]
690 follower = User.get_cached_by_id(follower.id)
692 assert User.following?(follower, followed) == true
695 test "it works for incoming accepts which are referenced by IRI only" do
696 follower = insert(:user)
697 followed = insert(:user, %{info: %User.Info{locked: true}})
699 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
702 File.read!("test/fixtures/mastodon-accept-activity.json")
704 |> Map.put("actor", followed.ap_id)
705 |> Map.put("object", follow_activity.data["id"])
707 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
708 assert activity.data["object"] == follow_activity.data["id"]
710 follower = User.get_cached_by_id(follower.id)
712 assert User.following?(follower, followed) == true
715 test "it fails for incoming accepts which cannot be correlated" do
716 follower = insert(:user)
717 followed = insert(:user, %{info: %User.Info{locked: true}})
720 File.read!("test/fixtures/mastodon-accept-activity.json")
722 |> Map.put("actor", followed.ap_id)
725 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
727 :error = Transmogrifier.handle_incoming(accept_data)
729 follower = User.get_cached_by_id(follower.id)
731 refute User.following?(follower, followed) == true
734 test "it fails for incoming rejects which cannot be correlated" do
735 follower = insert(:user)
736 followed = insert(:user, %{info: %User.Info{locked: true}})
739 File.read!("test/fixtures/mastodon-reject-activity.json")
741 |> Map.put("actor", followed.ap_id)
744 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
746 :error = Transmogrifier.handle_incoming(accept_data)
748 follower = User.get_cached_by_id(follower.id)
750 refute User.following?(follower, followed) == true
753 test "it works for incoming rejects which are orphaned" do
754 follower = insert(:user)
755 followed = insert(:user, %{info: %User.Info{locked: true}})
757 {:ok, follower} = User.follow(follower, followed)
758 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
760 assert User.following?(follower, followed) == true
763 File.read!("test/fixtures/mastodon-reject-activity.json")
765 |> Map.put("actor", followed.ap_id)
768 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
770 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
771 refute activity.local
773 follower = User.get_cached_by_id(follower.id)
775 assert User.following?(follower, followed) == false
778 test "it works for incoming rejects which are referenced by IRI only" do
779 follower = insert(:user)
780 followed = insert(:user, %{info: %User.Info{locked: true}})
782 {:ok, follower} = User.follow(follower, followed)
783 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
785 assert User.following?(follower, followed) == true
788 File.read!("test/fixtures/mastodon-reject-activity.json")
790 |> Map.put("actor", followed.ap_id)
791 |> Map.put("object", follow_activity.data["id"])
793 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
795 follower = User.get_cached_by_id(follower.id)
797 assert User.following?(follower, followed) == false
800 test "it rejects activities without a valid ID" do
804 File.read!("test/fixtures/mastodon-follow-activity.json")
806 |> Map.put("object", user.ap_id)
809 :error = Transmogrifier.handle_incoming(data)
812 test "it remaps video URLs as attachments if necessary" do
814 Fetcher.fetch_object_from_id(
815 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
820 "mediaType" => "video/mp4",
822 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
823 "mimeType" => "video/mp4",
828 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
829 "mediaType" => "video/mp4",
836 assert object.data["url"] ==
837 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
839 assert object.data["attachment"] == [attachment]
842 test "it accepts Flag activities" do
844 other_user = insert(:user)
846 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
847 object = Object.normalize(activity.data["object"])
850 "@context" => "https://www.w3.org/ns/activitystreams",
851 "cc" => [user.ap_id],
852 "object" => [user.ap_id, object.data["id"]],
854 "content" => "blocked AND reported!!!",
855 "actor" => other_user.ap_id
858 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
860 assert activity.data["object"] == [user.ap_id, object.data["id"]]
861 assert activity.data["content"] == "blocked AND reported!!!"
862 assert activity.data["actor"] == other_user.ap_id
863 assert activity.data["cc"] == [user.ap_id]
867 describe "prepare outgoing" do
868 test "it turns mentions into tags" do
870 other_user = insert(:user)
873 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
875 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
876 object = modified["object"]
878 expected_mention = %{
879 "href" => other_user.ap_id,
880 "name" => "@#{other_user.nickname}",
885 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
890 assert Enum.member?(object["tag"], expected_tag)
891 assert Enum.member?(object["tag"], expected_mention)
894 test "it adds the sensitive property" do
897 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
898 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
900 assert modified["object"]["sensitive"]
903 test "it adds the json-ld context and the conversation property" do
906 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
907 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
909 assert modified["@context"] ==
910 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
912 assert modified["object"]["conversation"] == modified["context"]
915 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
918 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
919 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
921 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
924 test "it translates ostatus IDs to external URLs" do
925 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
926 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
930 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
931 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
933 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
936 test "it translates ostatus reply_to IDs to external URLs" do
937 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
938 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
943 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
945 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
947 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
950 test "it strips internal hashtag data" do
953 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
956 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
961 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
963 assert modified["object"]["tag"] == [expected_tag]
966 test "it strips internal fields" do
969 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
971 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
973 assert length(modified["object"]["tag"]) == 2
975 assert is_nil(modified["object"]["emoji"])
976 assert is_nil(modified["object"]["like_count"])
977 assert is_nil(modified["object"]["announcements"])
978 assert is_nil(modified["object"]["announcement_count"])
979 assert is_nil(modified["object"]["context_id"])
982 test "it strips internal fields of article" do
983 activity = insert(:article_activity)
985 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
987 assert length(modified["object"]["tag"]) == 2
989 assert is_nil(modified["object"]["emoji"])
990 assert is_nil(modified["object"]["like_count"])
991 assert is_nil(modified["object"]["announcements"])
992 assert is_nil(modified["object"]["announcement_count"])
993 assert is_nil(modified["object"]["context_id"])
996 test "it adds like collection to object" do
997 activity = insert(:note_activity)
998 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1000 assert modified["object"]["likes"]["type"] == "OrderedCollection"
1001 assert modified["object"]["likes"]["totalItems"] == 0
1004 test "the directMessage flag is present" do
1005 user = insert(:user)
1006 other_user = insert(:user)
1008 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1010 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1012 assert modified["directMessage"] == false
1015 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1017 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1019 assert modified["directMessage"] == false
1022 CommonAPI.post(user, %{
1023 "status" => "@#{other_user.nickname} :moominmamma:",
1024 "visibility" => "direct"
1027 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1029 assert modified["directMessage"] == true
1032 test "it strips BCC field" do
1033 user = insert(:user)
1034 {:ok, list} = Pleroma.List.create("foo", user)
1037 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1039 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1041 assert is_nil(modified["bcc"])
1045 describe "user upgrade" do
1046 test "it upgrades a user to activitypub" do
1049 nickname: "rye@niu.moe",
1051 ap_id: "https://niu.moe/users/rye",
1052 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1055 user_two = insert(:user, %{following: [user.follower_address]})
1057 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1058 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1059 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1061 user = User.get_cached_by_id(user.id)
1062 assert user.info.note_count == 1
1064 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1065 assert user.info.ap_enabled
1066 assert user.info.note_count == 1
1067 assert user.follower_address == "https://niu.moe/users/rye/followers"
1069 user = User.get_cached_by_id(user.id)
1070 assert user.info.note_count == 1
1072 activity = Activity.get_by_id(activity.id)
1073 assert user.follower_address in activity.recipients
1079 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1088 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1091 } = user.info.banner
1093 refute "..." in activity.recipients
1095 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1096 refute user.follower_address in unrelated_activity.recipients
1098 user_two = User.get_cached_by_id(user_two.id)
1099 assert user.follower_address in user_two.following
1100 refute "..." in user_two.following
1104 describe "maybe_retire_websub" do
1105 test "it deletes all websub client subscripitions with the user as topic" do
1106 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1107 {:ok, ws} = Repo.insert(subscription)
1109 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1110 {:ok, ws2} = Repo.insert(subscription)
1112 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1114 refute Repo.get(WebsubClientSubscription, ws.id)
1115 assert Repo.get(WebsubClientSubscription, ws2.id)
1119 describe "actor rewriting" do
1120 test "it fixes the actor URL property to be a proper URI" do
1122 "url" => %{"href" => "http://example.com"}
1125 rewritten = Transmogrifier.maybe_fix_user_object(data)
1126 assert rewritten["url"] == "http://example.com"
1130 describe "actor origin containment" do
1131 test "it rejects activities which reference objects with bogus origins" do
1133 "@context" => "https://www.w3.org/ns/activitystreams",
1134 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1135 "actor" => "http://mastodon.example.org/users/admin",
1136 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1137 "object" => "https://info.pleroma.site/activity.json",
1138 "type" => "Announce"
1141 :error = Transmogrifier.handle_incoming(data)
1144 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1146 "@context" => "https://www.w3.org/ns/activitystreams",
1147 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1148 "actor" => "http://mastodon.example.org/users/admin",
1149 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1150 "object" => "https://info.pleroma.site/activity2.json",
1151 "type" => "Announce"
1154 :error = Transmogrifier.handle_incoming(data)
1157 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1159 "@context" => "https://www.w3.org/ns/activitystreams",
1160 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1161 "actor" => "http://mastodon.example.org/users/admin",
1162 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1163 "object" => "https://info.pleroma.site/activity3.json",
1164 "type" => "Announce"
1167 :error = Transmogrifier.handle_incoming(data)
1171 describe "reserialization" do
1172 test "successfully reserializes a message with inReplyTo == nil" do
1173 user = insert(:user)
1176 "@context" => "https://www.w3.org/ns/activitystreams",
1177 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1181 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1186 "attributedTo" => user.ap_id
1188 "actor" => user.ap_id
1191 {:ok, activity} = Transmogrifier.handle_incoming(message)
1193 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1196 test "successfully reserializes a message with AS2 objects in IR" do
1197 user = insert(:user)
1200 "@context" => "https://www.w3.org/ns/activitystreams",
1201 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1205 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1210 "attributedTo" => user.ap_id,
1212 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1213 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1216 "actor" => user.ap_id
1219 {:ok, activity} = Transmogrifier.handle_incoming(message)
1221 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)