1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
18 import ExUnit.CaptureLog
21 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 clear_config([:instance, :max_remote_account_fields])
27 describe "handle_incoming" do
28 test "it ignores an incoming notice if we already have it" do
29 activity = insert(:note_activity)
32 File.read!("test/fixtures/mastodon-post-activity.json")
34 |> Map.put("object", Object.normalize(activity).data)
36 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
38 assert activity == returned_activity
41 test "it fetches replied-to activities if we don't have them" do
43 File.read!("test/fixtures/mastodon-post-activity.json")
48 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
50 data = Map.put(data, "object", object)
51 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
52 returned_object = Object.normalize(returned_activity, false)
55 Activity.get_create_by_object_ap_id(
56 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
59 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
62 test "it does not fetch replied-to activities beyond max_replies_depth" do
64 File.read!("test/fixtures/mastodon-post-activity.json")
69 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
71 data = Map.put(data, "object", object)
73 with_mock Pleroma.Web.Federator,
74 allowed_incoming_reply_depth?: fn _ -> false end do
75 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
77 returned_object = Object.normalize(returned_activity, false)
79 refute Activity.get_create_by_object_ap_id(
80 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
83 assert returned_object.data["inReplyToAtomUri"] ==
84 "https://shitposter.club/notice/2827873"
88 test "it does not crash if the object in inReplyTo can't be fetched" do
90 File.read!("test/fixtures/mastodon-post-activity.json")
95 |> Map.put("inReplyTo", "https://404.site/whatever")
99 |> Map.put("object", object)
101 assert capture_log(fn ->
102 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
103 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
106 test "it works for incoming notices" do
107 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
109 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
112 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
114 assert data["context"] ==
115 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
117 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
119 assert data["cc"] == [
120 "http://mastodon.example.org/users/admin/followers",
121 "http://localtesting.pleroma.lol/users/lain"
124 assert data["actor"] == "http://mastodon.example.org/users/admin"
126 object_data = Object.normalize(data["object"]).data
128 assert object_data["id"] ==
129 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
131 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
133 assert object_data["cc"] == [
134 "http://mastodon.example.org/users/admin/followers",
135 "http://localtesting.pleroma.lol/users/lain"
138 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
139 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
141 assert object_data["context"] ==
142 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
144 assert object_data["sensitive"] == true
146 user = User.get_cached_by_ap_id(object_data["actor"])
148 assert user.note_count == 1
151 test "it works for incoming notices with hashtags" do
152 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
154 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
155 object = Object.normalize(data["object"])
157 assert Enum.at(object.data["tag"], 2) == "moo"
160 test "it works for incoming questions" do
161 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
163 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
165 object = Object.normalize(activity)
167 assert Enum.all?(object.data["oneOf"], fn choice ->
170 "Everyone knows that!",
171 "25 char limit is dumb",
172 "I can't even fit a funny"
177 test "it works for incoming listens" do
179 "@context" => "https://www.w3.org/ns/activitystreams",
180 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
183 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
184 "actor" => "http://mastodon.example.org/users/admin",
187 "id" => "http://mastodon.example.org/users/admin/listens/1234",
188 "attributedTo" => "http://mastodon.example.org/users/admin",
189 "title" => "lain radio episode 1",
191 "album" => "lain radio",
196 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
198 object = Object.normalize(activity)
200 assert object.data["title"] == "lain radio episode 1"
201 assert object.data["artist"] == "lain"
202 assert object.data["album"] == "lain radio"
203 assert object.data["length"] == 180_000
206 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
210 CommonAPI.post(user, %{
211 "status" => "suya...",
212 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
215 object = Object.normalize(activity)
218 File.read!("test/fixtures/mastodon-vote.json")
220 |> Kernel.put_in(["to"], user.ap_id)
221 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
222 |> Kernel.put_in(["object", "to"], user.ap_id)
224 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
225 answer_object = Object.normalize(activity)
226 assert answer_object.data["type"] == "Answer"
227 object = Object.get_by_ap_id(object.data["id"])
230 object.data["oneOf"],
232 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
238 test "it works for incoming notices with contentMap" do
240 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
242 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
243 object = Object.normalize(data["object"])
245 assert object.data["content"] ==
246 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
249 test "it works for incoming notices with to/cc not being an array (kroeg)" do
250 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
252 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
253 object = Object.normalize(data["object"])
255 assert object.data["content"] ==
256 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
259 test "it works for incoming announces with actor being inlined (kroeg)" do
260 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
262 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
264 assert data["actor"] == "https://puckipedia.com/"
267 test "it works for incoming notices with tag not being an array (kroeg)" do
268 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
270 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
271 object = Object.normalize(data["object"])
273 assert object.data["emoji"] == %{
274 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
277 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
279 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
280 object = Object.normalize(data["object"])
282 assert "test" in object.data["tag"]
285 test "it works for incoming notices with url not being a string (prismo)" do
286 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
288 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
289 object = Object.normalize(data["object"])
291 assert object.data["url"] == "https://prismo.news/posts/83"
294 test "it cleans up incoming notices which are not really DMs" do
296 other_user = insert(:user)
298 to = [user.ap_id, other_user.ap_id]
301 File.read!("test/fixtures/mastodon-post-activity.json")
311 data = Map.put(data, "object", object)
313 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
315 assert data["to"] == []
316 assert data["cc"] == to
318 object_data = Object.normalize(activity).data
320 assert object_data["to"] == []
321 assert object_data["cc"] == to
324 test "it works for incoming likes" do
326 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
329 File.read!("test/fixtures/mastodon-like.json")
331 |> Map.put("object", activity.data["object"])
333 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
335 assert data["actor"] == "http://mastodon.example.org/users/admin"
336 assert data["type"] == "Like"
337 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
338 assert data["object"] == activity.data["object"]
341 test "it returns an error for incoming unlikes wihout a like activity" do
343 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
346 File.read!("test/fixtures/mastodon-undo-like.json")
348 |> Map.put("object", activity.data["object"])
350 assert Transmogrifier.handle_incoming(data) == :error
353 test "it works for incoming unlikes with an existing like activity" do
355 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
358 File.read!("test/fixtures/mastodon-like.json")
360 |> Map.put("object", activity.data["object"])
362 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
365 File.read!("test/fixtures/mastodon-undo-like.json")
367 |> Map.put("object", like_data)
368 |> Map.put("actor", like_data["actor"])
370 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
372 assert data["actor"] == "http://mastodon.example.org/users/admin"
373 assert data["type"] == "Undo"
374 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
375 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
378 test "it works for incoming unlikes with an existing like activity and a compact object" do
380 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
383 File.read!("test/fixtures/mastodon-like.json")
385 |> Map.put("object", activity.data["object"])
387 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
390 File.read!("test/fixtures/mastodon-undo-like.json")
392 |> Map.put("object", like_data["id"])
393 |> Map.put("actor", like_data["actor"])
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"] == "Undo"
399 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
400 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
403 test "it works for incoming announces" do
404 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
406 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
408 assert data["actor"] == "http://mastodon.example.org/users/admin"
409 assert data["type"] == "Announce"
412 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
414 assert data["object"] ==
415 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
417 assert Activity.get_create_by_object_ap_id(data["object"])
420 test "it works for incoming announces with an existing activity" do
422 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
425 File.read!("test/fixtures/mastodon-announce.json")
427 |> Map.put("object", activity.data["object"])
429 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
431 assert data["actor"] == "http://mastodon.example.org/users/admin"
432 assert data["type"] == "Announce"
435 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
437 assert data["object"] == activity.data["object"]
439 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
442 test "it works for incoming announces with an inlined activity" do
444 File.read!("test/fixtures/mastodon-announce-private.json")
447 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
449 assert data["actor"] == "http://mastodon.example.org/users/admin"
450 assert data["type"] == "Announce"
453 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
455 object = Object.normalize(data["object"])
457 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
458 assert object.data["content"] == "this is a private toot"
461 test "it rejects incoming announces with an inlined activity from another origin" do
463 File.read!("test/fixtures/bogus-mastodon-announce.json")
466 assert :error = Transmogrifier.handle_incoming(data)
469 test "it does not clobber the addressing on announce activities" do
471 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
474 File.read!("test/fixtures/mastodon-announce.json")
476 |> Map.put("object", Object.normalize(activity).data["id"])
477 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
480 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
482 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
485 test "it ensures that as:Public activities make it to their followers collection" do
489 File.read!("test/fixtures/mastodon-post-activity.json")
491 |> Map.put("actor", user.ap_id)
492 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
497 |> Map.put("attributedTo", user.ap_id)
498 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
500 |> Map.put("id", user.ap_id <> "/activities/12345678")
502 data = Map.put(data, "object", object)
504 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
506 assert data["cc"] == [User.ap_followers(user)]
509 test "it ensures that address fields become lists" do
513 File.read!("test/fixtures/mastodon-post-activity.json")
515 |> Map.put("actor", user.ap_id)
516 |> Map.put("to", nil)
517 |> Map.put("cc", nil)
521 |> Map.put("attributedTo", user.ap_id)
522 |> Map.put("to", nil)
523 |> Map.put("cc", nil)
524 |> Map.put("id", user.ap_id <> "/activities/12345678")
526 data = Map.put(data, "object", object)
528 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
530 assert !is_nil(data["to"])
531 assert !is_nil(data["cc"])
534 test "it strips internal likes" do
536 File.read!("test/fixtures/mastodon-post-activity.json")
541 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
542 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
544 "type" => "OrderedCollection"
547 object = Map.put(data["object"], "likes", likes)
548 data = Map.put(data, "object", object)
550 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
552 refute Map.has_key?(object.data, "likes")
555 test "it works for incoming update activities" do
556 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
558 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
559 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
562 update_data["object"]
563 |> Map.put("actor", data["actor"])
564 |> Map.put("id", data["actor"])
568 |> Map.put("actor", data["actor"])
569 |> Map.put("object", object)
571 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
573 assert data["id"] == update_data["id"]
575 user = User.get_cached_by_ap_id(data["actor"])
576 assert user.name == "gargle"
578 assert user.avatar["url"] == [
581 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
585 assert user.banner["url"] == [
588 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
592 assert user.bio == "<p>Some bio</p>"
595 test "it works with alsoKnownAs" do
596 {:ok, %Activity{data: %{"actor" => actor}}} =
597 "test/fixtures/mastodon-post-activity.json"
600 |> Transmogrifier.handle_incoming()
602 assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"]
605 "test/fixtures/mastodon-update.json"
608 |> Map.put("actor", actor)
609 |> Map.update!("object", fn object ->
611 |> Map.put("actor", actor)
612 |> Map.put("id", actor)
613 |> Map.put("alsoKnownAs", [
614 "http://mastodon.example.org/users/foo",
615 "http://example.org/users/bar"
618 |> Transmogrifier.handle_incoming()
620 assert User.get_cached_by_ap_id(actor).also_known_as == [
621 "http://mastodon.example.org/users/foo",
622 "http://example.org/users/bar"
626 test "it works with custom profile fields" do
628 "test/fixtures/mastodon-post-activity.json"
631 |> Transmogrifier.handle_incoming()
633 user = User.get_cached_by_ap_id(activity.actor)
635 assert User.fields(user) == [
636 %{"name" => "foo", "value" => "bar"},
637 %{"name" => "foo1", "value" => "bar1"}
640 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
643 update_data["object"]
644 |> Map.put("actor", user.ap_id)
645 |> Map.put("id", user.ap_id)
649 |> Map.put("actor", user.ap_id)
650 |> Map.put("object", object)
652 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
654 user = User.get_cached_by_ap_id(user.ap_id)
656 assert User.fields(user) == [
657 %{"name" => "foo", "value" => "updated"},
658 %{"name" => "foo1", "value" => "updated"}
661 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
664 put_in(update_data, ["object", "attachment"], [
665 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
666 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
667 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
670 {:ok, _} = Transmogrifier.handle_incoming(update_data)
672 user = User.get_cached_by_ap_id(user.ap_id)
674 assert User.fields(user) == [
675 %{"name" => "foo", "value" => "updated"},
676 %{"name" => "foo1", "value" => "updated"}
679 update_data = put_in(update_data, ["object", "attachment"], [])
681 {:ok, _} = Transmogrifier.handle_incoming(update_data)
683 user = User.get_cached_by_ap_id(user.ap_id)
685 assert User.fields(user) == []
688 test "it works for incoming update activities which lock the account" do
689 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
691 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
692 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
695 update_data["object"]
696 |> Map.put("actor", data["actor"])
697 |> Map.put("id", data["actor"])
698 |> Map.put("manuallyApprovesFollowers", true)
702 |> Map.put("actor", data["actor"])
703 |> Map.put("object", object)
705 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
707 user = User.get_cached_by_ap_id(data["actor"])
708 assert user.locked == true
711 test "it works for incoming deletes" do
712 activity = insert(:note_activity)
713 deleting_user = insert(:user)
716 File.read!("test/fixtures/mastodon-delete.json")
721 |> Map.put("id", activity.data["object"])
725 |> Map.put("object", object)
726 |> Map.put("actor", deleting_user.ap_id)
728 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
729 Transmogrifier.handle_incoming(data)
731 assert id == data["id"]
732 refute Activity.get_by_id(activity.id)
733 assert actor == deleting_user.ap_id
736 test "it fails for incoming deletes with spoofed origin" do
737 activity = insert(:note_activity)
740 File.read!("test/fixtures/mastodon-delete.json")
745 |> Map.put("id", activity.data["object"])
749 |> Map.put("object", object)
751 assert capture_log(fn ->
752 :error = Transmogrifier.handle_incoming(data)
754 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, :nxdomain}"
756 assert Activity.get_by_id(activity.id)
759 test "it works for incoming user deletes" do
760 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
763 File.read!("test/fixtures/mastodon-delete-user.json")
766 {:ok, _} = Transmogrifier.handle_incoming(data)
767 ObanHelpers.perform_all()
769 refute User.get_cached_by_ap_id(ap_id)
772 test "it fails for incoming user deletes with spoofed origin" do
773 %{ap_id: ap_id} = insert(:user)
776 File.read!("test/fixtures/mastodon-delete-user.json")
778 |> Map.put("actor", ap_id)
780 assert :error == Transmogrifier.handle_incoming(data)
781 assert User.get_cached_by_ap_id(ap_id)
784 test "it works for incoming unannounces with an existing notice" do
786 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
789 File.read!("test/fixtures/mastodon-announce.json")
791 |> Map.put("object", activity.data["object"])
793 {:ok, %Activity{data: announce_data, local: false}} =
794 Transmogrifier.handle_incoming(announce_data)
797 File.read!("test/fixtures/mastodon-undo-announce.json")
799 |> Map.put("object", announce_data)
800 |> Map.put("actor", announce_data["actor"])
802 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
804 assert data["type"] == "Undo"
805 assert object_data = data["object"]
806 assert object_data["type"] == "Announce"
807 assert object_data["object"] == activity.data["object"]
809 assert object_data["id"] ==
810 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
813 test "it works for incomming unfollows with an existing follow" do
817 File.read!("test/fixtures/mastodon-follow-activity.json")
819 |> Map.put("object", user.ap_id)
821 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
824 File.read!("test/fixtures/mastodon-unfollow-activity.json")
826 |> Map.put("object", follow_data)
828 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
830 assert data["type"] == "Undo"
831 assert data["object"]["type"] == "Follow"
832 assert data["object"]["object"] == user.ap_id
833 assert data["actor"] == "http://mastodon.example.org/users/admin"
835 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
838 test "it works for incoming blocks" do
842 File.read!("test/fixtures/mastodon-block-activity.json")
844 |> Map.put("object", user.ap_id)
846 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
848 assert data["type"] == "Block"
849 assert data["object"] == user.ap_id
850 assert data["actor"] == "http://mastodon.example.org/users/admin"
852 blocker = User.get_cached_by_ap_id(data["actor"])
854 assert User.blocks?(blocker, user)
857 test "incoming blocks successfully tear down any follow relationship" do
858 blocker = insert(:user)
859 blocked = insert(:user)
862 File.read!("test/fixtures/mastodon-block-activity.json")
864 |> Map.put("object", blocked.ap_id)
865 |> Map.put("actor", blocker.ap_id)
867 {:ok, blocker} = User.follow(blocker, blocked)
868 {:ok, blocked} = User.follow(blocked, blocker)
870 assert User.following?(blocker, blocked)
871 assert User.following?(blocked, blocker)
873 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
875 assert data["type"] == "Block"
876 assert data["object"] == blocked.ap_id
877 assert data["actor"] == blocker.ap_id
879 blocker = User.get_cached_by_ap_id(data["actor"])
880 blocked = User.get_cached_by_ap_id(data["object"])
882 assert User.blocks?(blocker, blocked)
884 refute User.following?(blocker, blocked)
885 refute User.following?(blocked, blocker)
888 test "it works for incoming unblocks with an existing block" do
892 File.read!("test/fixtures/mastodon-block-activity.json")
894 |> Map.put("object", user.ap_id)
896 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
899 File.read!("test/fixtures/mastodon-unblock-activity.json")
901 |> Map.put("object", block_data)
903 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
904 assert data["type"] == "Undo"
905 assert data["object"]["type"] == "Block"
906 assert data["object"]["object"] == user.ap_id
907 assert data["actor"] == "http://mastodon.example.org/users/admin"
909 blocker = User.get_cached_by_ap_id(data["actor"])
911 refute User.blocks?(blocker, user)
914 test "it works for incoming accepts which were pre-accepted" do
915 follower = insert(:user)
916 followed = insert(:user)
918 {:ok, follower} = User.follow(follower, followed)
919 assert User.following?(follower, followed) == true
921 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
924 File.read!("test/fixtures/mastodon-accept-activity.json")
926 |> Map.put("actor", followed.ap_id)
929 accept_data["object"]
930 |> Map.put("actor", follower.ap_id)
931 |> Map.put("id", follow_activity.data["id"])
933 accept_data = Map.put(accept_data, "object", object)
935 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
936 refute activity.local
938 assert activity.data["object"] == follow_activity.data["id"]
940 assert activity.data["id"] == accept_data["id"]
942 follower = User.get_cached_by_id(follower.id)
944 assert User.following?(follower, followed) == true
947 test "it works for incoming accepts which were orphaned" do
948 follower = insert(:user)
949 followed = insert(:user, locked: true)
951 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
954 File.read!("test/fixtures/mastodon-accept-activity.json")
956 |> Map.put("actor", followed.ap_id)
959 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
961 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
962 assert activity.data["object"] == follow_activity.data["id"]
964 follower = User.get_cached_by_id(follower.id)
966 assert User.following?(follower, followed) == true
969 test "it works for incoming accepts which are referenced by IRI only" do
970 follower = insert(:user)
971 followed = insert(:user, locked: true)
973 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
976 File.read!("test/fixtures/mastodon-accept-activity.json")
978 |> Map.put("actor", followed.ap_id)
979 |> Map.put("object", follow_activity.data["id"])
981 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
982 assert activity.data["object"] == follow_activity.data["id"]
984 follower = User.get_cached_by_id(follower.id)
986 assert User.following?(follower, followed) == true
989 test "it fails for incoming accepts which cannot be correlated" do
990 follower = insert(:user)
991 followed = insert(:user, locked: true)
994 File.read!("test/fixtures/mastodon-accept-activity.json")
996 |> Map.put("actor", followed.ap_id)
999 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1001 :error = Transmogrifier.handle_incoming(accept_data)
1003 follower = User.get_cached_by_id(follower.id)
1005 refute User.following?(follower, followed) == true
1008 test "it fails for incoming rejects which cannot be correlated" do
1009 follower = insert(:user)
1010 followed = insert(:user, locked: true)
1013 File.read!("test/fixtures/mastodon-reject-activity.json")
1015 |> Map.put("actor", followed.ap_id)
1018 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
1020 :error = Transmogrifier.handle_incoming(accept_data)
1022 follower = User.get_cached_by_id(follower.id)
1024 refute User.following?(follower, followed) == true
1027 test "it works for incoming rejects which are orphaned" do
1028 follower = insert(:user)
1029 followed = insert(:user, locked: true)
1031 {:ok, follower} = User.follow(follower, followed)
1032 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1034 assert User.following?(follower, followed) == true
1037 File.read!("test/fixtures/mastodon-reject-activity.json")
1039 |> Map.put("actor", followed.ap_id)
1042 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1044 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1045 refute activity.local
1046 assert activity.data["id"] == reject_data["id"]
1048 follower = User.get_cached_by_id(follower.id)
1050 assert User.following?(follower, followed) == false
1053 test "it works for incoming rejects which are referenced by IRI only" do
1054 follower = insert(:user)
1055 followed = insert(:user, locked: true)
1057 {:ok, follower} = User.follow(follower, followed)
1058 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1060 assert User.following?(follower, followed) == true
1063 File.read!("test/fixtures/mastodon-reject-activity.json")
1065 |> Map.put("actor", followed.ap_id)
1066 |> Map.put("object", follow_activity.data["id"])
1068 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1070 follower = User.get_cached_by_id(follower.id)
1072 assert User.following?(follower, followed) == false
1075 test "it rejects activities without a valid ID" do
1076 user = insert(:user)
1079 File.read!("test/fixtures/mastodon-follow-activity.json")
1081 |> Map.put("object", user.ap_id)
1082 |> Map.put("id", "")
1084 :error = Transmogrifier.handle_incoming(data)
1087 test "it remaps video URLs as attachments if necessary" do
1089 Fetcher.fetch_object_from_id(
1090 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1095 "mediaType" => "video/mp4",
1097 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1098 "mimeType" => "video/mp4",
1099 "size" => 5_015_880,
1103 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1104 "mediaType" => "video/mp4",
1111 assert object.data["url"] ==
1112 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1114 assert object.data["attachment"] == [attachment]
1117 test "it accepts Flag activities" do
1118 user = insert(:user)
1119 other_user = insert(:user)
1121 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1122 object = Object.normalize(activity)
1125 "@context" => "https://www.w3.org/ns/activitystreams",
1126 "cc" => [user.ap_id],
1127 "object" => [user.ap_id, object.data["id"]],
1129 "content" => "blocked AND reported!!!",
1130 "actor" => other_user.ap_id
1133 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1135 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1136 assert activity.data["content"] == "blocked AND reported!!!"
1137 assert activity.data["actor"] == other_user.ap_id
1138 assert activity.data["cc"] == [user.ap_id]
1141 test "it correctly processes messages with non-array to field" do
1142 user = insert(:user)
1145 "@context" => "https://www.w3.org/ns/activitystreams",
1146 "to" => "https://www.w3.org/ns/activitystreams#Public",
1149 "content" => "blah blah blah",
1151 "attributedTo" => user.ap_id,
1154 "actor" => user.ap_id
1157 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1159 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
1162 test "it correctly processes messages with non-array cc field" do
1163 user = insert(:user)
1166 "@context" => "https://www.w3.org/ns/activitystreams",
1167 "to" => user.follower_address,
1168 "cc" => "https://www.w3.org/ns/activitystreams#Public",
1171 "content" => "blah blah blah",
1173 "attributedTo" => user.ap_id,
1176 "actor" => user.ap_id
1179 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1181 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
1182 assert [user.follower_address] == activity.data["to"]
1186 describe "prepare outgoing" do
1187 test "it inlines private announced objects" do
1188 user = insert(:user)
1190 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1192 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1194 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1196 assert modified["object"]["content"] == "hey"
1197 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1200 test "it turns mentions into tags" do
1201 user = insert(:user)
1202 other_user = insert(:user)
1205 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1207 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1208 object = modified["object"]
1210 expected_mention = %{
1211 "href" => other_user.ap_id,
1212 "name" => "@#{other_user.nickname}",
1217 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1218 "type" => "Hashtag",
1222 assert Enum.member?(object["tag"], expected_tag)
1223 assert Enum.member?(object["tag"], expected_mention)
1226 test "it adds the sensitive property" do
1227 user = insert(:user)
1229 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1230 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1232 assert modified["object"]["sensitive"]
1235 test "it adds the json-ld context and the conversation property" do
1236 user = insert(:user)
1238 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1239 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1241 assert modified["@context"] ==
1242 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1244 assert modified["object"]["conversation"] == modified["context"]
1247 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1248 user = insert(:user)
1250 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1251 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1253 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1256 test "it strips internal hashtag data" do
1257 user = insert(:user)
1259 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1262 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1263 "type" => "Hashtag",
1267 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1269 assert modified["object"]["tag"] == [expected_tag]
1272 test "it strips internal fields" do
1273 user = insert(:user)
1275 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1277 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1279 assert length(modified["object"]["tag"]) == 2
1281 assert is_nil(modified["object"]["emoji"])
1282 assert is_nil(modified["object"]["like_count"])
1283 assert is_nil(modified["object"]["announcements"])
1284 assert is_nil(modified["object"]["announcement_count"])
1285 assert is_nil(modified["object"]["context_id"])
1288 test "it strips internal fields of article" do
1289 activity = insert(:article_activity)
1291 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1293 assert length(modified["object"]["tag"]) == 2
1295 assert is_nil(modified["object"]["emoji"])
1296 assert is_nil(modified["object"]["like_count"])
1297 assert is_nil(modified["object"]["announcements"])
1298 assert is_nil(modified["object"]["announcement_count"])
1299 assert is_nil(modified["object"]["context_id"])
1300 assert is_nil(modified["object"]["likes"])
1303 test "the directMessage flag is present" do
1304 user = insert(:user)
1305 other_user = insert(:user)
1307 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1309 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1311 assert modified["directMessage"] == false
1314 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1316 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1318 assert modified["directMessage"] == false
1321 CommonAPI.post(user, %{
1322 "status" => "@#{other_user.nickname} :moominmamma:",
1323 "visibility" => "direct"
1326 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1328 assert modified["directMessage"] == true
1331 test "it strips BCC field" do
1332 user = insert(:user)
1333 {:ok, list} = Pleroma.List.create("foo", user)
1336 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1338 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1340 assert is_nil(modified["bcc"])
1343 test "it can handle Listen activities" do
1344 listen_activity = insert(:listen)
1346 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1348 assert modified["type"] == "Listen"
1350 user = insert(:user)
1352 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1354 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1358 describe "user upgrade" do
1359 test "it upgrades a user to activitypub" do
1362 nickname: "rye@niu.moe",
1364 ap_id: "https://niu.moe/users/rye",
1365 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1368 user_two = insert(:user, %{following: [user.follower_address]})
1370 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1371 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1372 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1374 user = User.get_cached_by_id(user.id)
1375 assert user.note_count == 1
1377 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1378 ObanHelpers.perform_all()
1380 assert user.ap_enabled
1381 assert user.note_count == 1
1382 assert user.follower_address == "https://niu.moe/users/rye/followers"
1383 assert user.following_address == "https://niu.moe/users/rye/following"
1385 user = User.get_cached_by_id(user.id)
1386 assert user.note_count == 1
1388 activity = Activity.get_by_id(activity.id)
1389 assert user.follower_address in activity.recipients
1395 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1404 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1409 refute "..." in activity.recipients
1411 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1412 refute user.follower_address in unrelated_activity.recipients
1414 user_two = User.get_cached_by_id(user_two.id)
1415 assert user.follower_address in user_two.following
1416 refute "..." in user_two.following
1420 describe "actor rewriting" do
1421 test "it fixes the actor URL property to be a proper URI" do
1423 "url" => %{"href" => "http://example.com"}
1426 rewritten = Transmogrifier.maybe_fix_user_object(data)
1427 assert rewritten["url"] == "http://example.com"
1431 describe "actor origin containment" do
1432 test "it rejects activities which reference objects with bogus origins" do
1434 "@context" => "https://www.w3.org/ns/activitystreams",
1435 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1436 "actor" => "http://mastodon.example.org/users/admin",
1437 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1438 "object" => "https://info.pleroma.site/activity.json",
1439 "type" => "Announce"
1442 :error = Transmogrifier.handle_incoming(data)
1445 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1447 "@context" => "https://www.w3.org/ns/activitystreams",
1448 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1449 "actor" => "http://mastodon.example.org/users/admin",
1450 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1451 "object" => "https://info.pleroma.site/activity2.json",
1452 "type" => "Announce"
1455 :error = Transmogrifier.handle_incoming(data)
1458 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1460 "@context" => "https://www.w3.org/ns/activitystreams",
1461 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1462 "actor" => "http://mastodon.example.org/users/admin",
1463 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1464 "object" => "https://info.pleroma.site/activity3.json",
1465 "type" => "Announce"
1468 :error = Transmogrifier.handle_incoming(data)
1472 describe "reserialization" do
1473 test "successfully reserializes a message with inReplyTo == nil" do
1474 user = insert(:user)
1477 "@context" => "https://www.w3.org/ns/activitystreams",
1478 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1482 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1487 "attributedTo" => user.ap_id
1489 "actor" => user.ap_id
1492 {:ok, activity} = Transmogrifier.handle_incoming(message)
1494 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1497 test "successfully reserializes a message with AS2 objects in IR" do
1498 user = insert(:user)
1501 "@context" => "https://www.w3.org/ns/activitystreams",
1502 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1506 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1511 "attributedTo" => user.ap_id,
1513 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1514 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1517 "actor" => user.ap_id
1520 {:ok, activity} = Transmogrifier.handle_incoming(message)
1522 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1526 test "Rewrites Answers to Notes" do
1527 user = insert(:user)
1529 {:ok, poll_activity} =
1530 CommonAPI.post(user, %{
1531 "status" => "suya...",
1532 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1535 poll_object = Object.normalize(poll_activity)
1536 # TODO: Replace with CommonAPI vote creation when implemented
1538 File.read!("test/fixtures/mastodon-vote.json")
1540 |> Kernel.put_in(["to"], user.ap_id)
1541 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1542 |> Kernel.put_in(["object", "to"], user.ap_id)
1544 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1545 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1547 assert data["object"]["type"] == "Note"
1550 describe "fix_explicit_addressing" do
1552 user = insert(:user)
1556 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1557 explicitly_mentioned_actors = [
1558 "https://pleroma.gold/users/user1",
1559 "https://pleroma.gold/user2"
1563 "actor" => user.ap_id,
1564 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1567 Enum.map(explicitly_mentioned_actors, fn href ->
1568 %{"type" => "Mention", "href" => href}
1572 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1573 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1574 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1575 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1578 test "does not move actor's follower collection to cc", %{user: user} do
1580 "actor" => user.ap_id,
1581 "to" => [user.follower_address],
1585 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1586 assert user.follower_address in fixed_object["to"]
1587 refute user.follower_address in fixed_object["cc"]
1590 test "removes recipient's follower collection from cc", %{user: user} do
1591 recipient = insert(:user)
1594 "actor" => user.ap_id,
1595 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1596 "cc" => [user.follower_address, recipient.follower_address]
1599 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1601 assert user.follower_address in fixed_object["cc"]
1602 refute recipient.follower_address in fixed_object["cc"]
1603 refute recipient.follower_address in fixed_object["to"]
1607 describe "fix_summary/1" do
1608 test "returns fixed object" do
1609 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1610 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1611 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1615 describe "fix_in_reply_to/2" do
1616 clear_config([:instance, :federation_incoming_replies_max_depth])
1619 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1623 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1624 assert Transmogrifier.fix_in_reply_to(data) == data
1627 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1628 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1631 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1633 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1634 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1635 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1638 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1640 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1641 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1642 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1645 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1647 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1648 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1649 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1651 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1652 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1653 assert modified_object["inReplyTo"] == []
1654 assert modified_object["inReplyToAtomUri"] == ""
1657 test "returns modified object when allowed incoming reply", %{data: data} do
1662 "https://shitposter.club/notice/2827873"
1665 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1666 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1668 assert modified_object["inReplyTo"] ==
1669 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1671 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1673 assert modified_object["conversation"] ==
1674 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1676 assert modified_object["context"] ==
1677 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1681 describe "fix_url/1" do
1682 test "fixes data for object when url is map" do
1686 "mimeType" => "video/mp4",
1687 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1691 assert Transmogrifier.fix_url(object) == %{
1692 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1696 test "fixes data for video object" do
1702 "mimeType" => "video/mp4",
1703 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1707 "mimeType" => "video/mp4",
1708 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1712 "mimeType" => "text/html",
1713 "href" => "https://peertube.-2d4c2d1630e3"
1717 "mimeType" => "text/html",
1718 "href" => "https://peertube.-2d4c2d16377-42"
1723 assert Transmogrifier.fix_url(object) == %{
1726 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1727 "mimeType" => "video/mp4",
1732 "url" => "https://peertube.-2d4c2d1630e3"
1736 test "fixes url for not Video object" do
1742 "mimeType" => "text/html",
1743 "href" => "https://peertube.-2d4c2d1630e3"
1747 "mimeType" => "text/html",
1748 "href" => "https://peertube.-2d4c2d16377-42"
1753 assert Transmogrifier.fix_url(object) == %{
1755 "url" => "https://peertube.-2d4c2d1630e3"
1758 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1764 test "retunrs not modified object" do
1765 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1769 describe "get_obj_helper/2" do
1770 test "returns nil when cannot normalize object" do
1771 refute Transmogrifier.get_obj_helper("test-obj-id")
1774 test "returns {:ok, %Object{}} for success case" do
1775 assert {:ok, %Object{}} =
1776 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1780 describe "fix_attachments/1" do
1781 test "returns not modified object" do
1782 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1783 assert Transmogrifier.fix_attachments(data) == data
1786 test "returns modified object when attachment is map" do
1787 assert Transmogrifier.fix_attachments(%{
1789 "mediaType" => "video/mp4",
1790 "url" => "https://peertube.moe/stat-480.mp4"
1795 "mediaType" => "video/mp4",
1798 "href" => "https://peertube.moe/stat-480.mp4",
1799 "mediaType" => "video/mp4",
1808 test "returns modified object when attachment is list" do
1809 assert Transmogrifier.fix_attachments(%{
1811 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1812 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1817 "mediaType" => "video/mp4",
1820 "href" => "https://pe.er/stat-480.mp4",
1821 "mediaType" => "video/mp4",
1827 "href" => "https://pe.er/stat-480.mp4",
1828 "mediaType" => "video/mp4",
1829 "mimeType" => "video/mp4",
1832 "href" => "https://pe.er/stat-480.mp4",
1833 "mediaType" => "video/mp4",
1843 describe "fix_emoji/1" do
1844 test "returns not modified object when object not contains tags" do
1845 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1846 assert Transmogrifier.fix_emoji(data) == data
1849 test "returns object with emoji when object contains list tags" do
1850 assert Transmogrifier.fix_emoji(%{
1852 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1853 %{"type" => "Hashtag"}
1856 "emoji" => %{"bib" => "/test"},
1858 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1859 %{"type" => "Hashtag"}
1864 test "returns object with emoji when object contains map tag" do
1865 assert Transmogrifier.fix_emoji(%{
1866 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1868 "emoji" => %{"bib" => "/test"},
1869 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}