1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Builders.ActivityBuilder
12 alias Pleroma.Notification
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.AdminAPI.AccountView
18 alias Pleroma.Web.CommonAPI
19 alias Pleroma.Web.Federator
21 import Pleroma.Factory
26 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
30 setup do: clear_config([:instance, :federating])
32 describe "streaming out participations" do
33 test "it streams them out" do
35 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
37 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
40 conversation.participations
41 |> Repo.preload(:user)
43 with_mock Pleroma.Web.Streamer,
44 stream: fn _, _ -> nil end do
45 ActivityPub.stream_out_participations(conversation.participations)
47 assert called(Pleroma.Web.Streamer.stream("participation", participations))
51 test "streams them out on activity creation" do
52 user_one = insert(:user)
53 user_two = insert(:user)
55 with_mock Pleroma.Web.Streamer,
56 stream: fn _, _ -> nil end do
58 CommonAPI.post(user_one, %{
59 "status" => "@#{user_two.nickname}",
60 "visibility" => "direct"
64 activity.data["context"]
65 |> Pleroma.Conversation.get_for_ap_id()
66 |> Repo.preload(participations: :user)
68 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
73 describe "fetching restricted by visibility" do
74 test "it restricts by the appropriate visibility" do
77 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
79 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
81 {:ok, unlisted_activity} =
82 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
84 {:ok, private_activity} =
85 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
88 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
90 assert activities == [direct_activity]
93 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
95 assert activities == [unlisted_activity]
98 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
100 assert activities == [private_activity]
103 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
105 assert activities == [public_activity]
108 ActivityPub.fetch_activities([], %{
109 :visibility => ~w[private public],
110 "actor_id" => user.ap_id
113 assert activities == [public_activity, private_activity]
117 describe "fetching excluded by visibility" do
118 test "it excludes by the appropriate visibility" do
121 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
123 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
125 {:ok, unlisted_activity} =
126 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
128 {:ok, private_activity} =
129 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
132 ActivityPub.fetch_activities([], %{
133 "exclude_visibilities" => "direct",
134 "actor_id" => user.ap_id
137 assert public_activity in activities
138 assert unlisted_activity in activities
139 assert private_activity in activities
140 refute direct_activity in activities
143 ActivityPub.fetch_activities([], %{
144 "exclude_visibilities" => "unlisted",
145 "actor_id" => user.ap_id
148 assert public_activity in activities
149 refute unlisted_activity in activities
150 assert private_activity in activities
151 assert direct_activity in activities
154 ActivityPub.fetch_activities([], %{
155 "exclude_visibilities" => "private",
156 "actor_id" => user.ap_id
159 assert public_activity in activities
160 assert unlisted_activity in activities
161 refute private_activity in activities
162 assert direct_activity in activities
165 ActivityPub.fetch_activities([], %{
166 "exclude_visibilities" => "public",
167 "actor_id" => user.ap_id
170 refute public_activity in activities
171 assert unlisted_activity in activities
172 assert private_activity in activities
173 assert direct_activity in activities
177 describe "building a user from his ap id" do
178 test "it returns a user" do
179 user_id = "http://mastodon.example.org/users/admin"
180 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
181 assert user.ap_id == user_id
182 assert user.nickname == "admin@mastodon.example.org"
183 assert user.ap_enabled
184 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
187 test "it returns a user that is invisible" do
188 user_id = "http://mastodon.example.org/users/relay"
189 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
190 assert User.invisible?(user)
193 test "it fetches the appropriate tag-restricted posts" do
196 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
197 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
198 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
200 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
203 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
206 ActivityPub.fetch_activities([], %{
208 "tag" => ["test", "essais"],
209 "tag_reject" => ["reject"]
213 ActivityPub.fetch_activities([], %{
216 "tag_all" => ["test", "reject"]
219 assert fetch_one == [status_one, status_three]
220 assert fetch_two == [status_one, status_two, status_three]
221 assert fetch_three == [status_one, status_two]
222 assert fetch_four == [status_three]
226 describe "insertion" do
227 test "drops activities beyond a certain limit" do
228 limit = Config.get([:instance, :remote_limit])
231 :crypto.strong_rand_bytes(limit + 1)
233 |> binary_part(0, limit + 1)
238 "content" => random_text
242 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
245 test "doesn't drop activities with content being null" do
249 "actor" => user.ap_id,
252 "actor" => user.ap_id,
259 assert {:ok, _} = ActivityPub.insert(data)
262 test "returns the activity if one with the same id is already in" do
263 activity = insert(:note_activity)
264 {:ok, new_activity} = ActivityPub.insert(activity.data)
266 assert activity.id == new_activity.id
269 test "inserts a given map into the activity database, giving it an id if it has none." do
273 "actor" => user.ap_id,
276 "actor" => user.ap_id,
283 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
284 assert activity.data["ok"] == data["ok"]
285 assert is_binary(activity.data["id"])
291 "actor" => user.ap_id,
293 "context" => "blabla",
295 "actor" => user.ap_id,
302 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
303 assert activity.data["ok"] == data["ok"]
304 assert activity.data["id"] == given_id
305 assert activity.data["context"] == "blabla"
306 assert activity.data["context_id"]
309 test "adds a context when none is there" do
313 "actor" => user.ap_id,
316 "actor" => user.ap_id,
323 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
324 object = Pleroma.Object.normalize(activity)
326 assert is_binary(activity.data["context"])
327 assert is_binary(object.data["context"])
328 assert activity.data["context_id"]
329 assert object.data["context_id"]
332 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
336 "actor" => user.ap_id,
339 "actor" => user.ap_id,
346 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
347 assert object = Object.normalize(activity)
348 assert is_binary(object.data["id"])
352 describe "listen activities" do
353 test "does not increase user note count" do
357 ActivityPub.listen(%{
358 to: ["https://www.w3.org/ns/activitystreams#Public"],
362 "actor" => user.ap_id,
363 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
365 "title" => "lain radio episode 1",
371 assert activity.actor == user.ap_id
373 user = User.get_cached_by_id(user.id)
374 assert user.note_count == 0
377 test "can be fetched into a timeline" do
378 _listen_activity_1 = insert(:listen)
379 _listen_activity_2 = insert(:listen)
380 _listen_activity_3 = insert(:listen)
382 timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]})
384 assert length(timeline) == 3
388 describe "create activities" do
389 test "it reverts create" do
392 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
393 assert {:error, :reverted} =
394 ActivityPub.create(%{
395 to: ["user1", "user2"],
399 "to" => ["user1", "user2"],
401 "content" => "testing"
406 assert Repo.aggregate(Activity, :count, :id) == 0
407 assert Repo.aggregate(Object, :count, :id) == 0
410 test "removes doubled 'to' recipients" do
414 ActivityPub.create(%{
415 to: ["user1", "user1", "user2"],
419 "to" => ["user1", "user1", "user2"],
421 "content" => "testing"
425 assert activity.data["to"] == ["user1", "user2"]
426 assert activity.actor == user.ap_id
427 assert activity.recipients == ["user1", "user2", user.ap_id]
430 test "increases user note count only for public activities" do
434 CommonAPI.post(User.get_cached_by_id(user.id), %{
436 "visibility" => "public"
440 CommonAPI.post(User.get_cached_by_id(user.id), %{
442 "visibility" => "unlisted"
446 CommonAPI.post(User.get_cached_by_id(user.id), %{
448 "visibility" => "private"
452 CommonAPI.post(User.get_cached_by_id(user.id), %{
454 "visibility" => "direct"
457 user = User.get_cached_by_id(user.id)
458 assert user.note_count == 2
461 test "increases replies count" do
463 user2 = insert(:user)
465 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
466 ap_id = activity.data["id"]
467 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
470 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
471 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
472 assert object.data["repliesCount"] == 1
475 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
476 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
477 assert object.data["repliesCount"] == 2
480 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
481 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
482 assert object.data["repliesCount"] == 2
485 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
486 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
487 assert object.data["repliesCount"] == 2
491 describe "fetch activities for recipients" do
492 test "retrieve the activities for certain recipients" do
493 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
494 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
495 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
497 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
498 assert length(activities) == 2
499 assert activities == [activity_one, activity_two]
503 describe "fetch activities in context" do
504 test "retrieves activities that have a given context" do
505 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
506 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
507 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
508 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
509 activity_five = insert(:note_activity)
512 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
514 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
515 assert activities == [activity_two, activity]
519 test "doesn't return blocked activities" do
520 activity_one = insert(:note_activity)
521 activity_two = insert(:note_activity)
522 activity_three = insert(:note_activity)
524 booster = insert(:user)
525 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
528 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
530 assert Enum.member?(activities, activity_two)
531 assert Enum.member?(activities, activity_three)
532 refute Enum.member?(activities, activity_one)
534 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
537 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
539 assert Enum.member?(activities, activity_two)
540 assert Enum.member?(activities, activity_three)
541 assert Enum.member?(activities, activity_one)
543 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
544 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
545 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
546 activity_three = Activity.get_by_id(activity_three.id)
549 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
551 assert Enum.member?(activities, activity_two)
552 refute Enum.member?(activities, activity_three)
553 refute Enum.member?(activities, boost_activity)
554 assert Enum.member?(activities, activity_one)
557 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
559 assert Enum.member?(activities, activity_two)
560 assert Enum.member?(activities, activity_three)
561 assert Enum.member?(activities, boost_activity)
562 assert Enum.member?(activities, activity_one)
565 test "doesn't return transitive interactions concerning blocked users" do
566 blocker = insert(:user)
567 blockee = insert(:user)
568 friend = insert(:user)
570 {:ok, _user_relationship} = User.block(blocker, blockee)
572 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
574 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
576 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
578 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
580 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
582 assert Enum.member?(activities, activity_one)
583 refute Enum.member?(activities, activity_two)
584 refute Enum.member?(activities, activity_three)
585 refute Enum.member?(activities, activity_four)
588 test "doesn't return announce activities concerning blocked users" do
589 blocker = insert(:user)
590 blockee = insert(:user)
591 friend = insert(:user)
593 {:ok, _user_relationship} = User.block(blocker, blockee)
595 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
597 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
599 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
602 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
603 |> Enum.map(fn act -> act.id end)
605 assert Enum.member?(activities, activity_one.id)
606 refute Enum.member?(activities, activity_two.id)
607 refute Enum.member?(activities, activity_three.id)
610 test "doesn't return activities from blocked domains" do
611 domain = "dogwhistle.zone"
612 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
613 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
614 activity = insert(:note_activity, %{note: note})
616 {:ok, user} = User.block_domain(user, domain)
619 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
621 refute activity in activities
623 followed_user = insert(:user)
624 ActivityPub.follow(user, followed_user)
625 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
628 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
630 refute repeat_activity in activities
633 test "does return activities from followed users on blocked domains" do
634 domain = "meanies.social"
635 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
636 blocker = insert(:user)
638 {:ok, blocker} = User.follow(blocker, domain_user)
639 {:ok, blocker} = User.block_domain(blocker, domain)
641 assert User.following?(blocker, domain_user)
642 assert User.blocks_domain?(blocker, domain_user)
643 refute User.blocks?(blocker, domain_user)
645 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
646 activity = insert(:note_activity, %{note: note})
649 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
651 assert activity in activities
653 # And check that if the guy we DO follow boosts someone else from their domain,
654 # that should be hidden
655 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
656 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
657 bad_activity = insert(:note_activity, %{note: bad_note})
658 {:ok, repeat_activity, _} = CommonAPI.repeat(bad_activity.id, domain_user)
661 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
663 refute repeat_activity in activities
666 test "doesn't return muted activities" do
667 activity_one = insert(:note_activity)
668 activity_two = insert(:note_activity)
669 activity_three = insert(:note_activity)
671 booster = insert(:user)
673 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
674 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
677 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
679 assert Enum.member?(activities, activity_two)
680 assert Enum.member?(activities, activity_three)
681 refute Enum.member?(activities, activity_one)
683 # Calling with 'with_muted' will deliver muted activities, too.
685 ActivityPub.fetch_activities([], %{
686 "muting_user" => user,
687 "with_muted" => true,
688 "skip_preload" => true
691 assert Enum.member?(activities, activity_two)
692 assert Enum.member?(activities, activity_three)
693 assert Enum.member?(activities, activity_one)
695 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
698 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
700 assert Enum.member?(activities, activity_two)
701 assert Enum.member?(activities, activity_three)
702 assert Enum.member?(activities, activity_one)
704 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
705 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
706 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
707 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
708 activity_three = Activity.get_by_id(activity_three.id)
711 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
713 assert Enum.member?(activities, activity_two)
714 refute Enum.member?(activities, activity_three)
715 refute Enum.member?(activities, boost_activity)
716 assert Enum.member?(activities, activity_one)
718 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
720 assert Enum.member?(activities, activity_two)
721 assert Enum.member?(activities, activity_three)
722 assert Enum.member?(activities, boost_activity)
723 assert Enum.member?(activities, activity_one)
726 test "doesn't return thread muted activities" do
728 _activity_one = insert(:note_activity)
729 note_two = insert(:note, data: %{"context" => "suya.."})
730 activity_two = insert(:note_activity, note: note_two)
732 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
734 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
737 test "returns thread muted activities when with_muted is set" do
739 _activity_one = insert(:note_activity)
740 note_two = insert(:note, data: %{"context" => "suya.."})
741 activity_two = insert(:note_activity, note: note_two)
743 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
745 assert [_activity_two, _activity_one] =
746 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
749 test "does include announces on request" do
750 activity_three = insert(:note_activity)
752 booster = insert(:user)
754 {:ok, user} = User.follow(user, booster)
756 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
758 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
760 assert announce_activity.id == announce.id
763 test "excludes reblogs on request" do
765 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
766 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
768 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
770 assert activity == expected_activity
773 describe "public fetch activities" do
774 test "doesn't retrieve unlisted activities" do
777 {:ok, _unlisted_activity} =
778 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
780 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
782 [activity] = ActivityPub.fetch_public_activities()
784 assert activity == listed_activity
787 test "retrieves public activities" do
788 _activities = ActivityPub.fetch_public_activities()
790 %{public: public} = ActivityBuilder.public_and_non_public()
792 activities = ActivityPub.fetch_public_activities()
793 assert length(activities) == 1
794 assert Enum.at(activities, 0) == public
797 test "retrieves a maximum of 20 activities" do
798 ActivityBuilder.insert_list(10)
799 expected_activities = ActivityBuilder.insert_list(20)
801 activities = ActivityPub.fetch_public_activities()
803 assert collect_ids(activities) == collect_ids(expected_activities)
804 assert length(activities) == 20
807 test "retrieves ids starting from a since_id" do
808 activities = ActivityBuilder.insert_list(30)
809 expected_activities = ActivityBuilder.insert_list(10)
810 since_id = List.last(activities).id
812 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
814 assert collect_ids(activities) == collect_ids(expected_activities)
815 assert length(activities) == 10
818 test "retrieves ids up to max_id" do
819 ActivityBuilder.insert_list(10)
820 expected_activities = ActivityBuilder.insert_list(20)
824 |> ActivityBuilder.insert_list()
827 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
829 assert length(activities) == 20
830 assert collect_ids(activities) == collect_ids(expected_activities)
833 test "paginates via offset/limit" do
834 _first_part_activities = ActivityBuilder.insert_list(10)
835 second_part_activities = ActivityBuilder.insert_list(10)
837 later_activities = ActivityBuilder.insert_list(10)
840 ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
842 assert length(activities) == 20
844 assert collect_ids(activities) ==
845 collect_ids(second_part_activities) ++ collect_ids(later_activities)
848 test "doesn't return reblogs for users for whom reblogs have been muted" do
849 activity = insert(:note_activity)
851 booster = insert(:user)
852 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
854 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
856 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
858 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
861 test "returns reblogs for users for whom reblogs have not been muted" do
862 activity = insert(:note_activity)
864 booster = insert(:user)
865 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
866 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
868 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
870 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
872 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
876 describe "react to an object" do
877 test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do
878 Config.put([:instance, :federating], true)
880 reactor = insert(:user)
881 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
882 assert object = Object.normalize(activity)
884 {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
886 assert called(Federator.publish(reaction_activity))
889 test "adds an emoji reaction activity to the db" do
891 reactor = insert(:user)
892 third_user = insert(:user)
893 fourth_user = insert(:user)
894 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
895 assert object = Object.normalize(activity)
897 {:ok, reaction_activity, object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
899 assert reaction_activity
901 assert reaction_activity.data["actor"] == reactor.ap_id
902 assert reaction_activity.data["type"] == "EmojiReact"
903 assert reaction_activity.data["content"] == "🔥"
904 assert reaction_activity.data["object"] == object.data["id"]
905 assert reaction_activity.data["to"] == [User.ap_followers(reactor), activity.data["actor"]]
906 assert reaction_activity.data["context"] == object.data["context"]
907 assert object.data["reaction_count"] == 1
908 assert object.data["reactions"] == [["🔥", [reactor.ap_id]]]
910 {:ok, _reaction_activity, object} = ActivityPub.react_with_emoji(third_user, object, "☕")
912 assert object.data["reaction_count"] == 2
913 assert object.data["reactions"] == [["🔥", [reactor.ap_id]], ["☕", [third_user.ap_id]]]
915 {:ok, _reaction_activity, object} = ActivityPub.react_with_emoji(fourth_user, object, "🔥")
917 assert object.data["reaction_count"] == 3
919 assert object.data["reactions"] == [
920 ["🔥", [fourth_user.ap_id, reactor.ap_id]],
921 ["☕", [third_user.ap_id]]
925 test "reverts emoji reaction on error" do
926 [user, reactor] = insert_list(2, :user)
928 {:ok, activity} = CommonAPI.post(user, %{"status" => "Status"})
929 object = Object.normalize(activity)
931 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
932 assert {:error, :reverted} = ActivityPub.react_with_emoji(reactor, object, "😀")
935 object = Object.get_by_ap_id(object.data["id"])
936 refute object.data["reaction_count"]
937 refute object.data["reactions"]
941 describe "unreacting to an object" do
942 test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do
943 Config.put([:instance, :federating], true)
945 reactor = insert(:user)
946 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
947 assert object = Object.normalize(activity)
949 {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
951 assert called(Federator.publish(reaction_activity))
953 {:ok, unreaction_activity, _object} =
954 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
956 assert called(Federator.publish(unreaction_activity))
959 test "adds an undo activity to the db" do
961 reactor = insert(:user)
962 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
963 assert object = Object.normalize(activity)
965 {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
967 {:ok, unreaction_activity, _object} =
968 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
970 assert unreaction_activity.actor == reactor.ap_id
971 assert unreaction_activity.data["object"] == reaction_activity.data["id"]
973 object = Object.get_by_ap_id(object.data["id"])
974 assert object.data["reaction_count"] == 0
975 assert object.data["reactions"] == []
978 test "reverts emoji unreact on error" do
979 [user, reactor] = insert_list(2, :user)
980 {:ok, activity} = CommonAPI.post(user, %{"status" => "Status"})
981 object = Object.normalize(activity)
983 {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "😀")
985 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
986 assert {:error, :reverted} =
987 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
990 object = Object.get_by_ap_id(object.data["id"])
992 assert object.data["reaction_count"] == 1
993 assert object.data["reactions"] == [["😀", [reactor.ap_id]]]
997 describe "unliking" do
998 test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do
999 Config.put([:instance, :federating], true)
1001 note_activity = insert(:note_activity)
1002 object = Object.normalize(note_activity)
1003 user = insert(:user)
1005 {:ok, object} = ActivityPub.unlike(user, object)
1006 refute called(Federator.publish())
1008 {:ok, _like_activity} = CommonAPI.favorite(user, note_activity.id)
1009 object = Object.get_by_id(object.id)
1010 assert object.data["like_count"] == 1
1012 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
1013 assert object.data["like_count"] == 0
1015 assert called(Federator.publish(unlike_activity))
1018 test "reverts unliking on error" do
1019 note_activity = insert(:note_activity)
1020 user = insert(:user)
1022 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
1023 object = Object.normalize(note_activity)
1024 assert object.data["like_count"] == 1
1026 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1027 assert {:error, :reverted} = ActivityPub.unlike(user, object)
1030 assert Object.get_by_ap_id(object.data["id"]) == object
1031 assert object.data["like_count"] == 1
1032 assert Activity.get_by_id(like_activity.id)
1035 test "unliking a previously liked object" do
1036 note_activity = insert(:note_activity)
1037 object = Object.normalize(note_activity)
1038 user = insert(:user)
1040 # Unliking something that hasn't been liked does nothing
1041 {:ok, object} = ActivityPub.unlike(user, object)
1042 assert object.data["like_count"] == 0
1044 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
1046 object = Object.get_by_id(object.id)
1047 assert object.data["like_count"] == 1
1049 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
1050 assert object.data["like_count"] == 0
1052 assert Activity.get_by_id(like_activity.id) == nil
1053 assert note_activity.actor in unlike_activity.recipients
1057 describe "announcing an object" do
1058 test "adds an announce activity to the db" do
1059 note_activity = insert(:note_activity)
1060 object = Object.normalize(note_activity)
1061 user = insert(:user)
1063 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
1064 assert object.data["announcement_count"] == 1
1065 assert object.data["announcements"] == [user.ap_id]
1067 assert announce_activity.data["to"] == [
1068 User.ap_followers(user),
1069 note_activity.data["actor"]
1072 assert announce_activity.data["object"] == object.data["id"]
1073 assert announce_activity.data["actor"] == user.ap_id
1074 assert announce_activity.data["context"] == object.data["context"]
1077 test "reverts annouce from object on error" do
1078 note_activity = insert(:note_activity)
1079 object = Object.normalize(note_activity)
1080 user = insert(:user)
1082 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1083 assert {:error, :reverted} = ActivityPub.announce(user, object)
1086 reloaded_object = Object.get_by_ap_id(object.data["id"])
1087 assert reloaded_object == object
1088 refute reloaded_object.data["announcement_count"]
1089 refute reloaded_object.data["announcements"]
1093 describe "announcing a private object" do
1094 test "adds an announce activity to the db if the audience is not widened" do
1095 user = insert(:user)
1096 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1097 object = Object.normalize(note_activity)
1099 {:ok, announce_activity, object} = ActivityPub.announce(user, object, nil, true, false)
1101 assert announce_activity.data["to"] == [User.ap_followers(user)]
1103 assert announce_activity.data["object"] == object.data["id"]
1104 assert announce_activity.data["actor"] == user.ap_id
1105 assert announce_activity.data["context"] == object.data["context"]
1108 test "does not add an announce activity to the db if the audience is widened" do
1109 user = insert(:user)
1110 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1111 object = Object.normalize(note_activity)
1113 assert {:error, _} = ActivityPub.announce(user, object, nil, true, true)
1116 test "does not add an announce activity to the db if the announcer is not the author" do
1117 user = insert(:user)
1118 announcer = insert(:user)
1119 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1120 object = Object.normalize(note_activity)
1122 assert {:error, _} = ActivityPub.announce(announcer, object, nil, true, false)
1126 describe "unannouncing an object" do
1127 test "unannouncing a previously announced object" do
1128 note_activity = insert(:note_activity)
1129 object = Object.normalize(note_activity)
1130 user = insert(:user)
1132 # Unannouncing an object that is not announced does nothing
1133 {:ok, object} = ActivityPub.unannounce(user, object)
1134 refute object.data["announcement_count"]
1136 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
1137 assert object.data["announcement_count"] == 1
1139 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
1140 assert object.data["announcement_count"] == 0
1142 assert unannounce_activity.data["to"] == [
1143 User.ap_followers(user),
1144 object.data["actor"]
1147 assert unannounce_activity.data["type"] == "Undo"
1148 assert unannounce_activity.data["object"] == announce_activity.data
1149 assert unannounce_activity.data["actor"] == user.ap_id
1150 assert unannounce_activity.data["context"] == announce_activity.data["context"]
1152 assert Activity.get_by_id(announce_activity.id) == nil
1155 test "reverts unannouncing on error" do
1156 note_activity = insert(:note_activity)
1157 object = Object.normalize(note_activity)
1158 user = insert(:user)
1160 {:ok, _announce_activity, object} = ActivityPub.announce(user, object)
1161 assert object.data["announcement_count"] == 1
1163 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1164 assert {:error, :reverted} = ActivityPub.unannounce(user, object)
1167 object = Object.get_by_ap_id(object.data["id"])
1168 assert object.data["announcement_count"] == 1
1172 describe "uploading files" do
1173 test "copies the file to the configured folder" do
1174 file = %Plug.Upload{
1175 content_type: "image/jpg",
1176 path: Path.absname("test/fixtures/image.jpg"),
1177 filename: "an_image.jpg"
1180 {:ok, %Object{} = object} = ActivityPub.upload(file)
1181 assert object.data["name"] == "an_image.jpg"
1184 test "works with base64 encoded images" do
1189 {:ok, %Object{}} = ActivityPub.upload(file)
1193 describe "fetch the latest Follow" do
1194 test "fetches the latest Follow activity" do
1195 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1196 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1197 followed = Repo.get_by(User, ap_id: activity.data["object"])
1199 assert activity == Utils.fetch_latest_follow(follower, followed)
1203 describe "following / unfollowing" do
1204 test "it reverts follow activity" do
1205 follower = insert(:user)
1206 followed = insert(:user)
1208 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1209 assert {:error, :reverted} = ActivityPub.follow(follower, followed)
1212 assert Repo.aggregate(Activity, :count, :id) == 0
1213 assert Repo.aggregate(Object, :count, :id) == 0
1216 test "it reverts unfollow activity" do
1217 follower = insert(:user)
1218 followed = insert(:user)
1220 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1222 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1223 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1226 activity = Activity.get_by_id(follow_activity.id)
1227 assert activity.data["type"] == "Follow"
1228 assert activity.data["actor"] == follower.ap_id
1230 assert activity.data["object"] == followed.ap_id
1233 test "creates a follow activity" do
1234 follower = insert(:user)
1235 followed = insert(:user)
1237 {:ok, activity} = ActivityPub.follow(follower, followed)
1238 assert activity.data["type"] == "Follow"
1239 assert activity.data["actor"] == follower.ap_id
1240 assert activity.data["object"] == followed.ap_id
1243 test "creates an undo activity for the last follow" do
1244 follower = insert(:user)
1245 followed = insert(:user)
1247 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1248 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1250 assert activity.data["type"] == "Undo"
1251 assert activity.data["actor"] == follower.ap_id
1253 embedded_object = activity.data["object"]
1254 assert is_map(embedded_object)
1255 assert embedded_object["type"] == "Follow"
1256 assert embedded_object["object"] == followed.ap_id
1257 assert embedded_object["id"] == follow_activity.data["id"]
1260 test "creates an undo activity for a pending follow request" do
1261 follower = insert(:user)
1262 followed = insert(:user, %{locked: true})
1264 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1265 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1267 assert activity.data["type"] == "Undo"
1268 assert activity.data["actor"] == follower.ap_id
1270 embedded_object = activity.data["object"]
1271 assert is_map(embedded_object)
1272 assert embedded_object["type"] == "Follow"
1273 assert embedded_object["object"] == followed.ap_id
1274 assert embedded_object["id"] == follow_activity.data["id"]
1278 describe "blocking / unblocking" do
1279 test "reverts block activity on error" do
1280 [blocker, blocked] = insert_list(2, :user)
1282 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1283 assert {:error, :reverted} = ActivityPub.block(blocker, blocked)
1286 assert Repo.aggregate(Activity, :count, :id) == 0
1287 assert Repo.aggregate(Object, :count, :id) == 0
1290 test "creates a block activity" do
1291 blocker = insert(:user)
1292 blocked = insert(:user)
1294 {:ok, activity} = ActivityPub.block(blocker, blocked)
1296 assert activity.data["type"] == "Block"
1297 assert activity.data["actor"] == blocker.ap_id
1298 assert activity.data["object"] == blocked.ap_id
1301 test "reverts unblock activity on error" do
1302 [blocker, blocked] = insert_list(2, :user)
1303 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1305 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1306 assert {:error, :reverted} = ActivityPub.unblock(blocker, blocked)
1309 assert block_activity.data["type"] == "Block"
1310 assert block_activity.data["actor"] == blocker.ap_id
1312 assert Repo.aggregate(Activity, :count, :id) == 1
1313 assert Repo.aggregate(Object, :count, :id) == 1
1316 test "creates an undo activity for the last block" do
1317 blocker = insert(:user)
1318 blocked = insert(:user)
1320 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1321 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
1323 assert activity.data["type"] == "Undo"
1324 assert activity.data["actor"] == blocker.ap_id
1326 embedded_object = activity.data["object"]
1327 assert is_map(embedded_object)
1328 assert embedded_object["type"] == "Block"
1329 assert embedded_object["object"] == blocked.ap_id
1330 assert embedded_object["id"] == block_activity.data["id"]
1334 describe "timeline post-processing" do
1335 test "it filters broken threads" do
1336 user1 = insert(:user)
1337 user2 = insert(:user)
1338 user3 = insert(:user)
1340 {:ok, user1} = User.follow(user1, user3)
1341 assert User.following?(user1, user3)
1343 {:ok, user2} = User.follow(user2, user3)
1344 assert User.following?(user2, user3)
1346 {:ok, user3} = User.follow(user3, user2)
1347 assert User.following?(user3, user2)
1349 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
1351 {:ok, private_activity_1} =
1352 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
1354 {:ok, private_activity_2} =
1355 CommonAPI.post(user2, %{
1357 "visibility" => "private",
1358 "in_reply_to_status_id" => private_activity_1.id
1361 {:ok, private_activity_3} =
1362 CommonAPI.post(user3, %{
1364 "visibility" => "private",
1365 "in_reply_to_status_id" => private_activity_2.id
1369 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1370 |> Enum.map(fn a -> a.id end)
1372 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1374 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1376 assert length(activities) == 3
1379 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{"user" => user1})
1380 |> Enum.map(fn a -> a.id end)
1382 assert [public_activity.id, private_activity_1.id] == activities
1383 assert length(activities) == 2
1387 describe "update" do
1388 setup do: clear_config([:instance, :max_pinned_statuses])
1390 test "it creates an update activity with the new user data" do
1391 user = insert(:user)
1392 {:ok, user} = User.ensure_keys_present(user)
1393 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1396 ActivityPub.update(%{
1397 actor: user_data["id"],
1398 to: [user.follower_address],
1403 assert update.data["actor"] == user.ap_id
1404 assert update.data["to"] == [user.follower_address]
1405 assert embedded_object = update.data["object"]
1406 assert embedded_object["id"] == user_data["id"]
1407 assert embedded_object["type"] == user_data["type"]
1411 test "returned pinned statuses" do
1412 Config.put([:instance, :max_pinned_statuses], 3)
1413 user = insert(:user)
1415 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1416 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1417 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1419 CommonAPI.pin(activity_one.id, user)
1420 user = refresh_record(user)
1422 CommonAPI.pin(activity_two.id, user)
1423 user = refresh_record(user)
1425 CommonAPI.pin(activity_three.id, user)
1426 user = refresh_record(user)
1428 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1430 assert 3 = length(activities)
1433 describe "flag/1" do
1435 reporter = insert(:user)
1436 target_account = insert(:user)
1438 {:ok, activity} = CommonAPI.post(target_account, %{"status" => content})
1439 context = Utils.generate_context_id()
1441 reporter_ap_id = reporter.ap_id
1442 target_ap_id = target_account.ap_id
1443 activity_ap_id = activity.data["id"]
1445 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1451 target_account: target_account,
1452 reported_activity: activity,
1454 activity_ap_id: activity_ap_id,
1455 activity_with_object: activity_with_object,
1456 reporter_ap_id: reporter_ap_id,
1457 target_ap_id: target_ap_id
1461 test "it can create a Flag activity",
1465 target_account: target_account,
1466 reported_activity: reported_activity,
1468 activity_ap_id: activity_ap_id,
1469 activity_with_object: activity_with_object,
1470 reporter_ap_id: reporter_ap_id,
1471 target_ap_id: target_ap_id
1473 assert {:ok, activity} =
1477 account: target_account,
1478 statuses: [reported_activity],
1484 "id" => activity_ap_id,
1485 "content" => content,
1486 "published" => activity_with_object.object.data["published"],
1487 "actor" => AccountView.render("show.json", %{user: target_account})
1491 actor: ^reporter_ap_id,
1494 "content" => ^content,
1495 "context" => ^context,
1496 "object" => [^target_ap_id, ^note_obj]
1501 test_with_mock "strips status data from Flag, before federating it",
1505 target_account: target_account,
1506 reported_activity: reported_activity,
1516 account: target_account,
1517 statuses: [reported_activity],
1522 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1524 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1528 test "fetch_activities/2 returns activities addressed to a list " do
1529 user = insert(:user)
1530 member = insert(:user)
1531 {:ok, list} = Pleroma.List.create("foo", user)
1532 {:ok, list} = Pleroma.List.follow(list, member)
1535 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1537 activity = Repo.preload(activity, :bookmark)
1538 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1540 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1544 File.read!("test/fixtures/avatar_data_uri")
1547 describe "fetch_activities_bounded" do
1548 test "fetches private posts for followed users" do
1549 user = insert(:user)
1552 CommonAPI.post(user, %{
1553 "status" => "thought I looked cute might delete later :3",
1554 "visibility" => "private"
1557 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1558 assert result.id == activity.id
1561 test "fetches only public posts for other users" do
1562 user = insert(:user)
1563 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1565 {:ok, _private_activity} =
1566 CommonAPI.post(user, %{
1567 "status" => "why is tenshi eating a corndog so cute?",
1568 "visibility" => "private"
1571 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1572 assert result.id == activity.id
1576 describe "fetch_follow_information_for_user" do
1577 test "syncronizes following/followers counters" do
1581 follower_address: "http://localhost:4001/users/fuser2/followers",
1582 following_address: "http://localhost:4001/users/fuser2/following"
1585 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1586 assert info.follower_count == 527
1587 assert info.following_count == 267
1590 test "detects hidden followers" do
1593 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1594 %Tesla.Env{status: 403, body: ""}
1597 apply(HttpRequestMock, :request, [env])
1604 follower_address: "http://localhost:4001/users/masto_closed/followers",
1605 following_address: "http://localhost:4001/users/masto_closed/following"
1608 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1609 assert follow_info.hide_followers == true
1610 assert follow_info.hide_follows == false
1613 test "detects hidden follows" do
1616 "http://localhost:4001/users/masto_closed/following?page=1" ->
1617 %Tesla.Env{status: 403, body: ""}
1620 apply(HttpRequestMock, :request, [env])
1627 follower_address: "http://localhost:4001/users/masto_closed/followers",
1628 following_address: "http://localhost:4001/users/masto_closed/following"
1631 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1632 assert follow_info.hide_followers == false
1633 assert follow_info.hide_follows == true
1636 test "detects hidden follows/followers for friendica" do
1640 follower_address: "http://localhost:8080/followers/fuser3",
1641 following_address: "http://localhost:8080/following/fuser3"
1644 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1645 assert follow_info.hide_followers == true
1646 assert follow_info.follower_count == 296
1647 assert follow_info.following_count == 32
1648 assert follow_info.hide_follows == true
1651 test "doesn't crash when follower and following counters are hidden" do
1654 "http://localhost:4001/users/masto_hidden_counters/following" ->
1656 "@context" => "https://www.w3.org/ns/activitystreams",
1657 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1660 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1661 %Tesla.Env{status: 403, body: ""}
1663 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1665 "@context" => "https://www.w3.org/ns/activitystreams",
1666 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1669 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1670 %Tesla.Env{status: 403, body: ""}
1677 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1678 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1681 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1683 assert follow_info.hide_followers == true
1684 assert follow_info.follower_count == 0
1685 assert follow_info.hide_follows == true
1686 assert follow_info.following_count == 0
1690 describe "fetch_favourites/3" do
1691 test "returns a favourite activities sorted by adds to favorite" do
1692 user = insert(:user)
1693 other_user = insert(:user)
1694 user1 = insert(:user)
1695 user2 = insert(:user)
1696 {:ok, a1} = CommonAPI.post(user1, %{"status" => "bla"})
1697 {:ok, _a2} = CommonAPI.post(user2, %{"status" => "traps are happy"})
1698 {:ok, a3} = CommonAPI.post(user2, %{"status" => "Trees Are "})
1699 {:ok, a4} = CommonAPI.post(user2, %{"status" => "Agent Smith "})
1700 {:ok, a5} = CommonAPI.post(user1, %{"status" => "Red or Blue "})
1702 {:ok, _} = CommonAPI.favorite(user, a4.id)
1703 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1704 {:ok, _} = CommonAPI.favorite(user, a3.id)
1705 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1706 {:ok, _} = CommonAPI.favorite(user, a5.id)
1707 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1708 {:ok, _} = CommonAPI.favorite(user, a1.id)
1709 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1710 result = ActivityPub.fetch_favourites(user)
1712 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1714 result = ActivityPub.fetch_favourites(user, %{"limit" => 2})
1715 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1719 describe "Move activity" do
1721 %{ap_id: old_ap_id} = old_user = insert(:user)
1722 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1723 follower = insert(:user)
1724 follower_move_opted_out = insert(:user, allow_following_move: false)
1726 User.follow(follower, old_user)
1727 User.follow(follower_move_opted_out, old_user)
1729 assert User.following?(follower, old_user)
1730 assert User.following?(follower_move_opted_out, old_user)
1732 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1737 "actor" => ^old_ap_id,
1738 "object" => ^old_ap_id,
1739 "target" => ^new_ap_id,
1746 "op" => "move_following",
1747 "origin_id" => old_user.id,
1748 "target_id" => new_user.id
1751 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1753 Pleroma.Workers.BackgroundWorker.perform(params, nil)
1755 refute User.following?(follower, old_user)
1756 assert User.following?(follower, new_user)
1758 assert User.following?(follower_move_opted_out, old_user)
1759 refute User.following?(follower_move_opted_out, new_user)
1761 activity = %Activity{activity | object: nil}
1763 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1765 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1768 test "old user must be in the new user's `also_known_as` list" do
1769 old_user = insert(:user)
1770 new_user = insert(:user)
1772 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1773 ActivityPub.move(old_user, new_user)
1777 test "doesn't retrieve replies activities with exclude_replies" do
1778 user = insert(:user)
1780 {:ok, activity} = CommonAPI.post(user, %{"status" => "yeah"})
1783 CommonAPI.post(user, %{"status" => "yeah", "in_reply_to_status_id" => activity.id})
1785 [result] = ActivityPub.fetch_public_activities(%{"exclude_replies" => "true"})
1787 assert result.id == activity.id
1789 assert length(ActivityPub.fetch_public_activities()) == 2
1792 describe "replies filtering with public messages" do
1793 setup :public_messages
1795 test "public timeline", %{users: %{u1: user}} do
1798 |> Map.put("type", ["Create", "Announce"])
1799 |> Map.put("local_only", false)
1800 |> Map.put("blocking_user", user)
1801 |> Map.put("muting_user", user)
1802 |> Map.put("reply_filtering_user", user)
1803 |> ActivityPub.fetch_public_activities()
1804 |> Enum.map(& &1.id)
1806 assert length(activities_ids) == 16
1809 test "public timeline with reply_visibility `following`", %{
1815 activities: activities
1819 |> Map.put("type", ["Create", "Announce"])
1820 |> Map.put("local_only", false)
1821 |> Map.put("blocking_user", user)
1822 |> Map.put("muting_user", user)
1823 |> Map.put("reply_visibility", "following")
1824 |> Map.put("reply_filtering_user", user)
1825 |> ActivityPub.fetch_public_activities()
1826 |> Enum.map(& &1.id)
1828 assert length(activities_ids) == 14
1831 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1833 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1836 test "public timeline with reply_visibility `self`", %{
1842 activities: activities
1846 |> Map.put("type", ["Create", "Announce"])
1847 |> Map.put("local_only", false)
1848 |> Map.put("blocking_user", user)
1849 |> Map.put("muting_user", user)
1850 |> Map.put("reply_visibility", "self")
1851 |> Map.put("reply_filtering_user", user)
1852 |> ActivityPub.fetch_public_activities()
1853 |> Enum.map(& &1.id)
1855 assert length(activities_ids) == 10
1856 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1857 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1860 test "home timeline", %{
1862 activities: activities,
1870 |> Map.put("type", ["Create", "Announce"])
1871 |> Map.put("blocking_user", user)
1872 |> Map.put("muting_user", user)
1873 |> Map.put("user", user)
1874 |> Map.put("reply_filtering_user", user)
1877 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1878 |> Enum.map(& &1.id)
1880 assert length(activities_ids) == 13
1895 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1898 test "home timeline with reply_visibility `following`", %{
1900 activities: activities,
1908 |> Map.put("type", ["Create", "Announce"])
1909 |> Map.put("blocking_user", user)
1910 |> Map.put("muting_user", user)
1911 |> Map.put("user", user)
1912 |> Map.put("reply_visibility", "following")
1913 |> Map.put("reply_filtering_user", user)
1916 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1917 |> Enum.map(& &1.id)
1919 assert length(activities_ids) == 11
1934 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1937 test "home timeline with reply_visibility `self`", %{
1939 activities: activities,
1947 |> Map.put("type", ["Create", "Announce"])
1948 |> Map.put("blocking_user", user)
1949 |> Map.put("muting_user", user)
1950 |> Map.put("user", user)
1951 |> Map.put("reply_visibility", "self")
1952 |> Map.put("reply_filtering_user", user)
1955 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1956 |> Enum.map(& &1.id)
1958 assert length(activities_ids) == 9
1971 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1975 describe "replies filtering with private messages" do
1976 setup :private_messages
1978 test "public timeline", %{users: %{u1: user}} do
1981 |> Map.put("type", ["Create", "Announce"])
1982 |> Map.put("local_only", false)
1983 |> Map.put("blocking_user", user)
1984 |> Map.put("muting_user", user)
1985 |> Map.put("user", user)
1986 |> ActivityPub.fetch_public_activities()
1987 |> Enum.map(& &1.id)
1989 assert activities_ids == []
1992 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1995 |> Map.put("type", ["Create", "Announce"])
1996 |> Map.put("local_only", false)
1997 |> Map.put("blocking_user", user)
1998 |> Map.put("muting_user", user)
1999 |> Map.put("reply_visibility", "following")
2000 |> Map.put("reply_filtering_user", user)
2001 |> Map.put("user", user)
2002 |> ActivityPub.fetch_public_activities()
2003 |> Enum.map(& &1.id)
2005 assert activities_ids == []
2008 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
2011 |> Map.put("type", ["Create", "Announce"])
2012 |> Map.put("local_only", false)
2013 |> Map.put("blocking_user", user)
2014 |> Map.put("muting_user", user)
2015 |> Map.put("reply_visibility", "self")
2016 |> Map.put("reply_filtering_user", user)
2017 |> Map.put("user", user)
2018 |> ActivityPub.fetch_public_activities()
2019 |> Enum.map(& &1.id)
2021 assert activities_ids == []
2024 test "home timeline", %{users: %{u1: user}} do
2027 |> Map.put("type", ["Create", "Announce"])
2028 |> Map.put("blocking_user", user)
2029 |> Map.put("muting_user", user)
2030 |> Map.put("user", user)
2033 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2034 |> Enum.map(& &1.id)
2036 assert length(activities_ids) == 12
2039 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
2042 |> Map.put("type", ["Create", "Announce"])
2043 |> Map.put("blocking_user", user)
2044 |> Map.put("muting_user", user)
2045 |> Map.put("user", user)
2046 |> Map.put("reply_visibility", "following")
2047 |> Map.put("reply_filtering_user", user)
2050 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2051 |> Enum.map(& &1.id)
2053 assert length(activities_ids) == 12
2056 test "home timeline with default reply_visibility `self`", %{
2058 activities: activities,
2066 |> Map.put("type", ["Create", "Announce"])
2067 |> Map.put("blocking_user", user)
2068 |> Map.put("muting_user", user)
2069 |> Map.put("user", user)
2070 |> Map.put("reply_visibility", "self")
2071 |> Map.put("reply_filtering_user", user)
2074 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2075 |> Enum.map(& &1.id)
2077 assert length(activities_ids) == 10
2080 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
2082 assert Enum.all?(visible_ids, &(&1 in activities_ids))
2086 defp public_messages(_) do
2087 [u1, u2, u3, u4] = insert_list(4, :user)
2088 {:ok, u1} = User.follow(u1, u2)
2089 {:ok, u2} = User.follow(u2, u1)
2090 {:ok, u1} = User.follow(u1, u4)
2091 {:ok, u4} = User.follow(u4, u1)
2093 {:ok, u2} = User.follow(u2, u3)
2094 {:ok, u3} = User.follow(u3, u2)
2096 {:ok, a1} = CommonAPI.post(u1, %{"status" => "Status"})
2099 CommonAPI.post(u2, %{
2100 "status" => "@#{u1.nickname} reply from u2 to u1",
2101 "in_reply_to_status_id" => a1.id
2105 CommonAPI.post(u3, %{
2106 "status" => "@#{u1.nickname} reply from u3 to u1",
2107 "in_reply_to_status_id" => a1.id
2111 CommonAPI.post(u4, %{
2112 "status" => "@#{u1.nickname} reply from u4 to u1",
2113 "in_reply_to_status_id" => a1.id
2116 {:ok, a2} = CommonAPI.post(u2, %{"status" => "Status"})
2119 CommonAPI.post(u1, %{
2120 "status" => "@#{u2.nickname} reply from u1 to u2",
2121 "in_reply_to_status_id" => a2.id
2125 CommonAPI.post(u3, %{
2126 "status" => "@#{u2.nickname} reply from u3 to u2",
2127 "in_reply_to_status_id" => a2.id
2131 CommonAPI.post(u4, %{
2132 "status" => "@#{u2.nickname} reply from u4 to u2",
2133 "in_reply_to_status_id" => a2.id
2136 {:ok, a3} = CommonAPI.post(u3, %{"status" => "Status"})
2139 CommonAPI.post(u1, %{
2140 "status" => "@#{u3.nickname} reply from u1 to u3",
2141 "in_reply_to_status_id" => a3.id
2145 CommonAPI.post(u2, %{
2146 "status" => "@#{u3.nickname} reply from u2 to u3",
2147 "in_reply_to_status_id" => a3.id
2151 CommonAPI.post(u4, %{
2152 "status" => "@#{u3.nickname} reply from u4 to u3",
2153 "in_reply_to_status_id" => a3.id
2156 {:ok, a4} = CommonAPI.post(u4, %{"status" => "Status"})
2159 CommonAPI.post(u1, %{
2160 "status" => "@#{u4.nickname} reply from u1 to u4",
2161 "in_reply_to_status_id" => a4.id
2165 CommonAPI.post(u2, %{
2166 "status" => "@#{u4.nickname} reply from u2 to u4",
2167 "in_reply_to_status_id" => a4.id
2171 CommonAPI.post(u3, %{
2172 "status" => "@#{u4.nickname} reply from u3 to u4",
2173 "in_reply_to_status_id" => a4.id
2177 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2178 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2179 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2180 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
2181 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
2182 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
2185 defp private_messages(_) do
2186 [u1, u2, u3, u4] = insert_list(4, :user)
2187 {:ok, u1} = User.follow(u1, u2)
2188 {:ok, u2} = User.follow(u2, u1)
2189 {:ok, u1} = User.follow(u1, u3)
2190 {:ok, u3} = User.follow(u3, u1)
2191 {:ok, u1} = User.follow(u1, u4)
2192 {:ok, u4} = User.follow(u4, u1)
2194 {:ok, u2} = User.follow(u2, u3)
2195 {:ok, u3} = User.follow(u3, u2)
2197 {:ok, a1} = CommonAPI.post(u1, %{"status" => "Status", "visibility" => "private"})
2200 CommonAPI.post(u2, %{
2201 "status" => "@#{u1.nickname} reply from u2 to u1",
2202 "in_reply_to_status_id" => a1.id,
2203 "visibility" => "private"
2207 CommonAPI.post(u3, %{
2208 "status" => "@#{u1.nickname} reply from u3 to u1",
2209 "in_reply_to_status_id" => a1.id,
2210 "visibility" => "private"
2214 CommonAPI.post(u4, %{
2215 "status" => "@#{u1.nickname} reply from u4 to u1",
2216 "in_reply_to_status_id" => a1.id,
2217 "visibility" => "private"
2220 {:ok, a2} = CommonAPI.post(u2, %{"status" => "Status", "visibility" => "private"})
2223 CommonAPI.post(u1, %{
2224 "status" => "@#{u2.nickname} reply from u1 to u2",
2225 "in_reply_to_status_id" => a2.id,
2226 "visibility" => "private"
2230 CommonAPI.post(u3, %{
2231 "status" => "@#{u2.nickname} reply from u3 to u2",
2232 "in_reply_to_status_id" => a2.id,
2233 "visibility" => "private"
2236 {:ok, a3} = CommonAPI.post(u3, %{"status" => "Status", "visibility" => "private"})
2239 CommonAPI.post(u1, %{
2240 "status" => "@#{u3.nickname} reply from u1 to u3",
2241 "in_reply_to_status_id" => a3.id,
2242 "visibility" => "private"
2246 CommonAPI.post(u2, %{
2247 "status" => "@#{u3.nickname} reply from u2 to u3",
2248 "in_reply_to_status_id" => a3.id,
2249 "visibility" => "private"
2252 {:ok, a4} = CommonAPI.post(u4, %{"status" => "Status", "visibility" => "private"})
2255 CommonAPI.post(u1, %{
2256 "status" => "@#{u4.nickname} reply from u1 to u4",
2257 "in_reply_to_status_id" => a4.id,
2258 "visibility" => "private"
2262 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2263 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2264 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2265 u2: %{r1: r2_1.id, r2: r2_2.id},
2266 u3: %{r1: r3_1.id, r2: r3_2.id},