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 works for incoming emoji reactions" do
316 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
319 File.read!("test/fixtures/emoji-reaction.json")
321 |> Map.put("object", activity.data["object"])
323 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
325 assert data["actor"] == "http://mastodon.example.org/users/admin"
326 assert data["type"] == "EmojiReaction"
327 assert data["id"] == "http://mastodon.example.org/users/admin#reactions/2"
328 assert data["object"] == activity.data["object"]
329 assert data["content"] == "👌"
332 test "it returns an error for incoming unlikes wihout a like activity" do
334 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
337 File.read!("test/fixtures/mastodon-undo-like.json")
339 |> Map.put("object", activity.data["object"])
341 assert Transmogrifier.handle_incoming(data) == :error
344 test "it works for incoming unlikes with an existing like activity" do
346 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
349 File.read!("test/fixtures/mastodon-like.json")
351 |> Map.put("object", activity.data["object"])
353 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
356 File.read!("test/fixtures/mastodon-undo-like.json")
358 |> Map.put("object", like_data)
359 |> Map.put("actor", like_data["actor"])
361 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
363 assert data["actor"] == "http://mastodon.example.org/users/admin"
364 assert data["type"] == "Undo"
365 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
366 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
369 test "it works for incoming announces" do
370 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
372 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
374 assert data["actor"] == "http://mastodon.example.org/users/admin"
375 assert data["type"] == "Announce"
378 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
380 assert data["object"] ==
381 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
383 assert Activity.get_create_by_object_ap_id(data["object"])
386 test "it works for incoming announces with an existing activity" do
388 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
391 File.read!("test/fixtures/mastodon-announce.json")
393 |> Map.put("object", activity.data["object"])
395 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
397 assert data["actor"] == "http://mastodon.example.org/users/admin"
398 assert data["type"] == "Announce"
401 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
403 assert data["object"] == activity.data["object"]
405 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
408 test "it does not clobber the addressing on announce activities" do
410 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
413 File.read!("test/fixtures/mastodon-announce.json")
415 |> Map.put("object", Object.normalize(activity).data["id"])
416 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
419 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
421 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
424 test "it ensures that as:Public activities make it to their followers collection" do
428 File.read!("test/fixtures/mastodon-post-activity.json")
430 |> Map.put("actor", user.ap_id)
431 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
436 |> Map.put("attributedTo", user.ap_id)
437 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
439 |> Map.put("id", user.ap_id <> "/activities/12345678")
441 data = Map.put(data, "object", object)
443 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
445 assert data["cc"] == [User.ap_followers(user)]
448 test "it ensures that address fields become lists" do
452 File.read!("test/fixtures/mastodon-post-activity.json")
454 |> Map.put("actor", user.ap_id)
455 |> Map.put("to", nil)
456 |> Map.put("cc", nil)
460 |> Map.put("attributedTo", user.ap_id)
461 |> Map.put("to", nil)
462 |> Map.put("cc", nil)
463 |> Map.put("id", user.ap_id <> "/activities/12345678")
465 data = Map.put(data, "object", object)
467 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
469 assert !is_nil(data["to"])
470 assert !is_nil(data["cc"])
473 test "it strips internal likes" do
475 File.read!("test/fixtures/mastodon-post-activity.json")
480 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
481 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
483 "type" => "OrderedCollection"
486 object = Map.put(data["object"], "likes", likes)
487 data = Map.put(data, "object", object)
489 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
491 refute Map.has_key?(object.data, "likes")
494 test "it strips internal reactions" do
496 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
497 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
499 %{object: object} = Activity.get_by_id_with_object(activity.id)
500 assert Map.has_key?(object.data, "reactions")
501 assert Map.has_key?(object.data, "reaction_count")
503 object_data = Transmogrifier.strip_internal_fields(object.data)
504 refute Map.has_key?(object_data, "reactions")
505 refute Map.has_key?(object_data, "reaction_count")
508 test "it works for incoming update activities" do
509 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
511 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
512 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
515 update_data["object"]
516 |> Map.put("actor", data["actor"])
517 |> Map.put("id", data["actor"])
521 |> Map.put("actor", data["actor"])
522 |> Map.put("object", object)
524 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
526 user = User.get_cached_by_ap_id(data["actor"])
527 assert user.name == "gargle"
529 assert user.avatar["url"] == [
532 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
536 assert user.info.banner["url"] == [
539 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
543 assert user.bio == "<p>Some bio</p>"
546 test "it works with custom profile fields" do
548 "test/fixtures/mastodon-post-activity.json"
551 |> Transmogrifier.handle_incoming()
553 user = User.get_cached_by_ap_id(activity.actor)
555 assert User.Info.fields(user.info) == [
556 %{"name" => "foo", "value" => "bar"},
557 %{"name" => "foo1", "value" => "bar1"}
560 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
563 update_data["object"]
564 |> Map.put("actor", user.ap_id)
565 |> Map.put("id", user.ap_id)
569 |> Map.put("actor", user.ap_id)
570 |> Map.put("object", object)
572 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
574 user = User.get_cached_by_ap_id(user.ap_id)
576 assert User.Info.fields(user.info) == [
577 %{"name" => "foo", "value" => "updated"},
578 %{"name" => "foo1", "value" => "updated"}
581 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
584 put_in(update_data, ["object", "attachment"], [
585 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
586 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
587 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
590 {:ok, _} = Transmogrifier.handle_incoming(update_data)
592 user = User.get_cached_by_ap_id(user.ap_id)
594 assert User.Info.fields(user.info) == [
595 %{"name" => "foo", "value" => "updated"},
596 %{"name" => "foo1", "value" => "updated"}
599 update_data = put_in(update_data, ["object", "attachment"], [])
601 {:ok, _} = Transmogrifier.handle_incoming(update_data)
603 user = User.get_cached_by_ap_id(user.ap_id)
605 assert User.Info.fields(user.info) == []
608 test "it works for incoming update activities which lock the account" do
609 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
611 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
612 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
615 update_data["object"]
616 |> Map.put("actor", data["actor"])
617 |> Map.put("id", data["actor"])
618 |> Map.put("manuallyApprovesFollowers", true)
622 |> Map.put("actor", data["actor"])
623 |> Map.put("object", object)
625 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
627 user = User.get_cached_by_ap_id(data["actor"])
628 assert user.info.locked == true
631 test "it works for incoming deletes" do
632 activity = insert(:note_activity)
635 File.read!("test/fixtures/mastodon-delete.json")
640 |> Map.put("id", activity.data["object"])
644 |> Map.put("object", object)
645 |> Map.put("actor", activity.data["actor"])
647 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
649 refute Activity.get_by_id(activity.id)
652 test "it fails for incoming deletes with spoofed origin" do
653 activity = insert(:note_activity)
656 File.read!("test/fixtures/mastodon-delete.json")
661 |> Map.put("id", activity.data["object"])
665 |> Map.put("object", object)
667 assert capture_log(fn ->
668 :error = Transmogrifier.handle_incoming(data)
670 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
672 assert Activity.get_by_id(activity.id)
675 test "it works for incoming user deletes" do
676 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
679 File.read!("test/fixtures/mastodon-delete-user.json")
682 {:ok, _} = Transmogrifier.handle_incoming(data)
684 refute User.get_cached_by_ap_id(ap_id)
687 test "it fails for incoming user deletes with spoofed origin" do
688 %{ap_id: ap_id} = insert(:user)
691 File.read!("test/fixtures/mastodon-delete-user.json")
693 |> Map.put("actor", ap_id)
695 assert :error == Transmogrifier.handle_incoming(data)
696 assert User.get_cached_by_ap_id(ap_id)
699 test "it works for incoming unannounces with an existing notice" do
701 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
704 File.read!("test/fixtures/mastodon-announce.json")
706 |> Map.put("object", activity.data["object"])
708 {:ok, %Activity{data: announce_data, local: false}} =
709 Transmogrifier.handle_incoming(announce_data)
712 File.read!("test/fixtures/mastodon-undo-announce.json")
714 |> Map.put("object", announce_data)
715 |> Map.put("actor", announce_data["actor"])
717 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
719 assert data["type"] == "Undo"
720 assert object_data = data["object"]
721 assert object_data["type"] == "Announce"
722 assert object_data["object"] == activity.data["object"]
724 assert object_data["id"] ==
725 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
728 test "it works for incomming unfollows with an existing follow" do
732 File.read!("test/fixtures/mastodon-follow-activity.json")
734 |> Map.put("object", user.ap_id)
736 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
739 File.read!("test/fixtures/mastodon-unfollow-activity.json")
741 |> Map.put("object", follow_data)
743 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
745 assert data["type"] == "Undo"
746 assert data["object"]["type"] == "Follow"
747 assert data["object"]["object"] == user.ap_id
748 assert data["actor"] == "http://mastodon.example.org/users/admin"
750 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
753 test "it works for incoming blocks" do
757 File.read!("test/fixtures/mastodon-block-activity.json")
759 |> Map.put("object", user.ap_id)
761 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
763 assert data["type"] == "Block"
764 assert data["object"] == user.ap_id
765 assert data["actor"] == "http://mastodon.example.org/users/admin"
767 blocker = User.get_cached_by_ap_id(data["actor"])
769 assert User.blocks?(blocker, user)
772 test "incoming blocks successfully tear down any follow relationship" do
773 blocker = insert(:user)
774 blocked = insert(:user)
777 File.read!("test/fixtures/mastodon-block-activity.json")
779 |> Map.put("object", blocked.ap_id)
780 |> Map.put("actor", blocker.ap_id)
782 {:ok, blocker} = User.follow(blocker, blocked)
783 {:ok, blocked} = User.follow(blocked, blocker)
785 assert User.following?(blocker, blocked)
786 assert User.following?(blocked, blocker)
788 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
790 assert data["type"] == "Block"
791 assert data["object"] == blocked.ap_id
792 assert data["actor"] == blocker.ap_id
794 blocker = User.get_cached_by_ap_id(data["actor"])
795 blocked = User.get_cached_by_ap_id(data["object"])
797 assert User.blocks?(blocker, blocked)
799 refute User.following?(blocker, blocked)
800 refute User.following?(blocked, blocker)
803 test "it works for incoming unblocks with an existing block" do
807 File.read!("test/fixtures/mastodon-block-activity.json")
809 |> Map.put("object", user.ap_id)
811 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
814 File.read!("test/fixtures/mastodon-unblock-activity.json")
816 |> Map.put("object", block_data)
818 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
819 assert data["type"] == "Undo"
820 assert data["object"]["type"] == "Block"
821 assert data["object"]["object"] == user.ap_id
822 assert data["actor"] == "http://mastodon.example.org/users/admin"
824 blocker = User.get_cached_by_ap_id(data["actor"])
826 refute User.blocks?(blocker, user)
829 test "it works for incoming accepts which were pre-accepted" do
830 follower = insert(:user)
831 followed = insert(:user)
833 {:ok, follower} = User.follow(follower, followed)
834 assert User.following?(follower, followed) == true
836 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
839 File.read!("test/fixtures/mastodon-accept-activity.json")
841 |> Map.put("actor", followed.ap_id)
844 accept_data["object"]
845 |> Map.put("actor", follower.ap_id)
846 |> Map.put("id", follow_activity.data["id"])
848 accept_data = Map.put(accept_data, "object", object)
850 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
851 refute activity.local
853 assert activity.data["object"] == follow_activity.data["id"]
855 follower = User.get_cached_by_id(follower.id)
857 assert User.following?(follower, followed) == true
860 test "it works for incoming accepts which were orphaned" do
861 follower = insert(:user)
862 followed = insert(:user, %{info: %User.Info{locked: true}})
864 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
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 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
875 assert activity.data["object"] == follow_activity.data["id"]
877 follower = User.get_cached_by_id(follower.id)
879 assert User.following?(follower, followed) == true
882 test "it works for incoming accepts which are referenced by IRI only" do
883 follower = insert(:user)
884 followed = insert(:user, %{info: %User.Info{locked: true}})
886 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
889 File.read!("test/fixtures/mastodon-accept-activity.json")
891 |> Map.put("actor", followed.ap_id)
892 |> Map.put("object", follow_activity.data["id"])
894 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
895 assert activity.data["object"] == follow_activity.data["id"]
897 follower = User.get_cached_by_id(follower.id)
899 assert User.following?(follower, followed) == true
902 test "it fails for incoming accepts which cannot be correlated" do
903 follower = insert(:user)
904 followed = insert(:user, %{info: %User.Info{locked: true}})
907 File.read!("test/fixtures/mastodon-accept-activity.json")
909 |> Map.put("actor", followed.ap_id)
912 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
914 :error = Transmogrifier.handle_incoming(accept_data)
916 follower = User.get_cached_by_id(follower.id)
918 refute User.following?(follower, followed) == true
921 test "it fails for incoming rejects which cannot be correlated" do
922 follower = insert(:user)
923 followed = insert(:user, %{info: %User.Info{locked: true}})
926 File.read!("test/fixtures/mastodon-reject-activity.json")
928 |> Map.put("actor", followed.ap_id)
931 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
933 :error = Transmogrifier.handle_incoming(accept_data)
935 follower = User.get_cached_by_id(follower.id)
937 refute User.following?(follower, followed) == true
940 test "it works for incoming rejects which are orphaned" do
941 follower = insert(:user)
942 followed = insert(:user, %{info: %User.Info{locked: true}})
944 {:ok, follower} = User.follow(follower, followed)
945 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
947 assert User.following?(follower, followed) == true
950 File.read!("test/fixtures/mastodon-reject-activity.json")
952 |> Map.put("actor", followed.ap_id)
955 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
957 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
958 refute activity.local
960 follower = User.get_cached_by_id(follower.id)
962 assert User.following?(follower, followed) == false
965 test "it works for incoming rejects which are referenced by IRI only" do
966 follower = insert(:user)
967 followed = insert(:user, %{info: %User.Info{locked: true}})
969 {:ok, follower} = User.follow(follower, followed)
970 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
972 assert User.following?(follower, followed) == true
975 File.read!("test/fixtures/mastodon-reject-activity.json")
977 |> Map.put("actor", followed.ap_id)
978 |> Map.put("object", follow_activity.data["id"])
980 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
982 follower = User.get_cached_by_id(follower.id)
984 assert User.following?(follower, followed) == false
987 test "it rejects activities without a valid ID" do
991 File.read!("test/fixtures/mastodon-follow-activity.json")
993 |> Map.put("object", user.ap_id)
996 :error = Transmogrifier.handle_incoming(data)
999 test "it remaps video URLs as attachments if necessary" do
1001 Fetcher.fetch_object_from_id(
1002 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1007 "mediaType" => "video/mp4",
1009 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1010 "mimeType" => "video/mp4",
1011 "size" => 5_015_880,
1015 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1016 "mediaType" => "video/mp4",
1023 assert object.data["url"] ==
1024 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1026 assert object.data["attachment"] == [attachment]
1029 test "it accepts Flag activities" do
1030 user = insert(:user)
1031 other_user = insert(:user)
1033 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1034 object = Object.normalize(activity)
1037 "@context" => "https://www.w3.org/ns/activitystreams",
1038 "cc" => [user.ap_id],
1039 "object" => [user.ap_id, object.data["id"]],
1041 "content" => "blocked AND reported!!!",
1042 "actor" => other_user.ap_id
1045 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1047 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1048 assert activity.data["content"] == "blocked AND reported!!!"
1049 assert activity.data["actor"] == other_user.ap_id
1050 assert activity.data["cc"] == [user.ap_id]
1054 describe "prepare outgoing" do
1055 test "it turns mentions into tags" do
1056 user = insert(:user)
1057 other_user = insert(:user)
1060 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1062 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1063 object = modified["object"]
1065 expected_mention = %{
1066 "href" => other_user.ap_id,
1067 "name" => "@#{other_user.nickname}",
1072 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1073 "type" => "Hashtag",
1077 assert Enum.member?(object["tag"], expected_tag)
1078 assert Enum.member?(object["tag"], expected_mention)
1081 test "it adds the sensitive property" do
1082 user = insert(:user)
1084 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1085 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1087 assert modified["object"]["sensitive"]
1090 test "it adds the json-ld context and the conversation property" do
1091 user = insert(:user)
1093 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1094 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1096 assert modified["@context"] ==
1097 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1099 assert modified["object"]["conversation"] == modified["context"]
1102 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1103 user = insert(:user)
1105 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1106 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1108 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1111 test "it translates ostatus IDs to external URLs" do
1112 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1113 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
1115 user = insert(:user)
1117 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
1118 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1120 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
1123 test "it translates ostatus reply_to IDs to external URLs" do
1124 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
1125 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
1127 user = insert(:user)
1130 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
1132 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1134 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
1137 test "it strips internal hashtag data" do
1138 user = insert(:user)
1140 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1143 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1144 "type" => "Hashtag",
1148 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1150 assert modified["object"]["tag"] == [expected_tag]
1153 test "it strips internal fields" do
1154 user = insert(:user)
1156 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1158 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1160 assert length(modified["object"]["tag"]) == 2
1162 assert is_nil(modified["object"]["emoji"])
1163 assert is_nil(modified["object"]["like_count"])
1164 assert is_nil(modified["object"]["announcements"])
1165 assert is_nil(modified["object"]["announcement_count"])
1166 assert is_nil(modified["object"]["context_id"])
1169 test "it strips internal fields of article" do
1170 activity = insert(:article_activity)
1172 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1174 assert length(modified["object"]["tag"]) == 2
1176 assert is_nil(modified["object"]["emoji"])
1177 assert is_nil(modified["object"]["like_count"])
1178 assert is_nil(modified["object"]["announcements"])
1179 assert is_nil(modified["object"]["announcement_count"])
1180 assert is_nil(modified["object"]["context_id"])
1181 assert is_nil(modified["object"]["likes"])
1184 test "the directMessage flag is present" do
1185 user = insert(:user)
1186 other_user = insert(:user)
1188 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1190 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1192 assert modified["directMessage"] == false
1195 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1197 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1199 assert modified["directMessage"] == false
1202 CommonAPI.post(user, %{
1203 "status" => "@#{other_user.nickname} :moominmamma:",
1204 "visibility" => "direct"
1207 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1209 assert modified["directMessage"] == true
1212 test "it strips BCC field" do
1213 user = insert(:user)
1214 {:ok, list} = Pleroma.List.create("foo", user)
1217 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1219 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1221 assert is_nil(modified["bcc"])
1225 describe "user upgrade" do
1226 test "it upgrades a user to activitypub" do
1229 nickname: "rye@niu.moe",
1231 ap_id: "https://niu.moe/users/rye",
1232 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1235 user_two = insert(:user, %{following: [user.follower_address]})
1237 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1238 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1239 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1241 user = User.get_cached_by_id(user.id)
1242 assert user.info.note_count == 1
1244 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1245 assert user.info.ap_enabled
1246 assert user.info.note_count == 1
1247 assert user.follower_address == "https://niu.moe/users/rye/followers"
1248 assert user.following_address == "https://niu.moe/users/rye/following"
1250 user = User.get_cached_by_id(user.id)
1251 assert user.info.note_count == 1
1253 activity = Activity.get_by_id(activity.id)
1254 assert user.follower_address in activity.recipients
1260 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1269 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1272 } = user.info.banner
1274 refute "..." in activity.recipients
1276 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1277 refute user.follower_address in unrelated_activity.recipients
1279 user_two = User.get_cached_by_id(user_two.id)
1280 assert user.follower_address in user_two.following
1281 refute "..." in user_two.following
1285 describe "maybe_retire_websub" do
1286 test "it deletes all websub client subscripitions with the user as topic" do
1287 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1288 {:ok, ws} = Repo.insert(subscription)
1290 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1291 {:ok, ws2} = Repo.insert(subscription)
1293 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1295 refute Repo.get(WebsubClientSubscription, ws.id)
1296 assert Repo.get(WebsubClientSubscription, ws2.id)
1300 describe "actor rewriting" do
1301 test "it fixes the actor URL property to be a proper URI" do
1303 "url" => %{"href" => "http://example.com"}
1306 rewritten = Transmogrifier.maybe_fix_user_object(data)
1307 assert rewritten["url"] == "http://example.com"
1311 describe "actor origin containment" do
1312 test "it rejects activities which reference objects with bogus origins" do
1314 "@context" => "https://www.w3.org/ns/activitystreams",
1315 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1316 "actor" => "http://mastodon.example.org/users/admin",
1317 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1318 "object" => "https://info.pleroma.site/activity.json",
1319 "type" => "Announce"
1322 :error = Transmogrifier.handle_incoming(data)
1325 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1327 "@context" => "https://www.w3.org/ns/activitystreams",
1328 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1329 "actor" => "http://mastodon.example.org/users/admin",
1330 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1331 "object" => "https://info.pleroma.site/activity2.json",
1332 "type" => "Announce"
1335 :error = Transmogrifier.handle_incoming(data)
1338 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1340 "@context" => "https://www.w3.org/ns/activitystreams",
1341 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1342 "actor" => "http://mastodon.example.org/users/admin",
1343 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1344 "object" => "https://info.pleroma.site/activity3.json",
1345 "type" => "Announce"
1348 :error = Transmogrifier.handle_incoming(data)
1352 describe "reserialization" do
1353 test "successfully reserializes a message with inReplyTo == nil" do
1354 user = insert(:user)
1357 "@context" => "https://www.w3.org/ns/activitystreams",
1358 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1362 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1367 "attributedTo" => user.ap_id
1369 "actor" => user.ap_id
1372 {:ok, activity} = Transmogrifier.handle_incoming(message)
1374 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1377 test "successfully reserializes a message with AS2 objects in IR" do
1378 user = insert(:user)
1381 "@context" => "https://www.w3.org/ns/activitystreams",
1382 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1386 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1391 "attributedTo" => user.ap_id,
1393 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1394 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1397 "actor" => user.ap_id
1400 {:ok, activity} = Transmogrifier.handle_incoming(message)
1402 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1406 test "Rewrites Answers to Notes" do
1407 user = insert(:user)
1409 {:ok, poll_activity} =
1410 CommonAPI.post(user, %{
1411 "status" => "suya...",
1412 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1415 poll_object = Object.normalize(poll_activity)
1416 # TODO: Replace with CommonAPI vote creation when implemented
1418 File.read!("test/fixtures/mastodon-vote.json")
1420 |> Kernel.put_in(["to"], user.ap_id)
1421 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1422 |> Kernel.put_in(["object", "to"], user.ap_id)
1424 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1425 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1427 assert data["object"]["type"] == "Note"
1430 describe "fix_explicit_addressing" do
1432 user = insert(:user)
1436 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1437 explicitly_mentioned_actors = [
1438 "https://pleroma.gold/users/user1",
1439 "https://pleroma.gold/user2"
1443 "actor" => user.ap_id,
1444 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1447 Enum.map(explicitly_mentioned_actors, fn href ->
1448 %{"type" => "Mention", "href" => href}
1452 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1453 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1454 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1455 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1458 test "does not move actor's follower collection to cc", %{user: user} do
1460 "actor" => user.ap_id,
1461 "to" => [user.follower_address],
1465 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1466 assert user.follower_address in fixed_object["to"]
1467 refute user.follower_address in fixed_object["cc"]
1470 test "removes recipient's follower collection from cc", %{user: user} do
1471 recipient = insert(:user)
1474 "actor" => user.ap_id,
1475 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1476 "cc" => [user.follower_address, recipient.follower_address]
1479 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1481 assert user.follower_address in fixed_object["cc"]
1482 refute recipient.follower_address in fixed_object["cc"]
1483 refute recipient.follower_address in fixed_object["to"]