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.ActivityPubTest do
8 alias Pleroma.Builders.ActivityBuilder
9 alias Pleroma.Instances
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Publisher
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
22 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 describe "streaming out participations" do
27 test "it streams them out" do
29 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
31 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
34 conversation.participations
35 |> Repo.preload(:user)
37 with_mock Pleroma.Web.Streamer,
38 stream: fn _, _ -> nil end do
39 ActivityPub.stream_out_participations(conversation.participations)
41 Enum.each(participations, fn participation ->
42 assert called(Pleroma.Web.Streamer.stream("participation", participation))
48 describe "fetching restricted by visibility" do
49 test "it restricts by the appropriate visibility" do
52 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
54 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
56 {:ok, unlisted_activity} =
57 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
59 {:ok, private_activity} =
60 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
63 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
65 assert activities == [direct_activity]
68 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
70 assert activities == [unlisted_activity]
73 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
75 assert activities == [private_activity]
78 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
80 assert activities == [public_activity]
83 ActivityPub.fetch_activities([], %{
84 :visibility => ~w[private public],
85 "actor_id" => user.ap_id
88 assert activities == [public_activity, private_activity]
92 describe "building a user from his ap id" do
93 test "it returns a user" do
94 user_id = "http://mastodon.example.org/users/admin"
95 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
96 assert user.ap_id == user_id
97 assert user.nickname == "admin@mastodon.example.org"
98 assert user.info.source_data
99 assert user.info.ap_enabled
100 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
103 test "it fetches the appropriate tag-restricted posts" do
106 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
107 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
108 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
110 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
113 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
116 ActivityPub.fetch_activities([], %{
118 "tag" => ["test", "essais"],
119 "tag_reject" => ["reject"]
123 ActivityPub.fetch_activities([], %{
126 "tag_all" => ["test", "reject"]
129 assert fetch_one == [status_one, status_three]
130 assert fetch_two == [status_one, status_two, status_three]
131 assert fetch_three == [status_one, status_two]
132 assert fetch_four == [status_three]
136 describe "insertion" do
137 test "drops activities beyond a certain limit" do
138 limit = Pleroma.Config.get([:instance, :remote_limit])
141 :crypto.strong_rand_bytes(limit + 1)
143 |> binary_part(0, limit + 1)
148 "content" => random_text
152 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
155 test "doesn't drop activities with content being null" do
159 "actor" => user.ap_id,
162 "actor" => user.ap_id,
169 assert {:ok, _} = ActivityPub.insert(data)
172 test "returns the activity if one with the same id is already in" do
173 activity = insert(:note_activity)
174 {:ok, new_activity} = ActivityPub.insert(activity.data)
176 assert activity.id == new_activity.id
179 test "inserts a given map into the activity database, giving it an id if it has none." do
183 "actor" => user.ap_id,
186 "actor" => user.ap_id,
193 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
194 assert activity.data["ok"] == data["ok"]
195 assert is_binary(activity.data["id"])
201 "actor" => user.ap_id,
203 "context" => "blabla",
205 "actor" => user.ap_id,
212 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
213 assert activity.data["ok"] == data["ok"]
214 assert activity.data["id"] == given_id
215 assert activity.data["context"] == "blabla"
216 assert activity.data["context_id"]
219 test "adds a context when none is there" do
223 "actor" => user.ap_id,
226 "actor" => user.ap_id,
233 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
234 object = Pleroma.Object.normalize(activity)
236 assert is_binary(activity.data["context"])
237 assert is_binary(object.data["context"])
238 assert activity.data["context_id"]
239 assert object.data["context_id"]
242 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
246 "actor" => user.ap_id,
249 "actor" => user.ap_id,
256 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
257 assert object = Object.normalize(activity)
258 assert is_binary(object.data["id"])
262 describe "create activities" do
263 test "removes doubled 'to' recipients" do
267 ActivityPub.create(%{
268 to: ["user1", "user1", "user2"],
272 "to" => ["user1", "user1", "user2"],
274 "content" => "testing"
278 assert activity.data["to"] == ["user1", "user2"]
279 assert activity.actor == user.ap_id
280 assert activity.recipients == ["user1", "user2", user.ap_id]
283 test "increases user note count only for public activities" do
287 CommonAPI.post(User.get_cached_by_id(user.id), %{
289 "visibility" => "public"
293 CommonAPI.post(User.get_cached_by_id(user.id), %{
295 "visibility" => "unlisted"
299 CommonAPI.post(User.get_cached_by_id(user.id), %{
301 "visibility" => "private"
305 CommonAPI.post(User.get_cached_by_id(user.id), %{
307 "visibility" => "direct"
310 user = User.get_cached_by_id(user.id)
311 assert user.info.note_count == 2
314 test "increases replies count" do
316 user2 = insert(:user)
318 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
319 ap_id = activity.data["id"]
320 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
323 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
324 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
325 assert object.data["repliesCount"] == 1
328 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
329 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
330 assert object.data["repliesCount"] == 2
333 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
334 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
335 assert object.data["repliesCount"] == 2
338 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
339 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
340 assert object.data["repliesCount"] == 2
344 describe "fetch activities for recipients" do
345 test "retrieve the activities for certain recipients" do
346 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
347 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
348 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
350 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
351 assert length(activities) == 2
352 assert activities == [activity_one, activity_two]
356 describe "fetch activities in context" do
357 test "retrieves activities that have a given context" do
358 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
359 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
360 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
361 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
362 activity_five = insert(:note_activity)
365 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
367 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
368 assert activities == [activity_two, activity]
372 test "doesn't return blocked activities" do
373 activity_one = insert(:note_activity)
374 activity_two = insert(:note_activity)
375 activity_three = insert(:note_activity)
377 booster = insert(:user)
378 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
381 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
383 assert Enum.member?(activities, activity_two)
384 assert Enum.member?(activities, activity_three)
385 refute Enum.member?(activities, activity_one)
387 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
390 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
392 assert Enum.member?(activities, activity_two)
393 assert Enum.member?(activities, activity_three)
394 assert Enum.member?(activities, activity_one)
396 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
397 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
398 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
399 activity_three = Activity.get_by_id(activity_three.id)
402 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
404 assert Enum.member?(activities, activity_two)
405 refute Enum.member?(activities, activity_three)
406 refute Enum.member?(activities, boost_activity)
407 assert Enum.member?(activities, activity_one)
410 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
412 assert Enum.member?(activities, activity_two)
413 assert Enum.member?(activities, activity_three)
414 assert Enum.member?(activities, boost_activity)
415 assert Enum.member?(activities, activity_one)
418 test "doesn't return transitive interactions concerning blocked users" do
419 blocker = insert(:user)
420 blockee = insert(:user)
421 friend = insert(:user)
423 {:ok, blocker} = User.block(blocker, blockee)
425 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
427 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
429 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
431 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
433 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
435 assert Enum.member?(activities, activity_one)
436 refute Enum.member?(activities, activity_two)
437 refute Enum.member?(activities, activity_three)
438 refute Enum.member?(activities, activity_four)
441 test "doesn't return announce activities concerning blocked users" do
442 blocker = insert(:user)
443 blockee = insert(:user)
444 friend = insert(:user)
446 {:ok, blocker} = User.block(blocker, blockee)
448 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
450 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
452 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
455 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
456 |> Enum.map(fn act -> act.id end)
458 assert Enum.member?(activities, activity_one.id)
459 refute Enum.member?(activities, activity_two.id)
460 refute Enum.member?(activities, activity_three.id)
463 test "doesn't return activities from blocked domains" do
464 domain = "dogwhistle.zone"
465 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
466 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
467 activity = insert(:note_activity, %{note: note})
469 {:ok, user} = User.block_domain(user, domain)
472 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
474 refute activity in activities
476 followed_user = insert(:user)
477 ActivityPub.follow(user, followed_user)
478 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
481 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
483 refute repeat_activity in activities
486 test "doesn't return muted activities" do
487 activity_one = insert(:note_activity)
488 activity_two = insert(:note_activity)
489 activity_three = insert(:note_activity)
491 booster = insert(:user)
492 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
495 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
497 assert Enum.member?(activities, activity_two)
498 assert Enum.member?(activities, activity_three)
499 refute Enum.member?(activities, activity_one)
501 # Calling with 'with_muted' will deliver muted activities, too.
503 ActivityPub.fetch_activities([], %{
504 "muting_user" => user,
505 "with_muted" => true,
506 "skip_preload" => true
509 assert Enum.member?(activities, activity_two)
510 assert Enum.member?(activities, activity_three)
511 assert Enum.member?(activities, activity_one)
513 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
516 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
518 assert Enum.member?(activities, activity_two)
519 assert Enum.member?(activities, activity_three)
520 assert Enum.member?(activities, activity_one)
522 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
523 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
524 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
525 activity_three = Activity.get_by_id(activity_three.id)
528 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
530 assert Enum.member?(activities, activity_two)
531 refute Enum.member?(activities, activity_three)
532 refute Enum.member?(activities, boost_activity)
533 assert Enum.member?(activities, activity_one)
535 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
537 assert Enum.member?(activities, activity_two)
538 assert Enum.member?(activities, activity_three)
539 assert Enum.member?(activities, boost_activity)
540 assert Enum.member?(activities, activity_one)
543 test "does include announces on request" do
544 activity_three = insert(:note_activity)
546 booster = insert(:user)
548 {:ok, user} = User.follow(user, booster)
550 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
552 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
554 assert announce_activity.id == announce.id
557 test "excludes reblogs on request" do
559 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
560 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
562 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
564 assert activity == expected_activity
567 describe "public fetch activities" do
568 test "doesn't retrieve unlisted activities" do
571 {:ok, _unlisted_activity} =
572 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
574 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
576 [activity] = ActivityPub.fetch_public_activities()
578 assert activity == listed_activity
581 test "retrieves public activities" do
582 _activities = ActivityPub.fetch_public_activities()
584 %{public: public} = ActivityBuilder.public_and_non_public()
586 activities = ActivityPub.fetch_public_activities()
587 assert length(activities) == 1
588 assert Enum.at(activities, 0) == public
591 test "retrieves a maximum of 20 activities" do
592 activities = ActivityBuilder.insert_list(30)
593 last_expected = List.last(activities)
595 activities = ActivityPub.fetch_public_activities()
596 last = List.last(activities)
598 assert length(activities) == 20
599 assert last == last_expected
602 test "retrieves ids starting from a since_id" do
603 activities = ActivityBuilder.insert_list(30)
604 later_activities = ActivityBuilder.insert_list(10)
605 since_id = List.last(activities).id
606 last_expected = List.last(later_activities)
608 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
609 last = List.last(activities)
611 assert length(activities) == 10
612 assert last == last_expected
615 test "retrieves ids up to max_id" do
616 _first_activities = ActivityBuilder.insert_list(10)
617 activities = ActivityBuilder.insert_list(20)
618 later_activities = ActivityBuilder.insert_list(10)
619 max_id = List.first(later_activities).id
620 last_expected = List.last(activities)
622 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
623 last = List.last(activities)
625 assert length(activities) == 20
626 assert last == last_expected
629 test "doesn't return reblogs for users for whom reblogs have been muted" do
630 activity = insert(:note_activity)
632 booster = insert(:user)
633 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
635 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
637 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
639 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
642 test "returns reblogs for users for whom reblogs have not been muted" do
643 activity = insert(:note_activity)
645 booster = insert(:user)
646 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
647 {:ok, user} = CommonAPI.show_reblogs(user, booster)
649 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
651 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
653 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
657 describe "like an object" do
658 test "adds a like activity to the db" do
659 note_activity = insert(:note_activity)
660 assert object = Object.normalize(note_activity)
663 user_two = insert(:user)
665 {:ok, like_activity, object} = ActivityPub.like(user, object)
667 assert like_activity.data["actor"] == user.ap_id
668 assert like_activity.data["type"] == "Like"
669 assert like_activity.data["object"] == object.data["id"]
670 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
671 assert like_activity.data["context"] == object.data["context"]
672 assert object.data["like_count"] == 1
673 assert object.data["likes"] == [user.ap_id]
675 # Just return the original activity if the user already liked it.
676 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
678 assert like_activity == same_like_activity
679 assert object.data["likes"] == [user.ap_id]
680 assert object.data["like_count"] == 1
682 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
683 assert note_activity.data["object"]["like_count"] == 1
685 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
686 assert object.data["like_count"] == 2
688 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
689 assert note_activity.data["object"]["like_count"] == 2
693 describe "unliking" do
694 test "unliking a previously liked object" do
695 note_activity = insert(:note_activity)
696 object = Object.normalize(note_activity)
699 # Unliking something that hasn't been liked does nothing
700 {:ok, object} = ActivityPub.unlike(user, object)
701 assert object.data["like_count"] == 0
703 {:ok, like_activity, object} = ActivityPub.like(user, object)
704 assert object.data["like_count"] == 1
706 {:ok, _, _, object} = ActivityPub.unlike(user, object)
707 assert object.data["like_count"] == 0
709 assert Activity.get_by_id(like_activity.id) == nil
713 describe "announcing an object" do
714 test "adds an announce activity to the db" do
715 note_activity = insert(:note_activity)
716 object = Object.normalize(note_activity)
719 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
720 assert object.data["announcement_count"] == 1
721 assert object.data["announcements"] == [user.ap_id]
723 assert announce_activity.data["to"] == [
724 User.ap_followers(user),
725 note_activity.data["actor"]
728 assert announce_activity.data["object"] == object.data["id"]
729 assert announce_activity.data["actor"] == user.ap_id
730 assert announce_activity.data["context"] == object.data["context"]
734 describe "unannouncing an object" do
735 test "unannouncing a previously announced object" do
736 note_activity = insert(:note_activity)
737 object = Object.normalize(note_activity)
740 # Unannouncing an object that is not announced does nothing
741 # {:ok, object} = ActivityPub.unannounce(user, object)
742 # assert object.data["announcement_count"] == 0
744 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
745 assert object.data["announcement_count"] == 1
747 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
748 assert object.data["announcement_count"] == 0
750 assert unannounce_activity.data["to"] == [
751 User.ap_followers(user),
752 announce_activity.data["actor"]
755 assert unannounce_activity.data["type"] == "Undo"
756 assert unannounce_activity.data["object"] == announce_activity.data
757 assert unannounce_activity.data["actor"] == user.ap_id
758 assert unannounce_activity.data["context"] == announce_activity.data["context"]
760 assert Activity.get_by_id(announce_activity.id) == nil
764 describe "uploading files" do
765 test "copies the file to the configured folder" do
767 content_type: "image/jpg",
768 path: Path.absname("test/fixtures/image.jpg"),
769 filename: "an_image.jpg"
772 {:ok, %Object{} = object} = ActivityPub.upload(file)
773 assert object.data["name"] == "an_image.jpg"
776 test "works with base64 encoded images" do
781 {:ok, %Object{}} = ActivityPub.upload(file)
785 describe "fetch the latest Follow" do
786 test "fetches the latest Follow activity" do
787 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
788 follower = Repo.get_by(User, ap_id: activity.data["actor"])
789 followed = Repo.get_by(User, ap_id: activity.data["object"])
791 assert activity == Utils.fetch_latest_follow(follower, followed)
795 describe "following / unfollowing" do
796 test "creates a follow activity" do
797 follower = insert(:user)
798 followed = insert(:user)
800 {:ok, activity} = ActivityPub.follow(follower, followed)
801 assert activity.data["type"] == "Follow"
802 assert activity.data["actor"] == follower.ap_id
803 assert activity.data["object"] == followed.ap_id
806 test "creates an undo activity for the last follow" do
807 follower = insert(:user)
808 followed = insert(:user)
810 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
811 {:ok, activity} = ActivityPub.unfollow(follower, followed)
813 assert activity.data["type"] == "Undo"
814 assert activity.data["actor"] == follower.ap_id
816 embedded_object = activity.data["object"]
817 assert is_map(embedded_object)
818 assert embedded_object["type"] == "Follow"
819 assert embedded_object["object"] == followed.ap_id
820 assert embedded_object["id"] == follow_activity.data["id"]
824 describe "blocking / unblocking" do
825 test "creates a block activity" do
826 blocker = insert(:user)
827 blocked = insert(:user)
829 {:ok, activity} = ActivityPub.block(blocker, blocked)
831 assert activity.data["type"] == "Block"
832 assert activity.data["actor"] == blocker.ap_id
833 assert activity.data["object"] == blocked.ap_id
836 test "creates an undo activity for the last block" do
837 blocker = insert(:user)
838 blocked = insert(:user)
840 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
841 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
843 assert activity.data["type"] == "Undo"
844 assert activity.data["actor"] == blocker.ap_id
846 embedded_object = activity.data["object"]
847 assert is_map(embedded_object)
848 assert embedded_object["type"] == "Block"
849 assert embedded_object["object"] == blocked.ap_id
850 assert embedded_object["id"] == block_activity.data["id"]
854 describe "deletion" do
855 test "it creates a delete activity and deletes the original object" do
856 note = insert(:note_activity)
857 object = Object.normalize(note)
858 {:ok, delete} = ActivityPub.delete(object)
860 assert delete.data["type"] == "Delete"
861 assert delete.data["actor"] == note.data["actor"]
862 assert delete.data["object"] == object.data["id"]
864 assert Activity.get_by_id(delete.id) != nil
866 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
869 test "decrements user note count only for public activities" do
870 user = insert(:user, info: %{note_count: 10})
873 CommonAPI.post(User.get_cached_by_id(user.id), %{
875 "visibility" => "public"
879 CommonAPI.post(User.get_cached_by_id(user.id), %{
881 "visibility" => "unlisted"
885 CommonAPI.post(User.get_cached_by_id(user.id), %{
887 "visibility" => "private"
891 CommonAPI.post(User.get_cached_by_id(user.id), %{
893 "visibility" => "direct"
896 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
897 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
898 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
899 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
901 user = User.get_cached_by_id(user.id)
902 assert user.info.note_count == 10
905 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
907 note = insert(:note_activity)
908 object = Object.normalize(note)
914 "actor" => object.data["actor"],
915 "id" => object.data["id"],
916 "to" => [user.ap_id],
920 |> Object.update_and_set_cache()
922 {:ok, delete} = ActivityPub.delete(object)
924 assert user.ap_id in delete.data["to"]
927 test "decreases reply count" do
929 user2 = insert(:user)
931 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
932 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
933 ap_id = activity.data["id"]
935 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
936 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
937 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
938 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
940 _ = CommonAPI.delete(direct_reply.id, user2)
941 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
942 assert object.data["repliesCount"] == 2
944 _ = CommonAPI.delete(private_reply.id, user2)
945 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
946 assert object.data["repliesCount"] == 2
948 _ = CommonAPI.delete(public_reply.id, user2)
949 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
950 assert object.data["repliesCount"] == 1
952 _ = CommonAPI.delete(unlisted_reply.id, user2)
953 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
954 assert object.data["repliesCount"] == 0
958 describe "timeline post-processing" do
959 test "it filters broken threads" do
960 user1 = insert(:user)
961 user2 = insert(:user)
962 user3 = insert(:user)
964 {:ok, user1} = User.follow(user1, user3)
965 assert User.following?(user1, user3)
967 {:ok, user2} = User.follow(user2, user3)
968 assert User.following?(user2, user3)
970 {:ok, user3} = User.follow(user3, user2)
971 assert User.following?(user3, user2)
973 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
975 {:ok, private_activity_1} =
976 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
978 {:ok, private_activity_2} =
979 CommonAPI.post(user2, %{
981 "visibility" => "private",
982 "in_reply_to_status_id" => private_activity_1.id
985 {:ok, private_activity_3} =
986 CommonAPI.post(user3, %{
988 "visibility" => "private",
989 "in_reply_to_status_id" => private_activity_2.id
993 ActivityPub.fetch_activities([user1.ap_id | user1.following])
994 |> Enum.map(fn a -> a.id end)
996 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
998 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1000 assert length(activities) == 3
1003 ActivityPub.fetch_activities([user1.ap_id | user1.following], %{"user" => user1})
1004 |> Enum.map(fn a -> a.id end)
1006 assert [public_activity.id, private_activity_1.id] == activities
1007 assert length(activities) == 2
1011 describe "update" do
1012 test "it creates an update activity with the new user data" do
1013 user = insert(:user)
1014 {:ok, user} = User.ensure_keys_present(user)
1015 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1018 ActivityPub.update(%{
1019 actor: user_data["id"],
1020 to: [user.follower_address],
1025 assert update.data["actor"] == user.ap_id
1026 assert update.data["to"] == [user.follower_address]
1027 assert embedded_object = update.data["object"]
1028 assert embedded_object["id"] == user_data["id"]
1029 assert embedded_object["type"] == user_data["type"]
1033 test "returned pinned statuses" do
1034 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1035 user = insert(:user)
1037 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1038 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1039 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1041 CommonAPI.pin(activity_one.id, user)
1042 user = refresh_record(user)
1044 CommonAPI.pin(activity_two.id, user)
1045 user = refresh_record(user)
1047 CommonAPI.pin(activity_three.id, user)
1048 user = refresh_record(user)
1050 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1052 assert 3 = length(activities)
1055 test "it can create a Flag activity" do
1056 reporter = insert(:user)
1057 target_account = insert(:user)
1058 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1059 context = Utils.generate_context_id()
1062 reporter_ap_id = reporter.ap_id
1063 target_ap_id = target_account.ap_id
1064 activity_ap_id = activity.data["id"]
1066 assert {:ok, activity} =
1070 account: target_account,
1071 statuses: [activity],
1076 actor: ^reporter_ap_id,
1079 "content" => ^content,
1080 "context" => ^context,
1081 "object" => [^target_ap_id, ^activity_ap_id]
1086 describe "publish_one/1" do
1087 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
1091 actor = insert(:user)
1092 inbox = "http://200.site/users/nick1/inbox"
1094 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1096 assert called(Instances.set_reachable(inbox))
1099 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
1103 actor = insert(:user)
1104 inbox = "http://200.site/users/nick1/inbox"
1107 Publisher.publish_one(%{
1112 unreachable_since: NaiveDateTime.utc_now()
1115 assert called(Instances.set_reachable(inbox))
1118 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1122 actor = insert(:user)
1123 inbox = "http://200.site/users/nick1/inbox"
1126 Publisher.publish_one(%{
1131 unreachable_since: nil
1134 refute called(Instances.set_reachable(inbox))
1137 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1141 actor = insert(:user)
1142 inbox = "http://404.site/users/nick1/inbox"
1144 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1146 assert called(Instances.set_unreachable(inbox))
1149 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1153 actor = insert(:user)
1154 inbox = "http://connrefused.site/users/nick1/inbox"
1156 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1158 assert called(Instances.set_unreachable(inbox))
1161 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1165 actor = insert(:user)
1166 inbox = "http://200.site/users/nick1/inbox"
1168 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1170 refute called(Instances.set_unreachable(inbox))
1173 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1177 actor = insert(:user)
1178 inbox = "http://connrefused.site/users/nick1/inbox"
1180 assert {:error, _} =
1181 Publisher.publish_one(%{
1186 unreachable_since: NaiveDateTime.utc_now()
1189 refute called(Instances.set_unreachable(inbox))
1194 File.read!("test/fixtures/avatar_data_uri")
1197 describe "fetch_activities_bounded" do
1198 test "fetches private posts for followed users" do
1199 user = insert(:user)
1202 CommonAPI.post(user, %{
1203 "status" => "thought I looked cute might delete later :3",
1204 "visibility" => "private"
1207 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1208 assert result.id == activity.id
1211 test "fetches only public posts for other users" do
1212 user = insert(:user)
1213 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1215 {:ok, _private_activity} =
1216 CommonAPI.post(user, %{
1217 "status" => "why is tenshi eating a corndog so cute?",
1218 "visibility" => "private"
1221 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1222 assert result.id == activity.id